Search in sources :

Example 1 with EmailGlobalConfigModel

use of com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel in project hub-alert by blackducksoftware.

the class UpdateEmailService method sendUpdateEmail.

public void sendUpdateEmail(UpdateModel updateModel) {
    String updateVersion = updateModel.getDockerTagVersion();
    if (wasEmailAlreadySentForVersion(updateVersion)) {
        return;
    }
    String username = "sysadmin";
    Optional<String> optionalEmailAddress = userAccessor.getUser(username).map(UserModel::getEmailAddress).filter(StringUtils::isNotBlank);
    if (optionalEmailAddress.isPresent()) {
        try {
            EmailGlobalConfigModel emailServerConfiguration = emailGlobalConfigAccessor.getConfiguration().orElseThrow(() -> new AlertException("No global email configuration found"));
            String alertServerUrl = alertProperties.getServerURL();
            Map<String, Object> templateFields = new HashMap<>();
            templateFields.put(EmailPropertyKeys.TEMPLATE_KEY_SUBJECT_LINE.getPropertyKey(), SUBJECT_LINE);
            templateFields.put("newVersionName", updateVersion);
            templateFields.put("repositoryUrl", updateModel.getRepositoryUrl());
            templateFields.put(FreemarkerTemplatingService.KEY_ALERT_SERVER_URL, alertServerUrl);
            handleSend(javamailPropertiesFactory.createJavaMailProperties(emailServerConfiguration), emailServerConfiguration.getSmtpFrom().orElse(StringUtils.EMPTY), emailServerConfiguration.getSmtpHost().orElse(StringUtils.EMPTY), emailServerConfiguration.getSmtpPort().orElse(0), emailServerConfiguration.getSmtpAuth().orElse(false), emailServerConfiguration.getSmtpUsername().orElse(StringUtils.EMPTY), emailServerConfiguration.getSmtpPassword().orElse(StringUtils.EMPTY), templateFields, optionalEmailAddress.get());
            settingsKeyAccessor.saveSettingsKey(SETTINGS_KEY_VERSION_FOR_UPDATE_EMAIL, updateVersion);
        } catch (AlertException e) {
            logger.debug("Problem sending version update email.", e);
        }
    } else {
        logger.debug("No email address configured for user: {}", username);
    }
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 2 with EmailGlobalConfigModel

use of com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationActionTest method testUpdate.

@Test
public void testUpdate() throws AlertConfigurationException {
    AuthenticationTestUtils authenticationTestUtils = new AuthenticationTestUtils();
    DescriptorKey descriptorKey = ChannelKeys.EMAIL;
    PermissionKey permissionKey = new PermissionKey(ConfigContextEnum.GLOBAL.name(), descriptorKey.getUniversalKey());
    Map<PermissionKey, Integer> permissions = Map.of(permissionKey, AuthenticationTestUtils.FULL_PERMISSIONS);
    AuthorizationManager authorizationManager = authenticationTestUtils.createAuthorizationManagerWithCurrentUserSet("admin", "admin", () -> new PermissionMatrixModel(permissions));
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailGlobalConfigAccessor emailGlobalConfigAccessor = Mockito.mock(EmailGlobalConfigAccessor.class);
    EmailGlobalConfigModel model = new EmailGlobalConfigModel();
    model.setName(AlertRestConstants.DEFAULT_CONFIGURATION_NAME);
    model.setSmtpHost("host");
    model.setSmtpFrom("from");
    model.setSmtpAuth(true);
    model.setSmtpUsername("user");
    model.setSmtpPassword("password");
    Mockito.when(emailGlobalConfigAccessor.getConfiguration()).thenReturn(Optional.of(model));
    Mockito.when(emailGlobalConfigAccessor.updateConfiguration(Mockito.eq(model))).thenReturn(model);
    Mockito.when(emailGlobalConfigAccessor.doesConfigurationExist()).thenReturn(true);
    EmailGlobalCrudActions configActions = new EmailGlobalCrudActions(authorizationManager, emailGlobalConfigAccessor, validator);
    ActionResponse<EmailGlobalConfigModel> response = configActions.update(model);
    assertEquals(HttpStatus.OK, response.getHttpStatus());
    assertTrue(response.hasContent());
    assertEquals(model.obfuscate(), response.getContent().get());
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) EmailGlobalConfigAccessor(com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor) AuthenticationTestUtils(com.synopsys.integration.alert.test.common.AuthenticationTestUtils) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Example 3 with EmailGlobalConfigModel

use of com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel in project hub-alert by blackducksoftware.

the class EmailGlobalTestActionTest method createEmailGlobalConfigModelObfuscated.

private EmailGlobalConfigModel createEmailGlobalConfigModelObfuscated(TestProperties testProperties) {
    EmailGlobalConfigModel emailGlobalConfigModel = createValidEmailGlobalConfigModel(testProperties);
    if (testProperties.getOptionalProperty(TestPropertyKey.TEST_EMAIL_SMTP_PASSWORD).isPresent()) {
        emailGlobalConfigModel.setIsSmtpPasswordSet(true);
        emailGlobalConfigModel.setSmtpPassword(null);
    }
    return emailGlobalConfigModel;
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)

Example 4 with EmailGlobalConfigModel

use of com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel in project hub-alert by blackducksoftware.

the class EmailGlobalTestActionTest method testPermissionConfigInvalidDestinationTest.

@Test
public void testPermissionConfigInvalidDestinationTest() {
    AuthorizationManager authorizationManager = createAuthorizationManager(AuthenticationTestUtils.FULL_PERMISSIONS);
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailGlobalTestAction emailGlobalTestAction = new EmailGlobalTestAction(authorizationManager, validator, null, null, configurationAccessor);
    ActionResponse<ValidationResponseModel> response = emailGlobalTestAction.testWithPermissionCheck("not a valid email address", new EmailGlobalConfigModel());
    assertEquals(HttpStatus.OK, response.getHttpStatus());
    assertTrue(response.hasContent());
    assertTrue(response.getContent().get().hasErrors(), "Expected the message result to not have errors");
}
Also used : ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Example 5 with EmailGlobalConfigModel

use of com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel in project hub-alert by blackducksoftware.

the class EmailGlobalTestActionTest method testPermissionConfigMissingDestinationTest.

@Test
public void testPermissionConfigMissingDestinationTest() {
    AuthorizationManager authorizationManager = createAuthorizationManager(AuthenticationTestUtils.FULL_PERMISSIONS);
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailAddressGatherer emailAddressGatherer = Mockito.mock(EmailAddressGatherer.class);
    Mockito.when(emailAddressGatherer.gatherEmailAddresses(Mockito.any(), Mockito.any())).thenReturn(Set.of());
    JobEmailAddressValidator emailAddressValidator = Mockito.mock(JobEmailAddressValidator.class);
    Mockito.when(emailAddressValidator.validate(Mockito.any(), Mockito.anyCollection())).thenReturn(new ValidatedEmailAddresses(Set.of(), Set.of()));
    MockAlertProperties testAlertProperties = new MockAlertProperties();
    MessageContentGroupCsvCreator messageContentGroupCsvCreator = new MessageContentGroupCsvCreator();
    Gson gson = new Gson();
    EmailAttachmentFileCreator emailAttachmentFileCreator = new EmailAttachmentFileCreator(testAlertProperties, messageContentGroupCsvCreator, gson);
    FreemarkerTemplatingService freemarkerTemplatingService = new FreemarkerTemplatingService();
    EmailMessagingService emailMessagingService = new EmailMessagingService(freemarkerTemplatingService);
    EmailChannelMessagingService emailChannelMessagingService = new EmailChannelMessagingService(testAlertProperties, emailMessagingService, emailAttachmentFileCreator);
    JavamailPropertiesFactory javamailPropertiesFactory = new JavamailPropertiesFactory();
    EmailGlobalTestAction emailGlobalTestAction = new EmailGlobalTestAction(authorizationManager, validator, emailChannelMessagingService, javamailPropertiesFactory, configurationAccessor);
    ActionResponse<ValidationResponseModel> response = emailGlobalTestAction.testWithPermissionCheck("", new EmailGlobalConfigModel());
    assertEquals(HttpStatus.OK, response.getHttpStatus());
    assertTrue(response.hasContent());
    assertTrue(response.getContent().get().hasErrors(), "Expected the message result to not have errors");
}
Also used : MessageContentGroupCsvCreator(com.synopsys.integration.alert.channel.email.attachment.MessageContentGroupCsvCreator) EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) EmailMessagingService(com.synopsys.integration.alert.service.email.EmailMessagingService) EmailAttachmentFileCreator(com.synopsys.integration.alert.channel.email.attachment.EmailAttachmentFileCreator) Gson(com.google.gson.Gson) EmailAddressGatherer(com.synopsys.integration.alert.channel.email.distribution.address.EmailAddressGatherer) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) EmailGlobalConfigurationValidator(com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator) FreemarkerTemplatingService(com.synopsys.integration.alert.service.email.template.FreemarkerTemplatingService) JobEmailAddressValidator(com.synopsys.integration.alert.channel.email.distribution.address.JobEmailAddressValidator) MockAlertProperties(com.synopsys.integration.alert.test.common.MockAlertProperties) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) ValidatedEmailAddresses(com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses) EmailChannelMessagingService(com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessagingService) JavamailPropertiesFactory(com.synopsys.integration.alert.service.email.JavamailPropertiesFactory) Test(org.junit.jupiter.api.Test)

Aggregations

EmailGlobalConfigModel (com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)51 Test (org.junit.jupiter.api.Test)41 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)27 EmailGlobalConfigurationValidator (com.synopsys.integration.alert.channel.email.validator.EmailGlobalConfigurationValidator)26 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)15 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)15 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)15 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)15 AuthenticationTestUtils (com.synopsys.integration.alert.test.common.AuthenticationTestUtils)15 EmailGlobalConfigAccessor (com.synopsys.integration.alert.channel.email.database.accessor.EmailGlobalConfigAccessor)14 JavamailPropertiesFactory (com.synopsys.integration.alert.service.email.JavamailPropertiesFactory)10 EmailChannelMessagingService (com.synopsys.integration.alert.channel.email.distribution.EmailChannelMessagingService)9 AlertFieldStatus (com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)8 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)6 ConfigurationTestResult (com.synopsys.integration.alert.common.message.model.ConfigurationTestResult)6 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)5 UUID (java.util.UUID)5 EmailGlobalCrudActions (com.synopsys.integration.alert.channel.email.action.EmailGlobalCrudActions)4 ValidatedEmailAddresses (com.synopsys.integration.alert.channel.email.distribution.address.ValidatedEmailAddresses)4 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)4