Ejemplo n.º 1
0
        public async Task AddToRoleAsyncShould_ReturnsTrueWhen_UserIsNotInRole()
        {
            const string Role = "Manager";

            //Arrange
            var admin = UserCreator.Create();
            var user  = UserCreator.Create();

            await Db.Users.AddRangeAsync(admin, user);

            await Db.SaveChangesAsync();

            var userManagerMock = UserManagerMock.New;

            userManagerMock.Setup(u => u.FindByIdAsync(user.Id)).Returns(Task.FromResult(user));
            userManagerMock.Setup(u => u.IsInRoleAsync(user, Role)).Returns(Task.FromResult(false));

            var activityService = new Mock <IAdminActivityService>();

            //TODO: rewrite this test (now there is no need for userManager)
            var service = new AdminUserService(Db, activityService.Object);

            //Act
            var result = await service.AddToRoleAsync(admin.Id, user.Id, Role);

            //Assert
            result.Should().BeTrue();

            activityService.Verify(a =>
                                   a.WriteInfoAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), AdminActivityType.AddedToRole), Times.Once);
        }
Ejemplo n.º 2
0
        public async Task AddToRoleAsyncShould_ReturnsFalseWhen_UserIsAlreadyInRole()
        {
            //Arrange
            var admin = UserCreator.Create();
            var user  = UserCreator.Create();

            await Db.Users.AddRangeAsync(admin, user);

            await Db.SaveChangesAsync();

            var userManagerMock = UserManagerMock.New;

            userManagerMock.Setup(u => u.FindByIdAsync(user.Id)).Returns(Task.FromResult(user));
            userManagerMock.Setup(u => u.IsInRoleAsync(user, ManagerRole)).Returns(Task.FromResult(true));

            await userManagerMock.Object.AddToRoleAsync(user, ManagerRole);

            //TODO: rewrite this test (now there is no need for userManager)
            var service = new AdminUserService(Db, null);

            //Act
            var result = await service.AddToRoleAsync(admin.Id, user.Id, ManagerRole);

            //Assert
            result.Should().Be(false);
        }