Commit 929ead31 authored by Nico Alt's avatar Nico Alt

Use pytest fixtures and no more unittest TestCases

Also randomly generate an auth_token now.
parent c86a3f8a
Pipeline #3550 failed with stage
in 4 minutes and 3 seconds
......@@ -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))
......@@ -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")
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment