Skip to content

Commit

Permalink
#1 tests and implementation for lookup token
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah committed Jun 14, 2021
1 parent dd042bb commit 6aa07bd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
16 changes: 14 additions & 2 deletions lowball_arangodb_authdb/authdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def add_token(self, token_object):
raise TypeError("AuthDB received a non Token Object to add to the database")

try:
existing_token = self.collection[token_object.token_id]
existing_token = self.collection.fetchDocument(token_object.token_id)
except DocumentNotFoundError:
existing_token = None

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

def lookup_token(self, token_id):

pass
try:
token_doc = self.collection.fetchDocument(token_id)
except DocumentNotFoundError:
return None

try:
token = Token(**token_doc.getStore())
return token
except:
token_doc.delete()
return None



def revoke_token(self, token_id):

Expand Down
19 changes: 15 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def mock_filled_token_collection(
# TestMockCollection.__getitem__
# TestMockCollection.fetchDocument

def mock_collection_getitem(self, key):
def mock_collection_getitem(key):

token_dict = token_dict_map.get(key)

Expand All @@ -391,7 +391,7 @@ def mock_collection_getitem(self, key):
else:
raise DocumentNotFoundError("not found")

def mock_collection_fetch_document(self, key, *args, **kwargs):
def mock_collection_fetch_document(key, *args, **kwargs):
token_dict = token_dict_map.get(key)

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


monkeypatch.setattr(TestMockCollection, "fetchDocument", mock_collection_fetch_document)
monkeypatch.setattr(TestMockCollection, "__getitem__", mock_collection_getitem)
monkeypatch.setattr(TestMockCollection, "fetchDocument", Mock(wraps=mock_collection_fetch_document))
monkeypatch.setattr(TestMockCollection, "__getitem__", Mock(wraps=mock_collection_getitem))

@pytest.fixture
def mock_document_save_no_issues(monkeypatch):
Expand All @@ -416,12 +416,23 @@ def mock_document_save_no_issues(monkeypatch):
def mock_document_save_creation_error(monkeypatch):
monkeypatch.setattr(TestMockDocument, "save", Mock(raises=CreationError))

@pytest.fixture
def mock_document_delete(monkeypatch):
monkeypatch.setattr(TestMockDocument, "delete", Mock())

@pytest.fixture
def mock_collection_create_document_all_good(monkeypatch):
monkeypatch.setattr(TestMockCollection, "createDocument", Mock(return_value=TestMockDocument()))


@pytest.fixture
def fetch_document_returns_bad_token_data(mock_filled_token_collection, monkeypatch):

doc = TestMockDocument()
doc.token_json = {
"invalid": "structure"
}
monkeypatch.setattr(TestMockCollection, "fetchDocument", Mock(return_value=doc))

###############
# TOKEN STUFF #
Expand Down
45 changes: 36 additions & 9 deletions tests/test_authdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyArango.collection import Collection, Collection_metaclass
from pyArango.database import Database
from pyArango.document import Document
from lowball.models.authentication_models import Token

class TestAuthDBInit:
"""Tests:
Expand Down Expand Up @@ -301,7 +302,6 @@ def test_failure_when_token_with_token_id_already_exists(self, mock_auth_db, bas
with pytest.raises(ValueError):
authdb.add_token(basic_user1_test_token1)


def test_add_token_calls_create_document_with_token_dictionary_sets_key_and_saves(self,
mock_auth_db,
mock_pyarango,
Expand All @@ -320,25 +320,52 @@ def test_add_token_calls_create_document_with_token_dictionary_sets_key_and_save
lowball_arangodb_authdb.authdb.Document.save.assert_called_once()


class TestLookupToken:

def test_returns_none_when_token_not_found(self,
test_token_id6,
mock_pyarango,
mock_filled_token_collection,
mock_auth_db
):

class TestLookupToken:
authdb = AuthDB()

def test_returns_none_when_token_not_found(self):
token = authdb.lookup_token(test_token_id6)
authdb.collection.fetchDocument.assert_called_once_with(test_token_id6)
assert token is None

pass
def test_deletes_token_if_token_information_cannot_be_loaded_into_token_when_found(self, mock_pyarango,
mock_auth_db,
fetch_document_returns_bad_token_data,
mock_document_delete
):
authdb = AuthDB()

def test_deletes_token_if_token_information_cannot_be_loaded_into_token_when_found(self):
pass
token = authdb.lookup_token("doesntmatter")
assert token is None
authdb.collection.fetchDocument.assert_called_once_with("doesntmatter")
lowball_arangodb_authdb.authdb.Document.delete.assert_called_once()

def test_returns_token_object_when_token_document_found(self):
def test_returns_token_object_when_token_document_found(self,
mock_pyarango,
mock_filled_token_collection,
mock_auth_db,
test_token_id5,
admin_user1_test_token1
):
authdb = AuthDB()

pass
token = authdb.lookup_token(test_token_id5)
assert isinstance(token, Token)
assert token.to_dict() == admin_user1_test_token1.to_dict()

authdb.collection.fetchDocument.assert_called_once_with(test_token_id5)


class TestRevokeToken:

def test_returns_none_when_token_not_found(self):
def test_returns_none(self):
pass

def test_calls_delete_on_token_document_when_found(self):
Expand Down

0 comments on commit 6aa07bd

Please sign in to comment.