@Test public void principal() { User user = new User(); this.builder.principal(user); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals(user, request.getUserPrincipal()); }
@Test public void loginWhenJeeFilterThenExtractsRoles() throws Exception { this.spring.configLocations(xml("JeeFilter")).autowire(); Principal user = mock(Principal.class); when(user.getName()).thenReturn("joe"); this.mvc.perform(get("/roles") .principal(user) .with(request -> { request.addUserRole("admin"); request.addUserRole("user"); request.addUserRole("unmapped"); return request; })) .andExpect(content().string("ROLE_admin,ROLE_user")); }
@Test public void testSpecialMessageWhenNoAppsAreAuthorized() throws Exception { Mockito.when(approvalStore.getApprovalsForUser(anyString(), anyString())).thenReturn(Collections.emptyList()); UaaPrincipal uaaPrincipal = new UaaPrincipal("fake-user-id", "username", "email@example.com", OriginKeys.UAA, null, IdentityZoneHolder.get().getId()); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(uaaPrincipal, null); mockMvc.perform(get("/profile").principal(authentication)) .andExpect(status().isOk()) .andExpect(model().attributeExists("approvals")) .andExpect(content().contentTypeCompatibleWith(TEXT_HTML)) .andExpect(content().string(containsString("You have not yet authorized any third party applications."))); }
requestBuilder.principal((Principal) authentication);
@Test public void testPasswordLinkHiddenWhenUsersOriginIsNotUaa() throws Exception { UaaPrincipal uaaPrincipal = new UaaPrincipal("fake-user-id", "username", "email@example.com", OriginKeys.LDAP, "dnEntryForLdapUser", IdentityZoneHolder.get().getId()); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(uaaPrincipal, null); mockMvc.perform(get("/profile").principal(authentication)) .andExpect(status().isOk()) .andExpect(model().attribute("isUaaManagedUser", false)) .andExpect(content().string(not(containsString("Change Password")))); }
public void testGetProfile(String name) throws Exception { UaaPrincipal uaaPrincipal = new UaaPrincipal("fake-user-id", "username", "email@example.com", OriginKeys.UAA, null, IdentityZoneHolder.get().getId()); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(uaaPrincipal, null); mockMvc.perform(get("/profile").principal(authentication)) .andExpect(status().isOk()) .andExpect(model().attributeExists("clientnames")) .andExpect(model().attribute("clientnames", hasKey("app"))) .andExpect(model().attribute("clientnames", hasValue(is(name)))) .andExpect(model().attribute("isUaaManagedUser", true)) .andExpect(model().attribute("approvals", hasKey("app"))) .andExpect(model().attribute("approvals", hasValue(hasSize(2)))) .andExpect(content().contentTypeCompatibleWith(TEXT_HTML)) .andExpect(content().string(containsString("These applications have been granted access to your account."))) .andExpect(content().string(containsString("Change Password"))) .andExpect(content().string(containsString("<h3>"+name))) .andExpect(content().string(containsString("Are you sure you want to revoke access to " + name))); }
@Test void testAccessConfirmationPage( @Autowired JdbcScimUserProvisioning jdbcScimUserProvisioning ) throws Exception { ScimUser marissa = jdbcScimUserProvisioning.query("username eq \"marissa\" and origin eq \"uaa\"", IdentityZoneHolder.get().getId()).get(0); UaaPrincipal uaaPrincipal = new UaaPrincipal(marissa.getId(), marissa.getUserName(), marissa.getPrimaryEmail(), marissa.getOrigin(), marissa.getExternalId(), IdentityZoneHolder.get().getId()); UaaAuthentication principal = new UaaAuthentication(uaaPrincipal, singletonList(UaaAuthority.fromAuthorities("uaa.user")), null); MockHttpSession session = new MockHttpSession(); SecurityContext securityContext = new SecurityContextImpl(); securityContext.setAuthentication(principal); session.putValue("SPRING_SECURITY_CONTEXT", securityContext); MockHttpServletRequestBuilder get = get("/oauth/authorize") .accept(TEXT_HTML) .param("response_type", "code") .param("client_id", "app") .param("state", "somestate") .param("redirect_uri", "http://localhost:8080/app/") .session(session) .principal(principal); mockMvc.perform(get) .andExpect(status().isOk()) .andExpect(forwardedUrl("/oauth/confirm_access")); }
@Test public void whenPostTimelineThenCreated() throws Exception { Mockito.doNothing().when(timelineService).createTimeline(Mockito.any(), Mockito.any(), Mockito.any()); mockMvc.perform(MockMvcRequestBuilders.post("/event-types/event_type/timelines") .contentType(MediaType.APPLICATION_JSON) .content(new JSONObject().put("storage_id", "default").toString()) .principal(PrincipalMockFactory.mockPrincipal("nakadi"))) .andExpect(MockMvcResultMatchers.status().isCreated()); }
protected ResultActions deleteEventType(final String eventTypeName, final String clientId) throws Exception { return mockMvc.perform(delete("/event-types/" + eventTypeName).principal(mockPrincipal(clientId))); }
@Test public void testDeleteUnusedStorage() throws Exception { doNothing().when(storageService).deleteStorage("s1", Optional.empty()); when(adminService.isAdmin(AuthorizationService.Operation.WRITE)).thenReturn(true); mockMvc.perform(delete("/storages/s1") .principal(mockPrincipal("nakadi"))) .andExpect(status().isNoContent()); }
@Test public void testPostStorage() throws Exception { final JSONObject json = createJsonKafkaStorage("test_storage"); doNothing().when(storageService).createStorage(any(), any()); when(adminService.isAdmin(AuthorizationService.Operation.WRITE)).thenReturn(true); mockMvc.perform(post("/storages") .contentType(APPLICATION_JSON) .content(json.toString()) .principal(mockPrincipal("nakadi"))) .andExpect(status().isCreated()); }
@Test public void whenGetTimelinesThenOk() throws Exception { final Storage kafkaStorage = StoragesControllerTest.createKafkaStorage("deafult"); final ImmutableList<Timeline> timelines = ImmutableList.of( Timeline.createTimeline("event_type", 0, kafkaStorage, "topic", new Date()), Timeline.createTimeline("event_type_1", 1, kafkaStorage, "topic_1", new Date())); Mockito.when(timelineService.getTimelines(Mockito.any())).thenReturn(timelines); final List<TimelineView> timelineViews = timelines.stream().map(TimelineView::new).collect(Collectors.toList()); mockMvc.perform(MockMvcRequestBuilders.get("/event-types/event_type/timelines") .contentType(MediaType.APPLICATION_JSON) .principal(PrincipalMockFactory.mockPrincipal("nakadi"))) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().json( TestUtils.OBJECT_MAPPER.writeValueAsString(timelineViews))); }
@Test public void testListStorages() throws Exception { final List<Storage> storages = createStorageList(); when(storageService.listStorages()) .thenReturn(storages); when(adminService.isAdmin(AuthorizationService.Operation.READ)).thenReturn(true); mockMvc.perform(get("/storages") .principal(mockPrincipal("nakadi"))) .andExpect(status().isOk()); }
@Test public void testSetDefaultStorageOk() throws Exception { when(storageService.setDefaultStorage("test_storage")) .thenReturn(createKafkaStorage("test_storage")); when(adminService.isAdmin(AuthorizationService.Operation.WRITE)).thenReturn(true); mockMvc.perform(put("/storages/default/test_storage") .contentType(APPLICATION_JSON) .principal(mockPrincipal("nakadi"))) .andExpect(status().isOk()); }
@Test public void testSetDefaultStorageAccessDenied() throws Exception { when(adminService.isAdmin(AuthorizationService.Operation.WRITE)).thenReturn(false); mockMvc.perform(put("/storages/default/test_storage") .contentType(APPLICATION_JSON) .principal(mockPrincipal("nakadi"))) .andExpect(status().isForbidden()); }
protected ResultActions putEventType(final String content, final String name, final String clientId) throws Exception { final MockHttpServletRequestBuilder requestBuilder = put("/event-types/" + name) .principal(mockPrincipal(clientId)) .contentType(APPLICATION_JSON) .content(content); return mockMvc.perform(requestBuilder); }
@Test public void loadsCustomerForHome() throws Exception { this.mockMvc.perform(get("/") .principal(() -> "rey@theresistance.com")) .andExpect(status().isOk()) .andExpect(model().attributeExists("currentCustomer")) .andExpect(view().name("home")); } }