EmailChangeEmailService implements ChangeEmailService { @Override public void beginEmailChange(String userId, String email, String newEmail, String clientId, String redirectUri) { ScimUser user = scimUserProvisioning.retrieve(userId, identityZoneManager.getCurrentIdentityZoneId()); List<ScimUser> results = scimUserProvisioning.retrieveByUsernameAndOriginAndZone(newEmail, OriginKeys.UAA, identityZoneManager.getCurrentIdentityZoneId()); if (user.getUserName().equals(user.getPrimaryEmail())) { if (!results.isEmpty()) { throw new UaaException("Conflict", 409); } } String code = generateExpiringCode(userId, newEmail, clientId, redirectUri); String htmlContent = getEmailChangeEmailHtml(email, newEmail, code); if (htmlContent != null) { String subject = getSubjectText(); messageService.sendMessage(newEmail, MessageType.CHANGE_EMAIL, subject, htmlContent); } } EmailChangeEmailService(final TemplateEngine templateEngine,
                            final MessageService messageService,
                            final ScimUserProvisioning scimUserProvisioning,
                            final ExpiringCodeStore codeStore,
                            final MultitenantClientServices clientDetailsService,
                            final IdentityZoneManager identityZoneManager); @Override void beginEmailChange(String userId, String email, String newEmail, String clientId, String redirectUri); @Override Map<String, String> completeVerification(String code);  }
@Test void beginEmailChange() { ScimUser user = new ScimUser("user-001", "user-name", "test-name", "test-name"); user.setPrimaryEmail("user@example.com"); Map<String, String> codeData = new HashMap<>(); codeData.put("user_id", "user-001"); codeData.put("client_id", "app"); codeData.put("redirect_uri", "http: codeData.put("email", "new@example.com"); when(mockScimUserProvisioning.retrieve("user-001", zoneId)).thenReturn(user); when(mockScimUserProvisioning.query(anyString(), eq(zoneId))).thenReturn(Collections.singletonList(new ScimUser())); String data = JsonUtils.writeValueAsString(codeData); when(mockExpiringCodeStore.generateCode(eq(data), any(Timestamp.class), eq(EMAIL.name()), anyString())).thenReturn(new ExpiringCode("the_secret_code", new Timestamp(System.currentTimeMillis()), data, EMAIL.name())); emailChangeEmailService.beginEmailChange("user-001", "user@example.com", "new@example.com", "app", "http: verify(mockExpiringCodeStore).generateCode(eq(JsonUtils.writeValueAsString(codeData)), any(Timestamp.class), eq(EMAIL.name()), eq(zoneId)); verify(mockEmailService).sendMessage( eq("new@example.com"), eq(MessageType.CHANGE_EMAIL), eq(String.format("%s Email change verification", zoneName)), contains("<a href=\"http: ); }
@Test void beginEmailChangeWithUsernameConflict() { ScimUser user = new ScimUser("user-001", "user@example.com", "test-name", "test-name"); user.setPrimaryEmail("user@example.com"); when(mockScimUserProvisioning.retrieve(anyString(), anyString())).thenReturn(user); when(mockScimUserProvisioning.retrieveByUsernameAndOriginAndZone( anyString(), anyString(), eq(zoneId)) ).thenReturn(Collections.singletonList(new ScimUser())); Assertions.assertThrows(UaaException.class, () -> emailChangeEmailService.beginEmailChange("user-001", "user@example.com", "new@example.com", null, null)); }
@Test void beginEmailChangeWithCompanyNameConfigured() { emailChangeEmailService = new EmailChangeEmailService( templateEngine, mockEmailService, mockScimUserProvisioning, mockExpiringCodeStore, mockMultitenantClientServices, mockIdentityZoneManager); ScimUser user = new ScimUser("user-001", "user-name", "test-name", "test-name"); user.setPrimaryEmail("user@example.com"); Map<String, String> codeData = new HashMap<>(); codeData.put("user_id", "user-001"); codeData.put("client_id", "app"); codeData.put("redirect_uri", "http: codeData.put("email", "new@example.com"); identityZone = IdentityZone.getUaa(); zoneId = identityZone.getId(); when(mockIdentityZoneManager.isCurrentZoneUaa()).thenReturn(true); when(mockIdentityZoneManager.getCurrentIdentityZoneId()).thenReturn(identityZone.getId()); BrandingInformation branding = new BrandingInformation(); branding.setCompanyName("Best Company"); IdentityZoneConfiguration config = new IdentityZoneConfiguration(); config.setBranding(branding); identityZone.setConfig(config); when(mockScimUserProvisioning.retrieve("user-001", zoneId)).thenReturn(user); when(mockScimUserProvisioning.query(anyString(), eq(zoneId))).thenReturn(Collections.singletonList(new ScimUser())); String data = JsonUtils.writeValueAsString(codeData); when(mockExpiringCodeStore.generateCode(eq(data), any(Timestamp.class), eq(EMAIL.name()), anyString())).thenReturn(new ExpiringCode("the_secret_code", new Timestamp(System.currentTimeMillis()), data, EMAIL.name())); setIdentityZoneHolder(identityZone); emailChangeEmailService.beginEmailChange("user-001", "user@example.com", "new@example.com", "app", "http: verify(mockExpiringCodeStore).generateCode(eq(JsonUtils.writeValueAsString(codeData)), any(Timestamp.class), eq(EMAIL.name()), eq(zoneId)); ArgumentCaptor<String> emailBodyArgument = ArgumentCaptor.forClass(String.class); verify(mockEmailService).sendMessage( eq("new@example.com"), eq(MessageType.CHANGE_EMAIL), eq("Best Company Email change verification"), emailBodyArgument.capture() ); String emailBody = emailBodyArgument.getValue(); assertThat(emailBody, containsString("<a href=\"http: assertThat(emailBody, containsString("a Best Company account")); }