Skip to content

Commit 18cc70f

Browse files
committed
Move blueshirt scope to constant
1 parent 8be3533 commit 18cc70f

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

code_submitter/auth.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22+
BLUESHIRT_SCOPE = 'blueshirt'
23+
2224

2325
class User(SimpleUser):
2426
def __init__(self, username: str, team: Optional[str]) -> None:
@@ -156,7 +158,7 @@ def get_scopes(self, info: NemesisUserInfo) -> List[str]:
156158
scopes = ['authenticated']
157159

158160
if info['is_blueshirt']:
159-
scopes.append('blueshirt')
161+
scopes.append(BLUESHIRT_SCOPE)
160162

161163
return scopes
162164

@@ -227,7 +229,7 @@ def get_scopes(self, username: str) -> List[str]:
227229
scopes = ['authenticated']
228230

229231
if username == self.BLUESHIRT_TEAM:
230-
scopes.append('blueshirt')
232+
scopes.append(BLUESHIRT_SCOPE)
231233

232234
return scopes
233235

@@ -242,6 +244,6 @@ async def validate(self, username: str, password: str) -> ValidationResult:
242244

243245
scopes = self.get_scopes(username)
244246

245-
if 'blueshirt' in scopes:
247+
if BLUESHIRT_SCOPE in scopes:
246248
return scopes, User("SR", None)
247249
return scopes, User(f"Team {username}", username)

code_submitter/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from starlette.middleware.authentication import AuthenticationMiddleware
1616

1717
from . import auth, utils, config
18-
from .auth import User
18+
from .auth import User, BLUESHIRT_SCOPE
1919
from .tables import Archive, ChoiceHistory
2020

2121
database = databases.Database(config.DATABASE_URL, force_rollback=config.TESTING)
@@ -53,6 +53,7 @@ async def homepage(request: Request) -> Response:
5353
'request': request,
5454
'chosen': chosen,
5555
'uploads': uploads,
56+
'BLUESHIRT_SCOPE': BLUESHIRT_SCOPE,
5657
})
5758

5859

@@ -121,7 +122,7 @@ async def upload(request: Request) -> Response:
121122
)
122123

123124

124-
@requires(['authenticated', 'blueshirt'])
125+
@requires(['authenticated', BLUESHIRT_SCOPE])
125126
async def download_submissions(request: Request) -> Response:
126127
buffer = io.BytesIO()
127128
with zipfile.ZipFile(buffer, mode='w') as zf:

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<body>
3232
<div class="container">
3333
<h1>Virtual Competition Code Submission</h1>
34-
{% if 'blueshirt' in request.auth.scopes %}
34+
{% if BLUESHIRT_SCOPE in request.auth.scopes %}
3535
<div class="row">
3636
<div class="col-sm-6">
3737
<a href="{{ url_for('download_submissions') }}">

tests/tests_app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import test_utils
77
from starlette.testclient import TestClient
88

9+
from code_submitter.auth import BLUESHIRT_SCOPE
910
from code_submitter.tables import Archive, ChoiceHistory
1011

1112

@@ -286,7 +287,7 @@ def test_no_download_link_for_non_blueshirt(self) -> None:
286287
self.assertNotIn(download_url, html)
287288

288289
def test_shows_download_link_for_blueshirt(self) -> None:
289-
self.session.auth = ('blueshirt', 'blueshirt')
290+
self.session.auth = (BLUESHIRT_SCOPE, BLUESHIRT_SCOPE)
290291

291292
download_url = self.url_for('download_submissions')
292293

@@ -299,7 +300,7 @@ def test_download_submissions_requires_blueshirt(self) -> None:
299300
self.assertEqual(403, response.status_code)
300301

301302
def test_download_submissions_when_none(self) -> None:
302-
self.session.auth = ('blueshirt', 'blueshirt')
303+
self.session.auth = (BLUESHIRT_SCOPE, BLUESHIRT_SCOPE)
303304

304305
response = self.session.get(self.url_for('download_submissions'))
305306
self.assertEqual(200, response.status_code)
@@ -311,7 +312,7 @@ def test_download_submissions_when_none(self) -> None:
311312
)
312313

313314
def test_download_submissions(self) -> None:
314-
self.session.auth = ('blueshirt', 'blueshirt')
315+
self.session.auth = (BLUESHIRT_SCOPE, BLUESHIRT_SCOPE)
315316

316317
self.await_(self.database.execute(
317318
Archive.insert().values(

tests/tests_auth.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from starlette.applications import Starlette
77
from starlette.authentication import AuthenticationError
88

9-
from code_submitter.auth import FileBackend, NemesisBackend, NemesisUserInfo
9+
from code_submitter.auth import (
10+
FileBackend,
11+
NemesisBackend,
12+
BLUESHIRT_SCOPE,
13+
NemesisUserInfo,
14+
)
1015

1116

1217
class NemesisAuthTests(test_utils.AsyncTestCase):
@@ -91,7 +96,7 @@ async def endpoint(request: Request) -> Response:
9196
self.assertEqual('user', user.username, "Wrong username for user")
9297

9398
self.assertEqual(
94-
['authenticated', 'blueshirt'],
99+
['authenticated', BLUESHIRT_SCOPE],
95100
scopes,
96101
"Wrong scopes for user",
97102
)
@@ -131,7 +136,7 @@ def test_blueshirt(self) -> None:
131136
self.assertEqual('SR', user.username, "Wrong username for user")
132137

133138
self.assertEqual(
134-
['authenticated', 'blueshirt'],
139+
['authenticated', BLUESHIRT_SCOPE],
135140
scopes,
136141
"Wrong scopes for user",
137142
)

0 commit comments

Comments
 (0)