Skip to content

Commit 631d428

Browse files
test for user export
1 parent 1074239 commit 631d428

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

backend/tests/api/routes/test_users.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,43 @@ def test_delete_user_me(client: TestClient, db: Session) -> None:
448448
assert user_db is None
449449

450450

451+
def test_export_users_as_superuser(
452+
client: TestClient, superuser_token_headers: dict[str, str], db: Session
453+
) -> None:
454+
# Create a known user so there is at least one row in the export
455+
username = random_email()
456+
password = random_lower_string()
457+
user_in = UserCreate(email=username, password=password)
458+
crud.create_user(session=db, user_create=user_in)
459+
460+
r = client.get(
461+
f"{settings.API_V1_STR}/users/export",
462+
headers=superuser_token_headers,
463+
)
464+
assert r.status_code == 200
465+
assert "text/csv" in r.headers["content-type"]
466+
assert "attachment" in r.headers["content-disposition"]
467+
assert "users.csv" in r.headers["content-disposition"]
468+
469+
lines = r.text.strip().splitlines()
470+
assert lines[0] == "id,email,full_name,is_active,is_superuser,created_at"
471+
assert len(lines) >= 2 # header + at least the user we created
472+
473+
# Verify the created user appears in the CSV
474+
emails_in_csv = [line.split(",")[1] for line in lines[1:]]
475+
assert username in emails_in_csv
476+
477+
478+
def test_export_users_forbidden_for_normal_user(
479+
client: TestClient, normal_user_token_headers: dict[str, str]
480+
) -> None:
481+
r = client.get(
482+
f"{settings.API_V1_STR}/users/export",
483+
headers=normal_user_token_headers,
484+
)
485+
assert r.status_code == 403
486+
487+
451488
def test_delete_user_me_as_superuser(
452489
client: TestClient, superuser_token_headers: dict[str, str]
453490
) -> None:

0 commit comments

Comments
 (0)