def test_updateFirstname(self): user = User("Rocher", "Jean", "test123") self.userImpl.insert(user) user.setFirstname("Jean") self.userImpl.updateFirstname("Rocher", "Jean", "Jean") usersTest = self.userImpl.selectAll() self.assertEqual(user, usersTest[len(usersTest) - 1])
def test_updateLastname(self): user = User("Jean", "Jean", "test123") self.userImpl.insert(user) user.setLastname("Rocher") self.userImpl.updateLastname("Jean", "Rocher", "Jean") usersTest = self.userImpl.select(["firstname", "lastname"], ["Jean", "Rocher"]) self.assertEqual(user, usersTest[len(usersTest) - 1])
def test_insert(self): user = User("Jean", "Roger", "test") self.userImpl.insert(user) self.cursor.execute("SELECT * FROM users") results = self.cursor.fetchall() self.assertEqual(5, len(results)) self.assertEqual(user.getFirstname(), results[4][1]) self.assertEqual(user.getLastname(), results[4][2]) self.assertEqual(user.getPassword(), results[4][3])
def test_selectAll(self): user1 = User("Maxime", "Hempte", "test123") user2 = User("Florent", "Veys", "test123") user3 = User("Bernard", "Test", "test123") user4 = User("Adrien", "Test", "test123") users = [user1, user2, user3, user4] usersTest = self.userImpl.selectAll() nbrUsersTest = len(usersTest) i = 0 while (i < nbrUsersTest): self.assertEqual(users[i], usersTest[i]) i += 1
def test_select(self): user = User("Florent", "Veys", "test123") columnsRef = ["lastname"] valuesRef = ["Veys"] usersTest = self.userImpl.select(columnsRef, valuesRef) self.assertEqual(len(usersTest), 1) self.assertEqual(user, usersTest[0]) user1 = User("Bernard", "Test", "test123") user2 = User("Adrien", "Test", "test123") users = [user1, user2] columnsRef2 = ["lastname"] valuesRef = ["Test"] usersTest2 = self.userImpl.select(columnsRef2, valuesRef) i = 0 nbrColumns = len(usersTest2) while (i < nbrColumns): self.assertEqual(users[i], usersTest2[i]) i += 1
def post(self): content = request.get_json() inputVoid = [] if content["login"] == "": inputVoid.append("login") if content["firstname"] == "": inputVoid.append("firstname") if content["lastname"] == "": inputVoid.append("lastname") if content["password"] == "": inputVoid.append("password") if content["confirmPassword"] == "": inputVoid.append("confirmPassword") if len(inputVoid) != 0: response = jsonify({ 'msg': 'One or more fields are not completed', 'column': inputVoid }) response.status_code = 400 return response if content["password"] != content["confirmPassword"]: inputVoid.append("password") inputVoid.append("confirmPassword") response = jsonify({ 'msg': 'Passwords not equals', 'column': inputVoid }) response.status_code = 400 return response checkLogin = self.userImpl.getUserByLogin(content["login"]) if checkLogin is not None: inputVoid.append("login") response = jsonify({ 'msg': 'Login already exist !', 'column': inputVoid }) response.status_code = 400 return response user = User(0, content["login"], content["firstname"], content["lastname"], content["password"], content["authorization"]) self.userImpl.insert(user) return "User added"
def select(self,id): sql = "SELECT * FROM user WHERE id = " + id db = pymysql.connect(self.ip,self.user,self.password,self.database) try: cursor = db.cursor() cursor.execute(sql) results = cursor.fetchall() users = [] for row in results: id = row[0] login = row[1] firstname = row[2] lastname = row[3] password = row[4] authorization = row[5] user = User(id,login, firstname, lastname, password,authorization) users.append(user) return users finally: db.close()
def put(self): curent_user = self.userImpl.select(get_jwt_identity()) content = request.get_json() if curent_user[0].getAuthorization() == 2 or curent_user[0].getId( ) == int(content["id"]): inputVoid = [] if content["firstname"] == "": inputVoid.append("firstname") if content["lastname"] == "": inputVoid.append("lastname") if content["password"] == "": inputVoid.append("password") if content["confirmPassword"] == "": inputVoid.append("confirmPassword") if len(inputVoid) != 0: response = jsonify({ 'msg': 'One or more fields are not completed', 'column': inputVoid }) response.status_code = 400 return response if content["password"] != content["confirmPassword"]: inputVoid.append("password") inputVoid.append("confirmPassword") response = jsonify({ 'msg': 'Passwords not equals', 'column': inputVoid }) response.status_code = 400 return response user = User(content["id"], content["login"], content["firstname"], content["lastname"], content["password"], content["authorization"]) self.userImpl.updateUser(content["id"], user) return "User updated" else: response = jsonify({'msg': 'Not authorize'}) response.status_code = 400 return response
def getUserByLogin(self,login): sql = "SELECT * FROM user where login = '******'" db = pymysql.connect(self.ip,self.user,self.password,self.database) try: cursor = db.cursor() cursor.execute(sql) results = cursor.fetchall() if(len(results) == 0): return None else: users = [] for row in results: id = row[0] login = row[1] firstname = row[2] lastname = row[3] password = row[4] authorization = row[5] user = User(id, login, firstname, lastname, password,authorization) users.append(user) return users finally: db.close()
def test_delete(self): user1 = User("Florent", "Veys", "test123") user2 = User("Adrien", "Test", "test123") user3 = User("Bernard", "Test", "test123") self.userImpl.delete(["firstname"], ["Florent"]) self.cursor.execute("Select * from users") results = self.cursor.fetchall() for row in results: userTest = User(row[1], row[2], row[3]) self.assertNotEqual(userTest, user1) self.userImpl.delete(["firstname"], ["Adrien"]) self.cursor.execute("SELECT * FROM users") results = self.cursor.fetchall() for row in results: userTest2 = User(row[1], row[2], row[3]) self.assertNotEqual(userTest2, user2) userTest3 = User(results[1][1], results[1][2], results[1][3]) self.assertEqual(user3, userTest3)
def setUp(self): self.user = User("Maxime", "Hempte", "test123") self.user2 = User("Maxime", "Hempte", "test123") self.user3 = User("Florent", "Veys", "test123")
class UserTest(unittest.TestCase): def setUp(self): self.user = User("Maxime", "Hempte", "test123") self.user2 = User("Maxime", "Hempte", "test123") self.user3 = User("Florent", "Veys", "test123") def test_eq(self): self.assertTrue(self.user == self.user2) self.assertFalse(self.user == self.user3) def test_nq(self): self.assertTrue(self.user != self.user3) self.assertFalse(self.user != self.user2) def test_getFirstname(self): self.assertEqual("Maxime", self.user.getFirstname()) def test_setFirstname(self): self.user.setFirstname("Florent") self.assertEqual("Florent", self.user.getFirstname()) def test_getLastname(self): self.assertEqual("Hempte", self.user.getLastname()) def test_setLastname(self): self.user.setLastname("Veys") self.assertEqual("Veys", self.user.getLastname()) def test_getPassword(self): self.assertEqual("test123", self.user.getPassword()) def test_setPassword(self): self.user.setPassword("change123") self.assertEqual("change123", self.user.getPassword())
def test_constructValues(self): user = User("Maxime", "Hempte", "test123") testValues = self.userImpl.constructValues(user) self.assertEqual("Maxime", testValues[0]) self.assertEqual("Hempte", testValues[1]) self.assertEqual("test123", testValues[2])