Commit c56173ae authored by Aman Verma's avatar Aman Verma
Browse files

adding UT for GCP

parent accfecea
Pipeline #62310 passed with stages
in 32 minutes and 52 seconds
package org.opengroup.osdu.schema.security;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.FromDataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.schema.configuration.PropertiesConfiguration;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@RunWith(Theories.class)
public class AuthorizationServiceForServicePrincipalImplTest {
private final String token = "abc";
private final String serviceAccountTail = "project-id.iam.gserviceaccount.com";
private final List<String> adminAccounts = ImmutableList.of("osdu-gcp-sa", "service.account@project-id.iam.gserviceaccount.com");
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@DataPoints("VALID_ACCOUNTS")
public static List<String> validTestSet() {
return ImmutableList.of(
"osdu-gcp-sa-first@project-id.iam.gserviceaccount.com",
"osdu-gcp-sa-second@project-id.iam.gserviceaccount.com",
"osdu-gcp-sa-third@project-id.iam.gserviceaccount.com",
"osdu-gcp-sa-fourth@project-id.iam.gserviceaccount.com");
}
@DataPoints("NOT_VALID_ACCOUNTS")
public static List<String> notValidTestSet() {
return ImmutableList.of(
"osdu-gcp-sa-first@google.com",
"osdu-gcp-sa-second@project-id.iam.gserviceaccount.com.not.valid",
"user-osdu-gcp-sa-third@project-id.iam.gserviceaccount.com");
}
@Mock
private PropertiesConfiguration configuration;
@Mock
private GoogleIdTokenVerifier verifier;
@Mock
private GoogleIdToken googleIdToken;
@Mock
private DpsHeaders headers;
private GoogleIdToken.Payload payload = new GoogleIdToken.Payload();
@InjectMocks
private AuthorizationServiceForServicePrincipalImpl authorizationService;
@Before
public void setUp() throws GeneralSecurityException, IOException {
MockitoAnnotations.initMocks(this);
when(configuration.getAdminAccounts()).thenReturn(adminAccounts);
when(configuration.getServiceAccountTail()).thenReturn(serviceAccountTail);
when(headers.getAuthorization()).thenReturn(token);
when(verifier.verify(token)).thenReturn(googleIdToken);
when(googleIdToken.getPayload()).thenReturn(payload);
}
@Test
public void testProvidedInConfigAdminAccountShouldReturnTrue() {
payload.setEmail("service.account@project-id.iam.gserviceaccount.com");
assertTrue(authorizationService.isDomainAdminServiceAccount());
}
@Test(expected = AppException.class)
public void testNotProvidedInConfigAdminAccountShouldThrowException() {
payload.setEmail("user@google.com");
authorizationService.isDomainAdminServiceAccount();
}
@Theory
public void testProvidedInConfigPatternShouldReturnTrue(@FromDataPoints("VALID_ACCOUNTS") String account) {
payload.setEmail(account);
assertTrue(authorizationService.isDomainAdminServiceAccount());
}
@Theory
public void testNotProvidedInConfigPatternShouldReturnTrue(@FromDataPoints("NOT_VALID_ACCOUNTS") String account) {
exceptionRule.expect(AppException.class);
payload.setEmail(account);
authorizationService.isDomainAdminServiceAccount();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment