SES: Correct Exception format + Java tests (#7346)

This commit is contained in:
Bert Blommers 2024-02-15 21:25:58 +00:00 committed by GitHub
parent a9cec5e780
commit e6818c3f1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 83 additions and 43 deletions

View File

@ -1,100 +1,82 @@
from typing import Any
from moto.core.exceptions import RESTError
class MessageRejectedError(RESTError):
class SesError(RESTError):
code = 400
def __init__(self, error_type: str, message: str, *args: Any, **kwargs: Any):
kwargs.setdefault("template", "wrapped_single_error")
super().__init__(error_type, message, *args, **kwargs)
class MessageRejectedError(SesError):
def __init__(self, message: str):
super().__init__("MessageRejected", message)
class ConfigurationSetDoesNotExist(RESTError):
code = 400
class ConfigurationSetDoesNotExist(SesError):
def __init__(self, message: str):
super().__init__("ConfigurationSetDoesNotExist", message)
class ConfigurationSetAlreadyExists(RESTError):
code = 400
class ConfigurationSetAlreadyExists(SesError):
def __init__(self, message: str):
super().__init__("ConfigurationSetAlreadyExists", message)
class EventDestinationAlreadyExists(RESTError):
code = 400
class EventDestinationAlreadyExists(SesError):
def __init__(self, message: str):
super().__init__("EventDestinationAlreadyExists", message)
class TemplateNameAlreadyExists(RESTError):
code = 400
class TemplateNameAlreadyExists(SesError):
def __init__(self, message: str):
super().__init__("TemplateNameAlreadyExists", message)
class ValidationError(RESTError):
code = 400
class ValidationError(SesError):
def __init__(self, message: str):
super().__init__("ValidationError", message)
class InvalidParameterValue(RESTError):
code = 400
class InvalidParameterValue(SesError):
def __init__(self, message: str):
super().__init__("InvalidParameterValue", message)
class InvalidRenderingParameterException(RESTError):
code = 400
class InvalidRenderingParameterException(SesError):
def __init__(self, message: str):
super().__init__("InvalidRenderingParameterException", message)
class TemplateDoesNotExist(RESTError):
code = 400
class TemplateDoesNotExist(SesError):
def __init__(self, message: str):
super().__init__("TemplateDoesNotExist", message)
class RuleSetNameAlreadyExists(RESTError):
code = 400
class RuleSetNameAlreadyExists(SesError):
def __init__(self, message: str):
super().__init__("RuleSetNameAlreadyExists", message)
class RuleAlreadyExists(RESTError):
code = 400
class RuleAlreadyExists(SesError):
def __init__(self, message: str):
super().__init__("RuleAlreadyExists", message)
class RuleSetDoesNotExist(RESTError):
code = 400
class RuleSetDoesNotExist(SesError):
def __init__(self, message: str):
super().__init__("RuleSetDoesNotExist", message)
class RuleDoesNotExist(RESTError):
code = 400
class RuleDoesNotExist(SesError):
def __init__(self, message: str):
super().__init__("RuleDoesNotExist", message)
class MissingRenderingAttributeException(RESTError):
code = 400
class MissingRenderingAttributeException(SesError):
def __init__(self, var: str):
super().__init__(
"MissingRenderingAttributeException",

View File

@ -40,6 +40,10 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ses</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>

View File

@ -6,6 +6,7 @@ import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.utils.AttributeMap;
@ -32,6 +33,14 @@ public class DependencyFactory {
.build();
}
public static SesClient sesClient() {
return SesClient.builder()
.region(Region.US_EAST_1)
.httpClientBuilder(ApacheHttpClient.builder())
.endpointOverride(MOTO_URI)
.build();
}
public static DynamoDbClient dynamoClient() {
final AttributeMap attributeMap = AttributeMap.builder()
.put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, true)

View File

@ -0,0 +1,45 @@
package moto.tests;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.Body;
import software.amazon.awssdk.services.ses.model.Content;
import software.amazon.awssdk.services.ses.model.Destination;
import software.amazon.awssdk.services.ses.model.Message;
import software.amazon.awssdk.services.ses.model.SendEmailRequest;
import software.amazon.awssdk.services.ses.model.SendEmailResponse;
import software.amazon.awssdk.services.ses.model.SesException;
public class SESTest {
@Test
public void testUnverifiedEmail() {
SesClient sesClient = DependencyFactory.sesClient();
Message message = Message
.builder()
.subject(Content.builder().charset("UTF-8").data("subject").build())
.body(Body.builder().text(Content.builder().charset("UTF-8").data("body").build()).build())
.build();
SendEmailRequest request = SendEmailRequest
.builder()
.source("noreply@thedomain.com")
.destination(Destination.builder().toAddresses(List.of("one@one.com")).build())
.message(message)
.build();
try {
System.out.println("Attempting to send an email...");
sesClient.sendEmail(request);
} catch (SesException e) {
assertEquals(e.awsErrorDetails().errorMessage(), "Email address not verified noreply@thedomain.com");
}
}
}