This commit is contained in:
1 parent
0cf6f62e23
commit
0c12ce5f33
5 files changed
+109
-1
No files matched your search
@@ -1,9 +1,11 @@
|
||||
import assert from "node:assert/strict";
|
||||
import authHelpers from "../auth.js";
|
||||
|
||||
const { getLoginUrlForPath, shouldShowAdminEntry } = authHelpers;
|
||||
const { getLoginUrlForPath, isUnauthorizedStatus, shouldShowAdminEntry } = authHelpers;
|
||||
|
||||
assert.equal(getLoginUrlForPath("/study.html?video=2"), "./login.html?next=%2Fstudy.html%3Fvideo%3D2");
|
||||
assert.equal(isUnauthorizedStatus(401), true);
|
||||
assert.equal(isUnauthorizedStatus(403), false);
|
||||
assert.equal(shouldShowAdminEntry({ role: "admin" }), true);
|
||||
assert.equal(shouldShowAdminEntry({ role: "user" }), false);
|
||||
assert.equal(shouldShowAdminEntry(null), false);
|
||||
@@ -105,6 +105,66 @@ def test_activation_registration_login_and_reuse_rejection(monkeypatch):
|
||||
raise AssertionError("used activation code should be rejected")
|
||||
|
||||
|
||||
def test_new_user_login_invalidates_previous_bearer_token(monkeypatch):
|
||||
configure_admin(monkeypatch)
|
||||
|
||||
with TempDatabase():
|
||||
db.create_activation_codes(["SL-ABC123-DEF456-GHI789"], "admin")
|
||||
main.api_auth_register(main.RegisterRequest(
|
||||
activationCode="SL-ABC123-DEF456-GHI789",
|
||||
username="student",
|
||||
password="secret123",
|
||||
))
|
||||
|
||||
first_login = main.api_auth_login(main.LoginRequest(username="student", password="secret123"))
|
||||
first_bearer = HTTPAuthorizationCredentials(scheme="Bearer", credentials=first_login["token"])
|
||||
assert main.api_auth_session(main.require_session(first_bearer))["user"]["username"] == "student"
|
||||
|
||||
second_login = main.api_auth_login(main.LoginRequest(username="student", password="secret123"))
|
||||
second_bearer = HTTPAuthorizationCredentials(scheme="Bearer", credentials=second_login["token"])
|
||||
assert main.api_auth_session(main.require_session(second_bearer))["user"]["username"] == "student"
|
||||
|
||||
try:
|
||||
main.require_session(first_bearer)
|
||||
except HTTPException as error:
|
||||
assert error.status_code == 401
|
||||
else:
|
||||
raise AssertionError("old bearer token should be invalid after a newer login")
|
||||
|
||||
|
||||
def test_legacy_bearer_token_works_until_user_logs_in_again(monkeypatch):
|
||||
configure_admin(monkeypatch)
|
||||
|
||||
with TempDatabase():
|
||||
db.create_activation_codes(["SL-ABC123-DEF456-GHI789"], "admin")
|
||||
registered = main.api_auth_register(main.RegisterRequest(
|
||||
activationCode="SL-ABC123-DEF456-GHI789",
|
||||
username="student",
|
||||
password="secret123",
|
||||
))
|
||||
legacy_token = main.create_auth_token(
|
||||
{"username": "student", "role": "user", "userId": registered["user"]["id"]},
|
||||
main.get_token_secret(),
|
||||
)
|
||||
with db.get_connection() as connection:
|
||||
connection.execute(
|
||||
"UPDATE users SET session_token = NULL WHERE id = ?",
|
||||
(registered["user"]["id"],),
|
||||
)
|
||||
|
||||
legacy_bearer = HTTPAuthorizationCredentials(scheme="Bearer", credentials=legacy_token)
|
||||
assert main.api_auth_session(main.require_session(legacy_bearer))["user"]["username"] == "student"
|
||||
|
||||
main.api_auth_login(main.LoginRequest(username="student", password="secret123"))
|
||||
|
||||
try:
|
||||
main.require_session(legacy_bearer)
|
||||
except HTTPException as error:
|
||||
assert error.status_code == 401
|
||||
else:
|
||||
raise AssertionError("legacy token should expire after the account logs in again")
|
||||
|
||||
|
||||
def test_normal_user_cannot_use_admin_dependency(monkeypatch):
|
||||
configure_admin(monkeypatch)
|
||||
|
||||
@@ -137,6 +197,12 @@ if __name__ == "__main__":
|
||||
test_activation_registration_login_and_reuse_rejection(patch)
|
||||
patch.restore()
|
||||
configure_admin(patch)
|
||||
test_new_user_login_invalidates_previous_bearer_token(patch)
|
||||
patch.restore()
|
||||
configure_admin(patch)
|
||||
test_legacy_bearer_token_works_until_user_logs_in_again(patch)
|
||||
patch.restore()
|
||||
configure_admin(patch)
|
||||
test_normal_user_cannot_use_admin_dependency(patch)
|
||||
finally:
|
||||
patch.restore()
|
||||
Reference in new issue
Block a user