Ejemplo n.º 1
0
        public void AddUserToFriendList_ShouldReturnAFriendUserNoExceptionThrown()
        {
            //Arrange
            FriendRelationship friendUser = null;

            //Act
            MessengerManager messengerManager = new MessengerManager();
            bool             isFriend;

            using (DatabaseContext db = new DatabaseContext())
            {
                MessengerServices msService = new MessengerServices(db);
                isFriend = msService.IsFriend(testAuthUserId, testContactUserId);
                if (isFriend == true)
                {
                    messengerManager.RemoveUserFromFriendList(testAuthUserId, testContactUserId);
                }
                db.SaveChanges();
            }

            friendUser = messengerManager.AddUserFriendList(testAuthUserId, testContactUsername);

            //Assert
            Assert.NotNull(friendUser);
        }
Ejemplo n.º 2
0
        public void RemoveUserFromFriendList_ShouldReturnFriendRelationship()
        {
            //Arrange
            FriendRelationship fs = null;

            //Act
            MessengerManager messengerManager = new MessengerManager();

            messengerManager.RemoveUserFromFriendList(testAuthUserId, testContactUserId);
            messengerManager.AddUserFriendList(testAuthUserId, testContactUsername);

            using (DatabaseContext db = new DatabaseContext())
            {
                var foundRelationship = db.FriendRelationships.Where(f => f.UserId == testAuthUserId && f.FriendId == testContactUserId).FirstOrDefault();
                fs = messengerManager.RemoveUserFromFriendList(foundRelationship.UserId, foundRelationship.FriendId);
            }



            //Assert
            Assert.NotNull(fs);
        }
        public IHttpActionResult AddFriendContactList(string addedUsername)
        {
            //The authentication part start from here
            //Create security context from the token
            SecurityContext securityContext = SecurityContextBuilder.CreateSecurityContext(
                Request.Headers
                );

            if (securityContext == null)
            {
                return(Unauthorized());;
            }

            //Validate the token
            SessionManager sm = new SessionManager();

            if (!sm.ValidateSession(securityContext.Token))
            {
                return(Unauthorized());;
            }

            //Create authorization manager from security context to check claims
            AuthorizationManager authorizationManager = new AuthorizationManager(
                securityContext
                );
            // TODO get this from table in database.
            List <string> requiredClaims = new List <string>()
            {
                "CanSendMessage"
            };

            if (!authorizationManager.CheckClaims(requiredClaims))
            {
                return(Unauthorized());;
            }
            else
            {
                UserManager um = new UserManager();

                // Get authUserId from auth username
                _authUserId = um.FindByUserName(securityContext.UserName).Id;
                FriendRelationship friendRelationship = null;
                try
                {
                    // Try to add friend
                    friendRelationship = _messengerManager.AddUserFriendList(_authUserId, addedUsername);
                }

                catch (Exception ex)
                {
                    if (ex is MessageReceiverNotFoundException)
                    {
                        return(Content(HttpStatusCode.NotFound, "User with the username does not exist to be added"));
                    }

                    else if (ex is DuplicatedFriendException)
                    {
                        return(Content(HttpStatusCode.Conflict, "User with the username is already in friend list "));
                    }

                    else if (ex is DbUpdateException)
                    {
                        return(Content(HttpStatusCode.InternalServerError, "There is a error when saving friend relationship to database"));
                    }
                }

                // Create FriendRelationship DTO to return to the front end to render the friendlist
                var friendRelationshipDTO = new FriendRelationshipDTO
                {
                    FriendId       = friendRelationship.FriendId,
                    FriendUsername = um.FindUserById(friendRelationship.FriendId).UserName
                };

                return(Ok(new { friend = friendRelationshipDTO } /*new { SITtoken = updatedToken }*/));
            }

            //string updatedToken = sm.RefreshSession(securityContext.Token);
        }