public SingleResponse<User> AddConnection(string connectionId, User user)
        {
            try
            {
                var existingUser = GetRavenUserByUsername(user);

                if (existingUser.ConnectionIds == null)
                {
                    existingUser.ConnectionIds = new List<string>();
                }

                if (existingUser.ConnectionIds.Contains(connectionId))
                {
                    return new SingleResponse<User>
                        {
                            Data = user,
                            Status = new Status
                                {
                                    ErrorLevel = ErrorLevel.Error,
                                    ErrorMessage = "Connection ID already exists"
                                }
                        };
                }

                existingUser.ConnectionIds.Add(connectionId);
                existingUser.LastConnectedDate = DateTime.Now;
                _database.Store(existingUser);
                return new SingleResponse<User>
                {
                    Data = Mapper.Map<User>(existingUser),
                    Status = new Status
                    {
                        ErrorLevel = ErrorLevel.None
                    }
                };
            }
            catch (Exception ex)
            {
                return new SingleResponse<User>
                {
                    Data = user,
                    Status = new Status
                    {
                        ErrorLevel = ErrorLevel.Critical,
                        ErrorMessage = string.Format("Fatal error adding connection: {0}", "ARG0"),
                        ErrorException = ex
                    }
                };
            }
        }
        public void ChangePassword()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
            {
                Username = "******",
                Password = "******",
                Email = "*****@*****.**"
            };
            var response = authentication.ChangePassword("testuser", "Abc123", "Abc12345");

            //Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
            Assert.IsTrue(response.Data, "Password was not changed successfully");

            var validateUserResponse = authentication.ValidateUser(user);
            Assert.AreEqual(ErrorLevel.None, validateUserResponse.Status.ErrorLevel, validateUserResponse.Status.ErrorMessage);
        }
        public void AddConnection()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
                {
                    Username = "******"
                };
            var connectionId = Guid.NewGuid().ToString();
            var response = authentication.AddConnection(connectionId, user);

            //Assert
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
            Assert.IsNotNull(response.Data);
            var getUserResponse = authentication.GetUserByUserId(new User
            {
                Id = "RavenUser/1"
            });
            Assert.AreEqual(ErrorLevel.None, getUserResponse.Status.ErrorLevel, getUserResponse.Status.ErrorMessage);
            Assert.IsNotNull(getUserResponse.Data);
            Assert.IsTrue(getUserResponse.Data.ConnectionIds.Contains(connectionId), "Connection ID failed to add");
        }
        public void Register_UsernameRequired()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
            {
                Password = "******",
                Email = "*****@*****.**"
            };
            var status = authentication.Register(user);

            //Assert
            Assert.IsNotNull(status);
            Assert.AreEqual(ErrorLevel.Error, status.ErrorLevel, "Username should be required");
            Assert.IsTrue(status.ErrorMessage.Contains("Username is required"), "Error message was: " + status.ErrorMessage);
        }
 private bool PasswordValid(User user)
 {
     string passwordRegex = _configuration.PasswordRegex;
     return Regex.IsMatch(user.Password, passwordRegex);
 }
        public void GetUserFeeds_UserIdRequired()
        {
            //Arrange
            IFeedManager feedManager = new FeedManager(_database);

            //Act
            var user = new User();
            var response = feedManager.GetUserFeeds(user);

            //Assert
            Assert.AreEqual(ErrorLevel.Error, response.Status.ErrorLevel);
            Assert.AreEqual("User Id is required", response.Status.ErrorMessage);
        }
        //TODO switch to response object
        public Status Register(User user)
        {
            try
            {
                if (user == null)
                {
                    return new Status
                        {
                            ErrorLevel = ErrorLevel.Error,
                            ErrorMessage = "User cannot be null"
                        };
                }
                if (string.IsNullOrEmpty(user.Username))
                {
                    return new Status
                        {
                            ErrorLevel = ErrorLevel.Error,
                            ErrorMessage = "Username is required"
                        };
                }
                if (string.IsNullOrEmpty(user.Password))
                {
                    return new Status
                        {
                            ErrorLevel = ErrorLevel.Error,
                            ErrorMessage = "Password is required"
                        };
                }
                if (string.IsNullOrEmpty(user.Email))
                {
                    return new Status
                        {
                            ErrorLevel = ErrorLevel.Error,
                            ErrorMessage = "Email is required"
                        };
                }
                var storageUser = new RavenUser
                    {
                        Username = user.Username,
                        PasswordHash = _cryptography.CreateHash(user.Password),
                        Email = user.Email
                    };

                Status status = ValidateNewUser(user);
                if (status.ErrorLevel > ErrorLevel.None)
                {
                    return status;
                }

                _database.Store(storageUser);
                return new Status
                    {
                        ErrorLevel = ErrorLevel.None
                    };
            }
            catch (Exception ex)
            {
                return new Status
                    {
                        ErrorLevel = ErrorLevel.Critical,
                        ErrorMessage = string.Format("Fatal error registering user: {0}", ex),
                        ErrorException = ex
                    };
            }
        }
Esempio n. 8
0
        public override Task OnReconnected()
        {
            var user = new User
                {
                    Username = Context.User.Identity.Name
                };
            var updateConnectionResponse = _authentication.UpdateConnection(Context.ConnectionId, user);
            //TODO log connection errors

            return base.OnReconnected();
        }
        public void GetUserFeeds()
        {
            //Arrange
            IFeedManager feedManager = new FeedManager(_database);

            //Act
            var user = new User
                {
                    Id = "ravenuser/1"
                };
            var response = feedManager.GetUserFeeds(user);

            //Assert
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
            Assert.IsNotNull(response.Data);
            Assert.IsNotEmpty(response.Data);
            var firstFeed = response.Data.First();
            Assert.IsNotNull(firstFeed);
            Assert.IsNotNull(firstFeed.Name);
        }
        public SingleResponse<User> UpdateConnection(string connectionId, User user)
        {
            try
            {
                var existingUser = GetRavenUserByUsername(user);

                if (existingUser.ConnectionIds == null)
                {
                    existingUser.ConnectionIds = new List<string>();
                }

                if (!existingUser.ConnectionIds.Contains(connectionId))
                {
                    existingUser.ConnectionIds.Add(connectionId);
                }

                existingUser.LastConnectedDate = DateTime.Now;
                _database.Store(existingUser);
                return ResponseBuilder.BuildSingleResponse(Mapper.Map<User>(existingUser), ErrorLevel.None);
            }
            catch (Exception ex)
            {
                return ResponseBuilder.BuildSingleResponse(user, ErrorLevel.Critical,
                    string.Format("Fatal error updating connection: {0}", "ARG0"), ex);
            }
        }
Esempio n. 11
0
        public override Task OnConnected()
        {
            var user = new User
                {
                    Username = Context.User.Identity.Name
                };

            var addConnectionResponse = _authentication.AddConnection(Context.ConnectionId, user);
            //TODO log connection errors
            Groups.Add(Context.ConnectionId, Context.User.Identity.Name);

            return base.OnConnected();
        }
 private bool PasswordLengthValid(User user)
 {
     return user.Password.Length >= 6;
 }
        public void ValidateUser()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
            {
                Username = "******",
                Password = "******",
                Email = "*****@*****.**"
            };
            var response = authentication.ValidateUser(user);

            //Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
        }
 private RavenUser GetRavenUserByUsername(User user)
 {
     var storageUser = _database.Query<RavenUser>()
                                .FirstOrDefault(u => u.Username == user.Username);
     return storageUser;
 }
 private bool EmailValid(User user)
 {
     return Regex.IsMatch(user.Email, @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b");
 }
 private bool EmailExists(User user)
 {
     return _database.Query<RavenUser>()
                     .Any(u => u.Email == user.Email);
 }
 public SingleResponse<bool> ValidateUser(User user)
 {
     try
     {
         var storageUser = GetRavenUserByUsername(user);
         var valid = storageUser != null && _cryptography.MatchHash(storageUser.PasswordHash, user.Password);
         return new SingleResponse<bool>
             {
                 Data = valid,
                 Status = new Status
                     {
                         ErrorLevel = valid ? ErrorLevel.None : ErrorLevel.Error,
                         ErrorMessage = valid ? "" : "Username or password is not valid"
                     }
             };
     }
     catch (Exception ex)
     {
         return new SingleResponse<bool>
             {
                 Data = false,
                 Status = new Status
                     {
                         ErrorLevel = ErrorLevel.Critical,
                         ErrorMessage = "Fatal error logging in: " + ex,
                         ErrorException = ex
                     }
             };
     }
 }
        public SingleResponse<User> UpdateUser(User user)
        {
            try
            {
                //var ravenUser = GetRavenUser(user.Id);
                var ravenUser = Mapper.Map<RavenUser>(user);

                _database.Store(ravenUser);

                return ResponseBuilder.BuildSingleResponse(user, ErrorLevel.None);
            }
            catch (Exception ex)
            {
                return ResponseBuilder.BuildSingleResponse(user, ErrorLevel.Critical,
                    string.Format("Fatal error updating user {0}: {1}", user.Username, ex), ex);
            }
        }
        public void ValidateUser_InvalidPassword()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
            {
                Username = "******",
                Password = "******",
                Email = "*****@*****.**"
            };
            var response = authentication.ValidateUser(user);

            //Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(ErrorLevel.Error, response.Status.ErrorLevel, "Password should be invalid");
            Assert.IsTrue(response.Status.ErrorMessage.Contains("Username or password is not valid"), "Error message was: " + response.Status.ErrorMessage);
        }
 private bool UserExists(User user)
 {
     return _database.Query<RavenUser>()
              .Any(u => u.Username == user.Username);
 }
Esempio n. 21
0
        public CollectionResponse<UserFeed> GetFeeds()
        {
            if (string.IsNullOrEmpty(Context.User.Identity.Name))
            {
                return ResponseBuilder.BuildCollectionResponse<UserFeed>(ErrorLevel.Error, "Please log in to view feeds");
            }

            //TODO use client connection for this
            Groups.Add(Context.ConnectionId, Context.User.Identity.Name);

            var userQuery = new User
                {
                    Username = Context.User.Identity.Name
                };
            var userResponse = _authentication.GetUserByUsername(userQuery);
            if (userResponse.Status.ErrorLevel > ErrorLevel.Warning || userResponse.Data == null)
            {
                return ResponseBuilder.BuildCollectionResponse<UserFeed>(userResponse.Status.ErrorLevel,
                    string.Format("Unable to retrieve feeds for user \"{0}\": {1}", userQuery.Username,
                        userResponse.Status.ErrorMessage));
            }

            var user = userResponse.Data;
            return _feedManager.GetUserFeeds(user);
        }
        private Status ValidateNewUser(User user)
        {
            if (UserExists(user))
            {
                return new Status
                {
                    ErrorLevel = ErrorLevel.Error,
                    ErrorMessage = "User name already exists. Please enter a different user name."
                };
            }

            if (EmailExists(user))
            {
                return new Status
                {
                    ErrorLevel = ErrorLevel.Error,
                    ErrorMessage = "A user name for that e-mail already exists. Please enter a different e-mail."
                };
            }

            if (!PasswordValid(user))
            {
                return new Status
                {
                    ErrorLevel = ErrorLevel.Error,
                    ErrorMessage = "The password provided is invalid. " + _configuration.PasswordMessage
                };
            }

            if (!PasswordLengthValid(user))
            {
                return new Status
                {
                    ErrorLevel = ErrorLevel.Error,
                    ErrorMessage = "The password provided is invalid. Passwords must be greater than 6 characters. "
                };
            }

            if (!EmailValid(user))
            {
                return new Status
                    {
                        ErrorLevel = ErrorLevel.Error,
                        ErrorMessage = "The e-mail address provided is invalid. Please check the value and try again."
                    };
            }

            return new Status
            {
                ErrorLevel = ErrorLevel.None
            };
        }
Esempio n. 23
0
        public override Task OnDisconnected(bool stopCalled)
        {
            var user = new User
            {
                Username = Context.User.Identity.Name
            };
            var removeConnectionResponse = _authentication.RemoveConnection(Context.ConnectionId, user);
            //TODO log connection errors
            Groups.Remove(Context.ConnectionId, Context.User.Identity.Name);

            return base.OnDisconnected(stopCalled);
        }
        public SingleResponse<bool> ChangePassword(string username, string oldPassword, string newPassword)
        {
            try
            {
                var user = new User
                {
                    Username = username,
                    Password = oldPassword
                };
                var storageUser = GetRavenUserByUsername(user);
                var valid = storageUser != null && _cryptography.MatchHash(storageUser.PasswordHash, user.Password);
                if (!valid)
                {
                    return new SingleResponse<bool>
                    {
                        Data = false,
                        Status = new Status
                        {
                            ErrorLevel = ErrorLevel.Error,
                            ErrorMessage = "Username or password is not valid"
                        }
                    };
                }

                user.Password = newPassword;
                if (!PasswordValid(user))
                {
                    return new SingleResponse<bool>
                    {
                        Data = false,
                        Status = new Status
                        {
                            ErrorLevel = ErrorLevel.Error,
                            ErrorMessage = "The password provided is invalid. " + _configuration.PasswordMessage
                        }
                    };
                }

                storageUser.PasswordHash = _cryptography.CreateHash(newPassword);
                _database.Store(storageUser);
                return new SingleResponse<bool>
                    {
                        Data = true,
                        Status = new Status
                            {
                                ErrorLevel = ErrorLevel.None
                            }
                    };
            }
            catch (Exception ex)
            {
                return new SingleResponse<bool>
                {
                    Data = false,
                    Status = new Status
                    {
                        ErrorLevel = ErrorLevel.Critical,
                        ErrorMessage = "Fatal error changing password",
                        ErrorException = ex
                    }
                };
            }
        }
        public void GetUserFeedByUserIdAndUrl()
        {
            //Arrange
            IFeedManager feedManager = new FeedManager(_database);

            //Act
            const string ravenUserId = "ravenuser/1";
            var user = new User
                {
                    Id = ravenUserId
                };
            const string url = "http://test.url.fake";
            var response = feedManager.GetUserFeedByUserIdAndUrl(user, url);

            //Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
            Assert.IsNotNull(response.Data);
            Assert.AreEqual(url, response.Data.Feed.Url);
            Assert.AreEqual(ravenUserId, response.Data.RavenUserId);
        }
        public void Register_EmailRequired()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
                {
                    Username = "******",
                    Password = "******"
                };
            var status = authentication.Register(user);

            //Assert
            Assert.IsNotNull(status);
            Assert.AreEqual(ErrorLevel.Error, status.ErrorLevel, "Email should be required");
            Assert.IsTrue(status.ErrorMessage.Contains("Email is required"), "Error message was: " + status.ErrorMessage);
        }
        public void GetUserFeedsTest()
        {
            //Arrange
            IFeedManager feedManager = new FeedManager(_database);

            //Act
            var user = new User
                {
                    Id = "ravenuser/1"
                };
            var response = feedManager.GetUserFeeds(user);

            //Assert
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
        }
        public void Register_UserExists()
        {
            //Arrange
            var authentication = TestRegistry.GetKernel().Get<IAuthentication>();

            //Act
            var user = new User
            {
                Username = "******",
                Password = "******",
                Email = "*****@*****.**"
            };
            var status = authentication.Register(user);

            //Assert
            Assert.IsNotNull(status);
            Assert.AreEqual(ErrorLevel.Error, status.ErrorLevel, "Username should throw error for existing");
            Assert.IsTrue(status.ErrorMessage.Contains("User name already exists"), "Error message was: " + status.ErrorMessage);
        }
        public void AddUserFeedItems()
        {
            //Arrange
            IFeedManager feedManager = new FeedManager(_database);

            //Act
            IEnumerable<FeedItem> newFeedItems = new List<FeedItem>
                {
                    new FeedItem
                        {
                            Id = "FeedItem/1"
                        }
                };
            var user = new User
                {
                    Id = "RavenUsers/1"
                };
            CollectionResponse<UserFeedItem> response = feedManager.AddUserFeedItems(newFeedItems, user);

            //Assert
            Assert.IsNotNull(response);
            Assert.AreEqual(ErrorLevel.None, response.Status.ErrorLevel, response.Status.ErrorMessage);
            Assert.IsNotNull(response.Data);
            Assert.Greater(response.Data.Count(), 0);
            Assert.IsNotNull(response.Data.First().FeedItemId);
            Assert.IsTrue(response.Data.First().IsUnread);
        }
        public SingleResponse<User> GetUserByUsername(User user)
        {
            try
            {
                var ravenUser = GetRavenUserByUsername(user);
                if (ravenUser == null)
                {
                    return new SingleResponse<User>
                        {
                            Data = new User(),
                            Status = new Status
                                {
                                    ErrorLevel = ErrorLevel.Error,
                                    ErrorMessage = string.Format("User \"{0}\" not found", user.Username)
                                }
                        };
                }

                return new SingleResponse<User>
                    {
                        Data = Mapper.Map<User>(ravenUser),
                        Status = new Status
                            {
                                ErrorLevel = ErrorLevel.None
                            }
                    };
            }
            catch (Exception ex)
            {
                return new SingleResponse<User>
                    {
                        Data = user,
                        Status = new Status
                            {
                                ErrorLevel = ErrorLevel.Critical,
                                ErrorMessage = string.Format("Fatal error retrieving user {0}: {1}", user.Username, ex)
                            }
                    };
            }
        }