From 98abe2a9737297cfa68f14be4e543563506bdbe3 Mon Sep 17 00:00:00 2001 From: EnesBaserr Date: Mon, 13 May 2024 23:25:18 +0300 Subject: [PATCH] Test cases updated. --- backend/pom.xml | 12 ++ .../controllers/AuthenticationController.java | 10 +- .../AuthenticationControllerTest.java | 151 ++++++------------ 3 files changed, 67 insertions(+), 106 deletions(-) diff --git a/backend/pom.xml b/backend/pom.xml index 470caae3..81791029 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -21,6 +21,18 @@ org.springframework.boot spring-boot-starter-security + + org.mockito + mockito-core + 5.2.0 + test + + + org.mockito + mockito-junit-jupiter + 5.2.0 + test + org.apache.jena apache-jena-libs diff --git a/backend/src/main/java/com/group1/cuisines/controllers/AuthenticationController.java b/backend/src/main/java/com/group1/cuisines/controllers/AuthenticationController.java index 9efcd63c..777b70ad 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/AuthenticationController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/AuthenticationController.java @@ -21,7 +21,7 @@ public class AuthenticationController { private final AuthenticationService authenticationService; // Authentication service @PostMapping("/signup") // Sign up endpoint - public ResponseEntity signup( + public ResponseEntity> signup( @RequestBody SignUpRequest request ) { ApiResponse response = authenticationService.signup(request); @@ -39,6 +39,12 @@ public ResponseEntity signup( public ResponseEntity> signin( @RequestBody SignInRequest request ) { - return ResponseEntity.ok(authenticationService.signin(request)); // Return response + ApiResponse response = authenticationService.signin(request); + + if (response.getStatus() == 401) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response); + } else { + return ResponseEntity.ok(response); + } } } diff --git a/backend/src/test/java/com/group1/cuisines/AuthenticationControllerTest.java b/backend/src/test/java/com/group1/cuisines/AuthenticationControllerTest.java index d3d6218b..4aa77e4c 100644 --- a/backend/src/test/java/com/group1/cuisines/AuthenticationControllerTest.java +++ b/backend/src/test/java/com/group1/cuisines/AuthenticationControllerTest.java @@ -14,6 +14,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; public class AuthenticationControllerTest { @@ -31,123 +32,65 @@ public void setUp() { @Test public void shouldReturnSuccessOnValidSignin() { - SignInRequest signInRequest = new SignInRequest( - "testUser", - "testPassword" - ); - ApiResponse apiResponse = - new ApiResponse<>( - 200, - "Success", - new AuthenticationTokenResponse("token") - ); - when(authenticationService.signin(signInRequest)).thenReturn( - apiResponse - ); - ResponseEntity< - ApiResponse - > responseEntity = authenticationController.signin(signInRequest); - assertEquals( - 200, - responseEntity.getBody().getStatus(), - "Status code does not match expected value on successful signin" - ); - assertEquals( - apiResponse, - responseEntity.getBody(), - "Response body does not match expected value on successful signin" - ); + SignInRequest signInRequest = new SignInRequest("testUser", "testPassword"); + ApiResponse apiResponse = new ApiResponse<>(200, "Success", new AuthenticationTokenResponse("token")); + when(authenticationService.signin(signInRequest)).thenReturn(apiResponse); + + ResponseEntity> responseEntity = authenticationController.signin(signInRequest); + + assertEquals(HttpStatus.OK.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on successful signin"); + assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on successful signin"); } @Test public void shouldReturnFailureOnInvalidSignin() { - SignInRequest signInRequest = new SignInRequest( - "testUser", - "wrongPassword" - ); - ApiResponse apiResponse = - new ApiResponse<>(401, "Failure", null); - when(authenticationService.signin(signInRequest)).thenReturn( - apiResponse - ); - ResponseEntity< - ApiResponse - > responseEntity = authenticationController.signin(signInRequest); - assertEquals( - 401, - responseEntity.getBody().getStatus(), - "Status code does not match expected value on failed signin" - ); - assertEquals( - apiResponse, - responseEntity.getBody(), - "Response body does not match expected value on failed signin" - ); + SignInRequest signInRequest = new SignInRequest("testUser", "wrongPassword"); + ApiResponse apiResponse = new ApiResponse<>(401, "Invalid email/username or password.", null); + when(authenticationService.signin(signInRequest)).thenReturn(apiResponse); + + ResponseEntity> responseEntity = authenticationController.signin(signInRequest); + + assertEquals(HttpStatus.UNAUTHORIZED.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on failed signin"); + assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on failed signin"); } @Test public void shouldReturnSuccessOnValidSignup() { SignUpRequest signUpRequest = SignUpRequest.builder() - .email("newUser@gmail.com") - .username("newUser") - .country("USA") - .bio("Bio of the new user") - .password("newPassword") - .firstName("New") - .lastName("User") - .build(); - ApiResponse apiResponse = - new ApiResponse<>( - 200, - "Success", - new AuthenticationTokenResponse("token") - ); - when(authenticationService.signup(signUpRequest)).thenReturn( - apiResponse - ); - ResponseEntity< - ApiResponse - > responseEntity = authenticationController.signup(signUpRequest); - assertEquals( - 200, - responseEntity.getBody().getStatus(), - "Status code does not match expected value on successful signup" - ); - assertEquals( - apiResponse, - responseEntity.getBody(), - "Response body does not match expected value on successful signup" - ); + .email("newUser@gmail.com") + .username("newUser") + .country("USA") + .bio("Bio of the new user") + .password("newPassword") + .firstName("New") + .lastName("User") + .build(); + ApiResponse apiResponse = new ApiResponse<>(201, "User registered successfully.", new AuthenticationTokenResponse("token")); + when(authenticationService.signup(signUpRequest)).thenReturn(apiResponse); + + ResponseEntity> responseEntity = authenticationController.signup(signUpRequest); + + assertEquals(HttpStatus.CREATED.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on successful signup"); + assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on successful signup"); } @Test public void shouldReturnFailureOnInvalidSignup() { SignUpRequest signUpRequest = SignUpRequest.builder() - .email("newUser@gmail.com") - .username("newUser") - .country("USA") - .bio("Bio of the new user") - .password("newPassword") - .firstName("New") - .lastName("User") - .build(); - ApiResponse apiResponse = - new ApiResponse<>(409, "Failure", null); - when(authenticationService.signup(signUpRequest)).thenReturn( - apiResponse - ); - ResponseEntity< - ApiResponse - > responseEntity = authenticationController.signup(signUpRequest); - assertEquals( - 409, - responseEntity.getBody().getStatus(), - "Status code does not match expected value on failed signup" - ); - assertEquals( - apiResponse, - responseEntity.getBody(), - "Response body does not match expected value on failed signup" - ); + .email("newUser@gmail.com") + .username("newUser") + .country("USA") + .bio("Bio of the new user") + .password("newPassword") + .firstName("New") + .lastName("User") + .build(); + ApiResponse apiResponse = new ApiResponse<>(409, "Email or username already exists.", null); + when(authenticationService.signup(signUpRequest)).thenReturn(apiResponse); + + ResponseEntity> responseEntity = authenticationController.signup(signUpRequest); + + assertEquals(HttpStatus.CONFLICT.value(), responseEntity.getStatusCodeValue(), "Status code does not match expected value on failed signup"); + assertEquals(apiResponse, responseEntity.getBody(), "Response body does not match expected value on failed signup"); } }