Skip to content

Commit bfee03a

Browse files
committed
Introduce another dummy backend more amenable for testing
This allows us to more easily mock the Nemesis responses a bit more directly, which is easier to use when not trying to test the auth logic specifically.
1 parent 00f37ff commit bfee03a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

code_submitter/auth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,32 @@ async def validate(self, username: str, password: str) -> ValidationResult:
161161
team = self.get_team(info)
162162

163163
return ['authenticated'], User(username, team)
164+
165+
166+
class DummyNemesisBackend(NemesisBackend):
167+
DEFAULT = [
168+
NemesisUserInfo({
169+
'username': 'blueshirt',
170+
'first_name': 'Blue',
171+
'last_name': 'Shirt',
172+
'teams': ['team-SRZ'],
173+
'is_blueshirt': True,
174+
'is_student': False,
175+
'is_team_leader': False,
176+
}),
177+
NemesisUserInfo({
178+
'username': 'competitor',
179+
'first_name': 'Competitor',
180+
'last_name': '',
181+
'teams': ['team-ABC'],
182+
'is_blueshirt': False,
183+
'is_student': False,
184+
'is_team_leader': False,
185+
}),
186+
]
187+
188+
def __init__(self, data: List[NemesisUserInfo] = DEFAULT) -> None:
189+
self.data = {x['username']: x for x in data}
190+
191+
async def load_user(self, username: str, password: str) -> NemesisUserInfo:
192+
return self.data[username]

tests/test_utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sqlalchemy import create_engine
1010
from alembic.config import Config # type: ignore[import]
1111
from starlette.config import environ
12+
from code_submitter.auth import NemesisUserInfo, DummyNemesisBackend
1213

1314
T = TypeVar('T')
1415

@@ -32,8 +33,19 @@ def ensure_database_configured() -> None:
3233
environ['DATABASE_URL'] = url
3334

3435
environ['AUTH_BACKEND'] = json.dumps({
35-
'backend': 'code_submitter.auth.DummyBackend',
36-
'kwargs': {'team': 'SRZ2'},
36+
'backend': 'code_submitter.auth.DummyNemesisBackend',
37+
'kwargs': {'data': [
38+
NemesisUserInfo({
39+
'username': 'test_user',
40+
'first_name': 'Test',
41+
'last_name': 'User',
42+
'teams': ['team-SRZ2'],
43+
'is_blueshirt': False,
44+
'is_student': False,
45+
'is_team_leader': False,
46+
}),
47+
*DummyNemesisBackend.DEFAULT,
48+
]},
3749
})
3850

3951
create_engine(url)

0 commit comments

Comments
 (0)