-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69628a0
commit 43020f5
Showing
7 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/quemistry/auth_ms/config/RestClientConfig.java
This file contains 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,8 @@ | ||
package com.quemistry.auth_ms.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RestClientConfig { | ||
|
||
} |
This file contains 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 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 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
79 changes: 79 additions & 0 deletions
79
src/test/java/com/quemistry/auth_ms/controller/AuthenticationControllerTest.java
This file contains 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,79 @@ | ||
package com.quemistry.auth_ms.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.quemistry.auth_ms.model.TokenRequest; | ||
import com.quemistry.auth_ms.model.TokenResponse; | ||
import com.quemistry.auth_ms.model.UserProfile; | ||
import com.quemistry.auth_ms.service.AuthenticationService; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.mockito.BDDMockito.given; | ||
@WebMvcTest(AuthenticationController.class) | ||
public class AuthenticationControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
@MockBean | ||
private AuthenticationService authenticationService; | ||
|
||
private TokenRequest tokenRequest; | ||
|
||
private TokenResponse tokenResponse; | ||
|
||
private UserProfile user; | ||
@BeforeEach | ||
void init(){ | ||
tokenRequest = new TokenRequest(); | ||
tokenRequest.setClientId("testclientId"); | ||
tokenRequest.setAuthCode("testAuthCode"); | ||
tokenRequest.setCodeVerifier("testCodeVerifier"); | ||
tokenRequest.setRedirectUrl("testUrl"); | ||
|
||
//output | ||
tokenResponse = new TokenResponse(); | ||
tokenResponse.setIdToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJlbWFpbCI6InRlc3RVc2VyQGVtYWlsLmNvbSJ9.cI8ybuRu1FP7_jR9-mHuS2w9EBVueQRMR5DeF2C3pWc"); | ||
tokenResponse.setEmail("[email protected]"); | ||
|
||
user = new UserProfile(); | ||
user.setEmail("[email protected]"); | ||
} | ||
@Test | ||
void givenGetAccessToken_Success() throws Exception{ | ||
|
||
given(authenticationService.getAccessToken(tokenRequest)).willReturn(tokenResponse); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
|
||
var result = mockMvc.perform(post("/v1/auth") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(mapper.writeValueAsString(tokenRequest))) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
||
var setCookieHeader = result.getResponse().getHeader("Set-Cookie"); | ||
Assertions.assertNotNull(setCookieHeader); | ||
|
||
} | ||
|
||
@Test | ||
void givenSignOut_Success() throws Exception{ | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Set-Cookie", String.format("quemistry_session=%s; Max-Age=86400; Path=/; HttpOnly",tokenResponse)); | ||
|
||
mockMvc.perform(post("/v1/auth/SignOut") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.headers(headers)) | ||
.andExpect(status().isOk()); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/quemistry/auth_ms/service/AuthenticationServiceImplTest.java
This file contains 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,68 @@ | ||
package com.quemistry.auth_ms.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.quemistry.auth_ms.model.TokenRequest; | ||
import com.quemistry.auth_ms.model.TokenResponse; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.http.*; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URISyntaxException; | ||
|
||
@WebMvcTest(AuthenticationService.class) | ||
public class AuthenticationServiceImplTest { | ||
|
||
@Value("${quemistry.cognito.url}") | ||
private String QUEMISTRY_COGNITO_URL; | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@InjectMocks | ||
private AuthenticationServiceImpl authenticationService; | ||
|
||
@Test | ||
void givenGetAccessToken_Success() throws Exception{ | ||
TokenResponse tokenResponse = new TokenResponse(); | ||
|
||
//idtoken with email set as [email protected] | ||
tokenResponse.setIdToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJlbWFpbCI6InRlc3RVc2VyQGVtYWlsLmNvbSJ9.cI8ybuRu1FP7_jR9-mHuS2w9EBVueQRMR5DeF2C3pWc"); | ||
|
||
String tokenUri = "https://quemistry.auth.ap-southeast-1.amazoncognito.com/oauth2/token"; | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
TokenRequest tokenRequest = new TokenRequest(); | ||
tokenRequest.setClientId("testclientId"); | ||
tokenRequest.setAuthCode("testAuthCode"); | ||
tokenRequest.setCodeVerifier("testCodeVerifier"); | ||
tokenRequest.setRedirectUrl("testUrl"); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.toString()); | ||
|
||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); | ||
formData.add("grant_type","authorization_code"); | ||
formData.add("redirect_uri",tokenRequest.getRedirectUrl()); | ||
formData.add("client_id",tokenRequest.getClientId()); | ||
formData.add("code",tokenRequest.getAuthCode()); | ||
formData.add("code_verifier",tokenRequest.getCodeVerifier()); | ||
HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<>(formData, headers); | ||
|
||
Mockito.when(restTemplate.postForEntity(tokenUri, formEntity, TokenResponse.class)) | ||
.thenReturn(new ResponseEntity(tokenResponse, HttpStatus.OK)); | ||
|
||
var result = authenticationService.getAccessToken(tokenRequest); | ||
tokenResponse.setEmail("[email protected]"); | ||
Assertions.assertEquals(result, result ); | ||
} | ||
} |