generated from bcgov/quickstart-openshift
-
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
Ricardo Campos
committed
Feb 8, 2024
1 parent
a2fa234
commit 744a815
Showing
9 changed files
with
442 additions
and
15 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,8 @@ nr-results-team-email-address = [email protected] | |
# Certificate for the Database | ||
ca.bc.gov.nrs.oracle.keystore = ${ORACLEDB_KEYSTORE:jssecacerts-path} | ||
ca.bc.gov.nrs.oracle.secret = ${ORACLEDB_SECRET:changeit} | ||
ca.bc.gov.nrs.oracle.host = ${DATABASE_HOST} | ||
ca.bc.gov.nrs.oracle.host = ${DATABASE_HOST} | ||
|
||
# FAM | ||
spring.security.oauth2.resourceserver.jwt.issuer-uri = ${AWS_COGNITO_ISSUER_URI:aws-cognito-any-url.com} | ||
spring.security.oauth2.resourceserver.jwt.jwk-set-uri = ${AWS_COGNITO_ISSUER_URI:aws-cognito-any-url.com}/.well-known/jwks.json |
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
113 changes: 113 additions & 0 deletions
113
backend/src/test/java/ca/bc/gov/restapi/results/security/UserAuthenticationHelperTest.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,113 @@ | ||
package ca.bc.gov.restapi.results.security; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
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.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
class UserAuthenticationHelperTest { | ||
|
||
private UserAuthenticationHelper userAuthenticationHelper; | ||
|
||
@BeforeEach | ||
void setup() { | ||
userAuthenticationHelper = new UserAuthenticationHelper(); | ||
} | ||
|
||
@Test | ||
@DisplayName("getUserInfoIdirTest") | ||
void getUserInfoIdirTest() { | ||
Authentication authentication = mock(Authentication.class); | ||
SecurityContext securityContext = mock(SecurityContext.class); | ||
SecurityContextHolder.setContext(securityContext); | ||
|
||
when(securityContext.getAuthentication()).thenReturn(authentication); | ||
when(authentication.isAuthenticated()).thenReturn(true); | ||
|
||
Jwt.Builder builder = Jwt.withTokenValue("myTokenValue"); | ||
builder.subject("BAGGINGS"); | ||
builder.header("alg", "HS256"); | ||
builder.header("typ", "JWT"); | ||
builder.claim("email", "[email protected]"); | ||
builder.claim("custom:idp_display_name", "from Baggings, Bilbo LWRS:EX"); | ||
builder.claim("custom:idp_username", "BAGGINGS"); | ||
builder.claim("custom:idp_name", "idir"); | ||
builder.claim("cognito:username", "IDIR@BAGGINGS"); | ||
builder.claim("client_roles", List.of("admin", "manager")); | ||
|
||
when(authentication.getPrincipal()).thenReturn(builder.build()); | ||
|
||
Optional<UserInfo> userInfoOptional = userAuthenticationHelper.getUserInfo(); | ||
Assertions.assertTrue(userInfoOptional.isPresent()); | ||
|
||
UserInfo userInfo = userInfoOptional.get(); | ||
Assertions.assertEquals("IDIR@BAGGINGS", userInfo.id()); | ||
Assertions.assertEquals("Bilbo", userInfo.firstName()); | ||
Assertions.assertEquals("Baggings", userInfo.lastName()); | ||
Assertions.assertEquals("[email protected]", userInfo.email()); | ||
Assertions.assertEquals("from Baggings, Bilbo LWRS:EX", userInfo.displayName()); | ||
Assertions.assertEquals("BAGGINGS", userInfo.idirUsername()); | ||
Assertions.assertEquals(IdentityProvider.IDIR, userInfo.identityProvider()); | ||
Assertions.assertEquals(2, userInfo.roles().size()); | ||
} | ||
|
||
@Test | ||
@DisplayName("getUserInfoBusinessBceidTest") | ||
void getUserInfoBusinessBceidTest() { | ||
Authentication authentication = mock(Authentication.class); | ||
SecurityContext securityContext = mock(SecurityContext.class); | ||
SecurityContextHolder.setContext(securityContext); | ||
|
||
when(securityContext.getAuthentication()).thenReturn(authentication); | ||
when(authentication.isAuthenticated()).thenReturn(true); | ||
|
||
Jwt.Builder builder = Jwt.withTokenValue("myTokenValue"); | ||
builder.subject("MORDOR-BCEID"); | ||
builder.header("alg", "HS256"); | ||
builder.header("typ", "JWT"); | ||
builder.claim("email", "[email protected]"); | ||
builder.claim("custom:idp_display_name", "Lord Sauron of Mordor"); | ||
builder.claim("custom:idp_username", "MORDOR-BCEID"); | ||
builder.claim("custom:idp_name", "bceidbusiness"); | ||
builder.claim("cognito:username", "BCEIDBUSINESS@MORDOR-BCEID"); | ||
|
||
when(authentication.getPrincipal()).thenReturn(builder.build()); | ||
|
||
Optional<UserInfo> userInfoOptional = userAuthenticationHelper.getUserInfo(); | ||
Assertions.assertTrue(userInfoOptional.isPresent()); | ||
|
||
UserInfo userInfo = userInfoOptional.get(); | ||
Assertions.assertEquals("BCEIDBUSINESS@MORDOR-BCEID", userInfo.id()); | ||
Assertions.assertEquals("Lord", userInfo.firstName()); | ||
Assertions.assertEquals("Sauron of Mordor", userInfo.lastName()); | ||
Assertions.assertEquals("[email protected]", userInfo.email()); | ||
Assertions.assertEquals("Lord Sauron of Mordor", userInfo.displayName()); | ||
Assertions.assertEquals("MORDOR-BCEID", userInfo.businessName()); | ||
Assertions.assertEquals(IdentityProvider.BUSINESS_BCEID, userInfo.identityProvider()); | ||
} | ||
|
||
@Test | ||
@DisplayName("getUserInfoTestNotAuthenticated") | ||
void getUserInfoTestNotAuthenticated() { | ||
Authentication authentication = mock(Authentication.class); | ||
SecurityContext securityContext = mock(SecurityContext.class); | ||
SecurityContextHolder.setContext(securityContext); | ||
|
||
when(securityContext.getAuthentication()).thenReturn(authentication); | ||
|
||
Optional<UserInfo> userInfoOptional = userAuthenticationHelper.getUserInfo(); | ||
Assertions.assertFalse(userInfoOptional.isPresent()); | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
backend/src/test/java/ca/bc/gov/restapi/results/security/UserInfoTest.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,142 @@ | ||
package ca.bc.gov.restapi.results.security; | ||
|
||
import java.util.Set; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class UserInfoTest { | ||
|
||
@Test | ||
@DisplayName("createUserInfo") | ||
void createUserInfo() { | ||
UserInfo userInfo = | ||
new UserInfo( | ||
"123456789@idir", | ||
"Bilbo", | ||
"Baggings", | ||
"[email protected]", | ||
"Baggings, Bilbo LWRS:EX", | ||
"BAGGINGS", | ||
null, | ||
IdentityProvider.IDIR, | ||
Set.of(), | ||
"abcdef123456789"); | ||
|
||
Assertions.assertNotNull(userInfo); | ||
Assertions.assertEquals("Bilbo", userInfo.firstName()); | ||
Assertions.assertEquals("Baggings", userInfo.lastName()); | ||
Assertions.assertEquals("[email protected]", userInfo.email()); | ||
Assertions.assertEquals("Baggings, Bilbo LWRS:EX", userInfo.displayName()); | ||
Assertions.assertEquals("BAGGINGS", userInfo.idirUsername()); | ||
Assertions.assertNull(userInfo.businessName()); | ||
Assertions.assertEquals(IdentityProvider.IDIR, userInfo.identityProvider()); | ||
Assertions.assertTrue(userInfo.roles().isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("createInvalidNullUser") | ||
void createInvalidNullUser() { | ||
// Id not null | ||
Assertions.assertThrows( | ||
NullPointerException.class, | ||
() -> { | ||
new UserInfo( | ||
null, | ||
"Bilbo", | ||
"Baggings", | ||
"[email protected]", | ||
"Baggings, Bilbo LWRS:EX", | ||
"BAGGINGS", | ||
null, | ||
IdentityProvider.IDIR, | ||
Set.of(), | ||
"abcdef123456789"); | ||
}); | ||
|
||
// E-mail not null | ||
Assertions.assertThrows( | ||
NullPointerException.class, | ||
() -> { | ||
new UserInfo( | ||
"123456789@idir", | ||
"Bilbo", | ||
"Baggings", | ||
null, | ||
"Baggings, Bilbo LWRS:EX", | ||
"BAGGINGS", | ||
null, | ||
IdentityProvider.IDIR, | ||
Set.of(), | ||
"abcdef123456789"); | ||
}); | ||
|
||
// Display name not null | ||
Assertions.assertThrows( | ||
NullPointerException.class, | ||
() -> { | ||
new UserInfo( | ||
"123456789@idir", | ||
"Bilbo", | ||
"Baggings", | ||
"[email protected]", | ||
null, | ||
"BAGGINGS", | ||
null, | ||
IdentityProvider.IDIR, | ||
Set.of(), | ||
"abcdef123456789"); | ||
}); | ||
|
||
// Identity provider not null | ||
Assertions.assertThrows( | ||
NullPointerException.class, | ||
() -> { | ||
new UserInfo( | ||
"123456789@idir", | ||
"Bilbo", | ||
"Baggings", | ||
"[email protected]", | ||
"Baggings, Bilbo LWRS:EX", | ||
"BAGGINGS", | ||
null, | ||
null, | ||
Set.of(), | ||
"abcdef123456789"); | ||
}); | ||
|
||
// Roles not null | ||
Assertions.assertThrows( | ||
NullPointerException.class, | ||
() -> { | ||
new UserInfo( | ||
"123456789@idir", | ||
"Bilbo", | ||
"Baggings", | ||
"[email protected]", | ||
"Baggings, Bilbo LWRS:EX", | ||
"BAGGINGS", | ||
null, | ||
IdentityProvider.IDIR, | ||
null, | ||
"abcdef123456789"); | ||
}); | ||
|
||
// Token not null | ||
Assertions.assertThrows( | ||
NullPointerException.class, | ||
() -> { | ||
new UserInfo( | ||
"123456789@idir", | ||
"Bilbo", | ||
"Baggings", | ||
"[email protected]", | ||
"Baggings, Bilbo LWRS:EX", | ||
"BAGGINGS", | ||
null, | ||
IdentityProvider.IDIR, | ||
Set.of(), | ||
null); | ||
}); | ||
} | ||
} |
Oops, something went wrong.