def test_create_user_bad_email(self): with pytest.raises(ValidationError): UserService.create_user( username='******', password='******', first_name=123, last_name='last', institution='university', email='not_an_email', )
def put(self, username): # get the json from the request, it's already validated req_json = request.get_json(force=True) # is the id specified if not id: return jsonify(message=strings.ENTITY_NO_ID), 400 # try and get the object from the database try: self.resource_type.objects.get(id) # if we git more than one, something is horribly wrong except self.entity_type.MultipleObjectsReturned: return jsonify(message=strings.ENTITY_MULTIPLE_RESULTS), 500 # if it doesn't exist, inform the user except self.entity_type.DoesNotExist: return jsonify(message=strings.ENTITY_NOT_FOUND), 404 # if everything went smoothly, update the entity UserService.update_user(req_json) # indicate success return jsonify(message=strings.API_SUCCESS), 200
def test_returns_none_when_user_does_not_exist(self): User( username='******', first_name='first', last_name='last', salt="$2a$12$DG39IJLyK/8DQ18Zz/GclO", password= "******", institution='university', email='*****@*****.**', ).save() user = UserService.get_user('not_a_username') assert user is None
def attempt_login(cls, username, password): user = UserService.get_user(username) if not user: return None hashed_pass = hash_password(password, user.salt) if hashed_pass != user.password: return None session = SessionService.get_session_by_user(user) if session: return session.token token = gen_token() SessionService.create_session(user, token) return token
def test_create_valid_user(self): user = UserService.create_user( username='******', password='******', first_name='first', last_name='last', institution='university', email='*****@*****.**', ) assert user is not None db_user = User.objects.get(username='******') assert db_user.username == 'username' assert db_user.first_name == 'first' assert db_user.last_name == 'last' assert db_user.institution == 'university' assert db_user.email == '*****@*****.**'
def get(self, username): if not username: return jsonify(message=strings.USER_NO_USERNAME), 400 # try and get the object from the database try: user = UserService.get_user(username) # if we get more than one, something is horribly wrong except User.MultipleObjectsReturned: res = jsonify(message=strings.ENTITY_MULTIPLE_RESULTS) res.status_code = 500 # if it doesn't exist, inform the user except User.DoesNotExist: res = jsonify(message=strings.ENTITY_NOT_FOUND) res.status_code = 404 return res # return the object json = UserViewMapper.to_json_from_model(user) res = jsonify(json) res.status_code = 200 return res