-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
11 changed files
with
194 additions
and
27 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
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
84 changes: 84 additions & 0 deletions
84
...i/src/test/java/ca/bc/gov/open/jag/aireviewerapi/config/KeycloakJwtAuthConverterTest.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,84 @@ | ||
package ca.bc.gov.open.jag.aireviewerapi.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
public class KeycloakJwtAuthConverterTest { | ||
|
||
@Test | ||
void testNull() { | ||
KeycloakJwtAuthConverter converter = new KeycloakJwtAuthConverter(null); | ||
assertThrows(NullPointerException.class, () -> converter.convert(null)); | ||
} | ||
|
||
@Test | ||
void testJwtMissingResourceAccess() throws Exception { | ||
Jwt jwt = Mockito.mock(Jwt.class); | ||
|
||
KeycloakJwtAuthConverter converter = new KeycloakJwtAuthConverter(null); | ||
AbstractAuthenticationToken authenticationToken = converter.convert(jwt); | ||
Collection<GrantedAuthority> authorities = authenticationToken.getAuthorities(); | ||
assertEquals(0, authorities.size()); | ||
} | ||
|
||
@Test | ||
void testJwtMissingResource() throws Exception { | ||
Jwt jwt = Mockito.mock(Jwt.class); | ||
Map<String, Object> resourceAccess = new HashMap<String, Object>(); | ||
when(jwt.getClaim(KeycloakJwtAuthConverter.KEYCLOAK_RESOURCE_ATTRIBUTE)).thenReturn(resourceAccess); | ||
|
||
KeycloakJwtAuthConverter converter = new KeycloakJwtAuthConverter(null); | ||
AbstractAuthenticationToken authenticationToken = converter.convert(jwt); | ||
Collection<GrantedAuthority> authorities = authenticationToken.getAuthorities(); | ||
assertEquals(0, authorities.size()); | ||
} | ||
|
||
@Test | ||
void testJwtMissingRoles() throws Exception { | ||
Jwt jwt = Mockito.mock(Jwt.class); | ||
|
||
Map<String, Object> resource = new HashMap<String, Object>(); | ||
|
||
Map<String, Object> resourceAccess = new HashMap<String, Object>(); | ||
resourceAccess.put("ai-reviewer", resource); | ||
when(jwt.getClaim(KeycloakJwtAuthConverter.KEYCLOAK_RESOURCE_ATTRIBUTE)).thenReturn(resourceAccess); | ||
|
||
KeycloakJwtAuthConverter converter = new KeycloakJwtAuthConverter("ai-reviewer"); | ||
AbstractAuthenticationToken authenticationToken = converter.convert(jwt); | ||
Collection<GrantedAuthority> authorities = authenticationToken.getAuthorities(); | ||
assertEquals(0, authorities.size()); | ||
} | ||
|
||
@Test | ||
void testJwtValid() throws Exception { | ||
Jwt jwt = Mockito.mock(Jwt.class); | ||
|
||
Collection<String> resourceRoles = new ArrayList<>(); | ||
resourceRoles.add("tester"); | ||
|
||
Map<String, Object> resource = new HashMap<String, Object>(); | ||
resource.put(KeycloakJwtAuthConverter.KEYCLOAK_ROLE_ATTRIBUTE, resourceRoles); | ||
|
||
Map<String, Object> resourceAccess = new HashMap<String, Object>(); | ||
resourceAccess.put("ai-reviewer", resource); | ||
when(jwt.getClaim(KeycloakJwtAuthConverter.KEYCLOAK_RESOURCE_ATTRIBUTE)).thenReturn(resourceAccess); | ||
|
||
KeycloakJwtAuthConverter converter = new KeycloakJwtAuthConverter("ai-reviewer"); | ||
AbstractAuthenticationToken authenticationToken = converter.convert(jwt); | ||
Collection<GrantedAuthority> authorities = authenticationToken.getAuthorities(); | ||
assertEquals(1, authorities.size()); | ||
assertEquals("ROLE_tester", authorities.iterator().next().getAuthority()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...eviewer-api/src/test/java/ca/bc/gov/open/jag/aireviewerapi/config/SecurityConfigTest.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,40 @@ | ||
package ca.bc.gov.open.jag.aireviewerapi.config; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.web.DefaultSecurityFilterChain; | ||
|
||
class SecurityConfigTest { | ||
|
||
@Mock | ||
KeycloakJwtAuthConverter jwtAuthConverter; | ||
|
||
@Mock | ||
HttpSecurity http; | ||
|
||
@Test | ||
void testFilterChain() throws Exception { | ||
MockitoAnnotations.openMocks(this); | ||
when(http.build()).thenReturn(Mockito.mock(DefaultSecurityFilterChain.class)); | ||
SecurityConfig config = new SecurityConfig(jwtAuthConverter); | ||
assertNotNull(config.filterChain(http)); | ||
assertNotNull(config.corsConfigurationSource()); | ||
} | ||
|
||
@Test | ||
void testSessionAuthenticationStrategy() throws Exception { | ||
KeycloakJwtAuthConverter jwtAuthConverter = Mockito.mock(KeycloakJwtAuthConverter.class); | ||
HttpSecurity http = Mockito.mock(HttpSecurity.class); | ||
|
||
SecurityConfig config = new SecurityConfig(jwtAuthConverter); | ||
assertNotNull(config.sessionAuthenticationStrategy()); | ||
} | ||
|
||
} |
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
30 changes: 30 additions & 0 deletions
30
...end/libs/ai-mail-it/src/test/java/ca/bc/gov/open/jag/jagmailit/AutoConfigurationTest.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,30 @@ | ||
package ca.bc.gov.open.jag.jagmailit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
import ca.bc.gov.open.jag.jagmailit.api.handler.ApiClient; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class AutoConfigurationTest { | ||
|
||
private ApplicationContextRunner context; | ||
|
||
@Test | ||
public void validConfigurationShouldProduceBeans() { | ||
|
||
context = new ApplicationContextRunner() | ||
.withUserConfiguration(AutoConfiguration.class) | ||
.withPropertyValues("mailsend.baseUrl=http://test.com") | ||
.withUserConfiguration(MailSendProperties.class); | ||
|
||
context.run(it -> { | ||
assertThat(it).hasSingleBean(ApiClient.class); | ||
}); | ||
|
||
} | ||
|
||
} |