private void txtUserName_Validating(object sender, CancelEventArgs e) { bool error = false; //check for username missing if (txtUserName.Text == "") { error = true; MessageBox.Show("Falta el nombre de usuario para poder acceder al programa."); } //Check if user name exist only if was changed if ((!error) && (txtUserName.Text!=Global.userUsed.userName) ) { Users aUserFound = new Users(); aUserFound = aUserFound.findUserByUserName(connString, txtUserName.Text); if (aUserFound.userID != -1) { error = true; MessageBox.Show("Ya existe ese nombre de usuario en la base de datos."); } } if (error) { txtUserName.BackColor = Color.Red; } else { txtUserName.BackColor = SystemColors.Window; } }
//Save an User object into BD using a connection, suposses data are checked with checkUserData. public Users saveUserData(string connString) { //Return the user saved. If error return an empty user with userId=-1 Users aUser = new Users(); using (SqlConnection conn = new SqlConnection(connString)) { string pass = userPass; string query = String.Format(@"INSERT INTO Users(UserFirstName, UserSecondName, UserBdate, UserName, UserPass, UserMail) VALUES(@userFirstName,@userSecondName,@userBDate,@userName,PwdEncrypt('{0}'),@userMail)", pass); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add(new SqlParameter("@userFirstName", SqlDbType.VarChar)); cmd.Parameters["@userFirstName"].Value = userFirstName; cmd.Parameters.Add(new SqlParameter("@userSecondName", SqlDbType.VarChar)); cmd.Parameters["@userSecondName"].Value = userSecondName; cmd.Parameters.Add(new SqlParameter("@userBDate", SqlDbType.Date)); cmd.Parameters["@userBDate"].Value = userBDate; cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar)); cmd.Parameters["@userName"].Value = userName; cmd.Parameters.Add(new SqlParameter("@userMail", SqlDbType.VarChar)); cmd.Parameters["@userMail"].Value = userMail; conn.Open(); int res = cmd.ExecuteNonQuery(); //Find the userID asigned. if (res >= 0) { string user = userName; aUser = aUser.findUserByUserName(connString, user); } } } return aUser; }
public void Users_loginTest() { //Creates an User with data. Users aUser = new Users(-1, "name", "", DateTime.Now, "", "", "", ""); //Try to save first aUser = aUser.LoadData(-1, "Nombre", "Apellido", Convert.ToDateTime("12/04/1982"), "NickToDel", "12345678", "12345678", "*****@*****.**"); aUser = aUser.saveUserData(connString); Assert.AreNotEqual(-1, aUser.userID); //1=save data without any problem. //Try to find it and take the userID //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 string expectedUserName = aUser.userName; Int64 userIDToDelete = aUser.userID; aUser = aUser.findUserByUserName(connString, expectedUserName); Assert.AreEqual(expectedUserName, aUser.userName); Int64 expectedUserID = aUser.userID; //Correct login string givenPassword = "******"; aUser = aUser.verifyPass(aUser.userName, givenPassword, connString); Assert.AreNotEqual(-1, aUser.userID); //Uncorrect Login givenPassword = ""; aUser = aUser.verifyPass(aUser.userName, givenPassword, connString); Assert.AreEqual(-1, aUser.userID); //Try to Delete previous saved user. int res = Users.deleteUserByUserID(connString, userIDToDelete); Assert.AreEqual(1, res); //Deleted with success }
public void Users_UpdateTestWithPass() { //Creates an User with data. Users aUser = new Users(-1, "name", "", DateTime.Now, "", "", "", ""); //Try to save first aUser = aUser.LoadData(-1, "Nombre", "Apellido", Convert.ToDateTime("12/04/1982"), "NickToDel", "12345678", "12345678", "*****@*****.**"); aUser = aUser.saveUserData(connString); Assert.AreNotEqual(-1, aUser.userID); //1=save data without any problem. //Try to find it and take the userID //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 string expectedUserName = aUser.userName; aUser = aUser.findUserByUserName(connString, expectedUserName); Assert.AreEqual(expectedUserName, aUser.userName); //User finded Int64 expectedUserID = aUser.userID; //Try to Update previous saved user. string newPass="******"; aUser.userPass = newPass; aUser.userConfirmPassword = newPass; aUser = aUser.updateUserData(connString, true); aUser = aUser.verifyPass(aUser.userName, newPass, connString); Assert.AreNotEqual(-1, aUser.userID); //Verify if new password was stored correctly //Try to Delete previous saved user. Int64 res = Users.deleteUserByUserID(connString, aUser.userID); Assert.AreEqual(1, res); //Deleted with success ////Try to find it ////findUserByUserName returns a user if find, if not find return a user wit UserID=-1 //expectedUserName = aUser.userName; //aUser = aUser.findUserByUserName(connString, expectedUserName); //Assert.AreEqual(-1, aUser.userID); //user not found because was deleted }
public void Users_UpdateTestWithoutPass() { //Creates an User with data. Users aUser = new Users(-1, "Nombre", "Apellido", Convert.ToDateTime("12/04/1982"), "NickToDel", "12345678", "12345678", "*****@*****.**"); //Try to save first aUser = aUser.saveUserData(connString); Assert.AreNotEqual(-1, aUser.userID); //1=save data without any problem. //Try to find it and take the userID //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 Users aUserToFind = aUser.findUserByUserName(connString, aUser.userName); Assert.AreEqual(aUser.userName, aUserToFind.userName); Int64 expectedUserID = aUser.userID; //Try to Update previous saved user. aUser.userSecondName = "Second Name Updated"; aUser = aUser.updateUserData(connString,false); Assert.AreNotEqual(-1, aUser.userID); //1=update data without any problem. //Try to find it //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 aUserToFind = aUser.findUserByUserID(connString,aUser.userID); Assert.AreEqual(aUser.userName, aUserToFind.userName); //Verify the update. Assert.AreEqual(aUser.userSecondName,aUserToFind.userSecondName); //Try to Delete previous saved user. int res = Users.deleteUserByUserID(connString, aUser.userID); Assert.AreEqual(1, res); //Deleted with success //Try to find it aUser = aUser.findUserByUserID(connString, aUser.userID); Assert.AreEqual(-1, aUser.userID); //user not found because was deleted }
public void Users_TestFindUserByUserName() { //Creates an User with data. string userName = "******"; //Put here the username of a user that exits in the BD. Users aUser = new Users(-1, "", "", DateTime.Now, "", "", "", ""); //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 aUser = aUser.findUserByUserName(connString, userName); Assert.AreNotEqual(-1, aUser.userID); Assert.AreEqual(userName, aUser.userName); }
public void Users_TestDeleteUserName() { //Creates an User with data. Users aUser = new Users(-1, "name", "", DateTime.Now, "", "", "", ""); //Try to save first aUser = aUser.LoadData(-1, "Nombre", "Apellido", Convert.ToDateTime("12/04/1982"), "NickToDel", "12345678", "12345678", "*****@*****.**"); aUser = aUser.saveUserData(connString); Assert.AreNotEqual(-1, aUser.userID); //1=save data without any problem. //Try to find it //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 string expectedUserName = aUser.userName; aUser = aUser.findUserByUserName(connString, expectedUserName); Assert.AreNotEqual(-1, aUser.userID); //User finded //Try to Delete previous saved user. int res = Users.deleteUserByUserID(connString, aUser.userID); Assert.AreEqual( 1, res); //Deleted with success //Try to find it //findUserByUserName returns a user if find, if not find return a user wit UserID=-1 aUser = aUser.findUserByUserName(connString, expectedUserName); Assert.AreEqual(-1, aUser.userID); //User not found because was deleted }