def post(username, password): if username is None or password is None: abort( 400, message="Missing arguments." ) has_user = UserModel \ .query \ .filter(UserModel.username == username) \ .first() if has_user is not None: abort( 400, message="User with username: "******" already exist." ) user = UserModel(username=username) user.hash_password(password) user.save() return ({'id': user.id, 'username': user.username}, 201)
def post(self): data = parser.parse_args() if User.query.filter_by(username=data['username']).scalar(): response = jsonify({ 'message': 'The user {} already exists.'.format(data['username']) }) response.status_code = 409 return response new_user = User(username=data['username'], password=User.generate_hash(data['password'])) new_user.save() access_token = create_access_token(identity=data['username']) refresh_token = create_refresh_token(identity=data['username']) return { 'message': 'User {} was created.'.format(data['username']), 'access_token': access_token, 'refresh_token': refresh_token }
def test_new_user_with_same_name(init_db, new_user): new_user.save() same_username = User(username=new_user.username, password='******') with pytest.raises(IntegrityError): same_username.save()
def test_save(self, mock_add, mock_commit): user = UserModel() user.username = "******" user.id = 12 user.save()