コード例 #1
0
        public async Task ShouldUpdateUserAccountCorrectly()
        {
            //Arrange
            User user            = new User("test", "test", "test", "test");
            var  usersRepository = new Mock <IUsersRepository>();
            var  dateUpdated     = new DateTime();

            usersRepository.Setup(x => x.Get(It.IsAny <Guid>()))
            .Returns(user);

            usersRepository.Setup(x => x.UpdateAsync(It.IsAny <User>()))
            .Callback <User>(x =>
            {
                user        = x;
                dateUpdated = user.ChangedAt;
            })
            .Returns(Task.CompletedTask);

            var updateUserHandler = new UpdateUserHandler(usersRepository.Object);
            var command           = new UpdateUserCommand(user.Id, "test2", "test2", "test2", "test2", "test2");

            //Act
            await updateUserHandler.HandleAsync(command);


            //Assert
            usersRepository.Verify(x => x.Get(It.IsAny <Guid>()), Times.Once);
            usersRepository.Verify(x => x.UpdateAsync(It.IsAny <User>()), Times.Once);
            Assert.That(user.ChangedAt, Is.EqualTo(dateUpdated));
            Assert.That(user.Username, Is.EqualTo(command.Username));
            Assert.That(user.Email, Is.EqualTo(command.Email));
            Assert.That(user.Forename, Is.EqualTo(command.Forename));
            Assert.That(user.Surname, Is.EqualTo(command.Surname));
            Assert.That(user.City, Is.EqualTo(command.City));
        }
コード例 #2
0
        public async Task HandleAsync_WithNonInternalAccess_ThrowsSecurityException(AuthorizationBuilder.UserType userType)
        {
            // Arrange
            IWeeeAuthorization            authorization = AuthorizationBuilder.CreateFromUserType(userType);
            UserManager <ApplicationUser> userManager   = A.Fake <UserManager <ApplicationUser> >();

            var handler = new UpdateUserHandler(authorization, userManager);

            var request = new UpdateUser(Guid.NewGuid().ToString(), "TestFirstName", "TestLastName");

            // Act
            Func <Task> action = async() => await handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync <SecurityException>(action);
        }
コード例 #3
0
        public async Task HandleAsync_WithNonInternalAccess_ThrowsSecurityException(AuthorizationBuilder.UserType userType)
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateFromUserType(userType);
            UserManager<ApplicationUser> userManager = A.Fake<UserManager<ApplicationUser>>();

            var handler = new UpdateUserHandler(authorization, userManager);

            var request = new UpdateUser(Guid.NewGuid().ToString(), "TestFirstName", "TestLastName");

            // Act
            Func<Task> action = async () => await handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync<SecurityException>(action);
        }
コード例 #4
0
        public async Task HandleAsync_WithUnknownUserId_ThrowsArgumentException()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            UserManager<ApplicationUser> userManager = A.Fake<UserManager<ApplicationUser>>();
            A.CallTo(() => userManager.FindByIdAsync("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1")).Returns((ApplicationUser)null);

            var handler = new UpdateUserHandler(authorization, userManager);
            var request = new UpdateUser("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1", "TestFirstName", "TestLastName");

            // Act
            Func<Task> action = async () => await handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync<ArgumentException>(action);
        }
コード例 #5
0
        public async Task HandleAsync_WithUnknownUserId_ThrowsArgumentException()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            UserManager <ApplicationUser> userManager = A.Fake <UserManager <ApplicationUser> >();

            A.CallTo(() => userManager.FindByIdAsync("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1")).Returns((ApplicationUser)null);

            var handler = new UpdateUserHandler(authorization, userManager);
            var request = new UpdateUser("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1", "TestFirstName", "TestLastName");

            // Act
            Func <Task> action = async() => await handler.HandleAsync(request);

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(action);
        }
コード例 #6
0
        public async Task HandleAsync_WithNonInternalAdminRole_ThrowsSecurityException()
        {
            // Arrange
            IWeeeAuthorization authorization = new AuthorizationBuilder()
                                               .AllowInternalAreaAccess()
                                               .DenyRole(Roles.InternalAdmin)
                                               .Build();

            UserManager <ApplicationUser> userManager = A.Fake <UserManager <ApplicationUser> >();

            var handler = new UpdateUserHandler(authorization, userManager);

            // Act
            Func <Task> action = async() => await handler.HandleAsync(A.Dummy <UpdateUser>());

            // Assert
            await Assert.ThrowsAsync <SecurityException>(action);
        }
コード例 #7
0
        public async Task HandleAsync_WithNonInternalAdminRole_ThrowsSecurityException()
        {
            // Arrange
            IWeeeAuthorization authorization = new AuthorizationBuilder()
                .AllowInternalAreaAccess()
                .DenyRole(Roles.InternalAdmin)
                .Build();

            UserManager<ApplicationUser> userManager = A.Fake<UserManager<ApplicationUser>>();

            var handler = new UpdateUserHandler(authorization, userManager);

            // Act
            Func<Task> action = async () => await handler.HandleAsync(A.Dummy<UpdateUser>());

            // Assert
            await Assert.ThrowsAsync<SecurityException>(action);
        }
コード例 #8
0
        public void UpdatesUserInRepository_RaisesIntegrationEvent()
        {
            var user = new Domain.User(Id, "A B", "*****@*****.**", false);

            var repository = new Mock <IUserRepository>(MockBehavior.Strict);

            repository.Setup(r => r.GetAsync(user.Id)).Returns(Task.FromResult(user)).Verifiable();
            repository.Setup(r => r.UpdateAsync(user)).Returns(Task.CompletedTask).Verifiable();
            repository.Setup(r => r.SaveChanges()).Returns(Task.CompletedTask).Verifiable();

            var busPublisher = new Mock <IBusPublisher>(MockBehavior.Strict);

            busPublisher.Setup(p => p.Publish <IUserUpdated>(It.Is <IUserUpdated>(e => ValidateEquality(e)))).Returns(Task.CompletedTask).Verifiable();

            var handler = new UpdateUserHandler(repository.Object, busPublisher.Object);
            var result  = handler.HandleAsync(Cmd, new Mock <ICorrelationContext>().Object).GetAwaiter().GetResult();

            Assert.IsTrue(result.Successful);
            repository.Verify();
            busPublisher.Verify();
            ValidateEquality(user);
        }
コード例 #9
0
        public async Task HandleAsync_HappyPath_UpdatedUserInformation()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            ApplicationUser user = new ApplicationUser();

            UserManager <ApplicationUser> userManager = A.Fake <UserManager <ApplicationUser> >();

            A.CallTo(() => userManager.FindByIdAsync("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1")).Returns(user);
            A.CallTo(() => userManager.UpdateAsync(user)).Returns(IdentityResult.Success);

            var handler = new UpdateUserHandler(authorization, userManager);

            var request = new UpdateUser("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1", "Test", "Test");

            // Act
            var result = await handler.HandleAsync(request);

            // Assert
            Assert.Equal(user.FirstName, "Test");
            Assert.Equal(user.Surname, "Test");
            A.CallTo(() => userManager.UpdateAsync(user)).MustHaveHappened();
        }
コード例 #10
0
        public async Task HandleAsync_HappyPath_UpdatedUserInformation()
        {
            // Arrange
            IWeeeAuthorization authorization = AuthorizationBuilder.CreateUserWithAllRights();

            ApplicationUser user = new ApplicationUser();

            UserManager<ApplicationUser> userManager = A.Fake<UserManager<ApplicationUser>>();
            A.CallTo(() => userManager.FindByIdAsync("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1")).Returns(user);
            A.CallTo(() => userManager.UpdateAsync(user)).Returns(IdentityResult.Success);

            var handler = new UpdateUserHandler(authorization, userManager);

            var request = new UpdateUser("722F0857-EE0A-4460-AA7B-0E8EF5B5C8A1", "Test", "Test");

            // Act
            var result = await handler.HandleAsync(request);

            // Assert
            Assert.Equal(user.FirstName, "Test");
            Assert.Equal(user.Surname, "Test");
            A.CallTo(() => userManager.UpdateAsync(user)).MustHaveHappened();
        }