Skip to content
Snippets Groups Projects
Commit 2084f3fd authored by Nico's avatar Nico
Browse files

Add unit tests

parent fa408b12
No related branches found
Tags 0.0.7
No related merge requests found
Pipeline #5948 failed
......@@ -4,7 +4,7 @@
"""
Wrapper around Briar API's _/contacts/_ resource
"""
from json.decoder import JSONDecodeError
from operator import itemgetter
from typing import Callable, List
from urllib.parse import urljoin
......@@ -160,7 +160,14 @@ class Contacts(Model):
@staticmethod
def _handle_add_pending_error(response):
error = response.json()
error = dict()
try:
error = response.json()
except JSONDecodeError:
raise BriarWrapperException(response,
"An unknown error occurred while"
"adding a pending contact: "
"JSONDecodeError")
if response.status_code == 400:
if error["error"] == "INVALID_PUBLIC_KEY":
raise PendingContactInvalidPublicKeyException(response)
......
......@@ -6,6 +6,15 @@ import json
import pytest
import requests_mock
from briar_wrapper.exception import BriarWrapperException
from briar_wrapper.exceptions.pending_already_exists_contact import \
PendingContactAlreadyExistsContact
from briar_wrapper.exceptions.pending_already_exists_pending_contact import \
PendingContactAlreadyExistsPendingContact
from briar_wrapper.exceptions.pending_invalid_link import \
PendingContactInvalidLinkException
from briar_wrapper.exceptions.pending_invalid_public_key import \
PendingContactInvalidPublicKeyException
from briar_wrapper.models.contacts import Contacts
from briar_wrapper.models.socket_listener import SocketListener
......@@ -14,6 +23,7 @@ BASE_HTTP_URL = "http://localhost:7000/v1/contacts/"
TEST_ALIAS = "Alice"
TEST_CONTACT_ID = 42
TEST_PENDING_CONTACT_ID = "jsTgWcsEQ2g9rnomeK1g/hmO8M1Ix6ZIGWAjgBtlS9U="
TEST_CONTACT_FIRST = {
"lastChatActivity": 1
......@@ -50,6 +60,86 @@ def match_request_add_pending(request):
return {"alias": TEST_ALIAS, "link": TEST_LINK} == request.json()
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending_invalid_public_key(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.post(BASE_HTTP_URL + "add/pending/",
status_code=400,
json={"error": "INVALID_PUBLIC_KEY"})
with pytest.raises(PendingContactInvalidPublicKeyException):
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending_invalid_link(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.post(BASE_HTTP_URL + "add/pending/",
status_code=400,
json={"error": "INVALID_LINK"})
with pytest.raises(PendingContactInvalidLinkException):
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending_contact_exists(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.post(BASE_HTTP_URL + "add/pending/",
status_code=403,
json={
"error": "CONTACT_EXISTS",
"remoteAuthorName": TEST_ALIAS
})
with pytest.raises(PendingContactAlreadyExistsContact):
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending_pending_exists(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.post(BASE_HTTP_URL + "add/pending/",
status_code=403,
json={
"error": "PENDING_EXISTS",
"pendingContactAlias": TEST_ALIAS,
"pendingContactId": TEST_PENDING_CONTACT_ID
})
with pytest.raises(PendingContactAlreadyExistsPendingContact):
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending_unknown_error(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.post(BASE_HTTP_URL + "add/pending/",
status_code=500,
json={"error": "CRAZY_ERROR"})
with pytest.raises(BriarWrapperException):
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending_no_json(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.post(BASE_HTTP_URL + "add/pending/",
status_code=500)
with pytest.raises(BriarWrapperException):
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@requests_mock.Mocker(kw="requests_mock")
def test_delete_pending(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.register_uri("DELETE",
BASE_HTTP_URL + "add/pending/",
request_headers=request_headers,
additional_matcher=match_request_delete_pending)
contacts.delete_pending(TEST_PENDING_CONTACT_ID)
def match_request_delete_pending(request):
return {"pendingContactId": TEST_PENDING_CONTACT_ID} == request.json()
@requests_mock.Mocker(kw="requests_mock")
def test_set_alias(api, request_headers, requests_mock):
contacts = Contacts(api)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment