def test_activate_user_by_id(self): """Test method activate_user_by_id & deactivate_user_by_id in User""" #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.deactivate_user_by_id(0) assert str(err.value) == "(404, 'User not found.')" with pytest.raises(DbException) as err: User.activate_user_by_id(0) assert str(err.value) == "(404, 'User not found.')" assert User.deactivate_user_by_id(self.userList[0].id) user = User.get_user_by_id(self.userList[0].id) assert user.id == self.userList[0].id assert user.name == self.userList[0].name assert user.email == self.userList[0].email assert user.active == False assert User.activate_user_by_id(self.userList[0].id) user = User.get_user_by_id(self.userList[0].id) assert user.id == self.userList[0].id assert user.name == self.userList[0].name assert user.email == self.userList[0].email assert user.active == True assert not User.activate_user_by_id(0, False) assert not User.deactivate_user_by_id(0, False)
def setup(self, session): """ SETUP funcion for TestUser class, this function is executed for each function in this TestClass """ user_types = [] user_types.append(UserType("ADMIN", 2)) user_types.append(UserType("GUEST", 0)) user_types.append(UserType("USER", 1)) user_type_db = UserType.query.all() for user in user_type_db: for seedUser in user_types: if (user.name == seedUser.name): user_types.remove(seedUser) for user_type in user_types: session.add(user_type) session.commit() user = User("Admin", "*****@*****.**", "SuperSecret", None, None, True) user.user_type_id = UserType.get_type_by_name('ADMIN').id user.activated = True session.add(user) session.commit() self.userList.append(user) yield session.query(User).delete() session.query(UserType).delete() session.commit() self.userList[:] = []
def test_get_user_type_from_user_id(self, session): """Test method get_user_type_from_user_id in User""" #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.get_user_type_from_user_id(0) assert str(err.value) == "(404, 'User not found.')" assert User.get_user_type_from_user_id( self.userList[0].id).id == self.userList[0].user_type_id
def test_get_user_all(self, session): """Test method get_user_all in User""" users = User.get_user_all() assert len(users) == len(self.userList) assert users[0].id == self.userList[0].id assert users[0].name == self.userList[0].name assert users[0].email == self.userList[0].email assert users[0].password == self.userList[0].password user = User("DUMMY", "*****@*****.**", "hackPr00f", None, None, True) user.user_type_id = UserType.get_type_by_name('USER').id user.activated = True session.add(user) session.commit() self.userList.append(user) users = User.get_user_all() assert len(users) == len(self.userList) session.query(User).delete() session.commit() users = User.get_user_all(False) assert users == [] #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.get_user_all() assert str(err.value) == "(404, 'No user found.')"
def test_get_user_by_id(self): """Test method get_user_by_id in User""" #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.get_user_by_id(0) assert str(err.value) == "(404, 'User not found.')" user = User.get_user_by_id(self.userList[0].id) assert user.id == self.userList[0].id user = User.get_user_by_id(0, False) assert user is None
def delete_user(): """ Deletes user by it's id (only deactivation) """ data = request.get_json() validate_data(data, {'token', 'user_id'}) token = data['token'] user_id = data['user_id'] validate_token(token) user = User.get_user_by_id(user_id) User.deactivate_user_by_id(user.id) data = {"data": "done"} return response_ok(data)
def test_confirm_password(self): """Test method confirm_password in User""" user = User.get_user_by_id(self.userList[0].id) assert user.confirm_password('SuperSecret') assert not user.confirm_password('SuperSecret2') assert not user.confirm_password('SuperSecre2') assert not user.confirm_password('supersecret') assert not user.confirm_password('') assert not user.confirm_password('\\*\\"\"')
def registration_action(): """ Registration """ data = request.get_json() validate_data(data, {'username', 'password', 'email'}) if not validate_email(data['email']): raise ApiException(400, "email") if not validate_username(data['username']): raise ApiException(400, "username") if not validate_password(data['password']): raise ApiException(400, "password") if User.check_unique(data['username'], data['email'], True): userEntity = User(data['username'], data['email'], data['password']) userEntity.save_entity() token = authenticate(None, True, userEntity.id) data = {"user_id": userEntity.id, "token": token.token} return response_ok(data)
def get_user_all_action(): """ Get all user, only for logged users Token is taken from url param """ token = request.args.get('token') validate_token(token) data = User.get_user_all() return response_ok_list(data)
def get_user_by_id_action(): """ Get user data by it's id """ token = request.args.get('token') user_id = request.args.get('user_id') validate_token(token) data = User.get_user_by_id(user_id) return response_ok_obj(data)
def test_get_user_by_username_or_email(self): """Test method get_user_by_username_or_email in User""" #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.get_user_by_email('@') assert str(err.value) == "(404, 'User not found.')" user = User.get_user_by_username_or_email(self.userList[0].name) assert user.id == self.userList[0].id assert user.name == self.userList[0].name assert user.email == self.userList[0].email user = User.get_user_by_username_or_email(self.userList[0].email) assert user.id == self.userList[0].id assert user.name == self.userList[0].name assert user.email == self.userList[0].email user = User.get_user_by_email('@', False) assert user is None
def update_user(): """ Update user with suplied data, now works only for email, password, name and user type """ data = request.get_json() validate_data(data, {'token', 'user_id'}) user_id = data['user_id'] token = data['token'] validate_token(token) user = User.update_user_by_id_from_array(user_id, data) return response_ok_obj(user)
def __userInsert(): userList = [] userAdmin = User("Admin", "*****@*****.**", "SuperSecret", None, None, True) userAdmin.user_type_id = UserType.get_type_by_name('ADMIN').id userAdmin.activated = True userList.append(userAdmin) userEntities = User.query.all() for user in userEntities: for seedUser in userList: if (user.name == seedUser.name): userList.remove(seedUser) for seedUser in userList: db.session.add(seedUser) db.session.commit() print("Inserting: ") print(seedUser.name)
def setup(self, session): """ SETUP funcion for TestUser class, this function is executed for each function in this TestClass """ user_types = [] user_types.append(UserType("ADMIN", 2)) user_types.append(UserType("GUEST", 0)) user_types.append(UserType("USER", 1)) user_type_db = UserType.query.all() for user in user_type_db: for seedUser in user_types: if (user.name == seedUser.name): user_types.remove(seedUser) for user_type in user_types: self.user_type_list.append(user_type) session.add(user_type) session.commit() user = User("Admin", "*****@*****.**", "SuperSecret", None, None, True) user.user_type_id = UserType.get_type_by_name('ADMIN').id user.activated = True session.add(user) session.commit() ttoken = str(user.id).zfill(10) + str(uuid.uuid4()) #Check if token is unique while (UserToken.query.filter_by(token=ttoken).count() != 0): ttoken = str(user.id).zfill(10) + str(uuid.uuid4()) ttoken = UserToken(user_id=user.id, token=ttoken) ttoken.update = datetime.utcnow() ttoken.created = datetime.utcnow() session.add(ttoken) session.commit() self.token_list.append(ttoken.token) self.user_list.append(user.id) user = User("Test", "*****@*****.**", "SuperSecret", None, None, True) session.add(user) session.commit() ttoken = str(user.id).zfill(10) + str(uuid.uuid4()) #Check if token is unique while (UserToken.query.filter_by(token=ttoken).count() != 0): ttoken = str(user.id).zfill(10) + str(uuid.uuid4()) ttoken = UserToken(user_id=user.id, token=ttoken) ttoken.update = datetime.utcnow() ttoken.created = datetime.utcnow() session.add(ttoken) session.commit() self.token_list.append(ttoken.token) self.user_list.append(user.id) yield session.query(User).delete() session.query(UserType).delete() session.query(UserToken).delete() session.commit()
def test_check_unique(self): """Test method check_unique in User""" #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.check_unique('Admin', '@', True) assert str(err.value) == "(400, 'username')" with pytest.raises(DbException) as err: User.check_unique('@', '*****@*****.**', True) assert str(err.value) == "(400, 'email')" assert User.check_unique('@', '@') assert not User.check_unique('Admin', '@') assert not User.check_unique('@', '*****@*****.**')
def setup(self, session): """ SETUP funcion for TestUser class, this function is executed for each function in this TestClass """ user_types = [] user_types.append(UserType("ADMIN", 2)) user_types.append(UserType("GUEST", 0)) user_types.append(UserType("USER", 1)) user_type_db = UserType.query.all() for user in user_type_db: for seedUser in user_types: if (user.name == seedUser.name): user_types.remove(seedUser) for user_type in user_types: session.add(user_type) session.commit() user = User("GUEST", "*****@*****.**", "GUEST", None, None, True) user.user_type_id = UserType.get_type_by_name('GUEST').id user.activated = True user1 = User("USER", "*****@*****.**", "USER", None, None, True) user1.user_type_id = UserType.get_type_by_name('USER').id user1.activated = True user2 = User("Admin", "*****@*****.**", "SuperSecret", None, None, True) user2.user_type_id = UserType.get_type_by_name('ADMIN').id user2.activated = True session.add(user) session.add(user1) session.add(user2) session.commit() self.userList.append(user) self.userList.append(user1) self.userList.append(user2) #make initial project session.add(Project.create_project('DummyProject_01')) session.add(Project.create_project('DummyProject_02')) session.commit() self.projectList = Project.query.all() session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'M11')) session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'M12')) session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'M13')) session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'read')) session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'write')) session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'share')) session.add( Module(Project.get_project_by_name('DummyProject_01').id, 'own')) session.commit() self.moduleList = Module.query.all() perm0 = ModulePerm(self.userList[0].id, self.moduleList[0].id, 15) perm1 = ModulePerm(self.userList[0].id, self.moduleList[1].id, 8) perm2 = ModulePerm(self.userList[0].id, self.moduleList[2].id, 0) session.add(perm0) session.add(perm1) session.add(perm2) session.commit() self.permList.append(perm0) self.permList.append(perm1) self.permList.append(perm2) yield session.query(ModulePerm).delete() self.permList[:] = [] session.query(Module).delete() session.query(Project).delete() session.commit() self.moduleList[:] = [] self.projectList[:] = [] session.query(User).delete() session.query(UserType).delete() session.commit() self.userList[:] = []
def test_update_user_by_id_from_array(self, session): """Test method update_user_by_id_from_array in User""" array = { 'name': 'kektimusMaximusPrime', 'email': '*****@*****.**', 'password': '******', 'user_type_id': 2 } #Test something that really shouldn't be there with pytest.raises(DbException) as err: User.update_user_by_id_from_array(0, array) assert str(err.value) == "(404, 'User not found.')" user = User("DUMMY", "*****@*****.**", "hackPr00f", None, None, True) user.user_type_id = UserType.get_type_by_name('GUEST').id user.activated = True session.add(user) session.commit() self.userList.append(user) assert len(self.userList) == 2 User.update_user_by_id_from_array(self.userList[1].id, array) user = User.get_user_by_id(self.userList[1].id) assert user.id == self.userList[1].id assert user.name == 'kektimusMaximusPrime' assert user.email == '*****@*****.**' assert user.confirm_password('passwd') assert user.user_type_id == 2 array = {} User.update_user_by_id_from_array(self.userList[1].id, array) user = User.get_user_by_id(self.userList[1].id) assert user.id == self.userList[1].id assert user.name == 'kektimusMaximusPrime' assert user.email == '*****@*****.**' assert user.confirm_password('passwd') assert user.user_type_id == 2 array = {'kek': 123, 'token': '01235'} User.update_user_by_id_from_array(self.userList[1].id, array) user = User.get_user_by_id(self.userList[1].id) assert user.id == self.userList[1].id assert user.name == 'kektimusMaximusPrime' assert user.email == '*****@*****.**' assert user.confirm_password('passwd') assert user.user_type_id == 2 array = {'name': 'kektimusMaximusPrime'} with pytest.raises(DbException) as err: User.update_user_by_id_from_array(self.userList[0].id, array) assert str(err.value) == "(400, 'username')" array = {'email': '*****@*****.**'} with pytest.raises(DbException) as err: User.update_user_by_id_from_array(self.userList[0].id, array) assert str(err.value) == "(400, 'email')" array = {'user_type_id': 200} with pytest.raises(DbException) as err: User.update_user_by_id_from_array(self.userList[0].id, array) assert str(err.value) == "(400, 'usertype')" array = {'name': 'kektimusMaximusPrime', 'password': '******'} with pytest.raises(DbException) as err: User.update_user_by_id_from_array(self.userList[0].id, array) assert str(err.value) == "(400, 'username')" user = User.get_user_by_id(self.userList[0].id) assert not user.confirm_password('passwd') array = {'email': '*****@*****.**', 'password': '******'} with pytest.raises(DbException) as err: User.update_user_by_id_from_array(self.userList[0].id, array) assert str(err.value) == "(400, 'email')" user = User.get_user_by_id(self.userList[0].id) assert not user.confirm_password('passwd') array = {'user_type_id': 200, 'password': '******'} with pytest.raises(DbException) as err: User.update_user_by_id_from_array(self.userList[0].id, array) assert str(err.value) == "(400, 'usertype')" user = User.get_user_by_id(self.userList[0].id) assert not user.confirm_password('passwd')