2018-04-27 17:16:43 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from werkzeug.exceptions import Unauthorized
|
|
|
|
|
2018-05-16 13:23:48 +00:00
|
|
|
from ereuse_devicehub.client import Client, UserClient
|
2018-04-27 17:16:43 +00:00
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
|
|
|
from tests.conftest import create_user
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-04-27 17:16:43 +00:00
|
|
|
def test_authenticate_success(app: Devicehub):
|
|
|
|
"""Checks the authenticate method."""
|
|
|
|
with app.app_context():
|
|
|
|
user = create_user()
|
|
|
|
response_user = app.auth.authenticate(token=str(user.token))
|
|
|
|
assert response_user == user
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-04-27 17:16:43 +00:00
|
|
|
def test_authenticate_error(app: Devicehub):
|
|
|
|
"""Tests the authenticate method with wrong token values."""
|
|
|
|
with app.app_context():
|
|
|
|
create_user()
|
|
|
|
# Token doesn't exist
|
2022-09-26 15:08:40 +00:00
|
|
|
with pytest.raises(Unauthorized):
|
2018-04-27 17:16:43 +00:00
|
|
|
app.auth.authenticate(token=str(uuid4()))
|
2022-09-26 15:08:40 +00:00
|
|
|
pytest.fail('Provide a suitable token.')
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
def test_authenticate_error_malformed_token(app: Devicehub):
|
|
|
|
"""Tests the authenticate method with malformed token."""
|
|
|
|
with app.app_context():
|
|
|
|
create_user()
|
2018-04-27 17:16:43 +00:00
|
|
|
# Wrong token format
|
2022-09-26 15:08:40 +00:00
|
|
|
with pytest.raises(Unauthorized):
|
2018-04-27 17:16:43 +00:00
|
|
|
app.auth.authenticate(token='this is a wrong uuid')
|
2022-09-26 15:08:40 +00:00
|
|
|
pytest.fail('Provide a suitable token.')
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-04-27 17:16:43 +00:00
|
|
|
def test_auth_view(user: UserClient, client: Client):
|
|
|
|
"""Tests authentication at endpoint / view."""
|
|
|
|
user.get(res='User', item=user.user['id'], status=200)
|
|
|
|
client.get(res='User', item=user.user['id'], status=Unauthorized)
|
2022-09-26 15:08:40 +00:00
|
|
|
client.get(
|
|
|
|
res='User', item=user.user['id'], token='wrong token', status=Unauthorized
|
|
|
|
)
|