Skip to content

Commit

Permalink
test: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Campos committed Feb 8, 2024
1 parent a2fa234 commit 744a815
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 15 deletions.
4 changes: 4 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>

<!-- Documentation -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,10 @@ public String getLoggedUserIdirOrBceId() {
}

UserInfo userInfo = userInfoOp.get();
switch (userInfo.identityProvider()) {
case IDIR:
{
return userInfo.idirUsername();
}
case BUSINESS_BCEID:
{
return userInfo.businessName();
}
default:
{
return "";
}
if (IdentityProvider.IDIR.equals(userInfo.identityProvider())) {
return userInfo.idirUsername();
}

return userInfo.businessName();
}
}
6 changes: 5 additions & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(OpeningEndpoint.class)
@WithMockUser(roles = "user_read")
class OpeningEndpointTest {

@Autowired private MockMvc mockMvc;
Expand Down
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());
}
}
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);
});
}
}
Loading

0 comments on commit 744a815

Please sign in to comment.