Пример #1
0
        public void CreateUserInvalidFullNameTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.Create(It.IsAny <Models.EFModels.User>())).Verifiable();
            userRepositoryMock.Setup(x => x.GetByLogin(It.IsAny <string>())).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult result = service.CreateUser(new Models.ServiceModels.User
            {
                Login    = "******",
                FullName = "",
                Roles    = new List <Models.ServiceModels.Role>()
                {
                    new Models.ServiceModels.Role("Admin")
                }
            }, "password");

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.False);
            Assert.That(result.Errors, Is.Not.Empty);
            Assert.That(result.Errors.Contains(OperationErrors.FullNameErr), Is.True);
            Assert.That(result.Errors.Count, Is.EqualTo(1));
            userRepositoryMock.Verify(x => x.Create(It.IsAny <Models.EFModels.User>()), Times.Never);
            userRepositoryMock.Verify(x => x.GetByLogin(It.IsAny <string>()), Times.Once);
        }
Пример #2
0
        public void ChangeUserPasswordUserNotExistTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.ChangePassword(It.IsAny <Models.EFModels.User>())).Verifiable();
            userRepositoryMock.Setup(x => x.GetById(It.IsAny <int>())).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult result = service.ChangePassword(new Models.ServiceModels.User
            {
                Login    = "******",
                FullName = "abc",
                Roles    = new List <Models.ServiceModels.Role>()
                {
                    new Models.ServiceModels.Role("Admin"),
                    new Models.ServiceModels.Role("Intern")
                }
            }, "password");

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.False);
            Assert.That(result.Errors.Contains(OperationErrors.UserNotExistErr), Is.True);
            Assert.That(result.Errors.Count, Is.EqualTo(1));
            userRepositoryMock.Verify(x => x.ChangePassword(It.IsAny <Models.EFModels.User>()), Times.Never);
            userRepositoryMock.Verify(x => x.GetById(It.IsAny <int>()), Times.Once);
        }
Пример #3
0
        public void EditUserNotExistUserTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.Update(It.IsAny <Models.EFModels.User>())).Verifiable();
            userRepositoryMock.Setup(x => x.GetById(It.IsAny <int>())).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult result = service.UpdateUser(new Models.ServiceModels.User
            {
                Login    = "******",
                FullName = "ivan",
                Roles    = new List <Models.ServiceModels.Role>()
                {
                    new Models.ServiceModels.Role("Admin")
                }
            });

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.False);
            Assert.That(result.Errors, Is.Not.Empty);
            Assert.That(result.Errors.Contains(OperationErrors.UserNotExistErr), Is.True);
            Assert.That(result.Errors.Count, Is.EqualTo(1));
            userRepositoryMock.Verify(x => x.Update(It.IsAny <Models.EFModels.User>()), Times.Never);
            userRepositoryMock.Verify(x => x.GetById(It.IsAny <int>()), Times.Once);
        }
Пример #4
0
        public void GetAllRolesTest()
        {
            Mock <IRoleRepository> roleRepositoryMock = new Mock <IRoleRepository>();

            roleRepositoryMock.Setup(x => x.GetAll()).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(Mock.Of <IUserRepository>(), roleRepositoryMock.Object);
            service.GetAllRoles();
            roleRepositoryMock.Verify(x => x.GetAll(), Times.Once);
        }
Пример #5
0
        public static void Authenticate(string email, string password)
        {
            AuthenticationService.AuthenticationService authenticationService = null;

            if (HttpContext.Current != null && HttpContext.Current.Session["LoggedUser"] == null)
            {
                HttpContext.Current.Session["LoggedUser"] = new AuthenticationService.AuthenticationService();
            }

            authenticationService = (AuthenticationService.AuthenticationService)HttpContext.Current.Session["LoggedUser"];
            authenticationService.AuthenticateUser(email, password);
        }
Пример #6
0
        public void SearchUserTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.Search(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(new List <Models.EFModels.User>()
            {
                new Models.EFModels.User()
            }).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            service.SearchUser("", "", "");
            userRepositoryMock.Verify(x => x.Search(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once);
        }
Пример #7
0
        public void AddRoleTest()
        {
            Mock <IRoleRepository> roleRepositoryMock = new Mock <IRoleRepository>();

            roleRepositoryMock.Setup(x => x.Create(It.IsAny <Models.EFModels.Role>())).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(Mock.Of <IUserRepository>(), roleRepositoryMock.Object);
            OperationResult result = service.AddRole("role");

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.True);
            Assert.That(result.Errors, Is.Empty);
            roleRepositoryMock.Verify(x => x.Create(It.IsAny <Models.EFModels.Role>()), Times.Once);
        }
Пример #8
0
        public void AutherizationUserInvalidUserTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetByLogin(It.IsAny <string>())).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult result = service.AuthorizationUser(null, null);

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.False);
            Assert.That(result.Errors, Is.Not.Empty);
            Assert.That(result.Errors.Contains(OperationErrors.AutorizationFiledErr), Is.True);
            Assert.That(result.Errors.Count, Is.EqualTo(1));
            userRepositoryMock.Verify(x => x.GetByLogin(It.IsAny <string>()), Times.Once);
        }
Пример #9
0
        public void EditUserInvalidUser()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.Update(It.IsAny <Models.EFModels.User>())).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult result = service.UpdateUser(null);

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.False);
            Assert.That(result.Errors, Is.Not.Empty);
            Assert.That(result.Errors.Contains(OperationErrors.NullErr), Is.True);
            Assert.That(result.Errors.Count, Is.EqualTo(1));
            userRepositoryMock.Verify(x => x.Update(It.IsAny <Models.EFModels.User>()), Times.Never);
            userRepositoryMock.Verify(x => x.GetById(It.IsAny <int>()), Times.Never);
        }
Пример #10
0
        public void AutherizationUserTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.GetByLogin(It.IsAny <string>())).Returns(new Models.EFModels.User
            {
                Password = "******",
                Salt     = "aMZ1Eh+6K6Eh4sK87E0ZVEG8jA9rnFn88r4Pm5MYtnA="
            }).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult <Models.ServiceModels.User> result  = service.AuthorizationUser("login", "z");

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Result, Is.Not.Null);
            Assert.That(result.Success, Is.True);
            Assert.That(result.Errors, Is.Empty);
            userRepositoryMock.Verify(x => x.GetByLogin(It.IsAny <string>()), Times.Once);
        }
Пример #11
0
        public void DeleteUserTest()
        {
            Mock <IUserRepository> userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(x => x.Delete(It.IsAny <Models.EFModels.User>())).Verifiable();
            userRepositoryMock.Setup(x => x.GetById(It.IsAny <int>())).Returns(new Models.EFModels.User()).Verifiable();
            AuthenticationService.AuthenticationService service = new AuthenticationService.AuthenticationService(userRepositoryMock.Object, Mock.Of <IRoleRepository>());
            OperationResult result = service.DeleteUser(new Models.ServiceModels.User
            {
                Login    = "******",
                FullName = "abc",
                Roles    = new List <Models.ServiceModels.Role>()
                {
                    new Models.ServiceModels.Role("Admin")
                }
            });

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Success, Is.True);
            Assert.That(result.Errors.Count, Is.EqualTo(0));
            userRepositoryMock.Verify(x => x.Delete(It.IsAny <Models.EFModels.User>()), Times.Once);
            userRepositoryMock.Verify(x => x.GetById(It.IsAny <int>()), Times.Once);
        }