Ejemplo n.º 1
0
        public void CreateUser_ShouldFailOnNullObject()
        {
            var handler = new UserHandler(new UserTestContext());

            ModelUser user   = null;
            var       result = handler.CreateUser(user, "123", "123");
        }
Ejemplo n.º 2
0
        public void Login_ShouldFailOnWrongPassword()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            ModelUser u1 = new ModelUser()
            {
                FirstName   = "Tester",
                LastName    = "Person 1",
                PhoneNumber = "123",
                Id          = 1,
                UserName    = "******"
            };
            var pass = "******";

            handler.CreateUser(u1, pass, pass);
            try
            {
                var result = handler.Login(u1.UserName, "123");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Wrong password entered", ex.Message);
            }
        }
Ejemplo n.º 3
0
        public ModelUser CreateUser(ModelUser user, string password, string repeatedPassword)
        {
            if (user == null)
            {
                throw new ArgumentException("Parameter is null");
            }
            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(repeatedPassword))
            {
                throw new ArgumentException("Parameter is null");
            }
            if (password != repeatedPassword)
            {
                throw new ArgumentException("Passwords doesn't match");
            }
            if (!CheckNonNullablePropertiesAreNotNull(user))
            {
                throw new ArgumentException("Non nullable properties are null");
            }
            if (UserNameExists(user.UserName))
            {
                throw new Exception("Username already exists");
            }
            EfUser u = user.ConvertObj <ModelUser, EfUser>();

            u.Password      = new Assets.Password();
            u.Password.Salt = Crypter.Blowfish.GenerateSalt();
            u.Password.Hash = HashPassword(password, u.Password.Salt);
            var s = db.UserSet.Add(u);

            db.SaveChanges();
            return(u.ConvertObj <EfUser, ModelUser>());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Not currently working correctly. UserHandler doesn't update an object currently when it's not connected to an actual dbcontext file.
        /// TestMethod should either be updated to implement a test database or UserHandler should be changed to take account for testing.
        /// Add [TestMethod] when finished
        /// </summary>
        public void UpdateUser_ShouldUpdateValues()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            ModelUser u1 = new ModelUser()
            {
                FirstName   = "Tester",
                LastName    = "Person 1",
                PhoneNumber = "123",
                Id          = 1
            };

            var pass = "******";

            try
            {
                var result = handler.CreateUser(u1, pass, pass);

                u1.LastName = "Person 2";

                var r2 = handler.UpdateUser(u1);

                if (!r2)
                {
                    Assert.Fail();
                }

                var newUser = handler.GetUser(u1.Id);
                Assert.AreEqual(u1.LastName, newUser.LastName);
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 5
0
 private bool CheckNonNullablePropertiesAreNotNull(ModelUser user)
 {
     if (string.IsNullOrEmpty(user.FirstName) || string.IsNullOrEmpty(user.LastName) || string.IsNullOrEmpty(user.PhoneNumber))
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 6
0
 public bool UpdateUser(ModelUser user)
 {
     try
     {
         return(UserHandler.UpdateUser(user));
     }
     catch (Exception e)
     {
         ErrorHandler.ReportError(e, CurrentUser.Id);
         return(false);
     }
 }
Ejemplo n.º 7
0
 public bool UpdateUser(ModelUser user)
 {
     if (user == null)
     {
         throw new Exception("Parameter is null");
     }
     if (!UserExists(user.Id))
     {
         throw new Exception("User doesn't exist");
     }
     db.MarkAsChanged(user.ConvertObj <ModelUser, EfUser>(), EntityState.Modified);
     db.SaveChanges();
     return(true);
 }
Ejemplo n.º 8
0
        public ModelUser GetUser(int id)
        {
            if (id == 0)
            {
                throw new Exception("ID is not set or 0");
            }
            if (!UserExists(id))
            {
                throw new Exception("User doesn't exist");
            }
            EfUser    user       = db.UserSet.Find(id);
            ModelUser returnUser = new ModelUser();

            returnUser = user.ConvertObj <EfUser, ModelUser>();
            return(returnUser);
        }
Ejemplo n.º 9
0
        public void DeleteUser_ShouldFailIfUserDoesNotExist()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            try
            {
                ModelUser u = new ModelUser();
                u.Id = 1;

                var result = handler.DeleteUser(u);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("User doesn't exist", ex.Message);
            }
        }
Ejemplo n.º 10
0
        public bool DeleteUser(ModelUser user)
        {
            if (user == null)
            {
                throw new Exception("Parameter is null");
            }
            if (!UserExists(user.Id))
            {
                throw new Exception("User doesn't exist");
            }
            EfUser u = user.ConvertObj <ModelUser, EfUser>();

            db.UserSet.Attach(u);
            db.MarkAsChanged(u, EntityState.Deleted);
            db.SaveChanges();
            return(true);
        }
Ejemplo n.º 11
0
 public ModelUser CreateUser(ModelUser user, string password, string repeatedPassword)
 {
     try
     {
         return(UserHandler.CreateUser(user, password, repeatedPassword));
     }
     catch (Exception e)
     {
         if (CurrentUser != null)
         {
             ErrorHandler.ReportError(e, CurrentUser.Id);
         }
         else
         {
             ErrorHandler.ReportError(e, 5);
         }
         return(null);
     }
 }
Ejemplo n.º 12
0
 public void SetCurrentUser(ModelUser user)
 {
     try
     {
         if (!UserExists(user.Id))
         {
             throw new Exception("No User found");
         }
         CurrentUser = user;
     }
     catch (Exception e)
     {
         if (!UserExists(user.Id))
         {
             user.Id = CurrentUser.Id;
         }
         ErrorHandler.ReportError(e, user.Id);
     }
 }
Ejemplo n.º 13
0
        public void CreateUser_ShouldFailOnNullProperties()
        {
            var handler = new UserHandler(new UserTestContext());

            ModelUser user = new ModelUser()
            {
                FirstName = null
            };

            try
            {
                var result = handler.CreateUser(user, "123", "123");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Non nullable properties are null", ex.Message);
            }
        }
Ejemplo n.º 14
0
        public void Login_ShouldReturnUser()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            ModelUser u1 = new ModelUser()
            {
                FirstName   = "Tester",
                LastName    = "Person 1",
                PhoneNumber = "123",
                Id          = 1,
                UserName    = "******"
            };
            var pass = "******";

            handler.CreateUser(u1, pass, pass);

            var result = handler.Login(u1.UserName, pass);

            Assert.IsNotNull(result);
        }
Ejemplo n.º 15
0
        public void UpdateUser_ShouldFailIfUserDoesNotExist()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            try
            {
                ModelUser u1 = new ModelUser()
                {
                    Id          = 1,
                    FirstName   = "Lol",
                    LastName    = "more lol",
                    PhoneNumber = "123"
                };
                var result = handler.UpdateUser(u1);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("User doesn't exist", ex.Message);
            }
        }
Ejemplo n.º 16
0
        public void Login_ShouldFailOnNullParameters()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            ModelUser u1 = new ModelUser()
            {
                FirstName   = "Tester",
                LastName    = "Person 1",
                PhoneNumber = "123",
                Id          = 1,
                UserName    = "******"
            };
            var pass = "******";

            handler.CreateUser(u1, pass, pass);

            try
            {
                handler.Login("", "123");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("All fields must be filled", ex.Message);
            }

            try
            {
                handler.Login("123", "");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("All fields must be filled", ex.Message);
            }
        }
Ejemplo n.º 17
0
        public void GetUser_ShouldThrowExceptionIfUserIdDoesNotExist()
        {
            UserHandler handler = new UserHandler(new UserTestContext());

            ModelUser u1 = new ModelUser()
            {
                Id          = 1,
                FirstName   = "lol",
                LastName    = "more lol",
                PhoneNumber = "123"
            };
            var pass = "******";

            try
            {
                var u2     = handler.CreateUser(u1, pass, pass);
                var result = handler.GetUser(2);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("User doesn't exist", ex.Message);
            }
        }
Ejemplo n.º 18
0
 public List <ModelError> GetCurrentErrorsForUser(ModelUser user)
 {
     return(ErrorHandler.GetCurrentErrorsForUser(user));
 }