-
-
Notifications
You must be signed in to change notification settings - Fork 27
Task #21 #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iccaprar
wants to merge
2
commits into
code4romania:develop
Choose a base branch
from
iccaprar:task/21-auth-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Task #21 #43
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
api/src/main/java/com/code4ro/nextdoor/core/exception/handler/AuthExceptionHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.code4ro.nextdoor.core.exception.handler; | ||
|
|
||
| import com.code4ro.nextdoor.authentication.controller.AuthenticationController; | ||
| import com.code4ro.nextdoor.core.exception.ExceptionResponse; | ||
| import com.code4ro.nextdoor.core.exception.I18nError; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.authentication.BadCredentialsException; | ||
| import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
|
||
| import java.util.Collections; | ||
|
|
||
| @Order(Ordered.HIGHEST_PRECEDENCE) | ||
| @ControllerAdvice(assignableTypes = AuthenticationController.class) | ||
| public class AuthExceptionHandler extends GlobalExceptionHandler { | ||
|
|
||
| @ExceptionHandler(BadCredentialsException.class) | ||
| protected ResponseEntity<Object> handleBadCredentials(final BadCredentialsException ex) { | ||
| final ExceptionResponse exceptionResponse = new ExceptionResponse(); | ||
| exceptionResponse.setI18nErrors(Collections.singletonList( | ||
| new I18nError("login.bad.credentials", null))); | ||
| return new ResponseEntity<>(exceptionResponse, HttpStatus.UNAUTHORIZED); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
122 changes: 122 additions & 0 deletions
122
...m/code4ro/nextdoor/authentication/controller/AuthenticationControllerIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package com.code4ro.nextdoor.authentication.controller; | ||
|
|
||
| import com.code4ro.nextdoor.authentication.dto.LoginRequest; | ||
| import com.code4ro.nextdoor.authentication.dto.RegistrationRequest; | ||
| import com.code4ro.nextdoor.common.controller.AbstractControllerIntegrationTest; | ||
| import com.code4ro.nextdoor.core.exception.ExceptionResponse; | ||
| import com.code4ro.nextdoor.util.RandomObjectFiller; | ||
| import org.assertj.core.api.SoftAssertions; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.test.context.jdbc.Sql; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @DisplayName("Integration test for /api/authentication endpoint") | ||
| class AuthenticationControllerIntegrationTest extends AbstractControllerIntegrationTest { | ||
|
|
||
| @DisplayName("tests registration scenarios") | ||
| @Test | ||
| void register() throws MalformedURLException { | ||
| RegistrationRequest registrationRequest = RandomObjectFiller.createAndFill(RegistrationRequest.class); | ||
| HttpEntity<RegistrationRequest> entity = new HttpEntity<>(registrationRequest, headers); | ||
|
|
||
| ResponseEntity<Void> response = restTemplate.postForEntity( | ||
| new URL(getBaseUrl() + "/register").toString(), entity, Void.class); | ||
|
|
||
| SoftAssertions.assertSoftly(softly -> { | ||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); | ||
| assertThat(response.getHeaders().getLocation()).isNotNull(); | ||
| assertThat(response.getHeaders().getLocation().toString().contains("/api/users/")).isTrue(); | ||
| }); | ||
|
|
||
| // TODO: we could now call the users API, when that gets implemented, and check if we get the actual user | ||
|
|
||
| // Try second registration with the same data, should result in error | ||
|
|
||
| ResponseEntity<ExceptionResponse> response_duplicate = restTemplate.postForEntity( | ||
| new URL(getBaseUrl() + "/register").toString(), entity, ExceptionResponse.class); | ||
|
|
||
| SoftAssertions.assertSoftly(softly -> { | ||
| assertThat(response_duplicate).isNotNull(); | ||
| assertThat(response_duplicate.getStatusCode()).isEqualTo(HttpStatus.CONFLICT); | ||
| assertThat(response_duplicate.getHeaders().getLocation()).isNull(); | ||
| assertThat(response_duplicate.getBody()).isNotNull(); | ||
| assertThat(response_duplicate.getBody().getI18nErrors().size()).isEqualTo(1); | ||
| assertThat(response_duplicate.getBody().getI18nErrors().get(0).getI18nErrorKey()).isEqualTo("user.duplicate.email"); | ||
| }); | ||
| } | ||
|
|
||
| @DisplayName("tests successful login") | ||
| @Sql(scripts = "AuthenticationControllerIntegrationTest.sql") | ||
| @Test | ||
| void login_success() throws MalformedURLException { | ||
| LoginRequest loginRequest = new LoginRequest(); | ||
| loginRequest.setEmail("test@test.com"); | ||
| loginRequest.setPassword("pass"); | ||
| HttpEntity<LoginRequest> entity = new HttpEntity<>(loginRequest, headers); | ||
|
|
||
| ResponseEntity<Void> response = restTemplate.postForEntity( | ||
| new URL(getBaseUrl() + "/login").toString(), entity, Void.class); | ||
|
|
||
| SoftAssertions.assertSoftly(softly -> { | ||
| assertThat(response).isNotNull(); | ||
| assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
| }); | ||
| } | ||
|
|
||
| @DisplayName("tests wrong password login") | ||
| @Sql(scripts = "AuthenticationControllerIntegrationTest.sql") | ||
| @Test | ||
| void login_bad_password() throws MalformedURLException { | ||
| LoginRequest loginRequestWrongPass = new LoginRequest(); | ||
| loginRequestWrongPass.setEmail("test@test.com"); | ||
| loginRequestWrongPass.setPassword("wrongpass"); | ||
| HttpEntity<LoginRequest> entity = new HttpEntity<>(loginRequestWrongPass, headers); | ||
|
|
||
| ResponseEntity<ExceptionResponse> response_wrong_pass = restTemplate.postForEntity( | ||
| new URL(getBaseUrl() + "/login").toString(), entity, ExceptionResponse.class); | ||
|
|
||
| SoftAssertions.assertSoftly(softly -> { | ||
| assertThat(response_wrong_pass).isNotNull(); | ||
| assertThat(response_wrong_pass.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); | ||
| assertThat(response_wrong_pass.getBody()).isNotNull(); | ||
| assertThat(response_wrong_pass.getBody().getI18nErrors().size()).isEqualTo(1); | ||
| assertThat(response_wrong_pass.getBody().getI18nErrors().get(0).getI18nErrorKey()).isEqualTo("login.bad.credentials"); | ||
| }); | ||
| } | ||
|
|
||
| @DisplayName("tests wrong username login") | ||
| @Sql(scripts = "AuthenticationControllerIntegrationTest.sql") | ||
| @Test | ||
| void login_bad_username() throws MalformedURLException { | ||
| LoginRequest loginRequestWrongUser = new LoginRequest(); | ||
| loginRequestWrongUser.setEmail("testwrong@test.com"); | ||
| loginRequestWrongUser.setPassword("pass"); | ||
| HttpEntity<LoginRequest> entity = new HttpEntity<>(loginRequestWrongUser, headers); | ||
|
|
||
| ResponseEntity<ExceptionResponse> response_wrong_user = restTemplate.postForEntity( | ||
| new URL(getBaseUrl() + "/login").toString(), entity, ExceptionResponse.class); | ||
|
|
||
| SoftAssertions.assertSoftly(softly -> { | ||
| assertThat(response_wrong_user).isNotNull(); | ||
| assertThat(response_wrong_user.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); | ||
| assertThat(response_wrong_user.getBody()).isNotNull(); | ||
| assertThat(response_wrong_user.getBody().getI18nErrors().size()).isEqualTo(1); | ||
| assertThat(response_wrong_user.getBody().getI18nErrors().get(0).getI18nErrorKey()).isEqualTo("login.bad.credentials"); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getBaseUrl() { | ||
| return "http://localhost:" + port + "/api/authentication"; | ||
| } | ||
| } |
144 changes: 144 additions & 0 deletions
144
...java/com/code4ro/nextdoor/authentication/controller/AuthenticationControllerUnitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package com.code4ro.nextdoor.authentication.controller; | ||
|
|
||
| import com.code4ro.nextdoor.authentication.dto.LoginRequest; | ||
| import com.code4ro.nextdoor.authentication.dto.RegistrationRequest; | ||
| import com.code4ro.nextdoor.authentication.entity.User; | ||
| import com.code4ro.nextdoor.authentication.service.AuthenticationService; | ||
| import com.code4ro.nextdoor.authentication.service.impl.CustomUserDetailsService; | ||
| import com.code4ro.nextdoor.common.controller.AbstractControllerUnitTest; | ||
| import com.code4ro.nextdoor.core.exception.NextDoorValidationException; | ||
| import org.assertj.core.api.SoftAssertions; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.security.authentication.AuthenticationManager; | ||
| import org.springframework.security.authentication.BadCredentialsException; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.Authentication; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.hamcrest.core.StringEndsWith.endsWith; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| @DisplayName("Unit test for AuthenticationController") | ||
| @WebMvcTest(AuthenticationController.class) | ||
| public class AuthenticationControllerUnitTest extends AbstractControllerUnitTest { | ||
|
|
||
| private static final String EMAIL = "test@test.com"; | ||
| private static final String PASSWORD = "pass"; | ||
| private static final UUID USER_ID = new UUID(10, 100); | ||
|
|
||
| @MockBean | ||
| private AuthenticationService authenticationService; | ||
|
|
||
| @MockBean | ||
| protected CustomUserDetailsService customUserDetailsService; | ||
|
|
||
| @MockBean | ||
| protected AuthenticationManager authenticationManager; | ||
|
|
||
| @Test | ||
| void register_success() throws Exception { | ||
| RegistrationRequest registrationRequest = new RegistrationRequest(); | ||
| registrationRequest.setEmail(EMAIL); | ||
| registrationRequest.setPassword(PASSWORD); | ||
|
|
||
| User user = new User(EMAIL, PASSWORD); | ||
| user.setId(USER_ID); | ||
|
|
||
| when(authenticationService.register(any(RegistrationRequest.class))).thenReturn(user); | ||
|
|
||
| String json = objectMapper.writeValueAsString(registrationRequest); | ||
|
|
||
| mvc.perform(post("/api/authentication/register") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(json) | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isCreated()) | ||
| .andExpect(header().string(HttpHeaders.LOCATION, endsWith("/api/users/" + USER_ID))); | ||
|
|
||
| ArgumentCaptor<RegistrationRequest> requestArgumentCaptor = ArgumentCaptor.forClass(RegistrationRequest.class); | ||
| verify(authenticationService, times(1)).register(requestArgumentCaptor.capture()); | ||
|
|
||
| SoftAssertions.assertSoftly(softly -> { | ||
| assertThat(requestArgumentCaptor.getValue()).isNotNull(); | ||
| assertThat(requestArgumentCaptor.getValue().getEmail()).isEqualTo(EMAIL); | ||
| assertThat(requestArgumentCaptor.getValue().getPassword()).isEqualTo(PASSWORD); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void register_duplicate_error() throws Exception { | ||
| RegistrationRequest registrationRequest = new RegistrationRequest(); | ||
| registrationRequest.setEmail(EMAIL); | ||
| registrationRequest.setPassword(PASSWORD); | ||
|
|
||
| User user = new User(EMAIL, PASSWORD); | ||
| user.setId(USER_ID); | ||
|
|
||
| when(authenticationService.register(any(RegistrationRequest.class))) | ||
| .thenThrow(new NextDoorValidationException("user.duplicate.email", HttpStatus.CONFLICT)); | ||
|
|
||
| String json = objectMapper.writeValueAsString(registrationRequest); | ||
|
|
||
| mvc.perform(post("/api/authentication/register") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(json) | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isConflict()) | ||
| .andExpect(jsonPath("$.i18nErrors[0].i18nErrorKey") | ||
| .value("user.duplicate.email")); | ||
| } | ||
|
|
||
| @Test | ||
| void login_success() throws Exception { | ||
| LoginRequest loginRequest = new LoginRequest(); | ||
| loginRequest.setEmail(EMAIL); | ||
| loginRequest.setPassword(PASSWORD); | ||
|
|
||
| Authentication authentication = new UsernamePasswordAuthenticationToken(EMAIL, PASSWORD); | ||
| when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class))).thenReturn(authentication); | ||
|
|
||
| String json = objectMapper.writeValueAsString(loginRequest); | ||
|
|
||
| mvc.perform(post("/api/authentication/login") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(json) | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()); | ||
| } | ||
|
|
||
| @Test | ||
| void login_badCredentials_error() throws Exception { | ||
| LoginRequest loginRequest = new LoginRequest(); | ||
| loginRequest.setEmail(EMAIL); | ||
| loginRequest.setPassword(PASSWORD); | ||
|
|
||
| when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class))) | ||
| .thenThrow(new BadCredentialsException("Invalid credentials")); | ||
|
|
||
| String json = objectMapper.writeValueAsString(loginRequest); | ||
|
|
||
| mvc.perform(post("/api/authentication/login") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(json) | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isUnauthorized()) | ||
| .andExpect(jsonPath("$.i18nErrors[0].i18nErrorKey") | ||
| .value("login.bad.credentials")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.