public void Invoke_ForActionDelete_ReturnsTrue() { // prepare var userAppFromDb = new DataAccess.Models.UserApps { User = new DataAccess.Models.User { Id = 1 }, App = new DataAccess.Models.App { Id = 1 }, Role = UserRole.Manager }; var userFromDb = new DataAccess.Models.User(); var findByResult = new List <DataAccess.Models.UserApps> { userAppFromDb }; var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedUserRepository = new Mock <IUserRepository>(); mockedUserRepository.Setup(r => r.GetById(It.IsAny <int>())).Returns(userFromDb); mockedUserAppRepository.Setup(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.UserApps, bool> > >())) .Returns(findByResult.AsQueryable); var action = new CheckUserPermissionToApp(mockedUserAppRepository.Object, mockedUserRepository.Object); // action var allow = action.Invoke(1, 1, ActionType.Delete); // assert Assert.True(allow); }
public void Invoke_EmptyDataReturnsEmptyList() { // prepare var userapp1 = new DataAccess.Models.UserApps(); var user = new DataAccess.Models.User(); var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.User> { user }; var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedUserRepository = new Mock <IUserRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedUserRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetAppUsers(mockedUserAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(999); // check Assert.Equal(0, list.Count); }
public void Invoke_ValidDataForSuperAdmin_ReturnsAllApps() { // prepare var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 2, Name = "app2" }, User = new DataAccess.Models.User { Id = 2, IsSuperAdmin = true } }; var app1 = new DataAccess.Models.App { Id = 1, Name = "app1" }; var app2 = new DataAccess.Models.App { Id = 2, Name = "app2" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.App> { app1, app2 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); mockedUserRepository.Setup(r => r.GetById(It.IsAny <int>())) .Returns(userapp1.User); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedAppRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetUserApps(mockedUserAppRepository.Object, mockedAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(2); // check Assert.Equal(2, list.Count); }
public void Invoke_ValidDataReturnsCorrectModel() { // prepare var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 2, Name = "app2" }, User = new DataAccess.Models.User { Id = 2 } }; var app1 = new DataAccess.Models.App { Id = 1, Name = "app1" }; var app2 = new DataAccess.Models.App { Id = 2, Name = "app2" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.App> { app1, app2 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedAppRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetUserApps(mockedUserAppRepository.Object, mockedAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(2); // check Assert.Equal(1, list.Count); Assert.Equal(2, list.FirstOrDefault(x => x.Id == 2).Id); Assert.Equal("app2", list.FirstOrDefault(x => x.Name == "app2").Name); }
public void Invoke_ValidData_DeleteAndSaveAreCalled() { // prepare var userToDelete = new UserModel() { Id = 1 }; var userFromDB = new DataAccess.Models.User() { Id = 1, }; var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 1, Name = "app1" }, User = new DataAccess.Models.User { Id = 1 } }; var applist = new List <DataAccess.Models.UserApps> { userapp1 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); mockedUserRepository.Setup(r => r.GetById(1)).Returns(userFromDB); mockedUserAppRepository.Setup(r => r.GetAll()).Returns(applist.AsQueryable); var action = new DeleteUser(mockedUserRepository.Object, mockedUserAppRepository.Object); // action var actionResult = action.Invoke(userToDelete.Id); // assert Assert.True(actionResult); mockedUserRepository.Verify(r => r.Delete(It.IsAny <DataAccess.Models.User>()), Times.Once()); mockedUserRepository.Verify(r => r.Save(), Times.Once()); mockedUserAppRepository.Verify(r => r.Delete(It.IsAny <DataAccess.Models.UserApps>()), Times.Once()); mockedUserAppRepository.Verify(r => r.Save(), Times.Once()); }
public void Invoke_InvalidDataReturnsEmptyList() { // prepare var userapp1 = new DataAccess.Models.UserApps { Id = 1, App = new DataAccess.Models.App { Id = 1 }, User = new DataAccess.Models.User { Id = 1 }, Role = UserRole.Manager }; var user = new DataAccess.Models.User { Id = 1, Name = "User1", Email = "*****@*****.**" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.User> { user }; var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedUserRepository = new Mock <IUserRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedUserRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetAppUsers(mockedUserAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(999); // check Assert.Equal(0, list.Count); }
public void Invoke_CorrectData_SavedInDatabase() { // Prepare var dataUserApp = new DataAccess.Models.UserApps { User = new DataAccess.Models.User { Id = 1 }, App = new DataAccess.Models.App { Id = 1 }, Role = UserRole.User }; var dataUser = new DataAccess.Models.User { IsSuperAdmin = false }; var dataUserAppSaved = dataUserApp; var getAllResults = new List <DataAccess.Models.UserApps> { dataUserApp }; var mockedUserAppsRepository = new Mock <IUserAppRepository>(); mockedUserAppsRepository.Setup(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.UserApps, bool> > >())) .Returns(getAllResults.AsQueryable()); mockedUserAppsRepository.Setup(r => r.Edit(It.IsAny <DataAccess.Models.UserApps>())) .Callback <DataAccess.Models.UserApps>(userApps => dataUserAppSaved = userApps); var mockedUserRepository = new Mock <IUserRepository>(); mockedUserRepository.Setup(r => r.GetById(It.IsAny <int>())) .Returns(dataUser); var action = new GrantRole(mockedUserAppsRepository.Object, mockedUserRepository.Object); // Action var actionResult = action.Invoke(1, 1, Enums.UserRole.Manager); // Check Assert.True(actionResult); Assert.NotEqual(UserRole.User, dataUserAppSaved.Role); Assert.Equal(UserRole.Manager, dataUserAppSaved.Role); mockedUserAppsRepository.Verify(r => r.Edit(It.IsAny <DataAccess.Models.UserApps>()), Times.Once); mockedUserAppsRepository.Verify(r => r.Save(), Times.Once); mockedUserRepository.Verify(r => r.Save(), Times.Never); }
public void Invoke_AlreadyExists_AddNorSaveAreCalled() { // prepare var userAppToAdd = new UserAppModel() { AppId = 1, UserId = 1, Role = Enums.UserRole.SuperAdmin }; var userAppFromDb = new DataAccess.Models.UserApps() { App = new DataAccess.Models.App() { Id = 1 }, User = new DataAccess.Models.User() { Id = 1 }, Role = DataAccess.Enums.UserRole.SuperAdmin }; var findByResult = new List <DataAccess.Models.UserApps>() { userAppFromDb }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); mockedUserAppRepository.Setup(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.UserApps, bool> > >())) .Returns(findByResult.AsQueryable); var action = new AddNewUserApp(mockedUserAppRepository.Object, mockedUserRepository.Object, mockedAppRepository.Object); // action var actionResult = action.Invoke(userAppToAdd); // assert Assert.Equal(0, actionResult); mockedUserAppRepository.Verify(r => r.Add(It.IsAny <DataAccess.Models.UserApps>()), Times.Never); mockedUserAppRepository.Verify(r => r.Save(), Times.Never); }
public void Invoke_InvalidDataReturnsEmptyList() { // prepare var userapp1 = new DataAccess.Models.UserApps { App = new DataAccess.Models.App { Id = 1, Name = "app1" }, User = new DataAccess.Models.User { Id = 2 } }; var app1 = new DataAccess.Models.App { Id = 1, Name = "app1" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1 }; var findByResultApp = new List <DataAccess.Models.App> { app1 }; var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedAppRepository = new Mock <IAppRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedAppRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetUserApps(mockedUserAppRepository.Object, mockedAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(999); // check Assert.Empty(list); }
public int Invoke(UserAppModel model) { var userApp = new DataAccess.Models.UserApps() { User = userRepository.GetById(model.UserId), App = appRepository.GetById(model.AppId), Role = (DataAccess.Enums.UserRole)Enum.Parse(typeof(DataAccess.Enums.UserRole), model.Role.ToString()), }; if (userApp.User == null || userApp.App == null || userAppRepository.FindBy(x => x.App.Id == userApp.App.Id && x.User.Id == userApp.User.Id).Any()) { return(0); } userAppRepository.Add(userApp); userAppRepository.Save(); return(userApp.Id); }
public void Invoke_LowerRole_NotSavedInDatabase(UserRole oldRole, Enums.UserRole newRole) { // Prepare var dataUserApp = new DataAccess.Models.UserApps { User = new DataAccess.Models.User { Id = 1 }, App = new DataAccess.Models.App { Id = 1 }, Role = oldRole }; var dataUser = new DataAccess.Models.User { IsSuperAdmin = false }; var getAllResults = new List <DataAccess.Models.UserApps> { dataUserApp }; var mockedUserAppsRepository = new Mock <IUserAppRepository>(); mockedUserAppsRepository.Setup(r => r.FindBy(It.IsAny <Expression <Func <DataAccess.Models.UserApps, bool> > >())) .Returns(getAllResults.AsQueryable()); var mockedUserRepository = new Mock <IUserRepository>(); mockedUserRepository.Setup(r => r.GetById(It.IsAny <int>())) .Returns(dataUser); var action = new GrantRole(mockedUserAppsRepository.Object, mockedUserRepository.Object); // Action var actionResult = action.Invoke(1, 1, newRole); // Check Assert.False(actionResult); mockedUserAppsRepository.Verify(r => r.Edit(It.IsAny <DataAccess.Models.UserApps>()), Times.Never); mockedUserAppsRepository.Verify(r => r.Save(), Times.Never); mockedUserRepository.Verify(r => r.Save(), Times.Never); }
public void Invoke_ValidDataReturnsCorrectModel() { // prepare var userapp1 = new DataAccess.Models.UserApps { Id = 1, App = new DataAccess.Models.App { Id = 1 }, User = new DataAccess.Models.User { Id = 1 }, Role = UserRole.Manager }; var userapp2 = new DataAccess.Models.UserApps { Id = 2, App = new DataAccess.Models.App { Id = 2 }, User = new DataAccess.Models.User { Id = 2 }, Role = UserRole.SuperAdmin }; var user = new DataAccess.Models.User { Id = 1, Name = "User1", Email = "*****@*****.**" }; var findByResult = new List <DataAccess.Models.UserApps> { userapp1, userapp2 }; var findByResultApp = new List <DataAccess.Models.User> { user }; var mockedUserAppRepository = new Mock <IUserAppRepository>(); var mockedUserRepository = new Mock <IUserRepository>(); mockedUserAppRepository.Setup(r => r.GetAll()) .Returns(findByResult.AsQueryable); mockedUserRepository.Setup(r => r.GetAll()) .Returns(findByResultApp.AsQueryable); var action = new GetAppUsers(mockedUserAppRepository.Object, mockedUserRepository.Object); // action var list = action.Invoke(1); // check Assert.Equal(1, list.FirstOrDefault(x => x.Id == 1).Id); Assert.Equal("User1", list.FirstOrDefault(x => x.Name == "User1").Name); Assert.Equal("*****@*****.**", list.FirstOrDefault(x => x.Email == "*****@*****.**").Email); //TODO Check UserRole (Comparing two roles doesn't work) }