Esempio n. 1
0
        public async Task Post_Should_LogMessage()
        {
            // arrange
            _fixture.Client.DefaultRequestHeaders.Authorization = _fixture.GetValidAuthHeader(_fixture.TestApp);
            var model = new PostEmailRequest
            {
                To = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Hello",
                Body    = "Hi bob"
            }.ToFormContent();

            // act
            var response = await _fixture.Client.PostAndGetJsonAsync <PostMessageResponse>("v1/Messages", model);

            // assert
            var decoded = response.Decode();

            Assert.Contains(Stubs.InMemoryEmailLog.ProcessingLog, m =>
                            m.Token.RequestId == decoded.RequestId &&
                            m.RetryCount == 0 &&
                            m.Status == ProcessingStatus.Pending &&
                            m.Token.TimeStamp == decoded.TimeStamp);
        }
Esempio n. 2
0
        public async Task Post_Should_ReturnBadRequest_IfNoRecipients()
        {
            // arrange
            _fixture.Client.DefaultRequestHeaders.Authorization = _fixture.GetValidAuthHeader(_fixture.TestApp);
            var model = new PostEmailRequest
            {
                Body = "This won't work"
            }.ToFormContent();

            // act
            var response = await _fixture.Client.PostAsync("v1/Messages", model);

            // assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
        private EmailMessageParams BuildMessage(PostEmailRequest args)
        {
            const string EmptyData = "{}";

            return(new EmailMessageParams
            {
                To = args.To,
                CC = args.CC,
                Bcc = args.Bcc,
                LogLevel = args.LogLevel,
                TemplateId = args.Template,
                Subject = args.Subject,
                BodyEncoded = EmailMessageParams.EncodeBody(args.Body),
                Data = JObject.Parse(args.Data ?? EmptyData).ToObject <Dictionary <string, object> >()
            });
        }
        public async Task <IActionResult> Post(
            [FromForm] PostEmailRequest args,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var applicationId = User.GetApplicationId();

            // the token will be used by both the processing engine and the
            // client to track this request from start to finish
            var token = EmailQueueToken.Create(applicationId);

            _logger.LogInformation("Sending email using token {0}", token);

            if (ModelState.IsValid)
            {
                // create an object that we then store as a BLOB (emails run
                // the risk of being too large to fit in the queue, so BLOB
                // storage is the best option)
                var message = BuildMessage(args);
                message.ApplicationId = applicationId;
                await _blobStore.AddAsync(token, message, cancellationToken);

                // now we can let the back-end processor know that there's a
                // new message that it has to process
                await _sender.SendAsync(token, cancellationToken);

                // log that we queued the message for processing
                await _logWriter.TryLogProcessAttemptAsync(token, 0, ProcessingStatus.Pending, token.TimeStamp, token.TimeStamp, null, cancellationToken);

                // all done - let the client know that we've accepted their
                // request, and what the tracking token is
                Response.StatusCode = (int)HttpStatusCode.Accepted;
                return(Json(new
                {
                    Token = token.EncodeString()
                }));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Esempio n. 5
0
        public async Task Post_Should_ReturnHttpAccepted()
        {
            // arrange
            _fixture.Client.DefaultRequestHeaders.Authorization = _fixture.GetValidAuthHeader(_fixture.TestApp);
            var model = new PostEmailRequest
            {
                To = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Hello",
                Body    = "Hi bob"
            }.ToFormContent();

            // act
            var response = await _fixture.Client.PostAsync("v1/Messages", model);

            // assert
            Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
        }
Esempio n. 6
0
        public async Task Post_Should_ReturnToken()
        {
            // arrange
            _fixture.Client.DefaultRequestHeaders.Authorization = _fixture.GetValidAuthHeader(_fixture.TestApp);
            var model = new PostEmailRequest
            {
                To = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Hello",
                Body    = "Hi bob"
            }.ToFormContent();

            // act
            var response = await _fixture.Client.PostAndGetJsonAsync <PostMessageResponse>("v1/Messages", model);

            // assert
            var decoded = response.Decode();

            Assert.Equal(_fixture.TestApp.Id, decoded.ApplicationId);
            Assert.NotEqual(Guid.Empty, decoded.RequestId);
        }
Esempio n. 7
0
        public async Task Post_Should_SerializeDataParams()
        {
            // arrange
            _fixture.Client.DefaultRequestHeaders.Authorization = _fixture.GetValidAuthHeader(_fixture.TestApp);
            var model = new PostEmailRequest
            {
                To = new List <string> {
                    "*****@*****.**"
                },
                Template = _fixture.WelcomeTemplate.Id,
                Data     = JsonConvert.SerializeObject(new { Name = "Bob" })
            }.ToFormContent();

            // act
            var response = await _fixture.Client.PostAndGetJsonAsync <PostMessageResponse>("v1/Messages", model);

            // assert
            var decoded = response.Decode();

            Assert.Contains(Stubs.InMemoryEmailQueueBlobStore.Blobs, b =>
                            b.Key.RequestId == decoded.RequestId &&
                            b.Value.Data?.ContainsKey("Name") == true);
        }
Esempio n. 8
0
        public async Task Post_Should_EnqueueMessage()
        {
            // arrange
            _fixture.Client.DefaultRequestHeaders.Authorization = _fixture.GetValidAuthHeader(_fixture.TestApp);
            var model = new PostEmailRequest
            {
                To = new List <string> {
                    "*****@*****.**"
                },
                Subject = "Hello",
                Body    = "Hi bob"
            }.ToFormContent();

            // act
            var response = await _fixture.Client.PostAndGetJsonAsync <PostMessageResponse>("v1/Messages", model);

            // assert
            var decoded = response.Decode();

            Assert.Contains(Stubs.InMemoryEmailQueue.Queue, m => m.Token.RequestId == decoded.RequestId);
            Assert.Contains(Stubs.InMemoryEmailQueueBlobStore.Blobs, b => b.Key.RequestId == decoded.RequestId);
            Assert.Contains(Stubs.InMemoryEmailQueueBlobStore.Blobs, b => b.Value.ApplicationId == _fixture.TestApp.Id);
        }