public void DenyFriend_WhenUnableToFindOtherFriendship_ShouldLogError()
        {
            var          friendshp = ModelTestHelper.CreateFriendship(1, "aFriend");
            var          friend    = ModelTestHelper.CreateUser(2, "aFriend");
            const string error     = "Unable to delete current friendship";
            var          errors    = new List <string> {
                error
            };

            _mockFriendshipRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockFriendshipRepo.SetupGet(s => s.Errors).Returns(new List <string>(0));
            _mockUserRepo.SetupGet(g => g.HasErrors).Returns(false);
            _mockUserRepo.SetupGet(g => g.Errors).Returns(new List <string>(0));
            _mockUserRepo.Setup(s => s.GetUserInfo(It.IsAny <string>())).Returns(() => friend);
            _mockFriendshipRepo.Setup(s => s.GetFriendship(2, "me")).Callback
            (
                () =>
            {
                _mockFriendshipRepo.SetupGet(s => s.HasErrors).Returns(true);
                _mockFriendshipRepo.SetupGet(s => s.Errors).Returns(errors);
            }
            );
            CreateFriendshipServices();
            _friendshipServices.DenyFriendship(friendshp, "me");
            Assert.IsTrue(_friendshipServices.HasErrors);
            Assert.IsTrue(_friendshipServices.Errors.Contains(error));
        }
Пример #2
0
        public void AcceptFriendship_WhenException_ShouldRollback()
        {
            var firstUser  = ModelTestHelper.CreateUser(-1, "aUser");
            var secondUser = ModelTestHelper.CreateUser(-1, "aFriend", "secondEmail");
            var userRepo   = new UserRepository();

            userRepo.SaveUser(firstUser);
            Assert.IsFalse(userRepo.HasErrors);
            userRepo.SaveUser(secondUser);
            Assert.IsFalse(userRepo.HasErrors);
            var friendship = ModelTestHelper.CreateFriendship(firstUser.Id, secondUser.Pseudo, DateTime.Now, true, true);
            var service    = new FriendshipServices();

            service.RequestFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            var mock = new Mock <IUserRepository>();

            mock.SetupGet(u => u.HasErrors).Throws(new Exception("Rollback"));
            var mockService = new FriendshipServices(mock.Object, new FriendshipRepository());

            mockService.AcceptFriendship(friendship, firstUser.Pseudo);
            Assert.IsTrue(mockService.HasErrors);
            var dbFriendship = service.GetFriendship(friendship.UserId, friendship.FriendName);

            Assert.IsTrue(dbFriendship.IsWaiting);
        }
Пример #3
0
        public void DeleteUser_WhenUnableToDeleteOtherFriendship_ShouldLogError()
        {
            var errors = new List <string> {
                "unable to delete other friendship"
            };
            var user = ModelTestHelper.CreateUser(1, "first");

            user.AddFriend(ModelTestHelper.CreateFriendship(user.Id, "aFriend"));

            _mockUserRepo.Setup(s => s.GetUserInfo("aFriend")).Returns(() => ModelTestHelper.CreateUser(2, "aFriend"));
            _mockUserRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockUserRepo.SetupGet(g => g.Errors).Returns(new List <string>());

            _mockFriendshipRepo.SetupGet(s => s.HasErrors).Returns(false);
            var otherFriendship = ModelTestHelper.CreateFriendship(2, user.Pseudo);

            _mockFriendshipRepo.Setup(s => s.GetFriendship(2, user.Pseudo)).Returns(() => otherFriendship);
            _mockFriendshipRepo.SetupGet(s => s.Errors).Returns(errors);
            _mockFriendshipRepo.Setup(s => s.DeleteFriendship(user.Friends.First())).Callback
            (
                () => _mockFriendshipRepo.Setup(s => s.HasErrors).Returns(false)
            );

            _mockFriendshipRepo.Setup(s => s.DeleteFriendship(otherFriendship)).Callback
            (
                () => _mockFriendshipRepo.Setup(s => s.HasErrors).Returns(true)
            );
            CreateUserServices();
            _userServices.DeleteUser(user);
            Assert.IsTrue(_userServices.HasErrors);
            Assert.IsTrue(_userServices.Errors.Contains("unable to delete other friendship"));
        }
Пример #4
0
        public void LoginByMail_WhenValid_ShouldNotLogError()
        {
            var user = ModelTestHelper.CreateUser(1, "toto");

            _mockUserRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockUserRepo.Setup(s => s.GetUserByMail(user.Mail, user.Password)).Returns(user);
            _mockUserRepo.SetupGet(s => s.Errors).Returns(new List <string>());
            _mockFriendshipRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockFriendshipRepo.Setup(s => s.GetUserFriendships(user.Id))
            .Returns(new List <Friendship> {
                ModelTestHelper.CreateFriendship(user.Id, "aFriend")
            });
            _mockTripRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockTripRepo.Setup(s => s.GetUserTrips(user.Id))
            .Returns(new List <UserTrip> {
                ModelTestHelper.CreateUserTrip(user.Id, "aTrip")
            });
            CreateUserServices();
            var dbUser = _userServices.LoginByMail(user.Mail, user.Password);

            Assert.IsFalse(_userServices.HasErrors);
            Assert.IsNotNull(dbUser);
            Assert.AreEqual(1, dbUser.Friends.Count());
            Assert.AreEqual(1, dbUser.Trips.Count());
        }
Пример #5
0
        public void DenyFriendship_WhenError_ShouldRollback()
        {
            var firstUser  = ModelTestHelper.CreateUser(-1, "aUser");
            var secondUser = ModelTestHelper.CreateUser(-1, "aFriend", "secondEmail");
            var userRepo   = new UserRepository();

            userRepo.SaveUser(firstUser);
            Assert.IsFalse(userRepo.HasErrors);
            userRepo.SaveUser(secondUser);
            Assert.IsFalse(userRepo.HasErrors);
            var friendship = ModelTestHelper.CreateFriendship(firstUser.Id, secondUser.Pseudo, DateTime.Now, true, true);
            var service    = new FriendshipServices();

            service.RequestFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            var mock = new Mock <IUserRepository>();

            mock.SetupGet(u => u.HasErrors).Returns(false);
            mock.Setup(s => s.GetUserInfo(It.IsAny <string>())).Returns(() => null);
            var mockService = new FriendshipServices(mock.Object, new FriendshipRepository());

            mockService.DenyFriendship(friendship, firstUser.Pseudo);
            Assert.IsTrue(mockService.HasErrors);
            var dbFriendship = service.GetFriendship(friendship.UserId, friendship.FriendName);

            Assert.IsNotNull(dbFriendship);
        }
Пример #6
0
        public void ToString_ShouldUseAgeAndPseudo()
        {
            var user     = ModelTestHelper.CreateUser(1, "ToStringPseudo");
            var expected = string.Format("{0}, {1}", user.Pseudo, user.Age);

            Assert.AreEqual(expected, user.ToString());
        }
Пример #7
0
        public void AcceptFriendship_WhenValid_ShouldCommit()
        {
            var firstUser  = ModelTestHelper.CreateUser(-1, "aUser");
            var secondUser = ModelTestHelper.CreateUser(-1, "aFriend", "secondEmail");
            var userRepo   = new UserRepository();

            userRepo.SaveUser(firstUser);
            Assert.IsFalse(userRepo.HasErrors);
            userRepo.SaveUser(secondUser);
            Assert.IsFalse(userRepo.HasErrors);
            var friendship = ModelTestHelper.CreateFriendship(firstUser.Id, secondUser.Pseudo, DateTime.Now, true, true);
            var service    = new FriendshipServices();

            service.RequestFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            service.AcceptFriendship(friendship, firstUser.Pseudo);
            Assert.IsFalse(service.HasErrors);
            var dbFriendship = service.GetFriendship(friendship.UserId, friendship.FriendName);

            Assert.IsNotNull(dbFriendship);
            Assert.IsFalse(dbFriendship.IsWaiting);
            var otherFriendship = service.GetFriendship(secondUser.Id, firstUser.Pseudo);

            Assert.IsNotNull(otherFriendship);
            Assert.IsFalse(dbFriendship.IsWaiting);
        }
        public void RequestFriendship_WhenUnableToSaveOtherFriendship_ShouldLogError()
        {
            var friendshp       = ModelTestHelper.CreateFriendship(1, "aFriend");
            var friend          = ModelTestHelper.CreateUser(2, "aFriend");
            var otherFriendship = ModelTestHelper.CreateFriendship(2, "me");

            _mockFriendshipRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockFriendshipRepo.SetupGet(s => s.Errors).Returns(new List <string>(0));
            _mockUserRepo.SetupGet(g => g.HasErrors).Returns(false);
            _mockUserRepo.SetupGet(g => g.Errors).Returns(new List <string>(0));
            _mockUserRepo.SetupGet(g => g.Errors).Returns(new List <string>(0));
            _mockUserRepo.Setup(s => s.GetUserInfo(It.IsAny <string>())).Returns(() => friend);
            _mockFriendshipRepo.Setup(s => s.GetFriendship(2, "me")).Returns(() => otherFriendship);
            _mockFriendshipRepo.Setup(s => s.SaveFriendship(otherFriendship)).Callback
            (
                () =>
            {
                _mockFriendshipRepo.SetupGet(s => s.HasErrors).Returns(true);
                _mockFriendshipRepo.SetupGet(s => s.Errors).Returns(new List <string> {
                    "Unable to save other friendship"
                });
            }
            );
            CreateFriendshipServices();
            _friendshipServices.RequestFriendship(friendshp, "me");
            Assert.IsTrue(_friendshipServices.HasErrors);
            Assert.IsTrue(_friendshipServices.Errors.Contains("Unable to save other friendship"));
        }
        public void CreateUser_WhenValid_ShouldCommit()
        {
            var user    = ModelTestHelper.CreateUser(-1, "CommitUser");
            var service = new UserServices();

            service.CreateUser(user);
            Assert.IsNotNull(service.GetUserInfo(user.Pseudo));
        }
Пример #10
0
        public void Equals_ShouldReturnCorrectValue(int firstId, int secondId,
                                                    bool expected)
        {
            var firstUser  = ModelTestHelper.CreateUser(firstId, "FirstUser");
            var secondUser = ModelTestHelper.CreateUser(secondId, "SecondUser");

            Assert.AreEqual(expected, firstUser.Equals(secondUser));
        }
Пример #11
0
        public void CopyConstructor_ShouldCopyValues()
        {
            var user = ModelTestHelper.CreateUser(1, "CopyPseudo");
            var copy = new User(user);

            Assert.IsFalse(ReferenceEquals(user, copy));
            Assert.IsTrue(ReferenceEquals(user.Friends, copy.Friends));
        }
Пример #12
0
        public void CreateUser_WhenValid_ShouldNotLogError()
        {
            var user = ModelTestHelper.CreateUser(1, "first");

            _mockUserRepo.SetupGet(s => s.HasErrors).Returns(false);
            CreateUserServices();
            _userServices.CreateUser(user);
            Assert.IsFalse(_userServices.HasErrors);
        }
Пример #13
0
        public void UpdateUser_WhenException_ShouldLogError()
        {
            var user = ModelTestHelper.CreateUser(1, "first");

            _mockUserRepo.SetupGet(s => s.HasErrors).Throws(new Exception("Exception For Test"));
            CreateUserServices();
            _userServices.UpdateUser(user);
            Assert.IsTrue(_userServices.HasErrors);
            Assert.IsTrue(_userServices.Errors.Contains("Exception For Test"));
        }
Пример #14
0
        public void Update_WhenDbUpdateSuccees_ShouldNotSetError()
        {
            var user = ModelTestHelper.CreateUser(2, "UpdateUser");
            var mock = CreateMock();

            mock.Setup(s => s.Update(user)).Returns(true);
            var repository = CreateRepository(mock.Object);

            repository.UpdateUser(user);
            Assert.IsFalse(repository.HasErrors);
        }
Пример #15
0
        public void LoginByPseudo_WhenException_ShouldLogError()
        {
            var user = ModelTestHelper.CreateUser(1, "aPseudo");

            _mockUserRepo.Setup(s => s.GetUserByPseudo(user.Pseudo, user.Password)).Returns(user);
            _mockUserRepo.SetupGet(s => s.HasErrors).Throws(new Exception("Exception For Test"));
            CreateUserServices();
            _userServices.LoginByPseudo(user.Pseudo, user.Password);
            Assert.IsTrue(_userServices.HasErrors);
            Assert.IsTrue(_userServices.Errors.Contains("Exception For Test"));
        }
Пример #16
0
        public void Save_WhenPseudoAlreadyExist_ShouldLogError()
        {
            var mock = CreateMock();

            mock.Setup(s => s.IsPseudoUsed(It.IsAny <string>())).Returns(true);
            var repository = CreateRepository(mock.Object);
            var user       = ModelTestHelper.CreateUser(1, "RepositoryUser");

            repository.SaveUser(user);
            CheckErrors(repository, "Pseudo is already used, please choose another one");
        }
Пример #17
0
        public void Delete_WhenExceptionIsThrown_ShouldLogError()
        {
            var user = ModelTestHelper.CreateUser(3, "DeleteUser");
            var mock = CreateMock();

            mock.Setup(s => s.Delete(user)).Throws(new ImportExportException("ExceptionForTest"));
            var repository = CreateRepository(mock.Object);

            repository.DeleteUser(user);
            CheckErrors(repository, "ExceptionForTest");
        }
Пример #18
0
        public void Save_WhenExceptionIsThrown_ShouldLogError()
        {
            var mock = CreateMock();

            mock.Setup(s => s.IsPseudoUsed(It.IsAny <string>())).Throws(new Exception("ExceptionForTest"));
            var repository = CreateRepository(mock.Object);
            var user       = ModelTestHelper.CreateUser(1, "RepositoryUser");

            repository.SaveUser(user);
            CheckErrors(repository, "Unexpected error occured ExceptionForTest");
        }
Пример #19
0
        public void Delete_WhenDbDeleteSuccessful_ShouldNotLogError()
        {
            var user = ModelTestHelper.CreateUser(3, "DeleteUser");
            var mock = CreateMock();

            mock.Setup(s => s.Delete(user)).Returns(true);
            var repository = CreateRepository(mock.Object);

            repository.DeleteUser(user);
            Assert.IsFalse(repository.HasErrors);
        }
Пример #20
0
        public void Update_WhenDbUpdateNotSucceed_ShouldLogError()
        {
            var user = ModelTestHelper.CreateUser(2, "UpdateUser");
            var mock = CreateMock();

            mock.Setup(s => s.Update(user)).Returns(false);
            var repository = CreateRepository(mock.Object);

            repository.UpdateUser(user);
            CheckErrors(repository, "Internal Error occured : user cannot be updated");
        }
Пример #21
0
        public void Delete_WhenErrorInDb_ShoudLogError()
        {
            var user = ModelTestHelper.CreateUser(3, "DeleteUser");
            var mock = CreateMock();

            mock.Setup(s => s.Delete(user)).Returns(false);
            var repository = CreateRepository(mock.Object);

            repository.DeleteUser(user);
            CheckErrors(repository, "Internal occured when deleting user : account has not been deleted");
        }
Пример #22
0
        public void Save_WhenCannotSaveInDb_ShouldLogError()
        {
            var mock = CreateMock();

            mock.Setup(s => s.IsPseudoUsed(It.IsAny <string>())).Returns(false);
            mock.Setup(s => s.IsMailUsed(It.IsAny <string>())).Returns(false);
            mock.Setup(s => s.Save(It.IsAny <User>())).Returns(false);
            var repository = CreateRepository(mock.Object);
            var user       = ModelTestHelper.CreateUser(1, "RepositoryUser");

            repository.SaveUser(user);
            CheckErrors(repository, "Internal error occured when saving user : account has not been created");
        }
Пример #23
0
        public void GetUser_ShouldReturnUser()
        {
            var       mock = CreateMock();
            const int id   = 1;

            mock.Setup(s => s.GetEntity(id)).Returns(ModelTestHelper.CreateUser(id, "GetUserTest"));
            var repo = CreateRepository(mock.Object);
            var user = repo.GetUser(id);

            Assert.IsFalse(repo.HasErrors);
            Assert.IsNotNull(user);
            Assert.AreEqual("GetUserTest", user.Pseudo);
        }
Пример #24
0
        public void GetUserInfo_WhenException_ShouldLogError()
        {
            var user = ModelTestHelper.CreateUser(1, "aUserPseudo");

            _mockUserRepo.SetupGet(s => s.HasErrors).Throws(new Exception("Exception for test"));
            _mockUserRepo.Setup(s => s.GetUserInfo(user.Pseudo)).Returns(user);
            CreateUserServices();
            var other = _userServices.GetUserInfo(user.Pseudo);

            Assert.IsTrue(_userServices.HasErrors);
            Assert.IsTrue(_userServices.Errors.Contains("Exception for test"));
            Assert.IsNull(other);
        }
        public void CreateUser_WhenException_ShouldRollback()
        {
            var user     = ModelTestHelper.CreateUser(-1, "Rollbackuser");
            var mock     = new Mock <IUserRepository>();
            var userRepo = new UserRepository();

            mock.SetupGet(s => s.HasErrors).Throws(new Exception("Rollback exception"));
            mock.Setup(s => s.SaveUser(user)).Callback(() => userRepo.SaveUser(user));
            var service = new UserServices(mock.Object, new UserTripRepository(), new FriendshipRepository());

            service.CreateUser(user);
            Assert.IsNull(service.GetUserInfo(user.Pseudo));
        }
Пример #26
0
        public void Save_WhenValid_ShouldNotSetErrors()
        {
            var user = ModelTestHelper.CreateUser(1, "RepositoryUser");
            var mock = CreateMock();

            mock.Setup(s => s.IsPseudoUsed(It.IsAny <string>())).Returns(false);
            mock.Setup(s => s.IsMailUsed(It.IsAny <string>())).Returns(false);
            mock.Setup(s => s.Save(user)).Returns(true);
            var repository = CreateRepository(mock.Object);

            repository.SaveUser(user);
            Assert.IsFalse(repository.HasErrors);
        }
Пример #27
0
        public void GetUserInfo_WhenValid_ShouldReturnValidUser()
        {
            var user = ModelTestHelper.CreateUser(1, "aUserPseudo");

            _mockUserRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockUserRepo.Setup(s => s.GetUserInfo(user.Pseudo)).Returns(user);
            _mockUserRepo.SetupGet(s => s.Errors).Returns(new List <string>());
            CreateUserServices();
            var other = _userServices.GetUserInfo(user.Pseudo);

            Assert.IsFalse(_userServices.HasErrors);
            Assert.IsNotNull(other);
        }
Пример #28
0
        public void Clone_ShoulDuplicateValues()
        {
            var user = ModelTestHelper.CreateUser(1, "Clone", mail: "CloneEmail", role: Role.Common,
                                                  phoneNumber: "ClonePhoneNumber");

            user.CenterOfInterests.Add("ACenterOfInterest");
            user.CenterOfInterests.Add("Tennis");
            user.AddFriend(ModelTestHelper.CreateFriendship(1, "AFriend"));
            user.AddFriend(ModelTestHelper.CreateFriendship(1, "TOTO"));
            user.AddTrip(ModelTestHelper.CreateUserTrip(1, "ATrip"));
            user.AddTrip(ModelTestHelper.CreateUserTrip(1, "SecondTrip"));
            TestClone(user);
        }
Пример #29
0
        public void SetCenterOfInterest_ShouldWork()
        {
            var user = ModelTestHelper.CreateUser(1, "CenterPseudo");

            Assert.IsNotNull(user.CenterOfInterests);
            Assert.AreEqual(0, user.CenterOfInterests.Count);
            var list = new List <string> {
                "Foot", "Tennis"
            };

            user.CenterOfInterests = list;
            Assert.AreEqual(2, user.CenterOfInterests.Count);
        }
Пример #30
0
        public void LoginByPseudo_WhenValid_ShouldNotLogError()
        {
            var user = ModelTestHelper.CreateUser(1, "aPseudo");

            _mockUserRepo.SetupGet(s => s.HasErrors).Returns(false);
            _mockUserRepo.Setup(s => s.GetUserByPseudo(user.Pseudo, user.Password)).Returns(user);
            _mockUserRepo.SetupGet(s => s.Errors).Returns(new List <string>());
            CreateUserServices();
            var dbUser = _userServices.LoginByPseudo(user.Pseudo, user.Password);

            Assert.IsFalse(_userServices.HasErrors);
            Assert.IsNotNull(dbUser);
        }