Skip to content

Commit

Permalink
#943: tests for helper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Jun 14, 2024
1 parent b90bcdf commit 1e53d4a
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package ch.puzzle.okr.security.helper;

import com.nimbusds.jwt.JWTClaimsSet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.text.ParseException;
import java.util.Optional;

import static ch.puzzle.okr.security.JwtHelper.CLAIM_ISS;
import static ch.puzzle.okr.security.JwtHelper.CLAIM_TENANT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ClaimHelperTest {

private static final String PITC = "pitc";
private ClaimHelper helper;

@BeforeEach
void setup() {
helper = new ClaimHelper();
}

@DisplayName("getTenantFromClaimsSetUsingClaimTenant() return tenant if claim tenant is found")
@Test
void getTenantFromClaimsSetUsingClaimTenantReturnTenantIfClaimTenantFound() throws ParseException {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);
when(claimsSetMock.getStringClaim(CLAIM_TENANT)).thenReturn(PITC);

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimTenant(claimsSetMock);

// assert
assertTrue(tenant.isPresent());
assertEquals(PITC, tenant.get());
}

@DisplayName("getTenantFromClaimsSetUsingClaimTenant() return empty if claim tenant is not found")
@Test
void getTenantFromClaimsSetUsingClaimTenantReturnEmptyIfClaimTenantNotFound() {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimTenant(claimsSetMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromClaimsSetUsingClaimTenant() return empty if claim tenant is found but value is null")
@Test
void getTenantFromClaimsSetUsingClaimTenantReturnEmptyIfClaimTenantFoundButValueIsNull() throws ParseException {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);
when(claimsSetMock.getStringClaim(CLAIM_TENANT)).thenReturn(null);

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimTenant(claimsSetMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromClaimsSetUsingClaimTenant() return empty if claim tenant is found but parsing of claim failed")
@Test
void getTenantFromClaimsSetUsingClaimTenantReturnEmptyIfParsingOfClaimFailed() throws ParseException {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);
when(claimsSetMock.getStringClaim(CLAIM_TENANT)).thenThrow(new ParseException("", 0));

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimTenant(claimsSetMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromClaimsSetUsingClaimIss() return tenant if claim iss is found")
@ParameterizedTest
@ValueSource(strings = {"https://sso.puzzle.ch/auth/realms/pitc", "http://localhost:8544/realms/pitc"})
void getTenantFromClaimsSetUsingClaimIssReturnTenantIfClaimIssFound(String issUrl) throws ParseException {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);
when(claimsSetMock.getStringClaim(CLAIM_ISS)).thenReturn(issUrl);

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimIss(claimsSetMock);

// assert
assertTrue(tenant.isPresent());
assertEquals(PITC, tenant.get());
}

@DisplayName("getTenantFromClaimsSetUsingClaimIss() return empty if claim iss is not found")
@Test
void getTenantFromClaimsSetUsingClaimIssReturnEmptyIfClaimIssNotFound() {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimIss(claimsSetMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromClaimsSetUsingClaimIss() return empty if claim iss is found but value is null")
@Test
void getTenantFromClaimsSetUsingClaimIssReturnEmptyIfClaimIssFoundButValueIsNull() throws ParseException {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);
when(claimsSetMock.getStringClaim(CLAIM_ISS)).thenReturn(null);

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimIss(claimsSetMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromClaimsSetUsingClaimIss() return empty if parsing of claim failed")
@Test
void getTenantFromClaimsSetUsingClaimIssReturnEmptyIfParsingOfClaimFailed() throws ParseException {
// arrange
JWTClaimsSet claimsSetMock = mock(JWTClaimsSet.class);
when(claimsSetMock.getStringClaim(CLAIM_ISS)).thenThrow(new ParseException("", 0));

// act
Optional<String> tenant = helper.getTenantFromClaimsSetUsingClaimIss(claimsSetMock);

// assert
assertTrue(tenant.isEmpty());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ch.puzzle.okr.security.helper;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.security.oauth2.jwt.Jwt;

import java.util.Optional;

import static ch.puzzle.okr.security.JwtHelper.CLAIM_ISS;
import static ch.puzzle.okr.security.JwtHelper.CLAIM_TENANT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TokenHelperTest {

private static final String PITC = "pitc";
private TokenHelper helper;

@BeforeEach
void setUp() {
helper = new TokenHelper();
}

@DisplayName("getTenantFromTokenUsingClaimTenant() return tenant if claim tenant is found")
@Test
void getTenantFromTokenUsingClaimTenantReturnTenantIfClaimTenantFound() {
// arrange
Jwt tokenMock = mock(Jwt.class);
when(tokenMock.getClaimAsString(CLAIM_TENANT)).thenReturn(PITC);

// act
Optional<String> tenant = helper.getTenantFromTokenUsingClaimTenant(tokenMock);

// assert
assertTrue(tenant.isPresent());
assertEquals(PITC, tenant.get());
}

@DisplayName("getTenantFromTokenUsingClaimTenant() return empty if claim tenant not found")
@Test
void getTenantFromTokenUsingClaimTenantReturnEmptyIfClaimTenantNotFound() {
// arrange
Jwt tokenMock = mock(Jwt.class);

// act
Optional<String> tenant = helper.getTenantFromTokenUsingClaimTenant(tokenMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromTokenUsingClaimTenant() return empty if claim tenant is found but value is null")
@Test
void getTenantFromTokenUsingClaimTenantReturnEmptyIfClaimTenantFoundButValueIsNull() {
// arrange
Jwt tokenMock = mock(Jwt.class);
when(tokenMock.getClaimAsString(CLAIM_TENANT)).thenReturn(null);

// act
Optional<String> tenant = helper.getTenantFromTokenUsingClaimTenant(tokenMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromTokenUsingClaimIss() return tenant if claim iss is found")
@ParameterizedTest
@ValueSource(strings = {"https://sso.puzzle.ch/auth/realms/pitc", "http://localhost:8544/realms/pitc"})
void getTenantFromTokenUsingClaimIssReturnTenantIfClaimIssFound(String issUrl) {
// arrange
Jwt tokenMock = mock(Jwt.class);
when(tokenMock.getClaimAsString(CLAIM_ISS)).thenReturn(issUrl);

// act
Optional<String> tenant = helper.getTenantFromTokenUsingClaimIss(tokenMock);

// assert
assertTrue(tenant.isPresent());
assertEquals(PITC, tenant.get());
}

@DisplayName("getTenantFromTokenUsingClaimIss() return empty if claim iss is not found")
@Test
void getTenantFromTokenUsingClaimIssReturnEmptyIfClaimIssNotFound() {
// arrange
Jwt tokenMock = mock(Jwt.class);

// act
Optional<String> tenant = helper.getTenantFromTokenUsingClaimIss(tokenMock);

// assert
assertTrue(tenant.isEmpty());
}

@DisplayName("getTenantFromTokenUsingClaimIss() return empty if claim iss is found but value is null")
@Test
void getTenantFromTokenUsingClaimIssReturnEmptyIfClaimIssFoundButValueIsNull() {
// arrange
Jwt tokenMock = mock(Jwt.class);
when(tokenMock.getClaimAsString(CLAIM_ISS)).thenReturn(null);

// act
Optional<String> tenant = helper.getTenantFromTokenUsingClaimIss(tokenMock);

// assert
assertTrue(tenant.isEmpty());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ch.puzzle.okr.security.helper;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class UrlHelperTest {

private static final String PITC = "pitc";

@DisplayName("extractTenantFromIssUrl() return tenant if Url contains slash")
@ParameterizedTest
@ValueSource(strings = { "https://sso.puzzle.ch/auth/realms/pitc", "http://localhost:8544/realms/pitc" })
void extractTenantFromIssUrlReturnTenantIfUrlContainSlash(String issUrl) {
// arrange

// act
String tenantFromIssUrl = UrlHelper.extractTenantFromIssUrl(issUrl);

// assert
assertEquals(PITC, tenantFromIssUrl);
}

@DisplayName("extractTenantFromIssUrl() return input url if url not contains slash")
@Test
void extractTenantFromIssUrlReturnInputIfUrlNotContainSlash() {
// arrange
String issUrl = "this_is_not_a_valid_url";

// act
String tenantFromIssUrl = UrlHelper.extractTenantFromIssUrl(issUrl);

// assert
assertEquals(issUrl, tenantFromIssUrl);
}
}

0 comments on commit 1e53d4a

Please sign in to comment.