Skip to content

Commit 6aa07bd

Browse files
author
Isaiah
committed
#1 tests and implementation for lookup token
1 parent dd042bb commit 6aa07bd

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

lowball_arangodb_authdb/authdb.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def add_token(self, token_object):
174174
raise TypeError("AuthDB received a non Token Object to add to the database")
175175

176176
try:
177-
existing_token = self.collection[token_object.token_id]
177+
existing_token = self.collection.fetchDocument(token_object.token_id)
178178
except DocumentNotFoundError:
179179
existing_token = None
180180

@@ -187,7 +187,19 @@ def add_token(self, token_object):
187187

188188
def lookup_token(self, token_id):
189189

190-
pass
190+
try:
191+
token_doc = self.collection.fetchDocument(token_id)
192+
except DocumentNotFoundError:
193+
return None
194+
195+
try:
196+
token = Token(**token_doc.getStore())
197+
return token
198+
except:
199+
token_doc.delete()
200+
return None
201+
202+
191203

192204
def revoke_token(self, token_id):
193205

tests/conftest.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def mock_filled_token_collection(
379379
# TestMockCollection.__getitem__
380380
# TestMockCollection.fetchDocument
381381

382-
def mock_collection_getitem(self, key):
382+
def mock_collection_getitem(key):
383383

384384
token_dict = token_dict_map.get(key)
385385

@@ -391,7 +391,7 @@ def mock_collection_getitem(self, key):
391391
else:
392392
raise DocumentNotFoundError("not found")
393393

394-
def mock_collection_fetch_document(self, key, *args, **kwargs):
394+
def mock_collection_fetch_document(key, *args, **kwargs):
395395
token_dict = token_dict_map.get(key)
396396

397397
if token_dict:
@@ -403,8 +403,8 @@ def mock_collection_fetch_document(self, key, *args, **kwargs):
403403
raise DocumentNotFoundError("not found")
404404

405405

406-
monkeypatch.setattr(TestMockCollection, "fetchDocument", mock_collection_fetch_document)
407-
monkeypatch.setattr(TestMockCollection, "__getitem__", mock_collection_getitem)
406+
monkeypatch.setattr(TestMockCollection, "fetchDocument", Mock(wraps=mock_collection_fetch_document))
407+
monkeypatch.setattr(TestMockCollection, "__getitem__", Mock(wraps=mock_collection_getitem))
408408

409409
@pytest.fixture
410410
def mock_document_save_no_issues(monkeypatch):
@@ -416,12 +416,23 @@ def mock_document_save_no_issues(monkeypatch):
416416
def mock_document_save_creation_error(monkeypatch):
417417
monkeypatch.setattr(TestMockDocument, "save", Mock(raises=CreationError))
418418

419+
@pytest.fixture
420+
def mock_document_delete(monkeypatch):
421+
monkeypatch.setattr(TestMockDocument, "delete", Mock())
422+
419423
@pytest.fixture
420424
def mock_collection_create_document_all_good(monkeypatch):
421425
monkeypatch.setattr(TestMockCollection, "createDocument", Mock(return_value=TestMockDocument()))
422426

423427

428+
@pytest.fixture
429+
def fetch_document_returns_bad_token_data(mock_filled_token_collection, monkeypatch):
424430

431+
doc = TestMockDocument()
432+
doc.token_json = {
433+
"invalid": "structure"
434+
}
435+
monkeypatch.setattr(TestMockCollection, "fetchDocument", Mock(return_value=doc))
425436

426437
###############
427438
# TOKEN STUFF #

tests/test_authdb.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pyArango.collection import Collection, Collection_metaclass
99
from pyArango.database import Database
1010
from pyArango.document import Document
11+
from lowball.models.authentication_models import Token
1112

1213
class TestAuthDBInit:
1314
"""Tests:
@@ -301,7 +302,6 @@ def test_failure_when_token_with_token_id_already_exists(self, mock_auth_db, bas
301302
with pytest.raises(ValueError):
302303
authdb.add_token(basic_user1_test_token1)
303304

304-
305305
def test_add_token_calls_create_document_with_token_dictionary_sets_key_and_saves(self,
306306
mock_auth_db,
307307
mock_pyarango,
@@ -320,25 +320,52 @@ def test_add_token_calls_create_document_with_token_dictionary_sets_key_and_save
320320
lowball_arangodb_authdb.authdb.Document.save.assert_called_once()
321321

322322

323+
class TestLookupToken:
323324

325+
def test_returns_none_when_token_not_found(self,
326+
test_token_id6,
327+
mock_pyarango,
328+
mock_filled_token_collection,
329+
mock_auth_db
330+
):
324331

325-
class TestLookupToken:
332+
authdb = AuthDB()
326333

327-
def test_returns_none_when_token_not_found(self):
334+
token = authdb.lookup_token(test_token_id6)
335+
authdb.collection.fetchDocument.assert_called_once_with(test_token_id6)
336+
assert token is None
328337

329-
pass
338+
def test_deletes_token_if_token_information_cannot_be_loaded_into_token_when_found(self, mock_pyarango,
339+
mock_auth_db,
340+
fetch_document_returns_bad_token_data,
341+
mock_document_delete
342+
):
343+
authdb = AuthDB()
330344

331-
def test_deletes_token_if_token_information_cannot_be_loaded_into_token_when_found(self):
332-
pass
345+
token = authdb.lookup_token("doesntmatter")
346+
assert token is None
347+
authdb.collection.fetchDocument.assert_called_once_with("doesntmatter")
348+
lowball_arangodb_authdb.authdb.Document.delete.assert_called_once()
333349

334-
def test_returns_token_object_when_token_document_found(self):
350+
def test_returns_token_object_when_token_document_found(self,
351+
mock_pyarango,
352+
mock_filled_token_collection,
353+
mock_auth_db,
354+
test_token_id5,
355+
admin_user1_test_token1
356+
):
357+
authdb = AuthDB()
335358

336-
pass
359+
token = authdb.lookup_token(test_token_id5)
360+
assert isinstance(token, Token)
361+
assert token.to_dict() == admin_user1_test_token1.to_dict()
362+
363+
authdb.collection.fetchDocument.assert_called_once_with(test_token_id5)
337364

338365

339366
class TestRevokeToken:
340367

341-
def test_returns_none_when_token_not_found(self):
368+
def test_returns_none(self):
342369
pass
343370

344371
def test_calls_delete_on_token_document_when_found(self):

0 commit comments

Comments
 (0)