public async Task AddRoleToUserRolesWhenCommandContainsValidData() { const string roleName = "RoleName"; const string userId = "UserId"; List <Role> userRoles = new List <Role>(); var usersStub = new[] { new User() { Id = userId, Roles = userRoles } } .AsQueryable() .BuildMock(); var userRepositoryStub = new Mock <IEfRepository <User> >(); userRepositoryStub.Setup(r => r.All(It.IsAny <bool>())) .Returns(usersStub.Object); var rolesStub = new[] { new Role() { Name = roleName } } .AsQueryable() .BuildMock(); var roleRepositoryStub = new Mock <IEfRepository <Role> >(); roleRepositoryStub.Setup(r => r.All(It.IsAny <bool>())) .Returns(rolesStub.Object); var contextSaveChangesStub = new Mock <MusicZoneDbContext>( new[] { "Fake Connection string" }); PromoteUserToRole command = new PromoteUserToRole() { UserId = userId, RoleName = roleName }; // Arrange PromoteUserToRoleCommandService sut = new PromoteUserToRoleCommandService( userRepository: userRepositoryStub.Object, roleRepository: roleRepositoryStub.Object, contextSaveChanges: contextSaveChangesStub.Object); // Act await sut.ExecuteAsync(command); // Assert Assert.AreEqual(1, userRoles.Count); }
public void ThrowsNotFoundExceptionWhenUserDoesNotExists() { var usersStub = new User[0] .AsQueryable() .BuildMock(); var userRepositoryStub = new Mock <IEfRepository <User> >(); userRepositoryStub.Setup(r => r.All(It.IsAny <bool>())) .Returns(usersStub.Object); var roleRepositoryStub = new Mock <IEfRepository <Role> >(); var contextSaveChangesStub = new Mock <MusicZoneDbContext>( new[] { "Fake Connection string" }); PromoteUserToRole command = new PromoteUserToRole() { UserId = "InvalidUserId" }; // Arrange PromoteUserToRoleCommandService sut = new PromoteUserToRoleCommandService( userRepository: userRepositoryStub.Object, roleRepository: roleRepositoryStub.Object, contextSaveChanges: contextSaveChangesStub.Object); // Act && Assert Assert.ThrowsAsync <NotFoundException>(async() => await sut.ExecuteAsync(command)); }
public void ThrowsNotFoundExceptionWhenTheRoleDoesNotExists() { const string roleName = "RoleName"; const string userId = "UserId"; var usersStub = new[] { new User() { Id = userId } } .AsQueryable() .BuildMock(); var userRepositoryStub = new Mock <IEfRepository <User> >(); userRepositoryStub.Setup(r => r.All(It.IsAny <bool>())) .Returns(usersStub.Object); var rolesStub = new[] { new Role() { Name = roleName } } .AsQueryable() .BuildMock(); var roleRepositoryStub = new Mock <IEfRepository <Role> >(); roleRepositoryStub.Setup(r => r.All(It.IsAny <bool>())) .Returns(rolesStub.Object); var contextSaveChangesStub = new Mock <MusicZoneDbContext>( new[] { "Fake Connection string" }); PromoteUserToRole command = new PromoteUserToRole() { UserId = userId, RoleName = "AnotherRoleName" }; // Arrange PromoteUserToRoleCommandService sut = new PromoteUserToRoleCommandService( userRepository: userRepositoryStub.Object, roleRepository: roleRepositoryStub.Object, contextSaveChanges: contextSaveChangesStub.Object); // Act && Assert var ex = Assert.ThrowsAsync <NotFoundException>( async() => await sut.ExecuteAsync(command)); StringAssert.Contains("does not exists", ex.Message); }
public async Task <IActionResult> PromoteUser(UserRoleViewModel model) { PromoteUserToRole command = Mapper.Map <PromoteUserToRole>(model); string message = await this.CallServiceAsync( async() => await this.promoteUser.ExecuteAsync(command)); if (message != null) { return(RedirectToAction(nameof(this.Index)) .WithErrorMessage(message)); } return(RedirectToAction(nameof(this.Index)) .WithSuccessMessage($"User {model.Username} successfully promoted to {model.RoleName} role.")); }