From e6df55e7ed4c67318eeea4807c265beb353adee9 Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 22:33:04 +0200 Subject: [PATCH 1/7] chore: add 70 procent as code coverage threshold --- frontend/vite.config.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 02248754..3d8e7058 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -3,6 +3,8 @@ import react from "@vitejs/plugin-react-swc"; import { defineConfig } from "vite"; +const threshold: number = 70 as const; + // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], @@ -12,5 +14,17 @@ export default defineConfig({ environment: "jsdom", setupFiles: ["./setup-tests.ts"], css: true, + coverage: { + provider: "v8", + reporter: ["text", "json", "html"], + thresholds: { + "src/**/*.{ts,tsx}": { + functions: threshold, + branches: threshold, + statements: threshold, + lines: threshold, + }, + }, + }, }, }); From 2cfa6e3edd51e668a72598b41d4967589edf3ec1 Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 22:39:01 +0200 Subject: [PATCH 2/7] chore: refine exclusion pattern --- frontend/vite.config.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3d8e7058..8cfc8ed8 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -15,6 +15,15 @@ export default defineConfig({ setupFiles: ["./setup-tests.ts"], css: true, coverage: { + exclude: [ + "src/main.tsx", + "vite.config.ts", + "dist/**/*", + "node_modules/**/*", + "coverage/**/*", + ".eslintrc.cjs", + "src/**/*.d.ts", + ], provider: "v8", reporter: ["text", "json", "html"], thresholds: { From 39d8dcb2c95ecf6bd76dbe6537c54eaa67391aa7 Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 23:08:49 +0200 Subject: [PATCH 3/7] chore: add jacoco (JAva COde COverage) tool to prevent untested code to be merged --- backend/pom.xml | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/backend/pom.xml b/backend/pom.xml index ee811371..ea6322e8 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -120,6 +120,68 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + prepare-agent + + + + check + + check + + + + + CLASS + + + LINE + COVEREDRATIO + 0.80 + + + COMPLEXITY + COVEREDRATIO + 0.80 + + + METHOD + COVEREDRATIO + 0.80 + + + CLASS + COVEREDRATIO + 0.80 + + + + INSTRUCTION + COVEREDRATIO + 0.80 + + + BRANCH + COVEREDRATIO + 0.80 + + + + com.greenfoxacademy.backend.models.* + com.greenfoxacademy.backend.BackendApplication + + + + + + + From 0ad66f13a066096636abfd84f8e1cc5b0c5f3a51 Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 23:15:38 +0200 Subject: [PATCH 4/7] test: test jwt config class --- .../backend/config/JwtConfig.java | 3 ++- .../backend/config/JwtConfigTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java diff --git a/backend/src/main/java/com/greenfoxacademy/backend/config/JwtConfig.java b/backend/src/main/java/com/greenfoxacademy/backend/config/JwtConfig.java index bafcaa6e..aff7e63f 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/config/JwtConfig.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/config/JwtConfig.java @@ -1,6 +1,7 @@ package com.greenfoxacademy.backend.config; import lombok.Data; +import lombok.Getter; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @@ -9,7 +10,7 @@ */ @Configuration -@Data +@Getter public class JwtConfig { @Value("${jwt.expiration-time}") private Long expirationTime; diff --git a/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java b/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java new file mode 100644 index 00000000..b9e159bf --- /dev/null +++ b/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java @@ -0,0 +1,19 @@ +package com.greenfoxacademy.backend.config; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class JwtConfigTest { + + @Autowired + private JwtConfig jwtConfig = new JwtConfig(); + + @Test + void getExpirationTime() { + assertEquals(86400000, jwtConfig.getExpirationTime()); + } +} \ No newline at end of file From 17d3f187f77673d0979b9bd7917e607161306f2e Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 23:51:27 +0200 Subject: [PATCH 5/7] chore: add test coverage 75 procent for all --- backend/pom.xml | 29 +++- .../backend/services/user/UserService.java | 3 +- .../services/user/UserServiceImpl.java | 28 ++-- .../services/user/UserServiceImplTest.java | 153 ++++++++++++++++++ 4 files changed, 186 insertions(+), 27 deletions(-) create mode 100644 backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java diff --git a/backend/pom.xml b/backend/pom.xml index ea6322e8..2e76021c 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -143,33 +143,33 @@ LINE COVEREDRATIO - 0.80 + 0.75 COMPLEXITY COVEREDRATIO - 0.80 + 0.75 METHOD COVEREDRATIO - 0.80 + 0.75 CLASS COVEREDRATIO - 0.80 + 0.75 INSTRUCTION COVEREDRATIO - 0.80 + 0.75 BRANCH COVEREDRATIO - 0.80 + 0.75 @@ -184,5 +184,20 @@ - + + + + org.jacoco + jacoco-maven-plugin + 0.8.12 + + + + report + + + + + + diff --git a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java index 7cc97017..17e0b974 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java @@ -20,6 +20,5 @@ public interface UserService extends UserDetailsService { LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception; - ProfileUpdateResponseDto profileUpdate( - String user, ProfileUpdateRequestDto profileUpdateRequestDto) throws Exception; + ProfileUpdateResponseDto profileUpdate(String user, ProfileUpdateRequestDto profileUpdateRequestDto) throws Exception; } diff --git a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java index cb681e8f..46f42a79 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java @@ -27,15 +27,9 @@ public class UserServiceImpl implements UserService { private final AuthService authService; @Override - public RegisterResponseDto register( - RegisterRequestDto registerRequestDto) throws UserAlreadyExistsError { + public RegisterResponseDto register(RegisterRequestDto registerRequestDto) throws UserAlreadyExistsError { - User user = User.builder() - .email(registerRequestDto.email()) - .firstName(registerRequestDto.firstName()) - .lastName(registerRequestDto.lastName()) - .password(passwordEncoder.encode(registerRequestDto.password())) - .build(); + User user = User.builder().email(registerRequestDto.email()).firstName(registerRequestDto.firstName()).lastName(registerRequestDto.lastName()).password(passwordEncoder.encode(registerRequestDto.password())).build(); try { return new RegisterResponseDto(userRepository.save(user).getId()); } catch (Exception e) { @@ -45,10 +39,8 @@ public RegisterResponseDto register( @Override - public LoginResponseDto login( - LoginRequestDto loginRequestDto) throws Exception { - User user = userRepository - .findByEmail(loginRequestDto.email()).orElseThrow(() -> new Exception("User not found")); + public LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception { + User user = userRepository.findByEmail(loginRequestDto.email()).orElseThrow(() -> new Exception("User not found")); if (!passwordEncoder.matches(loginRequestDto.password(), user.getPassword())) { throw new Exception("Invalid password"); } @@ -56,10 +48,11 @@ public LoginResponseDto login( } @Override - public ProfileUpdateResponseDto profileUpdate( - String email, ProfileUpdateRequestDto profileUpdateRequestDto) throws Exception { - User user = userRepository - .findByEmail(email).orElseThrow(() -> new UsernameNotFoundException("User not found")); + public ProfileUpdateResponseDto profileUpdate(String email, ProfileUpdateRequestDto profileUpdateRequestDto) throws Exception { + User user = userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException("User not found")); + if (userRepository.existsByEmail(profileUpdateRequestDto.email())) { + throw new UserAlreadyExistsError("Email is already taken!"); + } user.setEmail(profileUpdateRequestDto.email()); user.setFirstName(profileUpdateRequestDto.firstName()); user.setLastName(profileUpdateRequestDto.lastName()); @@ -71,7 +64,6 @@ public ProfileUpdateResponseDto profileUpdate( @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return userRepository - .findByEmail(username).orElseThrow(() -> new UsernameNotFoundException("No such user!")); + return userRepository.findByEmail(username).orElseThrow(() -> new UsernameNotFoundException("No such user!")); } } \ No newline at end of file diff --git a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java new file mode 100644 index 00000000..9d2bcb4a --- /dev/null +++ b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java @@ -0,0 +1,153 @@ +package com.greenfoxacademy.backend.services.user; + + +import com.greenfoxacademy.backend.dtos.LoginRequestDto; +import com.greenfoxacademy.backend.dtos.ProfileUpdateRequestDto; +import com.greenfoxacademy.backend.dtos.RegisterRequestDto; +import com.greenfoxacademy.backend.errors.UserAlreadyExistsError; +import com.greenfoxacademy.backend.models.User; +import com.greenfoxacademy.backend.repositories.UserRepository; +import com.greenfoxacademy.backend.services.auth.AuthService; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class UserServiceImplTest { + private UserServiceImpl userService; + + private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + + @Mock + private UserRepository userRepository; + + @Mock + private AuthService authService; + + + @BeforeEach + void setUp() { + Mockito.reset(userRepository); + userService = new UserServiceImpl(userRepository, passwordEncoder, authService); + } + + @DisplayName("Register a new user if email not taken") + @Test + void register() { + // Given + User asSaved = User.builder().id(1).build(); + RegisterRequestDto registerRequestDto = new RegisterRequestDto("fistName", "lastName", "some@email.com", "SomePassword123"); + + // When + when(userRepository.save(any())).thenReturn(asSaved); + userService.register(registerRequestDto); + + // Then + Mockito.verify(userRepository, Mockito.times(1)).save(any()); + } + + @DisplayName("Does not register a new user if email is taken") + @Test + void registerFails() { + // Given + RegisterRequestDto registerRequestDto = new RegisterRequestDto("fistName", "lastName", "some@email.com", "SomePassword123"); + + // When + when(userRepository.save(any())).thenThrow(new UserAlreadyExistsError("Email is already taken!")); + + Assertions.assertThrows(UserAlreadyExistsError.class, () -> userService.register(registerRequestDto)); + + } + + @DisplayName("Login a user successfully") + @Test + void login() throws Exception { + // Given + User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + LoginRequestDto userLoginRequestDto = new LoginRequestDto("email", "password"); + + // When + when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); + when(authService.generateToken(any())).thenReturn("token"); + + userService.login(userLoginRequestDto); + + // Then + Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + } + + @DisplayName("Login a user unsuccessfully") + @Test + void loginUnsuccessful() throws Exception { + // Given + User user = User.builder().id(1).email("email").password(passwordEncoder.encode("passwordNOOP")).build(); + LoginRequestDto userLoginRequestDto = new LoginRequestDto("email", "password"); + + // When + when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); + + Assertions.assertThrows(Exception.class, () -> userService.login(userLoginRequestDto)); + + // Then + Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + } + + @DisplayName("Update a user profile successfully") + @Test + void profileUpdate() throws Exception { + // Given + User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + String email = "email"; + ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto("newEmail", "newFirstName", "newLastName", "newPassword"); + + // When + when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); + when(userRepository.save(any())).thenReturn(user); + + userService.profileUpdate(email, profileUpdateRequestDto); + + // Then + Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + } + + @DisplayName("Update a user profile unsuccessfully") + @Test + void profileUpdateUnsuccessful() throws Exception { + // Given + User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + String email = "email"; + ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto("newEmail", "newFirstName", "new@email.com", "newPassword"); + + // When + when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); + when(userRepository.existsByEmail("new@email.com")).thenReturn(true); + + // Then + Assertions.assertThrows(UserAlreadyExistsError.class, () -> userService.profileUpdate(email, profileUpdateRequestDto)); + } + + @Test + void loadUserByUsername() { + // Given + User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + String email = "email"; + // When + when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); + userService.loadUserByUsername(email); + // Then + Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + } +} \ No newline at end of file From d135c08674e06c69b29c5397f4e4db624ef04986 Mon Sep 17 00:00:00 2001 From: markkovari Date: Thu, 15 Aug 2024 23:57:29 +0200 Subject: [PATCH 6/7] fix: reduce long lines --- .../services/user/UserServiceImplTest.java | 92 +++++++++++++++---- 1 file changed, 75 insertions(+), 17 deletions(-) diff --git a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java index 9d2bcb4a..bf8c93d8 100644 --- a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java +++ b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java @@ -49,7 +49,11 @@ void setUp() { void register() { // Given User asSaved = User.builder().id(1).build(); - RegisterRequestDto registerRequestDto = new RegisterRequestDto("fistName", "lastName", "some@email.com", "SomePassword123"); + RegisterRequestDto registerRequestDto = new RegisterRequestDto( + "fistName", + "lastName", + "some@email.com", + "SomePassword123"); // When when(userRepository.save(any())).thenReturn(asSaved); @@ -63,12 +67,20 @@ void register() { @Test void registerFails() { // Given - RegisterRequestDto registerRequestDto = new RegisterRequestDto("fistName", "lastName", "some@email.com", "SomePassword123"); + RegisterRequestDto registerRequestDto = new RegisterRequestDto( + "fistName", + "lastName", + "some@email.com", + "SomePassword123"); // When - when(userRepository.save(any())).thenThrow(new UserAlreadyExistsError("Email is already taken!")); + when(userRepository.save(any())) + .thenThrow(new UserAlreadyExistsError("Email is already taken!")); - Assertions.assertThrows(UserAlreadyExistsError.class, () -> userService.register(registerRequestDto)); + Assertions.assertThrows( + UserAlreadyExistsError.class, + () -> userService.register(registerRequestDto) + ); } @@ -76,7 +88,11 @@ void registerFails() { @Test void login() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("password")) + .build(); LoginRequestDto userLoginRequestDto = new LoginRequestDto("email", "password"); // When @@ -86,15 +102,25 @@ void login() throws Exception { userService.login(userLoginRequestDto); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito.verify( + userRepository, + Mockito.times(1)).findByEmail(anyString() + ); } @DisplayName("Login a user unsuccessfully") @Test void loginUnsuccessful() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("passwordNOOP")).build(); - LoginRequestDto userLoginRequestDto = new LoginRequestDto("email", "password"); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("passwordNOOP")) + .build(); + LoginRequestDto userLoginRequestDto = new LoginRequestDto( + "email", + "password" + ); // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); @@ -102,16 +128,27 @@ void loginUnsuccessful() throws Exception { Assertions.assertThrows(Exception.class, () -> userService.login(userLoginRequestDto)); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito.verify( + userRepository, + Mockito.times(1)).findByEmail(anyString() + ); } @DisplayName("Update a user profile successfully") @Test void profileUpdate() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("password")) + .build(); String email = "email"; - ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto("newEmail", "newFirstName", "newLastName", "newPassword"); + ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto( + "newEmail", + "newFirstName", + "newLastName", + "newPassword"); // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); @@ -120,34 +157,55 @@ void profileUpdate() throws Exception { userService.profileUpdate(email, profileUpdateRequestDto); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito + .verify(userRepository, Mockito.times(1)) + .findByEmail(anyString()); } @DisplayName("Update a user profile unsuccessfully") @Test void profileUpdateUnsuccessful() throws Exception { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder().id(1).email("email") + .password(passwordEncoder.encode("password")) + .build(); String email = "email"; - ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto("newEmail", "newFirstName", "new@email.com", "newPassword"); + // @formatter:off + + ProfileUpdateRequestDto profileUpdateRequestDto = new ProfileUpdateRequestDto( + "newEmail", + "newFirstName", + "new@email.com", + "newPassword"); + // @formatter:on // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); when(userRepository.existsByEmail("new@email.com")).thenReturn(true); // Then - Assertions.assertThrows(UserAlreadyExistsError.class, () -> userService.profileUpdate(email, profileUpdateRequestDto)); + Assertions.assertThrows( + UserAlreadyExistsError.class, + () -> userService.profileUpdate(email, profileUpdateRequestDto) + ); } @Test void loadUserByUsername() { // Given - User user = User.builder().id(1).email("email").password(passwordEncoder.encode("password")).build(); + User user = User.builder() + .id(1) + .email("email") + .password(passwordEncoder.encode("password")) + .build(); String email = "email"; // When when(userRepository.findByEmail(anyString())).thenReturn(Optional.of(user)); userService.loadUserByUsername(email); // Then - Mockito.verify(userRepository, Mockito.times(1)).findByEmail(anyString()); + Mockito.verify( + userRepository, + Mockito.times(1) + ).findByEmail(anyString()); } } \ No newline at end of file From 73fb3b2f6db638df48da49319f2062cbd0860b2a Mon Sep 17 00:00:00 2001 From: markkovari Date: Fri, 16 Aug 2024 00:02:38 +0200 Subject: [PATCH 7/7] style: fix checkstyle errors :heart: --- .../backend/services/user/UserService.java | 5 +++- .../services/user/UserServiceImpl.java | 26 ++++++++++++++----- .../backend/config/JwtConfigTest.java | 15 ++++++----- .../services/user/UserServiceImplTest.java | 10 +++---- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java index 17e0b974..9f0064ad 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserService.java @@ -20,5 +20,8 @@ public interface UserService extends UserDetailsService { LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception; - ProfileUpdateResponseDto profileUpdate(String user, ProfileUpdateRequestDto profileUpdateRequestDto) throws Exception; + ProfileUpdateResponseDto profileUpdate( + String user, + ProfileUpdateRequestDto profileUpdateRequestDto + ) throws Exception; } diff --git a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java index 46f42a79..3328ce5c 100644 --- a/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java +++ b/backend/src/main/java/com/greenfoxacademy/backend/services/user/UserServiceImpl.java @@ -27,9 +27,17 @@ public class UserServiceImpl implements UserService { private final AuthService authService; @Override - public RegisterResponseDto register(RegisterRequestDto registerRequestDto) throws UserAlreadyExistsError { + public RegisterResponseDto register(RegisterRequestDto registerRequestDto) + throws UserAlreadyExistsError { - User user = User.builder().email(registerRequestDto.email()).firstName(registerRequestDto.firstName()).lastName(registerRequestDto.lastName()).password(passwordEncoder.encode(registerRequestDto.password())).build(); + // @formatter:off + User user = User.builder() + .email(registerRequestDto.email()) + .firstName(registerRequestDto.firstName()) + .lastName(registerRequestDto.lastName()) + .password(passwordEncoder.encode(registerRequestDto.password())) + .build(); + // @formatter:on try { return new RegisterResponseDto(userRepository.save(user).getId()); } catch (Exception e) { @@ -40,7 +48,8 @@ public RegisterResponseDto register(RegisterRequestDto registerRequestDto) throw @Override public LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception { - User user = userRepository.findByEmail(loginRequestDto.email()).orElseThrow(() -> new Exception("User not found")); + User user = userRepository.findByEmail(loginRequestDto.email()) + .orElseThrow(() -> new Exception("User not found")); if (!passwordEncoder.matches(loginRequestDto.password(), user.getPassword())) { throw new Exception("Invalid password"); } @@ -48,8 +57,12 @@ public LoginResponseDto login(LoginRequestDto loginRequestDto) throws Exception } @Override - public ProfileUpdateResponseDto profileUpdate(String email, ProfileUpdateRequestDto profileUpdateRequestDto) throws Exception { - User user = userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException("User not found")); + public ProfileUpdateResponseDto profileUpdate( + String email, + ProfileUpdateRequestDto profileUpdateRequestDto + ) { + User user = userRepository.findByEmail(email) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); if (userRepository.existsByEmail(profileUpdateRequestDto.email())) { throw new UserAlreadyExistsError("Email is already taken!"); } @@ -64,6 +77,7 @@ public ProfileUpdateResponseDto profileUpdate(String email, ProfileUpdateRequest @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - return userRepository.findByEmail(username).orElseThrow(() -> new UsernameNotFoundException("No such user!")); + return userRepository.findByEmail(username) + .orElseThrow(() -> new UsernameNotFoundException("No such user!")); } } \ No newline at end of file diff --git a/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java b/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java index b9e159bf..18185b33 100644 --- a/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java +++ b/backend/src/test/java/com/greenfoxacademy/backend/config/JwtConfigTest.java @@ -1,19 +1,20 @@ package com.greenfoxacademy.backend.config; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class JwtConfigTest { - @Autowired - private JwtConfig jwtConfig = new JwtConfig(); + @Autowired + private JwtConfig jwtConfig = new JwtConfig(); - @Test - void getExpirationTime() { - assertEquals(86400000, jwtConfig.getExpirationTime()); - } + @Test + void getExpirationTime() { + assertEquals(86400000, jwtConfig.getExpirationTime()); + } } \ No newline at end of file diff --git a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java index bf8c93d8..c081cae6 100644 --- a/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java +++ b/backend/src/test/java/com/greenfoxacademy/backend/services/user/UserServiceImplTest.java @@ -1,5 +1,8 @@ package com.greenfoxacademy.backend.services.user; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; import com.greenfoxacademy.backend.dtos.LoginRequestDto; import com.greenfoxacademy.backend.dtos.ProfileUpdateRequestDto; @@ -8,6 +11,7 @@ import com.greenfoxacademy.backend.models.User; import com.greenfoxacademy.backend.repositories.UserRepository; import com.greenfoxacademy.backend.services.auth.AuthService; +import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -19,12 +23,6 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; -import java.util.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class UserServiceImplTest { private UserServiceImpl userService;