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);
}
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());
}
}
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);
}
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"))));
}
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;
}
Aggregations