@Test public void testMatcher() throws Exception { this.mockMvc.perform(get("/badRequest")).andExpect(status().is(equalTo(400))); }
/** * http/http-basic@entry-point-ref */ @Test public void basicAuthenticationWhenUsingEntryPointRefThenMatchesNamespace() throws Exception { this.spring.register(EntryPointRefHttpBasicConfig.class, UserConfig.class).autowire(); this.mvc.perform(get("/")) .andExpect(status().is(999)); this.mvc.perform(get("/") .with(httpBasic("user", "invalid"))) .andExpect(status().is(999)); this.mvc.perform(get("/") .with(httpBasic("user", "password"))) .andExpect(status().isNotFound()); }
private ResultActions[] addAndDeleteMemberstoZoneManagementGroups(String displayName, HttpStatus create, HttpStatus delete) throws Exception { ResultActions[] result = new ResultActions[2]; IdentityZone zone = MockMvcUtils.createZoneUsingWebRequest(getMockMvc(), identityClientToken); ScimGroupMember member = new ScimGroupMember(scimUser.getId()); ScimGroup group = new ScimGroup(String.format(displayName, zone.getId())); group.setMembers(Arrays.asList(member)); result[0] = createZoneScope(group); result[0].andExpect(status().is(create.value())); if (delete != null) { result[1] = deleteZoneScope(zone, group); result[1].andExpect(status().is(delete.value())); } return result; }
public static void deleteClient(MockMvc mockMvc, String accessToken, String clientId, String zoneSubdomain) throws Exception { MockHttpServletRequestBuilder createClientDelete = delete("/oauth/clients/" + clientId) .header("Authorization", "Bearer " + accessToken) .accept(APPLICATION_JSON); if (!zoneSubdomain.equals(IdentityZone.getUaa())) { createClientDelete = createClientDelete.header(IdentityZoneSwitchingFilter.SUBDOMAIN_HEADER, zoneSubdomain); } mockMvc.perform(createClientDelete) .andExpect(status().is(not(500))); }
@Test void groups_can_read() throws Exception { mockMvc.perform( get("/Groups") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(OK.value())); mockMvc.perform( get("/Groups/{id}", "some-invalid-id") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(NOT_FOUND.value())); }
@Test void identity_zone_can_read() throws Exception { mockMvc.perform( get("/identity-zones") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(OK.value())); mockMvc.perform( get("/identity-zones/{id}", "some-invalid-id") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(NOT_FOUND.value())); }
@Test void identity_provider_can_read() throws Exception { mockMvc.perform( get("/identity-providers") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(OK.value())); mockMvc.perform( get("/identity-providers/{id}", "some-invalid-id") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(NOT_FOUND.value())); }
@Test void users_can_read() throws Exception { mockMvc.perform( get("/Users") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(OK.value())); mockMvc.perform( get("/Users/{id}", "some-invalid-id") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(NOT_FOUND.value())); }
@Test void clients_can_read() throws Exception { mockMvc.perform( get("/oauth/clients") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(OK.value())); mockMvc.perform( get("/oauth/clients/{id}", "some-invalid-id") .accept(MediaType.APPLICATION_JSON) .header("Authorization", "bearer " + adminToken)) .andExpect(status().is(NOT_FOUND.value())); }
public List<Map<String, String>> performExternalGroupFilter(MockHttpServletRequestBuilder get, HttpStatus status) throws Exception { ResultActions result = getMockMvc().perform(get); result.andExpect(status().is(status.value())); if (status.equals(HttpStatus.OK)) { String content = result.andReturn().getResponse().getContentAsString(); Map<String, Object> map = JsonUtils.readValue(content, new TypeReference<Map<String, Object>>() { }); return (List<Map<String, String>>) map.get("resources"); } else { return null; } }
private IdentityZone getIdentityZone(String id, HttpStatus expect, String token) throws Exception { MvcResult result = mockMvc.perform( get("/identity-zones/" + id) .header("Authorization", "Bearer " + token)) .andExpect(status().is(expect.value())) .andReturn(); if (expect.is2xxSuccessful()) { return JsonUtils.readValue(result.getResponse().getContentAsString(), IdentityZone.class); } return null; }
private MvcResult performAuthentication(String username, String password, HttpStatus status) throws Exception { MockHttpServletRequestBuilder post = post("/authenticate") .header(HOST, host) .accept(MediaType.APPLICATION_JSON) .param("username", username) .param("password", password); return getMockMvc().perform(post) .andExpect(status().is(status.value())) .andReturn(); }
@Test @WithMockUser public void configureWhenAccessDeniedHandlerRefThenAutowire() throws Exception { this.spring.configLocations(this.xml("AccessDeniedHandler")).autowire(); this.mvc.perform(get("/")) .andExpect(status().is(HttpStatus.GONE_410)); }
private void createZone(IdentityZone identityZone) throws Exception { String zoneJson = mockMvc.perform( post("/identity-zones") .header("Authorization", "Bearer " + token) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(identityZone))) .andExpect(status().is(HttpStatus.CREATED.value())) .andReturn().getResponse().getContentAsString(); zone = JsonUtils.readValue(zoneJson, IdentityZone.class); }
private IdentityZone updateZone(String id, IdentityZone identityZone, HttpStatus expect, String expectedContent, String token) throws Exception { MvcResult result = mockMvc.perform( put("/identity-zones/" + id) .header("Authorization", "Bearer " + token) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(identityZone))) .andExpect(status().is(expect.value())) .andExpect(content().string(containsString(expectedContent))) .andReturn(); if (expect.is2xxSuccessful()) { return JsonUtils.readValue(result.getResponse().getContentAsString(), IdentityZone.class); } return null; }
@Test void put_updateNothing_shouldFail() throws Exception { mockMvc.perform(put("/Users/" + seededUser.getId()) .headers(zoneSeeder.getZoneIdRequestHeader()) .header("Authorization", "Bearer " + uaaAdminToken) .header("If-Match", "\"" + seededUser.getVersion() + "\"") .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsBytes(seededUser))) .andDo(print()) .andExpect(status().is(403)) .andExpect(content().string(JsonObjectMatcherUtils.matchesJsonObject( new JSONObject() .put("error_description", "Internal User Creation is currently disabled. External User Store is in use.") .put("message", "Internal User Creation is currently disabled. External User Store is in use.") .put("error", "internal_user_management_disabled")))); }
private MvcResult performUiAuthentication(String username, String password, HttpStatus status, boolean authenticated) throws Exception { MockHttpServletRequestBuilder post = post("/login.do") .with(cookieCsrf()) .header(HOST, host) .accept(MediaType.TEXT_HTML) .param("username", username) .param("password", password); return getMockMvc().perform(post) .andExpect(status().is(status.value())) .andExpect(authenticated ? authenticated() : unauthenticated()) .andReturn(); }
private IdentityZone createZone(String id, HttpStatus expect, String expectedContent, String token, IdentityZoneConfiguration zoneConfiguration) throws Exception { IdentityZone identityZone = createSimpleIdentityZone(id); identityZone.setConfig(zoneConfiguration); identityZone.getConfig().getSamlConfig().setPrivateKey(serviceProviderKey); identityZone.getConfig().getSamlConfig().setPrivateKeyPassword(serviceProviderKeyPassword); identityZone.getConfig().getSamlConfig().setCertificate(serviceProviderCertificate); MvcResult result = mockMvc.perform( post("/identity-zones") .header("Authorization", "Bearer " + token) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(identityZone))) .andExpect(status().is(expect.value())) .andExpect(content().string(containsString(expectedContent))) .andReturn(); if (expect.is2xxSuccessful()) { return JsonUtils.readValue(result.getResponse().getContentAsString(), IdentityZone.class); } return null; }
private ScimUser getAndReturnUser(int status, ScimUser user, String token) throws Exception { MockHttpServletRequestBuilder get = MockMvcRequestBuilders.get("/Users/" + user.getId()) .header("Authorization", "Bearer " + token) .accept(APPLICATION_JSON); if (status == HttpStatus.OK.value()) { String json = mockMvc.perform(get) .andExpect(status().is(status)) .andExpect(header().string("ETag", "\"" + user.getVersion() + "\"")) .andExpect(jsonPath("$.userName").value(user.getPrimaryEmail())) .andExpect(jsonPath("$.emails[0].value").value(user.getPrimaryEmail())) .andExpect(jsonPath("$.name.familyName").value(user.getFamilyName())) .andExpect(jsonPath("$.name.givenName").value(user.getGivenName())) .andReturn().getResponse().getContentAsString(); return JsonUtils.readValue(json, ScimUser.class); } else { mockMvc.perform(get) .andExpect(status().is(status)); return null; } }