Skip to content

Commit

Permalink
add_assertion: don't assume a random user if not given
Browse files Browse the repository at this point in the history
Fixes: #68

Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed May 22, 2024
1 parent 3f1e397 commit 091b06f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 3 additions & 4 deletions tahrir_api/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,9 @@ def add_invitation(self, badge_id, created_on=None, expires_on=None, created_by_

created_on = created_on or datetime.now(timezone.utc)
expires_on = expires_on or (created_on + timedelta(hours=1))
if self.person_exists(email=created_by_email):
created_by = self.get_person(created_by_email).id
else:
created_by = self.session.query(Person).first().id
if not created_by_email or not self.person_exists(email=created_by_email):
raise ValueError(f"No user with email {created_by_email!r}. Ask them to login first.")
created_by = self.get_person(created_by_email).id

invitation = Invitation(
created_on=created_on,
Expand Down
21 changes: 20 additions & 1 deletion tests/test_dbapi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_add_badges(api):
issuer_id = api.add_issuer("TestOrigin", "TestName", "TestOrg", "TestContact")
api.add_badge(
Expand Down Expand Up @@ -76,9 +79,25 @@ def test_add_invitation(api):
"TestCriteria",
issuer_id,
)
_id = api.add_invitation(badge_id)
_id = api.add_invitation(badge_id, created_by_email="[email protected]")

assert api.invitation_exists(_id)
invitation = api.get_invitation(_id)
assert api.get_person(id=invitation.created_by).email == "[email protected]"


def test_add_invitation_no_created_by(api):
issuer_id = api.add_issuer("TestOrigin", "TestName", "TestOrg", "TestContact")
api.add_person("[email protected]")
badge_id = api.add_badge(
"TestBadge",
"TestImage",
"A test badge for doing unit tests",
"TestCriteria",
issuer_id,
)
with pytest.raises(ValueError):
api.add_invitation(badge_id)


def test_last_login(api, callback_calls):
Expand Down

0 comments on commit 091b06f

Please sign in to comment.