コード例 #1
0
        public async Task <IActionResult> UpdateEmailAddress([FromRoute][Required] int id, [FromBody] UpdateEmailAddressDto dto)
        {
            Logger.Trace($"Executing '{nameof(UpdateEmailAddress)}'.");

            var userId = HttpContext.GetUserId();

            if (userId == null)
            {
                return(Unauthorized());
            }

            var request = new UpdateEmailAddressRequest(id, dto.Address);

            request.SetUserId(userId.Value);

            try
            {
                var result = await _mediator.Send(request);

                return(result.GetResult());
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Unexpected error thrown while executing '{nameof(UpdateEmailAddressHandler)}'.");
                throw;
            }
        }
コード例 #2
0
        public async Task Should_ReturnBadRequest_When_PassInvalidEmailAddress(string updatedEmailAddress, int emailAddressId, int userId)
        {
            var cancellationToken = new CancellationToken();

            var emailAddressValidator = Substitute.For <IEmailAddressValidatorUtility>();

            emailAddressValidator.ValidateMail("*****@*****.**").Returns(false);

            var databaseContext = Create.MockedDbContextFor <DatabaseContext>();

            databaseContext.SaveChanges();

            var request = new UpdateEmailAddressRequest(emailAddressId, updatedEmailAddress);

            request.SetUserId(userId);

            var result = await new UpdateEmailAddressHandler(databaseContext, emailAddressValidator).Handle(request, cancellationToken);

            Assert.Multiple(() =>
            {
                Assert.IsFalse(result.Success);
                Assert.AreEqual(result.StatusCode, HttpStatusCode.BadRequest);
                Assert.IsTrue(result.Message.Contains("Invalid email address"));
            });
        }
コード例 #3
0
        public async Task Should_ReturnNotFound_When_PassNotExistingEmailAddressForUser(string updatedEmailAddress, int emailAddressId, int userId)
        {
            var cancellationToken = new CancellationToken();

            var emailAddressValidator = Substitute.For <IEmailAddressValidatorUtility>();

            emailAddressValidator.ValidateMail("*****@*****.**").Returns(true);

            var databaseContext = Create.MockedDbContextFor <DatabaseContext>();

            databaseContext.Set <MailingGroup>().Add(new MailingGroup()
            {
                Id           = 1,
                Name         = "MailingGroupName1",
                SystemUserId = 1
            });
            databaseContext.Set <MailingGroup>().Add(new MailingGroup()
            {
                Id           = 2,
                Name         = "MailingGroupName2",
                SystemUserId = 2
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 1,
                Value          = "*****@*****.**",
                MailingGroupId = 2
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 2,
                Value          = "*****@*****.**",
                MailingGroupId = 2
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 3,
                Value          = "*****@*****.**",
                MailingGroupId = 2
            });

            databaseContext.SaveChanges();

            var request = new UpdateEmailAddressRequest(emailAddressId, updatedEmailAddress);

            request.SetUserId(userId);

            var result = await new UpdateEmailAddressHandler(databaseContext, emailAddressValidator).Handle(request, cancellationToken);

            Assert.Multiple(() =>
            {
                Assert.IsFalse(result.Success);
                Assert.AreEqual(result.StatusCode, HttpStatusCode.NotFound);
            });
        }