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

Add more tests for contacts

parent 36f2de49
No related branches found
No related tags found
1 merge request!4More test coverage
...@@ -16,21 +16,23 @@ class Contacts(Model): ...@@ -16,21 +16,23 @@ class Contacts(Model):
API_ENDPOINT = "contacts/" API_ENDPOINT = "contacts/"
_on_contact_added_callback = None
def add_pending(self, link, alias): def add_pending(self, link, alias):
url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT + "add/" + "pending/") url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT + "add/pending/")
_post(url, headers=self._headers, json={"link": link, "alias": alias}) _post(url, headers=self._headers, json={"link": link, "alias": alias})
def get(self): def get(self):
url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT) url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT)
request = _get(url, headers=self._headers) request = _get(url, headers=self._headers)
contacts = request.json() contacts = request.json()
contacts.sort(key=itemgetter("lastChatActivity"), contacts = Contacts._sort_contact_list(contacts)
reverse=True)
return contacts return contacts
def get_link(self): def get_link(self):
url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT + "add/" + "link/") url = urljoin(BASE_HTTP_URL, self.API_ENDPOINT + "add/link/")
request = _get(url, headers=self._headers).json() request = _get(url, headers=self._headers).json()
return request['link'] return request['link']
def _sort_contact_list(contacts):
contacts.sort(key=itemgetter("lastChatActivity"),
reverse=True)
return contacts
...@@ -10,14 +10,32 @@ from briar_wrapper.models.contacts import Contacts ...@@ -10,14 +10,32 @@ from briar_wrapper.models.contacts import Contacts
BASE_HTTP_URL = "http://localhost:7000/v1/contacts/" BASE_HTTP_URL = "http://localhost:7000/v1/contacts/"
TEST_LINK = "briar://wvui4uvhbfv4tzo6xwngknebsxrafainnhldyfj63x6ipp4q2vigy"
TEST_ALIAS = "Alice" TEST_ALIAS = "Alice"
TEST_CONTACT_FIRST = {
"lastChatActivity": 1
}
TEST_CONTACT_SECOND = {
"lastChatActivity": 2
}
TEST_CONTACT_RESPONSE_SINGLE = [
TEST_CONTACT_FIRST
]
TEST_CONTACT_RESPONSE_TWO_UNORDERED = [
TEST_CONTACT_FIRST,
TEST_CONTACT_SECOND
]
TEST_CONTACT_RESPONSE_TWO_ORDERED = [
TEST_CONTACT_SECOND,
TEST_CONTACT_FIRST
]
TEST_LINK = "briar://wvui4uvhbfv4tzo6xwngknebsxrafainnhldyfj63x6ipp4q2vigy"
@requests_mock.Mocker(kw="requests_mock") @requests_mock.Mocker(kw="requests_mock")
def test_add_pending(api, request_headers, requests_mock): def test_add_pending(api, request_headers, requests_mock):
contacts = Contacts(api) contacts = Contacts(api)
requests_mock.register_uri("POST", requests_mock.register_uri("POST",
BASE_HTTP_URL + "add/pending/", BASE_HTTP_URL + "add/pending/",
request_headers=request_headers, request_headers=request_headers,
...@@ -25,15 +43,48 @@ def test_add_pending(api, request_headers, requests_mock): ...@@ -25,15 +43,48 @@ def test_add_pending(api, request_headers, requests_mock):
contacts.add_pending(TEST_LINK, TEST_ALIAS) contacts.add_pending(TEST_LINK, TEST_ALIAS)
def match_request_add_pending(request):
return {"alias": TEST_ALIAS, "link": TEST_LINK} == request.json()
@requests_mock.Mocker(kw='requests_mock')
def test_get(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.register_uri("GET", BASE_HTTP_URL,
request_headers=request_headers,
text=json.dumps(TEST_CONTACT_RESPONSE_SINGLE))
assert contacts.get() == TEST_CONTACT_RESPONSE_SINGLE
@requests_mock.Mocker(kw='requests_mock') @requests_mock.Mocker(kw='requests_mock')
def test_get_empty(api, request_headers, requests_mock): def test_get_empty(api, request_headers, requests_mock):
contacts = Contacts(api) contacts = Contacts(api)
response = [] requests_mock.register_uri("GET", BASE_HTTP_URL,
request_headers=request_headers,
text=json.dumps([]))
assert contacts.get() == []
@requests_mock.Mocker(kw='requests_mock')
def test_get_unordered(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.register_uri("GET", BASE_HTTP_URL, requests_mock.register_uri("GET", BASE_HTTP_URL,
request_headers=request_headers, request_headers=request_headers,
text=json.dumps(response)) text=json.dumps(
assert contacts.get() == response TEST_CONTACT_RESPONSE_TWO_UNORDERED)
)
assert contacts.get() == TEST_CONTACT_RESPONSE_TWO_ORDERED
@requests_mock.Mocker(kw='requests_mock')
def test_get_ordered(api, request_headers, requests_mock):
contacts = Contacts(api)
requests_mock.register_uri("GET", BASE_HTTP_URL,
request_headers=request_headers,
text=json.dumps(
TEST_CONTACT_RESPONSE_TWO_ORDERED)
)
assert contacts.get() == TEST_CONTACT_RESPONSE_TWO_ORDERED
@requests_mock.Mocker(kw='requests_mock') @requests_mock.Mocker(kw='requests_mock')
...@@ -45,7 +96,3 @@ def test_get_link(api, request_headers, requests_mock): ...@@ -45,7 +96,3 @@ def test_get_link(api, request_headers, requests_mock):
request_headers=request_headers, request_headers=request_headers,
text=json.dumps(response)) text=json.dumps(response))
assert contacts.get_link() == TEST_LINK 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