//Create Access public bool CreateOperation(IdentityOperation identity) { var isSuccess = false; try { using (var conn = new SqlConnection(_connectionString)) { var parameters = new Dictionary <string, object> { { "@AccessId", identity.AccessId }, { "@ActionName", identity.ActionName }, { "@OperationName", identity.OperationName } }; var query = @"INSERT INTO aspnetoperations(OperationName,Enabled,AccessId,ActionName) values(@OperationName,1,@AccessId,@ActionName)"; MsSqlHelper.ExecuteNonQuery(conn, query, parameters); isSuccess = true; } } catch (Exception ex) { isSuccess = false; } return(isSuccess); }
//Update Access public bool UpdateOperation(IdentityOperation identity) { var isSuccess = false; try { using (var conn = new SqlConnection(_connectionString)) { var parameters = new Dictionary <string, object> { { "@Id", identity.Id }, { "@AccessId", identity.AccessId }, { "@ActionName", identity.ActionName }, { "@OperationName", identity.OperationName } }; var query = @"Update aspnetoperations SET AccessId = @AccessId, ActionName = @ActionName, OperationName = @OperationName WHERE 1=1 AND Id = @Id;"; MsSqlHelper.ExecuteNonQuery(conn, query, parameters); isSuccess = true; } } catch (Exception ex) { isSuccess = false; } return(isSuccess); }
public async Task CreateUserAsyncMethod_UserAlreadyExists_ShouldBeReturnedFailedIdentityOperation() { UserDTO user = new UserDTO { Email = "*****@*****.**", FName = "Josh", LName = "Collins", Password = "******", Role = "Worker", UserName = "******" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); IList <ApplicationUser> applicationUsers = new List <ApplicationUser>(1); var expected = new IdentityOperation(false, "", ""); mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); var actual = await service.CreateUserAsync(user); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public bool CheckOperationDuplicate(IdentityOperation identity) { var existed = false; try { using (var conn = new SqlConnection(_connectionString)) { var parameters = new Dictionary <string, object> { { "@Id", identity.Id }, { "@AccessId", identity.AccessId }, { "@ActionName", identity.ActionName } }; var query = @"SELECT 1 FROM aspnetoperations WHERE 1=1 AND AccessId = @AccessId AND ActionName = @ActionName AND Id != @Id"; var result = MsSqlHelper.ExecuteScalar(conn, CommandType.Text, query, parameters); if (Convert.ToBoolean(result)) { existed = true; } } } catch (Exception ex) { existed = false; } return(existed); }
public async Task CreateUserAsyncMethod_WithFirstUser_ShouldBeReturnedSuccessfulIdentityOperation() { UserDTO user = new UserDTO { Email = "*****@*****.**", FName = "Josh", LName = "Collins", Password = "******", Role = "Worker", UserName = "******" }; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); IList <ApplicationUser> applicationUsers = new List <ApplicationUser>(); applicationUsers.Insert(0, new ApplicationUser()); var expected = new IdentityOperation(true, "Sign up is success", ""); mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null); mock.Setup(x => x.Users.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())).Returns(Task.FromResult(new IdentityResult())); mock.Setup(x => x.Users.GetUsersInRoleAsync("Admin")).Returns(Task.FromResult(applicationUsers)); mock.Setup(x => x.People.Create(It.IsAny <Person>())); var actual = await service.CreateUserAsync(user); Assert.AreSame(expected.Message, actual.Message); Assert.AreSame(expected.Property, actual.Property); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public async Task UpdateUserRoleAsyncMethod_ManagerWithTeamToWorker_ShouldBeReturnedFailedIdentityOperation() { string userId = "1a", rolename = "Worker"; IList <string> roles = new List <string>(); roles.Add("Manager"); Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(false, "", ""); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles); mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync(new Person { TeamId = 1 }); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public ActionResult Update(FunctionViewModel model) { var result = false; if (ModelState.IsValid) { var strError = string.Empty; var identity = new IdentityOperation { Id = model.Id, AccessId = model.AccessId, ActionName = model.ActionName, OperationName = model.OperationName }; try { var isDuplicated = _identityStore.CheckOperationDuplicate(identity); if (isDuplicated) { this.AddNotification(string.Format("Could not update due to the function [{0} of {1}] is existed", model.ActionName, model.AccessName), NotificationType.ERROR); return(RedirectToAction("Index")); } result = _identityStore.UpdateOperation(identity); if (result) { this.AddNotification(string.Format("The function [{0} of {1}] is updated succesfully", model.ActionName, model.AccessName), NotificationType.SUCCESS); //Write log var activityText = "Updated the function [{0} of {1}]"; activityText = string.Format(activityText, model.ActionName, model.AccessName); WriteActivityLog(activityText, ActivityLogType.UpdateFunction, model.Id.ToString(), TargetObjectType.Function); return(RedirectToAction("Index")); } else { this.AddNotification("Could not update function due to database exception occurred", NotificationType.ERROR); } } catch (Exception ex) { strError = string.Format("Could not UpdateFunction because: {0}", ex.ToString()); logger.Error(strError); this.AddNotification(strError, NotificationType.ERROR); } } return(RedirectToAction("Index")); }
public async Task UpdateUserRoleAsyncMethod_UserIdNotExists_ShouldBeReturnedFailedIdentityOperation() { string userId = "1a", rolename = "Worker"; Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(false, "", ""); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public async Task UpdateUserRoleAsyncMethod_WithoutChanges_ShouldBeReturnedSuccessfulIdentityOperation() { string userId = "1a", rolename = "Worker"; IList <string> roles = new List <string>(); roles.Add("Worker"); Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(true, "", ""); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public async Task UpdateUserRoleAsyncMethod_FromAdmin_ShouldBeReturnedFailedIdentityOperation() { string userId = "1a", rolename = "Manager"; IList <string> roles = new List <string>(); roles.Add("Admin"); Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>(); IIdentityUnitOfWork uow = mock.Object; UserService service = new UserService(uow); var expected = new IdentityOperation(false, "Only the one administrator have to be in database", "userRole"); mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser()); mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles); var actual = await service.UpdateUserRoleAsync(userId, rolename); Assert.AreSame(expected.Message, actual.Message); Assert.AreSame(expected.Property, actual.Property); Assert.AreEqual(expected.Succeeded, actual.Succeeded); }
public bool UpdateOperation(IdentityOperation identity) { return(_identityRepository.UpdateOperation(identity)); }
public bool CheckOperationDuplicate(IdentityOperation identity) { return(_identityRepository.CheckOperationDuplicate(identity)); }