Skip to content
Snippets Groups Projects
Commit 23952bb8 authored by Nico's avatar Nico
Browse files

Add method in API wrapper for pending contacts

parent ea0f0890
No related branches found
No related tags found
No related merge requests found
......@@ -5,15 +5,26 @@
from urllib.parse import urljoin
from requests import get as _get
from requests import post as _post
from briar.api.constants import BASE_HTTP_URL
from briar.api.model import Model
# TODO: remove pylint disable once we have more methods
class Contacts(Model): # pylint: disable=too-few-public-methods
class Contacts(Model):
API_ENDPOINT = "contacts/"
def add_pending(self, link, alias):
url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT + "add/" + "pending/")
_post(url, headers=self._headers, json={"link": link, "alias": alias})
def get(self):
url = urljoin(BASE_HTTP_URL, 'contacts')
url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT)
request = _get(url, headers=self._headers)
return request.json()
def get_link(self):
url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT + "add/" + "link/")
request = _get(url, headers=self._headers).json()
return request['link']
......@@ -8,13 +8,44 @@ import requests_mock
from briar.api.models.contacts import Contacts
BASE_HTTP_URL = "http://localhost:7000/v1/contacts/"
TEST_LINK = "briar://wvui4uvhbfv4tzo6xwngknebsxrafainnhldyfj63x6ipp4q2vigy"
TEST_ALIAS = "Alice"
@requests_mock.Mocker(kw="requests_mock")
def test_add_pending(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.register_uri("POST",
BASE_HTTP_URL + "add/pending/",
request_headers=request_headers,
additional_matcher=match_request_add_pending)
contacts.add_pending(TEST_LINK, TEST_ALIAS)
@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",
requests_mock.register_uri("GET", BASE_HTTP_URL,
request_headers=request_headers,
text=json.dumps(response))
assert contacts.get() == response
@requests_mock.Mocker(kw='requests_mock')
def test_get_link(api, request_headers, requests_mock):
contacts = Contacts(api)
response = {"link": TEST_LINK}
requests_mock.register_uri("GET", BASE_HTTP_URL + "add/link/",
request_headers=request_headers,
text=json.dumps(response))
assert contacts.get_link() == TEST_LINK
def match_request_add_pending(request):
return {"alias": TEST_ALIAS, "link": TEST_LINK} == request.json()
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