diff --git a/tests/briar/api/models/test_contacts.py b/tests/briar/api/models/test_contacts.py index b3498fe98cd36b38b431dbd952ac8d64641ecda4..70f35c99193ac120131deb2ffed2fb116ee0c2aa 100644 --- a/tests/briar/api/models/test_contacts.py +++ b/tests/briar/api/models/test_contacts.py @@ -3,39 +3,42 @@ # License-Filename: LICENSE.md import json +from random import choices import requests_mock -from unittest import mock, TestCase +from string import ascii_letters, digits +from unittest import mock + +import pytest from briar.api.models.contacts import Contacts -class TestContacts(TestCase): +@requests_mock.Mocker(kw='requests_mock') +def test_get_empty(api, request_headers, requests_mock): + contacts = Contacts(api) + response = [] + + requests_mock.register_uri("GET", "http://localhost:7000/v1/contacts", + request_headers=request_headers, + text=json.dumps(response)) + assert contacts.get() == response - # TODO: randomly generate token - AUTH_TOKEN = "xxhQhFNdq5R2YcipNcT3uYoMyOT2PxBl3tpWiO5Qy65w=" - @requests_mock.Mocker() - def test_get_empty(self, requests_mock): - api = TestContacts._get_mocked_api() - request_headers = TestContacts._get_mocked_request_headers() +@pytest.fixture +def api(auth_token): + api = mock.Mock() + api.auth_token = auth_token + return api - contacts = Contacts(api) - response = list() - requests_mock.register_uri("GET", "http://localhost:7000/v1/contacts", - request_headers=request_headers, - text=json.dumps(response)) - assert contacts.get() == response +@pytest.fixture +def request_headers(auth_token): + request_headers = { + "Authorization": 'Bearer %s' % auth_token + } + return request_headers - # TODO: Use pytest fixtures - def _get_mocked_api(): - api = mock.Mock() - api.auth_token = TestContacts.AUTH_TOKEN - return api - # TODO: Use pytest fixtures - def _get_mocked_request_headers(): - request_headers = {} - authorization_header = 'Bearer %s' % TestContacts.AUTH_TOKEN - request_headers["Authorization"] = authorization_header - return request_headers +@pytest.fixture +def auth_token(): + return ''.join(choices(ascii_letters + digits, k=33)) diff --git a/tests/briar/api/test_constants.py b/tests/briar/api/test_constants.py index 9ddf5ff94d815761ddaaa5f66a1e23592226c933..45376bcf68ec7fd9ad2d7ee6e52a85026c93c54c 100644 --- a/tests/briar/api/test_constants.py +++ b/tests/briar/api/test_constants.py @@ -4,28 +4,29 @@ from os.path import join from pathlib import Path -from unittest import TestCase + +import pytest from briar.api.constants import BASE_HTTP_URL, BRIAR_AUTH_TOKEN from briar.api.constants import BRIAR_DB, WEBSOCKET_URL -class TestConstants(TestCase): +def test_base_http_url(): + assert BASE_HTTP_URL == "http://localhost:7000/v1/" + + +def test_briar_auth_token(briar_dir): + assert BRIAR_AUTH_TOKEN == join(briar_dir, "auth_token") + - def test_base_http_url(self): - assert BASE_HTTP_URL == "http://localhost:7000/v1/" +def test_briar_db(briar_dir): + assert BRIAR_DB == join(briar_dir, "db", "db.mv.db") - def test_briar_auth_token(self): - briar_dir = TestConstants._get_briar_dir() - assert BRIAR_AUTH_TOKEN == join(briar_dir, "auth_token") - def test_briar_db(self): - briar_dir = TestConstants._get_briar_dir() - assert BRIAR_DB == join(briar_dir, "db", "db.mv.db") +def test_websocket_url(): + assert WEBSOCKET_URL == "ws://localhost:7000/v1/ws" - def test_websocket_url(self): - assert WEBSOCKET_URL == "ws://localhost:7000/v1/ws" - # TODO: Use pytest fixtures - def _get_briar_dir(): - return join(str(Path.home()), ".briar") +@pytest.fixture +def briar_dir(): + return join(str(Path.home()), ".briar") diff --git a/tests/briar/gtk/test_define.py b/tests/briar/gtk/test_define.py index 24ecb68d6690a31d5dd7b580f7d06173373c02a5..9be75b70e591b1991b802cac882d2b585c587143 100644 --- a/tests/briar/gtk/test_define.py +++ b/tests/briar/gtk/test_define.py @@ -2,14 +2,10 @@ # SPDX-License-Identifier: AGPL-3.0-only # License-Filename: LICENSE.md -from unittest import TestCase - from gi.repository import Gio from briar.gtk.define import App -class TestDefine(TestCase): - - def test_app(self): - assert App == Gio.Application.get_default +def test_app(): + assert App == Gio.Application.get_default