Skip to content

Commit e302f3c

Browse files
committed
Ensure tests actually generate expected failures
1 parent a00d184 commit e302f3c

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

tests/auth.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import time
4+
import re
45

56
import pytest
67
import quart
@@ -9,6 +10,10 @@
910
from asfquart.auth import Requirements as R
1011

1112

13+
def _string_to_re(s):
14+
"""convert arbitrary string to fullmatch regex"""
15+
return re.escape(s) + '$'
16+
1217
@pytest.mark.auth
1318
async def test_auth_basics():
1419
app = asfquart.construct("foobar", token_file=None)
@@ -20,10 +25,8 @@ async def requires_session():
2025

2126
# Test with no session, should fail
2227
quart.session = {}
23-
try:
28+
with pytest.raises(asfquart.auth.AuthenticationFailed, match=_string_to_re(R.E_NOT_LOGGED_IN)):
2429
await requires_session()
25-
except asfquart.auth.AuthenticationFailed as e:
26-
assert e.message is R.E_NOT_LOGGED_IN
2730

2831
# Test with session, should work.
2932
quart.session = {app.app_id: {"uts": time.time(), "foo": "bar"}}
@@ -53,17 +56,13 @@ async def requires_mfa():
5356

5457
# Test MFA with no session, should fail exactly like auth_required
5558
quart.session = {}
56-
try:
59+
with pytest.raises(asfquart.auth.AuthenticationFailed, match=_string_to_re(R.E_NOT_LOGGED_IN)):
5760
await requires_mfa()
58-
except asfquart.auth.AuthenticationFailed as e:
59-
assert e.message is R.E_NOT_LOGGED_IN
6061

6162
# Test with session without MFA, should fail.
6263
quart.session = {app.app_id: {"uts": time.time(), "foo": "bar"}}
63-
try:
64+
with pytest.raises(asfquart.auth.AuthenticationFailed, match=_string_to_re(R.E_NO_MFA)):
6465
await requires_mfa()
65-
except asfquart.auth.AuthenticationFailed as e:
66-
assert e.message is R.E_NO_MFA
6766

6867
# Test with session with MFA, should work.
6968
quart.session = {app.app_id: {"uts": time.time(), "foo": "bar", "mfa": True}}
@@ -95,27 +94,21 @@ async def test_member_or_chair_auth():
9594

9695
# Test role with no session, should fail exactly like auth_required
9796
quart.session = {}
98-
try:
97+
with pytest.raises(asfquart.auth.AuthenticationFailed, match=_string_to_re(R.E_NOT_LOGGED_IN)):
9998
await test_committer_auth()
100-
except asfquart.auth.AuthenticationFailed as e:
101-
assert e.message is R.E_NOT_LOGGED_IN
10299

103100
# Test with session , should work
104101
quart.session = {app.app_id: {"uts": time.time(), "foo": "bar"}}
105102
await test_committer_auth()
106103

107104
# Test with a role we don't have, should fail
108-
try:
105+
with pytest.raises(asfquart.auth.AuthenticationFailed, match=_string_to_re(R.E_NOT_MEMBER)):
109106
await test_member_auth()
110-
except asfquart.auth.AuthenticationFailed as e:
111-
assert e.message is R.E_NOT_MEMBER
112107

113108
# Test with for both member and chair, while only being member. should pass on member check, fail on chair
114109
quart.session = {app.app_id: {"uts": time.time(), "foo": "bar", "isMember": True}}
115-
try:
110+
with pytest.raises(asfquart.auth.AuthenticationFailed, match=_string_to_re(R.E_NOT_CHAIR)):
116111
await test_member_and_chair_auth()
117-
except asfquart.auth.AuthenticationFailed as e:
118-
assert e.message is R.E_NOT_CHAIR
119112

120113
# Test for either member of chair, should work as we have chair (but not member)
121114
quart.session = {app.app_id: {"uts": time.time(), "foo": "bar", "isChair": True}}

0 commit comments

Comments
 (0)