Search in sources :

Example 1 with MockAuthentication

use of org.cloudfoundry.identity.uaa.test.MockAuthentication in project uaa by cloudfoundry.

the class UaaChangePasswordServiceTest method setUp.

@Before
public void setUp() {
    SecurityContextHolder.clearContext();
    SecurityContextHolder.getContext().setAuthentication(new MockAuthentication());
    scimUserProvisioning = mock(ScimUserProvisioning.class);
    passwordValidator = mock(PasswordValidator.class);
    subject = new UaaChangePasswordService(scimUserProvisioning, passwordValidator);
}
Also used : MockAuthentication(org.cloudfoundry.identity.uaa.test.MockAuthentication) PasswordValidator(org.cloudfoundry.identity.uaa.scim.validate.PasswordValidator) ScimUserProvisioning(org.cloudfoundry.identity.uaa.scim.ScimUserProvisioning) UaaChangePasswordService(org.cloudfoundry.identity.uaa.account.UaaChangePasswordService) Before(org.junit.Before)

Example 2 with MockAuthentication

use of org.cloudfoundry.identity.uaa.test.MockAuthentication in project uaa by cloudfoundry.

the class UaaResetPasswordServiceTests method resetPassword_InvalidCodeData.

@Test
void resetPassword_InvalidCodeData() {
    ExpiringCode expiringCode = new ExpiringCode("good_code", new Timestamp(System.currentTimeMillis() + UaaResetPasswordService.PASSWORD_RESET_LIFETIME), "user-id", null);
    when(codeStore.retrieveCode("good_code", currentZoneId)).thenReturn(expiringCode);
    SecurityContext securityContext = mock(SecurityContext.class);
    when(securityContext.getAuthentication()).thenReturn(new MockAuthentication());
    SecurityContextHolder.setContext(securityContext);
    try {
        uaaResetPasswordService.resetPassword(expiringCode, "password");
        fail();
    } catch (InvalidCodeException e) {
        assertEquals("Sorry, your reset password link is no longer valid. Please request a new one", e.getMessage());
    }
}
Also used : ExpiringCode(org.cloudfoundry.identity.uaa.codestore.ExpiringCode) MockAuthentication(org.cloudfoundry.identity.uaa.test.MockAuthentication) SecurityContext(org.springframework.security.core.context.SecurityContext) Timestamp(java.sql.Timestamp) InvalidCodeException(org.cloudfoundry.identity.uaa.authentication.InvalidCodeException) Test(org.junit.jupiter.api.Test)

Example 3 with MockAuthentication

use of org.cloudfoundry.identity.uaa.test.MockAuthentication in project uaa by cloudfoundry.

the class PasswordResetEndpointTest method changingAPasswordForUnverifiedUser.

@Test
void changingAPasswordForUnverifiedUser() throws Exception {
    ExpiringCode code = new ExpiringCode("secret_code", new Timestamp(System.currentTimeMillis() + UaaResetPasswordService.PASSWORD_RESET_LIFETIME), "{\"user_id\":\"eyedee\",\"username\":\"user@example.com\",\"passwordModifiedTime\":null,\"client_id\":\"\",\"redirect_uri\":\"\"}", null);
    when(mockExpiringCodeStore.retrieveCode("secret_code", currentZoneId)).thenReturn(code);
    ScimUser scimUser = new ScimUser("eyedee", "user@example.com", "User", "Man");
    scimUser.setMeta(new ScimMeta(new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)), new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)), 0));
    scimUser.addEmail("user@example.com");
    scimUser.setVerified(false);
    when(mockScimUserProvisioning.retrieve("eyedee", currentZoneId)).thenReturn(scimUser);
    ExpiringCode autologinCode = new ExpiringCode("autologin-code", new Timestamp(System.currentTimeMillis() + 5 * 60 * 1000), "data", AUTOLOGIN.name());
    when(mockExpiringCodeStore.generateCode(anyString(), any(Timestamp.class), eq(AUTOLOGIN.name()), anyString())).thenReturn(autologinCode);
    MockHttpServletRequestBuilder post = post("/password_change").contentType(APPLICATION_JSON).content("{\"code\":\"secret_code\",\"new_password\":\"new_secret\"}").accept(APPLICATION_JSON);
    SecurityContextHolder.getContext().setAuthentication(new MockAuthentication());
    mockMvc.perform(post).andExpect(status().isOk()).andExpect(jsonPath("$.user_id").value("eyedee")).andExpect(jsonPath("$.username").value("user@example.com"));
    verify(mockScimUserProvisioning).changePassword("eyedee", null, "new_secret", currentZoneId);
    verify(mockScimUserProvisioning).verifyUser(scimUser.getId(), -1, currentZoneId);
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) ScimMeta(org.cloudfoundry.identity.uaa.scim.ScimMeta) ExpiringCode(org.cloudfoundry.identity.uaa.codestore.ExpiringCode) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockAuthentication(org.cloudfoundry.identity.uaa.test.MockAuthentication) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with MockAuthentication

use of org.cloudfoundry.identity.uaa.test.MockAuthentication in project uaa by cloudfoundry.

the class PasswordResetEndpointTest method changePassword_Returns422UnprocessableEntity_NewPasswordSameAsOld.

@Test
void changePassword_Returns422UnprocessableEntity_NewPasswordSameAsOld() throws Exception {
    Mockito.reset(mockPasswordValidator);
    when(mockExpiringCodeStore.retrieveCode("emailed_code", currentZoneId)).thenReturn(new ExpiringCode("emailed_code", new Timestamp(System.currentTimeMillis() + UaaResetPasswordService.PASSWORD_RESET_LIFETIME), "{\"user_id\":\"eyedee\",\"username\":\"user@example.com\",\"passwordModifiedTime\":null,\"client_id\":\"\",\"redirect_uri\":\"\"}", null));
    ScimUser scimUser = new ScimUser("eyedee", "user@example.com", "User", "Man");
    scimUser.setMeta(new ScimMeta(new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)), new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)), 0));
    scimUser.addEmail("user@example.com");
    scimUser.setVerified(true);
    when(mockScimUserProvisioning.retrieve("eyedee", currentZoneId)).thenReturn(scimUser);
    when(mockScimUserProvisioning.checkPasswordMatches("eyedee", "new_secret", currentZoneId)).thenReturn(true);
    MockHttpServletRequestBuilder post = post("/password_change").contentType(APPLICATION_JSON).content("{\"code\":\"emailed_code\",\"new_password\":\"new_secret\"}").accept(APPLICATION_JSON);
    SecurityContextHolder.getContext().setAuthentication(new MockAuthentication());
    mockMvc.perform(post).andExpect(status().isUnprocessableEntity()).andExpect(content().string(JsonObjectMatcherUtils.matchesJsonObject(new JSONObject().put("error_description", "Your new password cannot be the same as the old password.").put("message", "Your new password cannot be the same as the old password.").put("error", "invalid_password"))));
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) ScimMeta(org.cloudfoundry.identity.uaa.scim.ScimMeta) ExpiringCode(org.cloudfoundry.identity.uaa.codestore.ExpiringCode) JSONObject(org.json.JSONObject) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockAuthentication(org.cloudfoundry.identity.uaa.test.MockAuthentication) Timestamp(java.sql.Timestamp) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 5 with MockAuthentication

use of org.cloudfoundry.identity.uaa.test.MockAuthentication in project uaa by cloudfoundry.

the class UaaResetPasswordServiceTests method setupResetPassword.

private ExpiringCode setupResetPassword(String clientId, String redirectUri) {
    ScimUser user = new ScimUser("usermans-id", "userman", "firstName", "lastName");
    user.setMeta(new ScimMeta(new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)), new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)), 0));
    user.setPrimaryEmail("user@example.com");
    String zoneId = currentZoneId;
    when(scimUserProvisioning.retrieve(eq("usermans-id"), eq(zoneId))).thenReturn(user);
    ExpiringCode code = new ExpiringCode("code", new Timestamp(System.currentTimeMillis()), "{\"user_id\":\"usermans-id\",\"username\":\"userman\",\"passwordModifiedTime\":null,\"client_id\":\"" + clientId + "\",\"redirect_uri\":\"" + redirectUri + "\"}", null);
    when(codeStore.retrieveCode(eq("secret_code"), anyString())).thenReturn(code);
    SecurityContext securityContext = mock(SecurityContext.class);
    when(securityContext.getAuthentication()).thenReturn(new MockAuthentication());
    SecurityContextHolder.setContext(securityContext);
    return code;
}
Also used : ScimUser(org.cloudfoundry.identity.uaa.scim.ScimUser) ScimMeta(org.cloudfoundry.identity.uaa.scim.ScimMeta) ExpiringCode(org.cloudfoundry.identity.uaa.codestore.ExpiringCode) MockAuthentication(org.cloudfoundry.identity.uaa.test.MockAuthentication) SecurityContext(org.springframework.security.core.context.SecurityContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

MockAuthentication (org.cloudfoundry.identity.uaa.test.MockAuthentication)8 Timestamp (java.sql.Timestamp)6 ExpiringCode (org.cloudfoundry.identity.uaa.codestore.ExpiringCode)6 Test (org.junit.jupiter.api.Test)6 Date (java.util.Date)5 ScimMeta (org.cloudfoundry.identity.uaa.scim.ScimMeta)5 ScimUser (org.cloudfoundry.identity.uaa.scim.ScimUser)5 SecurityContext (org.springframework.security.core.context.SecurityContext)3 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)3 UaaChangePasswordService (org.cloudfoundry.identity.uaa.account.UaaChangePasswordService)1 Approval (org.cloudfoundry.identity.uaa.approval.Approval)1 ApprovalModifiedEvent (org.cloudfoundry.identity.uaa.audit.event.ApprovalModifiedEvent)1 InvalidCodeException (org.cloudfoundry.identity.uaa.authentication.InvalidCodeException)1 ScimUserProvisioning (org.cloudfoundry.identity.uaa.scim.ScimUserProvisioning)1 InvalidPasswordException (org.cloudfoundry.identity.uaa.scim.exception.InvalidPasswordException)1 PasswordValidator (org.cloudfoundry.identity.uaa.scim.validate.PasswordValidator)1 UaaTestAccounts (org.cloudfoundry.identity.uaa.test.UaaTestAccounts)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 JSONObject (org.json.JSONObject)1 Before (org.junit.Before)1