@Test public void testCreatingAPasswordResetWhenTheUserHasNonUaaOrigin() throws Exception { when(scimUserProvisioning.query("userName eq \"user@example.com\" and origin eq \"" + OriginKeys.UAA + "\"", IdentityZoneHolder.get().getId())) .thenReturn(Arrays.<ScimUser>asList()); ScimUser user = new ScimUser("id001", "user@example.com", null, null); user.setMeta(new ScimMeta(new Date(System.currentTimeMillis()-(1000*60*60*24)), new Date(System.currentTimeMillis()-(1000*60*60*24)), 0)); user.addEmail("user@example.com"); user.setOrigin(OriginKeys.LDAP); when(scimUserProvisioning.query("userName eq \"user@example.com\"", IdentityZoneHolder.get().getId())) .thenReturn(Arrays.<ScimUser>asList(user)); MockHttpServletRequestBuilder post = post("/password_resets") .contentType(APPLICATION_JSON) .content("user@example.com") .accept(APPLICATION_JSON); mockMvc.perform(post) .andExpect(status().isConflict()) .andExpect(content().string(containsString("\"user_id\":\"id001\""))); }
@Test void testCreateClientsTxDuplicateId() throws Exception { BaseClientDetails[] details = createBaseClients(5, SECRET, null); details[details.length - 1] = details[0]; MockHttpServletRequestBuilder createClientPost = post("/oauth/clients/tx") .header("Authorization", "Bearer " + adminToken) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(details)); mockMvc.perform(createClientPost).andExpect(status().isConflict()); for (ClientDetails client : details) { assertNull(getClient(client.getClientId())); } verify(mockApplicationEventPublisher, times(0)).publishEvent(abstractUaaEventCaptor.capture()); }
@Test public void testGenerateEmailChangeCodeWithExistingUsernameChange() throws Exception { String data = "{\"userId\":\"user-id-001\",\"email\":\"new@example.com\",\"client_id\":null}"; ScimUser userChangingEmail = new ScimUser("id001", "user@example.com", null, null); userChangingEmail.setPrimaryEmail("user@example.com"); when(scimUserProvisioning.retrieve("user-id-001", IdentityZoneHolder.get().getId())).thenReturn(userChangingEmail); ScimUser existingUser = new ScimUser("id001", "new@example.com", null, null); when(scimUserProvisioning.query("userName eq \"new@example.com\" and origin eq \"" + OriginKeys.UAA + "\"", IdentityZoneHolder.get().getId())) .thenReturn(Arrays.asList(existingUser)); MockHttpServletRequestBuilder post = post("/email_verifications") .contentType(APPLICATION_JSON) .content(data) .accept(APPLICATION_JSON); mockMvc.perform(post) .andExpect(status().isConflict()); }
.andExpect(status().isConflict());
@Test public void duplicateServiceProvider_isConflict() throws Exception { performCreateServiceProvider() .andExpect(status().isCreated()); performCreateServiceProvider() .andExpect(status().isConflict()); }
@Test void testCreatesZonesWithDuplicateSubdomains() throws Exception { String subdomain = UUID.randomUUID().toString(); String id1 = UUID.randomUUID().toString(); String id2 = UUID.randomUUID().toString(); IdentityZone identityZone1 = MultitenancyFixture.identityZone(id1, subdomain); IdentityZone identityZone2 = MultitenancyFixture.identityZone(id2, subdomain); mockMvc.perform( post("/identity-zones") .header("Authorization", "Bearer " + identityClientToken) .contentType(APPLICATION_JSON) .accept(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(identityZone1))) .andExpect(status().isCreated()); checkZoneAuditEventInUaa(1, AuditEventType.IdentityZoneCreatedEvent); mockMvc.perform( post("/identity-zones") .header("Authorization", "Bearer " + identityClientToken) .contentType(APPLICATION_JSON) .accept(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(identityZone2))) .andExpect(status().isConflict()); assertEquals(1, zoneModifiedEventListener.getEventCount()); }
createIdentityProvider(null, created, accessToken, status().isConflict()); SamlIdentityProviderDefinition samlCreated = created.getConfig(); assertEquals(Arrays.asList("test.com", "test2.com"), samlCreated.getEmailDomain());
@Test public void testDeleteZoneActiveMfaProviderShouldFail() throws Exception { IdentityZone identityZone = MockMvcUtils.createZoneUsingWebRequest(mockMvc, adminToken); MfaProvider<GoogleMfaProviderConfig> mfaProvider = constructGoogleMfaProvider(); mfaProvider = JsonUtils.readValue(mockMvc.perform(post("/mfa-providers") .header("Authorization", "Bearer " + adminToken) .header("X-Identity-Zone-Id", identityZone.getId()) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(mfaProvider))).andReturn().getResponse().getContentAsString(), MfaProvider.class); identityZone.getConfig().getMfaConfig().setEnabled(true).setProviderName(mfaProvider.getName()); MockMvcUtils.updateIdentityZone(identityZone, webApplicationContext); String deleteResponse = mockMvc.perform(delete("/mfa-providers/" + mfaProvider.getId()) .header("Authorization", "Bearer " + adminToken) .header("X-Identity-Zone-Id", identityZone.getId())) .andExpect(status().isConflict()).andReturn().getResponse().getContentAsString(); assertThat(deleteResponse, containsString("MFA provider is currently active on zone: " + identityZone.getId())); }
@Test public void add_member_to_group_twice() throws Exception { ScimUser user = createUserAndAddToGroups(IdentityZone.getUaa(), Collections.EMPTY_SET); String groupId = getGroupId("scim.read"); ScimGroupMember scimGroupMember = new ScimGroupMember(user.getId(), ScimGroupMember.Type.USER); getMockMvc().perform(post("/Groups/" + groupId + "/members") .header("Authorization", "Bearer " + scimWriteToken) .header("Content-Type", APPLICATION_JSON_VALUE) .content(JsonUtils.writeValueAsString(scimGroupMember))) .andExpect(status().isCreated()); getMockMvc().perform(post("/Groups/" + groupId + "/members") .header("Authorization", "Bearer " + scimWriteToken) .header("Content-Type", APPLICATION_JSON_VALUE) .content(JsonUtils.writeValueAsString(scimGroupMember))) .andExpect(status().isConflict()); }
@Test public void testCreateDuplicate() throws Exception { MfaProvider<GoogleMfaProviderConfig> mfaProvider = constructGoogleProvider(); mfaProvider.setConfig(null); mockMvc.perform( post("/mfa-providers") .header("Authorization", "Bearer " + adminToken) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(mfaProvider))).andReturn(); mockMvc.perform( post("/mfa-providers") .header("Authorization", "Bearer " + adminToken) .contentType(APPLICATION_JSON) .content(JsonUtils.writeValueAsString(mfaProvider))) .andDo(print()) .andExpect(status().isConflict()) .andExpect(jsonPath("$.error").value("invalid_mfa_provider")) .andExpect(jsonPath("$.error_description").value("An MFA Provider with that name already exists.")); }
@Test void test_Create_Duplicate_Saml_Identity_Provider_In_Default_Zone() throws Exception { String origin1 = "IDPEndpointsMockTests3-" + new RandomValueStringGenerator().generate(); String origin2 = "IDPEndpointsMockTests4-" + new RandomValueStringGenerator().generate(); String userAccessToken = setUpAccessToken(); eventListener.clearEvents(); IdentityProvider identityProvider = MultitenancyFixture.identityProvider(origin1, IdentityZone.getUaa().getId()); identityProvider.setType(OriginKeys.SAML); SamlIdentityProviderDefinition providerDefinition = new SamlIdentityProviderDefinition() .setMetaDataLocation(String.format(BootstrapSamlIdentityProviderDataTests.xmlWithoutID, "http://www.okta.com/" + identityProvider.getOriginKey())) .setIdpEntityAlias(identityProvider.getOriginKey()) .setNameID("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress") .setLinkText("IDPEndpointsMockTests Saml Provider:" + identityProvider.getOriginKey()) .setZoneId(IdentityZone.getUaa().getId()); identityProvider.setConfig(providerDefinition); IdentityProvider createdIDP = createIdentityProvider(null, identityProvider, userAccessToken, status().isCreated()); assertNotNull(createdIDP.getId()); assertEquals(identityProvider.getName(), createdIDP.getName()); assertEquals(identityProvider.getOriginKey(), createdIDP.getOriginKey()); identityProvider.setOriginKey(origin2); providerDefinition = new SamlIdentityProviderDefinition() .setMetaDataLocation(providerDefinition.getMetaDataLocation()) .setIdpEntityAlias(identityProvider.getOriginKey()) .setNameID("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress") .setLinkText("IDPEndpointsMockTests Saml Provider:" + identityProvider.getOriginKey()) .setZoneId(IdentityZone.getUaa().getId()); identityProvider.setConfig(providerDefinition); createIdentityProvider(null, identityProvider, userAccessToken, status().isConflict()); }
identityProvider.setConfig(providerDefinition); createIdentityProvider(zone.getId(), identityProvider, userAccessToken, status().isConflict());
@Test public void whenDeleteEventTypeThatHasSubscriptionsThenConflict() throws Exception { final EventType eventType = buildDefaultEventType(); when(eventTypeRepository.findByNameO(eventType.getName())).thenReturn(Optional.of(eventType)); when(subscriptionRepository .listSubscriptions(eq(ImmutableSet.of(eventType.getName())), eq(Optional.empty()), anyInt(), anyInt())) .thenReturn(ImmutableList.of(mock(Subscription.class))); final Problem expectedProblem = Problem.valueOf(CONFLICT, "Can't remove event type " + eventType.getName() + ", as it has subscriptions"); deleteEventType(eventType.getName()) .andExpect(status().isConflict()) .andExpect(content().contentType("application/problem+json")) .andExpect(content().string(matchesProblem(expectedProblem))); }
@Test public void whenPostDuplicatedEventTypeReturn409() throws Exception { final Problem expectedProblem = Problem.valueOf(CONFLICT, "some-name"); doThrow(new DuplicatedEventTypeNameException("some-name")).when(eventTypeRepository).saveEventType(any( EventTypeBase.class)); postEventType(buildDefaultEventType()).andExpect(status().isConflict()) .andExpect(content().contentType("application/problem+json")).andExpect( content().string(matchesProblem(expectedProblem))); }
@Test public void add_repeated_page_model_return_conflict() throws Exception { // pageModel home always exists because it's created with DB. String payload = createPageModelPayload("home"); ResultActions result = mockMvc.perform( post("/pageModels") .content(payload) .contentType(MediaType.APPLICATION_JSON_VALUE) .header("Authorization", "Bearer " + accessToken)); result.andExpect(status().isConflict()); }
@Test public void POST_whenUserTriesToAddAPermissionThatAlreadyExists_theyReceiveAConflict() throws Exception { final String credentialName = "/user-a/*"; final MockHttpServletRequestBuilder addPermissionRequest = post("/api/v2/permissions") .header("Authorization", "Bearer " + ALL_PERMISSIONS_TOKEN) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content("{" + " \"actor\": \"" + USER_A_ACTOR_ID + "\",\n" + " \"path\": \"" + credentialName + "\",\n" + " \"operations\": [\"read\"]\n" + "}"); mockMvc.perform(addPermissionRequest).andExpect(status().isConflict()); }
@Test public void POST_whenUserTriesToAddAnAdditionalOperationToAPermissionThatAlreadyExists_theyReceiveAConflict() throws Exception { final String credentialName = "/test"; PermissionsV2EndToEndTestHelper.setPermissions(mockMvc, credentialName, PermissionOperation.WRITE); final MockHttpServletRequestBuilder addPermissionRequestWithRead = post("/api/v2/permissions") .header("Authorization", "Bearer " + ALL_PERMISSIONS_TOKEN) .accept(APPLICATION_JSON) .contentType(APPLICATION_JSON) .content("{" + " \"actor\": \"" + USER_A_ACTOR_ID + "\",\n" + " \"path\": \"" + credentialName + "\",\n" + " \"operations\": [\"write\", \"read\"]\n" + "}"); mockMvc.perform(addPermissionRequestWithRead).andExpect(status().isConflict()); }
@Test public void testAddExistingGroup() throws Exception { UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build(); String accessToken = mockOAuthInterceptor(user); GroupDto group = this.groupService.getGroup(Group.FREE_GROUP_NAME); GroupRequest groupRequest = new GroupRequest(); groupRequest.setCode(group.getCode()); groupRequest.setName(group.getName()); ObjectMapper mapper = new ObjectMapper(); String payload = mapper.writeValueAsString(groupRequest); ResultActions result = mockMvc.perform( post("/groups") .content(payload) .contentType(MediaType.APPLICATION_JSON_VALUE) .header("Authorization", "Bearer " + accessToken)); result.andExpect(status().isConflict()); }
@Test public void createBindingWithDuplicateIdFails() throws Exception { setupCatalogService(); when(serviceInstanceBindingService.createServiceInstanceBinding(any(CreateServiceInstanceBindingRequest.class))) .thenThrow(new ServiceInstanceBindingExistsException(SERVICE_INSTANCE_ID, SERVICE_INSTANCE_BINDING_ID)); MvcResult mvcResult = mockMvc.perform(put(buildCreateUrl()) .content(createRequestBody) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isConflict()) .andExpect(jsonPath("$.description", containsString(SERVICE_INSTANCE_ID))) .andExpect(jsonPath("$.description", containsString(SERVICE_INSTANCE_BINDING_ID))); }
@Test public void createDuplicateServiceInstanceIdFails() throws Exception { setupCatalogService(); setupServiceInstanceService(new ServiceInstanceExistsException(SERVICE_INSTANCE_ID, serviceDefinition.getId())); MvcResult mvcResult = mockMvc.perform(put(buildCreateUpdateUrl()) .content(createRequestBody) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(request().asyncStarted()) .andReturn(); mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isConflict()) .andExpect(jsonPath("$.description", containsString(SERVICE_INSTANCE_ID))); }