async def test_token_invalid_data(data_payload, client): password_hash = pwd_context.hash("abc123") await UserFactory().create(username="******", password=password_hash) response = client.post("/token", data=data_payload) assert response.status_code == status.HTTP_401_UNAUTHORIZED assert response.json() == {"detail": "Incorrect username or password"}
async def test_token_success_creation(client): password_hash = pwd_context.hash("abc123") await UserFactory().create(username="******", password=password_hash) response = client.post("/token", data={"username": "******", "password": "******"}) assert response.status_code == status.HTTP_200_OK assert "access_token" in response.json().keys()
async def test_authenticate_user(client): password_hash = pwd_context.hash("abc123") new_user = await UserFactory().create(username="******", password=password_hash) RequestFormMock = namedtuple("OAuth2PasswordRequestForm", ["username", "password"]) form_data = RequestFormMock("username", "abc123") user = await authenticate_user(form_data) assert user.id == new_user.id
async def test_authenticate_user_ivalid_username(client): password_hash = pwd_context.hash("abc123") await UserFactory().create(username="******", password=password_hash) RequestFormMock = namedtuple("OAuth2PasswordRequestForm", ["username", "password"]) form_data = RequestFormMock("username", "abc123") with pytest.raises(HTTPException) as exinfo: await authenticate_user(form_data) assert "Incorrect username or password" == exinfo.value.detail
def signup(): '''Authenticate user and return token ''' if not request.is_json: return make_response( jsonify(msg='Missing JSON in request'), 400) schema = UserSchema() user, errors = schema.load(request.json) if errors: return jsonify(errors), 422 try: user.passwd_digest = pwd_context.hash(user.passwd_digest) user.save() except NotUniqueError as e: return make_response( jsonify(msg='User exists with under that email/username'), 422 ) return schema.jsonify(user)
def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.password = pwd_context.hash(self.password)
def password(self, value): self._password = pwd_context.hash(value)
def clean(self, **kwargs): self.password = pwd_context.hash(self.password)
async def test_token_creation(mock_encode, client): mock_encode.return_value = "mock_hash" password_hash = pwd_context.hash("abc123") new_user = await UserFactory().create(username="******", password=password_hash) token = Token(new_user) assert token.create() == {"access_token": "mock_hash", "token_type": "bearer"}