Search in sources :

Example 26 with WithMockUhUser

use of edu.hawaii.its.groupings.controller.WithMockUhUser in project uhgroupings by uhawaii-system-its-ti-iam.

the class GroupingsRestControllerTest method adminListsTest.

@Test
@WithMockUhUser(username = "admin")
public void adminListsTest() throws Exception {
    String uri = REST_CONTROLLER_BASE + "adminLists";
    given(httpRequestService.makeApiRequest(eq(ADMIN_USERNAME), anyString(), eq(HttpMethod.GET))).willReturn(new ResponseEntity(HttpStatus.OK));
    assertNotNull(mockMvc.perform(get(uri).with(csrf())).andExpect(status().isOk()).andReturn());
    verify(httpRequestService, times(1)).makeApiRequest(eq(ADMIN_USERNAME), anyString(), eq(HttpMethod.GET));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser)

Example 27 with WithMockUhUser

use of edu.hawaii.its.groupings.controller.WithMockUhUser in project uhgroupings by uhawaii-system-its-ti-iam.

the class GroupingsRestControllerTest method shouldDoGrouperHandshake.

@Test
@WithMockUhUser
public void shouldDoGrouperHandshake() throws Exception {
    GroupingsRestController controller = applicationContext.getBean(GroupingsRestController.class);
    Realm realm = controller.getRealm();
    assertFalse(realm.isAnyProfileActive("default"));
    assertTrue(realm.isAnyProfileActive("localTest"));
    // What we are testing.
    assertFalse(controller.shouldDoApiHandshake());
    Realm realmMock = mock(Realm.class);
    // Swap in mock.
    controller.setRealm(realmMock);
    assertTrue(mockingDetails(controller.getRealm()).isMock());
    given(realmMock.isAnyProfileActive("default", "localTest")).willReturn(false);
    // What we are testing.
    assertTrue(controller.shouldDoApiHandshake());
    // Mock real realm back.
    controller.setRealm(realm);
    assertFalse(mockingDetails(controller.getRealm()).isMock());
    // Let's cheat a little to get at some private fields.
    Class<?> c0 = controller.getClass();
    Field field0 = c0.getDeclaredField("API_HANDSHAKE_ENABLED");
    field0.setAccessible(true);
    Boolean existingValue = (Boolean) field0.get(controller);
    assertTrue(existingValue);
    field0.set(controller, Boolean.FALSE);
    // What we are testing.
    assertFalse(controller.shouldDoApiHandshake());
    // Put property value back.
    field0.set(controller, Boolean.TRUE);
}
Also used : Field(java.lang.reflect.Field) Realm(edu.hawaii.its.groupings.configuration.Realm) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser)

Example 28 with WithMockUhUser

use of edu.hawaii.its.groupings.controller.WithMockUhUser in project uhgroupings by uhawaii-system-its-ti-iam.

the class GroupingsRestControllerTest method optInGroupsTest.

@Test
@WithMockUhUser(username = "admin")
public void optInGroupsTest() throws Exception {
    String uri = REST_CONTROLLER_BASE + "groupings/optInGroups";
    given(httpRequestService.makeApiRequest(eq(ADMIN_USERNAME), anyString(), eq(HttpMethod.GET))).willReturn(new ResponseEntity(HttpStatus.OK));
    assertNotNull(mockMvc.perform(get(uri).with(csrf())).andExpect(status().isOk()).andReturn());
    verify(httpRequestService, times(1)).makeApiRequest(eq(ADMIN_USERNAME), anyString(), eq(HttpMethod.GET));
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser)

Example 29 with WithMockUhUser

use of edu.hawaii.its.groupings.controller.WithMockUhUser in project uhgroupings by uhawaii-system-its-ti-iam.

the class AuthorizationServiceTest method fetchPrivilegesTest.

@Test
@WithMockUhUser
public void fetchPrivilegesTest() {
    // Setup for the mocking.
    User user = userContextService.getCurrentUser();
    String uhUuid = user.getUhUuid();
    Principal principal = new SimplePrincipal(uhUuid);
    given(groupingsRestController.hasOwnerPrivs(principal)).willReturn(new ResponseEntity<>("true", HttpStatus.OK));
    given(groupingsRestController.hasAdminPrivs(principal)).willReturn(new ResponseEntity<>("true", HttpStatus.OK));
    // What we are testing.
    RoleHolder roleHolder = authorizationService.fetchRoles(uhUuid, "test");
    // Check results.
    assertThat(roleHolder.size(), equalTo(4));
    assertTrue(roleHolder.contains(Role.ANONYMOUS));
    assertTrue(roleHolder.contains(Role.UH));
    assertTrue(roleHolder.contains(Role.OWNER));
    assertTrue(roleHolder.contains(Role.ADMIN));
}
Also used : WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser) Principal(java.security.Principal) SimplePrincipal(org.jasig.cas.client.authentication.SimplePrincipal) SimplePrincipal(org.jasig.cas.client.authentication.SimplePrincipal) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser)

Example 30 with WithMockUhUser

use of edu.hawaii.its.groupings.controller.WithMockUhUser in project uhgroupings by uhawaii-system-its-ti-iam.

the class AuthorizationServiceTest method fetchDefaultTest.

@Test
@WithMockUhUser
public void fetchDefaultTest() {
    // Setup for the mocking.
    User user = userContextService.getCurrentUser();
    String uhUuid = user.getUhUuid();
    Principal principal = new SimplePrincipal(uhUuid);
    given(groupingsRestController.hasOwnerPrivs(principal)).willReturn(new ResponseEntity<>(null, HttpStatus.OK));
    given(groupingsRestController.hasAdminPrivs(principal)).willReturn(new ResponseEntity<>(null, HttpStatus.OK));
    // What we are testing.
    RoleHolder roleHolder = authorizationService.fetchRoles(uhUuid, "test");
    // Check results.
    assertThat(roleHolder.size(), equalTo(2));
    assertTrue(roleHolder.contains(Role.ANONYMOUS));
    assertTrue(roleHolder.contains(Role.UH));
    assertFalse(roleHolder.contains(Role.EMPLOYEE));
    assertFalse(roleHolder.contains(Role.ADMIN));
    assertFalse(roleHolder.contains(Role.OWNER));
}
Also used : WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser) Principal(java.security.Principal) SimplePrincipal(org.jasig.cas.client.authentication.SimplePrincipal) SimplePrincipal(org.jasig.cas.client.authentication.SimplePrincipal) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) WithMockUhUser(edu.hawaii.its.groupings.controller.WithMockUhUser)

Aggregations

WithMockUhUser (edu.hawaii.its.groupings.controller.WithMockUhUser)69 Test (org.junit.Test)69 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)69 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)34 ResponseEntity (org.springframework.http.ResponseEntity)31 GroupingsServiceResult (edu.hawaii.its.api.type.GroupingsServiceResult)15 ArrayList (java.util.ArrayList)10 Grouping (edu.hawaii.its.api.type.Grouping)9 GroupingAssignment (edu.hawaii.its.api.type.GroupingAssignment)7 Principal (java.security.Principal)3 SimplePrincipal (org.jasig.cas.client.authentication.SimplePrincipal)3 AdminListsHolder (edu.hawaii.its.api.type.AdminListsHolder)2 Realm (edu.hawaii.its.groupings.configuration.Realm)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpRequestService (edu.hawaii.its.api.service.HttpRequestService)1 Group (edu.hawaii.its.api.type.Group)1 GroupingsHTTPException (edu.hawaii.its.api.type.GroupingsHTTPException)1 ApiServerHandshakeException (edu.hawaii.its.groupings.exceptions.ApiServerHandshakeException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1