예제 #1
0
        public void Equal_ReturnsTrue_Bool()
        {
            EndUser oneUser = new EndUser("Jerry", "password");
            EndUser twoUser = new EndUser("Jerry", "password");

            Assert.Equal(oneUser, twoUser);
        }
예제 #2
0
        public static EndUser Find(int id)
        {
            SqlConnection conn = DB.Connection();

            conn.Open();
            SqlCommand   cmd = new SqlCommand("SELECT * FROM end_users WHERE id = @UserId;", conn);
            SqlParameter uId = new SqlParameter("@UserId", id.ToString());

            cmd.Parameters.Add(uId);
            SqlDataReader rdr           = cmd.ExecuteReader();
            int           foundId       = 0;
            string        foundName     = null;
            string        foundPassword = null;

            while (rdr.Read())
            {
                foundId       = rdr.GetInt32(0);
                foundName     = rdr.GetString(1);
                foundPassword = rdr.GetString(2);
            }
            EndUser foundUser = new EndUser(foundName, foundPassword, foundId);

            if (rdr != null)
            {
                rdr.Close();
            }
            if (conn != null)
            {
                conn.Close();
            }
            return(foundUser);
        }
예제 #3
0
        public void Find_FindsUserInDB_Object()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            EndUser foundUser = EndUser.Find(testUser.Id);

            Assert.Equal(foundUser, testUser);
        }
예제 #4
0
        public void LoginAttempt_ReturnTrueIfSuccess_Bool()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            bool result = EndUser.LoginAttempt("Jerry", "password");

            Assert.Equal(true, result);
        }
예제 #5
0
        public void UpdatePassword_UpdatesPasswordInDB_Object()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            string newPassword = "******";

            testUser.UpdatePassword(newPassword);
            string result = testUser.Password;

            Assert.Equal(result, newPassword);
        }
예제 #6
0
        public void Update_UpdatesUserInfoInDB_Object()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            string newName = "Gerry";

            testUser.Update(newName);
            string result = testUser.Name;

            Assert.Equal(result, newName);
        }
예제 #7
0
        public void Save_SavesToDB_Object()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            List <EndUser> allUsers = EndUser.GetAll();
            List <EndUser> result   = new List <EndUser> {
                testUser
            };

            Assert.Equal(result, allUsers);
        }
예제 #8
0
        public void PasswordHash_VerifyHash_HashesPasswordAndVerifies_String_Bool()
        {
            EndUser testUser = new EndUser("Jerry", "password");
            string  unhashed = testUser.Password;

            byte[] tmp    = new byte[0];
            string hashed = EndUser.PasswordHash(unhashed, tmp);

            testUser.Password = hashed;
            testUser.Save();
            string loginTest = "password";
            bool   result    = EndUser.VerifyHash(loginTest, hashed);

            Assert.Equal(true, result);
        }
예제 #9
0
 public override bool Equals(System.Object otherEndUser)
 {
     if (!(otherEndUser is EndUser))
     {
         return(false);
     }
     else
     {
         EndUser newEndUser       = (EndUser)otherEndUser;
         bool    idEquality       = (this.Id == newEndUser.Id);
         bool    nameEquality     = (this.Name == newEndUser.Name);
         bool    passwordEquality = (this.Password == newEndUser.Password);
         return(idEquality && nameEquality && passwordEquality);
     }
 }
예제 #10
0
        public void Delete_DeletesUserFromDB_Object()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            EndUser anotherUser = new EndUser("Rachel", "12345");

            anotherUser.Save();
            testUser.Delete();
            List <EndUser> allUsers = EndUser.GetAll();
            List <EndUser> result   = new List <EndUser> {
                anotherUser
            };

            Assert.Equal(result, allUsers);
        }
예제 #11
0
        public void GetSnippets_RetrievesUserSnippets_List()
        {
            EndUser testUser = new EndUser("Jerry", "password");

            testUser.Save();
            Snippet testSnippet = new Snippet("Some Code", "x = 'Foo'", new DateTime(2017, 6, 19, 12, 55, 00));

            testSnippet.Save();
            Snippet anotherSnippet = new Snippet("More Code", "y = 'Bar'", new DateTime(2017, 1, 01, 11, 55, 00));

            anotherSnippet.Save();
            testUser.AddSnippet(testSnippet);
            List <Snippet> allUserSnippets = testUser.GetSnippets();
            List <Snippet> result          = new List <Snippet> {
                testSnippet
            };

            Assert.Equal(result, allUserSnippets);
        }
예제 #12
0
        public static List <EndUser> GetAll()
        {
            SqlConnection conn = DB.Connection();

            conn.Open();
            SqlCommand     cmd      = new SqlCommand("SELECT * FROM end_users;", conn);
            SqlDataReader  rdr      = cmd.ExecuteReader();
            List <EndUser> allUsers = new List <EndUser> {
            };

            while (rdr.Read())
            {
                int     uId       = rdr.GetInt32(0);
                string  uName     = rdr.GetString(1);
                string  uPassword = rdr.GetString(2);
                EndUser newUser   = new EndUser(uName, uPassword, uId);
                allUsers.Add(newUser);
            }
            return(allUsers);
        }
예제 #13
0
 public void Dispose()
 {
     EndUser.DeleteAll();
 }
예제 #14
0
        public void EmptyDB_0()
        {
            int result = EndUser.GetAll().Count;

            Assert.Equal(0, result);
        }
예제 #15
0
        public HomeModule()
        {
//CREATE USER
            Get["/user_create"] = _ =>
            {
                return(View["user_create.cshtml"]);
            };
            Post["/user_create"] = _ =>
            {
                EndUser newUser = new EndUser(Request.Form["user-name"], Request.Form["user-password"]);
                byte[]  tmp     = new byte[0];
                string  hash    = EndUser.PasswordHash(newUser.Password, tmp);
                newUser.Password = hash;
                newUser.Save();
                return(View["user_login.cshtml"]);
            };
//USER LOGIN
            Get["/"] = _ =>
            {
                return(View["user_login.cshtml"]);
            };
            Post["/user_login"] = _ =>
            {
                string unhashed    = Request.Form["user-password"];
                byte[] tmp         = new byte[0];
                string hash        = EndUser.PasswordHash(unhashed, tmp);
                bool   loginResult = EndUser.LoginAttempt(Request.Form["user-name"], hash);
                return(View["loginsuccess.cshtml", loginResult]);
            };
//ALL SNIPPETS
            Get["/snippet/view"] = _ => {
                List <Snippet> allSnippets = Snippet.GetAll();
                return(View["snippet_view.cshtml", allSnippets]);
            };
//SPECIFIC SNIPPET
//---VIEW SNIPPET
            Get["/snippet/{id}"] = param => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };
                Snippet    SelectedSnippet        = Snippet.Find(param.id);
                List <Tag> SnippetTags            = SelectedSnippet.GetTags();

                model.Add("snippet", SelectedSnippet);
                model.Add("tag", SnippetTags);
                return(View["this_snippet.cshtml", model]);
            };

            //----UPDATE SNUPPET

            Get["/snippet/{id}/update"] = param => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };
                Snippet SelectedSnippet           = Snippet.Find(param.id);


                model.Add("snippet", SelectedSnippet);
                return(View["snippet_update.cshtml", model]);
            };
            Patch["/snippet/{id}/update"] = param => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };
                Snippet  SelectedSnippet          = Snippet.Find(param.id);
                DateTime SnippetDateTime          = DateTime.Now;


                List <Tag> SnippetTags = SelectedSnippet.GetTags();

                SelectedSnippet.Update(Request.Form["snippet-text"], SnippetDateTime);

                model.Add("tag", SnippetTags);
                model.Add("snippet", SelectedSnippet);
                return(View["this_snippet.cshtml", model]);
            };

//----DELETE SNIPPET
            Get["/snippet/{id}/delete"] = param => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };
                Snippet    SelectedSnippet        = Snippet.Find(param.id);
                List <Tag> SnippetTags            = SelectedSnippet.GetTags();

                model.Add("tag", SnippetTags);
                model.Add("snippet", SelectedSnippet);
                return(View["delete_confirm.cshtml", model]);
            };
            Delete["/snippet/{id}/delete/"] = param => {
                Snippet selectedSnippet = Snippet.Find(param.id);
                selectedSnippet.Delete();
                return(View["action_success.cshtml"]);
            };
//----DOWNLOAD SNIPPET

            Get["/snippet/{id}/download"] = param =>
            {
                Snippet selectedSnippet = Snippet.Find(param.id);
                string  snippetContent  = selectedSnippet.Text;
                using (StreamWriter objWriter = new StreamWriter("snippetText" + param.id + ".txt"))
                {
                    objWriter.Write(snippetContent);
                }
                return(View["action_success.cshtml"]);
            };
//ADD TAG TO SNIPPET
            Post["/snippet/{id}/add_tag"] = param => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };
                Snippet SelectedSnippet           = Snippet.Find(param.id);

                Tag newTag = new Tag(Request.Form["tag-text"]);
                newTag.Save();

                SelectedSnippet.AddTag(newTag);
                List <Tag> SnippetTags = SelectedSnippet.GetTags();

                model.Add("snippet", SelectedSnippet);
                model.Add("tag", SnippetTags);
                return(View["this_snippet.cshtml", model]);
            };
//CREATE SNIPPET
            Get["/snippet/create"] = _ =>
            {
                return(View["snippet_create.cshtml"]);
            };
            Post["/snippet/create"] = _ => {
                DateTime snippetDateTime = DateTime.Now;
                Snippet  newSnippet      = new Snippet(Request.Form["snippet-description"], Request.Form["snippet-text"], snippetDateTime);

                newSnippet.Save();
                List <Snippet> allSnippets = Snippet.GetAll();
                return(View["snippet_view.cshtml", allSnippets]);
            };
//SearchSnippet
            Get["/search/snippets"] = _ => {
                List <Snippet> allSnippets = new List <Snippet> {
                };
                return(View["search_snippet.cshtml", allSnippets]);
            };
            Post["/search/snippets"] = _ => {
                List <Snippet> allSnippets = Snippet.SearchSnippetText(Request.Form["search-string"]);
                return(View["search_snippet.cshtml", allSnippets]);
            };
//SearchTag
            Get["/search/tags"] = _ => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };

                List <Tag>     allTags          = Tag.GetAll();
                List <Snippet> searchedSnippets = new List <Snippet> {
                };

                model.Add("searched", searchedSnippets);
                model.Add("Tags", allTags);

                return(View["search_tag.cshtml", model]);
            };
            Post["/search/tags"] = _ => {
                Dictionary <string, object> model = new Dictionary <string, object> {
                };

                List <Tag>     allTags          = Tag.GetAll();
                List <Snippet> searchedSnippets = Tag.SearchSnippetsByTag(Request.Form["tag-id"]);

                model.Add("searched", searchedSnippets);
                model.Add("Tags", allTags);

                return(View["search_tag.cshtml", model]);
            };
        }