132 lines
3.7 KiB
Python
132 lines
3.7 KiB
Python
"""Tests for authentication: registration, login, logout."""
|
|
|
|
|
|
def test_register_page(client):
|
|
resp = client.get("/register")
|
|
assert resp.status_code == 200
|
|
assert b"Create Account" in resp.data
|
|
|
|
|
|
def test_register_success(client):
|
|
resp = client.post("/register", data={
|
|
"username": "testuser",
|
|
"password": "secret123",
|
|
"confirm": "secret123",
|
|
}, follow_redirects=True)
|
|
assert resp.status_code == 200
|
|
assert b"Account created" in resp.data
|
|
|
|
|
|
def test_register_first_user_is_admin(client):
|
|
"""First registered user should be admin."""
|
|
client.post("/register", data={
|
|
"username": "firstuser",
|
|
"password": "secret123",
|
|
"confirm": "secret123",
|
|
})
|
|
from models import get_user_by_username
|
|
user = get_user_by_username("firstuser")
|
|
assert user is not None
|
|
assert user["is_admin"] == 1
|
|
|
|
|
|
def test_register_duplicate_username(client):
|
|
client.post("/register", data={
|
|
"username": "dup",
|
|
"password": "pass1234",
|
|
"confirm": "pass1234",
|
|
})
|
|
client.get("/logout")
|
|
resp = client.post("/register", data={
|
|
"username": "dup",
|
|
"password": "other5678",
|
|
"confirm": "other5678",
|
|
}, follow_redirects=True)
|
|
assert b"Username already taken" in resp.data
|
|
|
|
|
|
def test_register_password_mismatch(client):
|
|
resp = client.post("/register", data={
|
|
"username": "someone",
|
|
"password": "abc12345",
|
|
"confirm": "abc12346",
|
|
}, follow_redirects=True)
|
|
assert b"Passwords do not match" in resp.data
|
|
|
|
|
|
def test_register_short_username(client):
|
|
resp = client.post("/register", data={
|
|
"username": "x",
|
|
"password": "pass1234",
|
|
"confirm": "pass1234",
|
|
}, follow_redirects=True)
|
|
assert b"at least 2 characters" in resp.data
|
|
|
|
|
|
def test_register_short_password(client):
|
|
resp = client.post("/register", data={
|
|
"username": "validname",
|
|
"password": "ab",
|
|
"confirm": "ab",
|
|
}, follow_redirects=True)
|
|
assert b"at least 4 characters" in resp.data
|
|
|
|
|
|
def test_login_page(client):
|
|
resp = client.get("/login")
|
|
assert resp.status_code == 200
|
|
assert b"Log In" in resp.data
|
|
|
|
|
|
def test_login_success(client):
|
|
# Register first
|
|
client.post("/register", data={
|
|
"username": "logintest",
|
|
"password": "mypassword",
|
|
"confirm": "mypassword",
|
|
})
|
|
client.get("/logout")
|
|
# Then login
|
|
resp = client.post("/login", data={
|
|
"username": "logintest",
|
|
"password": "mypassword",
|
|
}, follow_redirects=True)
|
|
assert resp.status_code == 200
|
|
assert b"Today" in resp.data # Dashboard
|
|
|
|
|
|
def test_login_wrong_password(client):
|
|
client.post("/register", data={
|
|
"username": "logintest2",
|
|
"password": "mypassword",
|
|
"confirm": "mypassword",
|
|
})
|
|
client.get("/logout")
|
|
resp = client.post("/login", data={
|
|
"username": "logintest2",
|
|
"password": "wrongpassword",
|
|
}, follow_redirects=True)
|
|
assert b"Invalid username or password" in resp.data
|
|
|
|
|
|
def test_login_nonexistent_user(client):
|
|
resp = client.post("/login", data={
|
|
"username": "nobody",
|
|
"password": "whatever",
|
|
}, follow_redirects=True)
|
|
assert b"Invalid username or password" in resp.data
|
|
|
|
|
|
def test_logout(auth_client):
|
|
resp = auth_client.get("/logout", follow_redirects=True)
|
|
assert resp.status_code == 200
|
|
assert b"Log In" in resp.data # Redirected to login
|
|
|
|
|
|
def test_unauthenticated_redirect(client):
|
|
"""Dashboard and root should redirect to login when not logged in."""
|
|
for path in ["/", "/dashboard", "/history"]:
|
|
resp = client.get(path, follow_redirects=True)
|
|
assert resp.status_code == 200
|
|
assert b"Log In" in resp.data
|