Skip to content

Commit

Permalink
Test cases updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesBaserr committed May 13, 2024
1 parent a992124 commit 98abe2a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 106 deletions.
12 changes: 12 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AuthenticationController {
private final AuthenticationService authenticationService; // Authentication service

@PostMapping("/signup") // Sign up endpoint
public ResponseEntity<?> signup(
public ResponseEntity<ApiResponse<AuthenticationTokenResponse>> signup(
@RequestBody SignUpRequest request
) {
ApiResponse<AuthenticationTokenResponse> response = authenticationService.signup(request);
Expand All @@ -39,6 +39,12 @@ public ResponseEntity<?> signup(
public ResponseEntity<ApiResponse<AuthenticationTokenResponse>> signin(
@RequestBody SignInRequest request
) {
return ResponseEntity.ok(authenticationService.signin(request)); // Return response
ApiResponse<AuthenticationTokenResponse> response = authenticationService.signin(request);

if (response.getStatus() == 401) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
} else {
return ResponseEntity.ok(response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -31,123 +32,65 @@ public void setUp() {

@Test
public void shouldReturnSuccessOnValidSignin() {
SignInRequest signInRequest = new SignInRequest(
"testUser",
"testPassword"
);
ApiResponse<AuthenticationTokenResponse> apiResponse =
new ApiResponse<>(
200,
"Success",
new AuthenticationTokenResponse("token")
);
when(authenticationService.signin(signInRequest)).thenReturn(
apiResponse
);
ResponseEntity<
ApiResponse<AuthenticationTokenResponse>
> 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<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(200, "Success", new AuthenticationTokenResponse("token"));
when(authenticationService.signin(signInRequest)).thenReturn(apiResponse);

ResponseEntity<ApiResponse<AuthenticationTokenResponse>> 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<AuthenticationTokenResponse> apiResponse =
new ApiResponse<>(401, "Failure", null);
when(authenticationService.signin(signInRequest)).thenReturn(
apiResponse
);
ResponseEntity<
ApiResponse<AuthenticationTokenResponse>
> 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<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(401, "Invalid email/username or password.", null);
when(authenticationService.signin(signInRequest)).thenReturn(apiResponse);

ResponseEntity<ApiResponse<AuthenticationTokenResponse>> 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("[email protected]")
.username("newUser")
.country("USA")
.bio("Bio of the new user")
.password("newPassword")
.firstName("New")
.lastName("User")
.build();
ApiResponse<AuthenticationTokenResponse> apiResponse =
new ApiResponse<>(
200,
"Success",
new AuthenticationTokenResponse("token")
);
when(authenticationService.signup(signUpRequest)).thenReturn(
apiResponse
);
ResponseEntity<
ApiResponse<AuthenticationTokenResponse>
> 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("[email protected]")
.username("newUser")
.country("USA")
.bio("Bio of the new user")
.password("newPassword")
.firstName("New")
.lastName("User")
.build();
ApiResponse<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(201, "User registered successfully.", new AuthenticationTokenResponse("token"));
when(authenticationService.signup(signUpRequest)).thenReturn(apiResponse);

ResponseEntity<ApiResponse<AuthenticationTokenResponse>> 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("[email protected]")
.username("newUser")
.country("USA")
.bio("Bio of the new user")
.password("newPassword")
.firstName("New")
.lastName("User")
.build();
ApiResponse<AuthenticationTokenResponse> apiResponse =
new ApiResponse<>(409, "Failure", null);
when(authenticationService.signup(signUpRequest)).thenReturn(
apiResponse
);
ResponseEntity<
ApiResponse<AuthenticationTokenResponse>
> 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("[email protected]")
.username("newUser")
.country("USA")
.bio("Bio of the new user")
.password("newPassword")
.firstName("New")
.lastName("User")
.build();
ApiResponse<AuthenticationTokenResponse> apiResponse = new ApiResponse<>(409, "Email or username already exists.", null);
when(authenticationService.signup(signUpRequest)).thenReturn(apiResponse);

ResponseEntity<ApiResponse<AuthenticationTokenResponse>> 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");
}
}

0 comments on commit 98abe2a

Please sign in to comment.