public void AddReplyServerTests()
 {
     DataManager target = new DataManager(); // TODO: Initialize to an appropriate value
     target.CleanForumData();
     User user = new User("user", "user");
     target.AddUser(user);
     target.AddSubforum(new Subforum("subforumName"));
     Post post = new Post(new Postkey("user", DateTime.Now), "MainPost", "", null, "subforumName");
     Thread.Sleep(1000);
     Post reply = new Post(new Postkey("user", DateTime.Now), "ReplyToMainPost", "", post.Key, "subforumName");
     bool actualPost = target.AddPost(post, "subforumName");
     bool actualReply = target.AddReply(reply, post.Key);
     Post replyFromDB =  target.GetPost(reply.Key);
     Assert.IsTrue(actualPost);
     Assert.IsTrue(actualReply);
     Assert.AreEqual(reply.Key.Time, replyFromDB.Key.Time);
     Assert.AreEqual(reply.Key.Username, replyFromDB.Key.Username);
     Assert.AreEqual(reply.Title, replyFromDB.Title);
     Assert.AreEqual(reply.Body, replyFromDB.Body);
     Post postFromDB = target.GetPost(target.GetPost(reply.ParentPost).Key);
     Assert.AreEqual(post.Key.Time, postFromDB.Key.Time);
     Assert.AreEqual(post.Key.Username, postFromDB.Key.Username);
     Assert.AreEqual(post.Title, postFromDB.Title);
     Assert.AreEqual(post.Body, postFromDB.Body);
     target.RemoveSubforum("subforumName");
 }
Beispiel #2
0
 public DataManagerLocal()
 {
     users = new ConcurrentDictionary<string, User>();
     subforumsList = new ConcurrentDictionary<string, Subforum>();
     string adminName = ConfigurationManager.AppSettings["adminName"];
     string adminPass = ConfigurationManager.AppSettings["adminPassword"];
     User admin = new User(adminName, adminPass);
     SetAdmin(admin);
 }
 public void AddPostServerTests()
 {
     DataManager target = new DataManager();
     target.CleanForumData();
     User user = new User("user", "user");
     target.AddUser(user);
     target.AddSubforum(new Subforum("subforumName"));
     Postkey pk = new Postkey("user", DateTime.Now);
     Post p = new Post(pk, "Post", "body", null, "subforumName");
     bool actual = target.AddPost(p, "subforumName");
     Post p2 = target.GetPost(pk);
     Assert.IsTrue(actual);
     Assert.AreEqual(p.Key.Time, p2.Key.Time);
     Assert.AreEqual(p.Key.Username, p2.Key.Username);
     Assert.AreEqual(p.Title, p2.Title);
     Assert.AreEqual(p.Body, p2.Body);
     target.RemoveSubforum("subforumName");
 }
Beispiel #4
0
 // Add to interface
 public bool ChangeAdmin(User newAdmin, User oldAdmin)
 {
     throw new NotImplementedException();
 }
Beispiel #5
0
 public bool AddUser(User user)
 {
     try
     {
         UserEntity ue = new UserEntity();
         ue.UserName = user.Username;
         ue.Password = user.Password;
         ue.Authentication = user.Level.ToString();
         ue.State = user.CurrentState.ToString();
         //TODO - Add friends
         ForumContext.UserEntities.AddObject(ue);
         ForumContext.SaveChanges();
         return true;
     }
     catch (Exception)
     {
         //TODO
         throw;
     }
 }
Beispiel #6
0
        public Result ReplaceAdmin(string oldAdminUsername, string oldAdminPassword, string newAdminUsername, string newAdminPassword)
        {
            try
            {
                log.Info("got request to replace admin");

                Result res = securityManager.AuthenticateAdmin(oldAdminUsername, oldAdminPassword);
                if (res == Result.OK)
                {
                    User newAdmin;
                    try
                    {
                        newAdmin = dataManager.GetUser(newAdminUsername);
                    }
                    catch (UserNotFoundException)
                    {
                        newAdmin = new User(newAdminUsername, newAdminPassword);
                        dataManager.AddUser(newAdmin);
                    }

                    User oldAdmin = dataManager.GetAdmin();
                    if (CheckIfModerator(oldAdminUsername))
                        oldAdmin.Level = AuthorizationLevel.MODERATOR;
                    else
                        oldAdmin.Level = AuthorizationLevel.MEMBER;
                    newAdmin.Level = AuthorizationLevel.ADMIN;
                    if (dataManager.SetAdmin(newAdmin))
                        return Result.OK;
                    else return Result.ENTRY_EXISTS;
                }
                else return res;

            }
            catch (Exception e)
            {
                log.Error("failed to replace admin", e);
                throw e;
            }
        }
Beispiel #7
0
 public bool AddUser(User user)
 {
     try
     {
         users.TryAdd(user.Username, user);
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public void DataManagerConstructorServerTests()
        {
            DataManager target = new DataManager();
            Assert.IsNotNull(target);

            target.CleanForumData();
            User user = new User("user", "user");
            target.AddUser(user);
            target.AddSubforum(new Subforum("subforumName"));
        }
Beispiel #9
0
 public Result AuthorizedRegister(string username, string password)
 {
     User user = dataManager.GetUser(username);
     if (user != null)
         return Result.SECURITY_ERROR;
     else
     {
         user = new User(username, password);
         user.Level = AuthorizationLevel.MEMBER;
         dataManager.AddUser(user);
         return Result.OK;
     }
 }
Beispiel #10
0
 public bool SetAdmin(User admin)
 {
     try
     {
         IEnumerable<UserEntity> usersQuery = from u in ForumContext.UserEntities
                                              where u.UserName == admin.Username
                                              select u;
         UserEntity ue = usersQuery.First();
         ue.Authentication = AuthorizationLevel.ADMIN.ToString();
         ForumContext.SaveChanges();
         return true;
     }
     catch (Exception)
     {
         //TODO
         throw;
     }
 }
Beispiel #11
0
 public void UpdateUserServerTests()
 {
     DataManager target = new DataManager();
     target.CleanForumData();
     User userTemp = new User("user", "user");
     target.AddUser(userTemp);
     target.AddSubforum(new Subforum("subforumName"));
     User user = target.GetUser("user");
     user.Password = "******";
     bool actual;
     actual = target.UpdateUser(user);
     Assert.IsTrue(actual);
     Assert.AreEqual(user.Password, target.GetUser("user").Password);
     target.RemoveSubforum("subforumName");
 }
Beispiel #12
0
 public void RemoveSubforumServerTests()
 {
     DataManager target = new DataManager(); // TODO: Initialize to an appropriate value
     target.CleanForumData();
     User user = new User("user", "user");
     target.AddUser(user);
     target.AddSubforum(new Subforum("subforumName"));
     string subforum = "subforum";
     Subforum actual = null;
     bool ans = target.AddSubforum(new Subforum(subforum));
     actual = target.GetSubforum(subforum);
     Assert.IsNotNull(actual);
     Assert.IsTrue(target.RemoveSubforum(subforum));
     try
     {
         actual = target.GetSubforum(subforum);
     }
     catch (Exception)
     {
         Assert.IsTrue(true);
     }
     target.RemoveSubforum("subforumName");
 }
Beispiel #13
0
        public void RemovePostServerTests()
        {
            DataManager target = new DataManager(); // TODO: Initialize to an appropriate value
            target.CleanForumData();
            User user = new User("user", "user");
            target.AddUser(user);
            target.AddSubforum(new Subforum("subforumName"));
            Postkey pk = new Postkey("user", DateTime.Now);
            Thread.Sleep(1001);
            Postkey pk2 = new Postkey("user", DateTime.Now);
            target.AddPost(new Post(pk, "post-TEST", "", null, ""), "SubforumName");
            target.AddReply(new Post(pk2, "reply-TEST", "body", pk, "SubforumName"), pk);
            bool actual;
            actual = target.RemovePost(pk2);
            Assert.IsTrue(actual);
            try
            {
                Assert.IsNull(target.GetPost(pk2));
            }
            catch (Exception)
            {

                Assert.IsTrue(true);
            }
            target.RemoveSubforum("subforumName");
        }
Beispiel #14
0
 public void RemoveModeratorsServerTests()
 {
     DataManager target = new DataManager();
     target.CleanForumData();
     User user = new User("user", "user");
     target.AddUser(user);
     target.AddSubforum(new Subforum("subforumName"));
     List<string> actual;
     List<string> moderators = new List<string>();
     moderators.Add("user");
     target.SetModerators("subforumName", moderators);
     actual = target.GetModerators("subforumName");
     Assert.AreEqual(moderators.Count, actual.Count);
     Assert.AreEqual(moderators[0], actual[0]);
     Assert.IsTrue(target.RemoveModerator("subforumName", "user"));
     actual = target.GetModerators("subforumName");
     Assert.IsTrue(actual.Count == 0);
     target.RemoveSubforum("subforumName");
 }
Beispiel #15
0
 public void EditPostServerTests()
 {
     DataManager target = new DataManager();
     target.CleanForumData();
     User user = new User("user", "user");
     target.AddUser(user);
     target.AddSubforum(new Subforum("subforumName"));
     Postkey pk = new Postkey("user", DateTime.Now);
     Thread.Sleep(1000);
     target.AddPost(new Post(pk, "Post", "", null, null), "subforumName");
     Post reply = new Post(new Postkey("user", DateTime.Now), "Reply", "", pk, null);
     Thread.Sleep(1000);
     reply.Body = "reply body";
     bool ans = target.AddReply(reply, pk);
     Post reply2 = new Post(new Postkey("user", DateTime.Now), "Reply2 - new Update", "", pk, null);
     reply2.Body = " reply 2 body";
     bool ans2 = target.EditPost(reply2, reply.Key);
     Assert.IsTrue(ans2); // Need to check with debugger the content of reply
     Post editedPost = target.GetPost(reply.Key);
     Assert.AreEqual(reply.Key.Time, editedPost.Key.Time);
     Assert.AreEqual(reply.Key.Username, editedPost.Key.Username);
     Assert.AreEqual(reply2.Title, editedPost.Title);
     Assert.AreEqual(reply2.Body, editedPost.Body);
     target.RemoveSubforum("subforumName");
 }
Beispiel #16
0
        public User GetUser(string username)
        {
            try
            {
                IEnumerable<UserEntity> usersQuery = from u in ForumContext.UserEntities
                                                     where u.UserName == username
                                                     select u;
                if (usersQuery.Count() != 1)
                {
                    // User wasn't found or more than one was found
                    return null;
                }
                else
                {
                    UserEntity ue = usersQuery.First();
                    User user = new User(ue.UserName.Trim(), ue.Password.Trim());
                    switch (ue.Authentication.Trim())
                    {
                        case "GUEST":
                            user.Level = AuthorizationLevel.GUEST;
                            break;
                        case "MEMBER":
                            user.Level = AuthorizationLevel.MEMBER;
                            break;
                        case "MODERATOR":
                            user.Level = AuthorizationLevel.MODERATOR;
                            break;
                        case "ADMIN":
                            user.Level = AuthorizationLevel.ADMIN;
                            break;
                        default:
                            break;
                    }

                    switch (ue.State.Trim())
                    {
                        //case "Login":
                        //    user.CurrentState = UserState.Login;
                        //    break;
                        //case "Logout":
                        //    user.CurrentState = UserState.Logout;
                        //    break;
                        case "Active":
                            user.CurrentState = UserState.Active;
                            break;
                        case "NotActive":
                            user.CurrentState = UserState.NotActive;
                            break;
                        case "Banned":
                            user.CurrentState = UserState.Banned;
                            break;
                        case "ShouldBeBanned":
                            user.CurrentState = UserState.ShouldBeBanned;
                            break;
                        default:
                            break;
                    }
                    // TODO - Add friends:
                    user.Friends = null;

                    return user;
                }
            }
            catch (Exception)
            {
                //TODO
                throw;
            }
        }
Beispiel #17
0
        public void InitForumData()
        {
            try
            {
                int numberOfSubforums = Convert.ToInt32(ConfigurationManager.AppSettings["initializeNumberOfSubforums"].ToString());
                string[] subforumsNamesList = ConfigurationManager.AppSettings["subforumsNamesList"].ToString().Split(',');
                int numberOfPosts = Convert.ToInt32(ConfigurationManager.AppSettings["numberOfPostsInEachSubforum"].ToString());
                // Add admin:
                string adminName = ConfigurationManager.AppSettings["adminName"];
                string adminPass = ConfigurationManager.AppSettings["adminPassword"];
                User admin = new User(adminName, adminPass);
                AddUser(admin);
                SetAdmin(admin);

                for (int i = 0 ; i < numberOfSubforums ; i++)
                {
                    Subforum s = new Subforum(subforumsNamesList[i]);
                    this.AddSubforum(s);
                    for (int j = 0 ; j < numberOfPosts ; j++)
                    {
                        Thread.Sleep(1000);
                        this.AddPost(new Post(new Postkey(adminName, DateTime.Now),
                            "Post" + j + " in Subforum: " + s.Name, "content", new Postkey("", DateTime.Now), s.Name), s.Name);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
Beispiel #18
0
 public bool SetAdmin(User admin)
 {
     try
     {
         admin.Level = AuthorizationLevel.ADMIN;
         users.TryAdd(admin.Username, admin);
         this.adminName = admin.Username;
         return true;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Beispiel #19
0
 public bool UpdateUser(User user)
 {
     try
     {
         IEnumerable<UserEntity> usersQuery = from u in ForumContext.UserEntities
                                              where u.UserName == user.Username
                                              select u;
         UserEntity ue = usersQuery.First();
         ue.Password = user.Password;
         ue.State = user.CurrentState.ToString();
         ue.Authentication = user.Level.ToString();
         //TODO:
         //ue.friends - TODO
         ForumContext.SaveChanges();
         return true;
     }
     catch (Exception)
     {
         //TODO
         throw;
     }
 }
Beispiel #20
0
 public bool UpdateUser(User user)
 {
     try
     {
         users[user.Username] = user;
         return true;
     }
     catch (Exception)
     {
         throw new UserNotFoundException();
     }
 }
Beispiel #21
0
 private bool IsUserLoggendIn(User user)
 {
     return user != null && subscribed.ContainsKey(user.Username);
 }
Beispiel #22
0
 public void AddUserServerTests()
 {
     DataManager target = new DataManager();
     target.CleanForumData();
     User user = new User("user2", "user2");
     target.AddUser(user);
     User user2 = target.GetUser("user2");
     Assert.AreEqual(user.Username, user2.Username);
     Assert.AreEqual(user.Password, user2.Password);
     target.CleanForumData();
 }