test_auth_api.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. """Integration tests for Authentication API endpoints.
  2. Tests the full request/response cycle for /api/v1/auth/ and /api/v1/users/ endpoints.
  3. """
  4. import pytest
  5. from httpx import AsyncClient
  6. class TestAuthStatusAPI:
  7. """Integration tests for /api/v1/auth/status endpoint."""
  8. @pytest.mark.asyncio
  9. @pytest.mark.integration
  10. async def test_get_auth_status_disabled(self, async_client: AsyncClient):
  11. """Verify auth status returns disabled when not configured."""
  12. response = await async_client.get("/api/v1/auth/status")
  13. assert response.status_code == 200
  14. result = response.json()
  15. assert "auth_enabled" in result
  16. assert result["auth_enabled"] is False
  17. assert result["requires_setup"] is True
  18. class TestAuthSetupAPI:
  19. """Integration tests for /api/v1/auth/setup endpoint."""
  20. @pytest.mark.asyncio
  21. @pytest.mark.integration
  22. async def test_setup_auth_disabled(self, async_client: AsyncClient):
  23. """Verify auth can be set up with auth disabled (no password required)."""
  24. response = await async_client.post(
  25. "/api/v1/auth/setup",
  26. json={"auth_enabled": False},
  27. )
  28. assert response.status_code == 200
  29. result = response.json()
  30. assert result["auth_enabled"] is False
  31. assert result["admin_created"] is False
  32. @pytest.mark.asyncio
  33. @pytest.mark.integration
  34. async def test_setup_auth_enabled_requires_credentials(self, async_client: AsyncClient):
  35. """Verify enabling auth requires admin username and password."""
  36. response = await async_client.post(
  37. "/api/v1/auth/setup",
  38. json={"auth_enabled": True},
  39. )
  40. assert response.status_code == 400
  41. assert "Admin username and password are required" in response.json()["detail"]
  42. @pytest.mark.asyncio
  43. @pytest.mark.integration
  44. async def test_setup_auth_enabled_with_credentials(self, async_client: AsyncClient):
  45. """Verify auth can be enabled with admin credentials."""
  46. response = await async_client.post(
  47. "/api/v1/auth/setup",
  48. json={
  49. "auth_enabled": True,
  50. "admin_username": "testadmin",
  51. "admin_password": "testpassword123",
  52. },
  53. )
  54. assert response.status_code == 200
  55. result = response.json()
  56. assert result["auth_enabled"] is True
  57. assert result["admin_created"] is True
  58. class TestAuthLoginAPI:
  59. """Integration tests for /api/v1/auth/login endpoint."""
  60. @pytest.mark.asyncio
  61. @pytest.mark.integration
  62. async def test_login_auth_disabled(self, async_client: AsyncClient):
  63. """Verify login fails when auth is not enabled."""
  64. response = await async_client.post(
  65. "/api/v1/auth/login",
  66. json={"username": "admin", "password": "password"},
  67. )
  68. assert response.status_code == 400
  69. assert "Authentication is not enabled" in response.json()["detail"]
  70. @pytest.mark.asyncio
  71. @pytest.mark.integration
  72. async def test_login_success(self, async_client: AsyncClient):
  73. """Verify login succeeds with valid credentials after setup."""
  74. # First enable auth
  75. await async_client.post(
  76. "/api/v1/auth/setup",
  77. json={
  78. "auth_enabled": True,
  79. "admin_username": "logintest",
  80. "admin_password": "loginpassword123",
  81. },
  82. )
  83. # Now login
  84. response = await async_client.post(
  85. "/api/v1/auth/login",
  86. json={"username": "logintest", "password": "loginpassword123"},
  87. )
  88. assert response.status_code == 200
  89. result = response.json()
  90. assert "access_token" in result
  91. assert result["token_type"] == "bearer"
  92. assert result["user"]["username"] == "logintest"
  93. assert result["user"]["role"] == "admin"
  94. @pytest.mark.asyncio
  95. @pytest.mark.integration
  96. async def test_login_invalid_credentials(self, async_client: AsyncClient):
  97. """Verify login fails with invalid credentials."""
  98. # First enable auth
  99. await async_client.post(
  100. "/api/v1/auth/setup",
  101. json={
  102. "auth_enabled": True,
  103. "admin_username": "invalidtest",
  104. "admin_password": "correctpassword",
  105. },
  106. )
  107. # Try login with wrong password
  108. response = await async_client.post(
  109. "/api/v1/auth/login",
  110. json={"username": "invalidtest", "password": "wrongpassword"},
  111. )
  112. assert response.status_code == 401
  113. assert "Incorrect username or password" in response.json()["detail"]
  114. class TestAuthMeAPI:
  115. """Integration tests for /api/v1/auth/me endpoint."""
  116. @pytest.mark.asyncio
  117. @pytest.mark.integration
  118. async def test_me_without_token(self, async_client: AsyncClient):
  119. """Verify /me fails without authentication token."""
  120. response = await async_client.get("/api/v1/auth/me")
  121. assert response.status_code == 401
  122. @pytest.mark.asyncio
  123. @pytest.mark.integration
  124. async def test_me_with_valid_token(self, async_client: AsyncClient):
  125. """Verify /me returns user info with valid token."""
  126. # Setup and login
  127. await async_client.post(
  128. "/api/v1/auth/setup",
  129. json={
  130. "auth_enabled": True,
  131. "admin_username": "metest",
  132. "admin_password": "mepassword123",
  133. },
  134. )
  135. login_response = await async_client.post(
  136. "/api/v1/auth/login",
  137. json={"username": "metest", "password": "mepassword123"},
  138. )
  139. token = login_response.json()["access_token"]
  140. # Get current user
  141. response = await async_client.get(
  142. "/api/v1/auth/me",
  143. headers={"Authorization": f"Bearer {token}"},
  144. )
  145. assert response.status_code == 200
  146. result = response.json()
  147. assert result["username"] == "metest"
  148. assert result["role"] == "admin"
  149. assert result["is_active"] is True
  150. @pytest.mark.asyncio
  151. @pytest.mark.integration
  152. async def test_me_with_api_key_bearer(self, async_client: AsyncClient, db_session):
  153. """Verify /me returns synthetic admin user when using API key via Bearer token."""
  154. from backend.app.core.auth import generate_api_key
  155. from backend.app.models.api_key import APIKey
  156. # Create an API key directly in the database
  157. full_key, key_hash, key_prefix = generate_api_key()
  158. api_key = APIKey(name="test-kiosk", key_hash=key_hash, key_prefix=key_prefix, enabled=True)
  159. db_session.add(api_key)
  160. await db_session.commit()
  161. # Call /me with the API key as Bearer token
  162. response = await async_client.get(
  163. "/api/v1/auth/me",
  164. headers={"Authorization": f"Bearer {full_key}"},
  165. )
  166. assert response.status_code == 200
  167. result = response.json()
  168. assert result["id"] == 0
  169. assert result["username"].startswith("api-key:")
  170. assert result["role"] == "admin"
  171. assert result["is_admin"] is True
  172. assert result["is_active"] is True
  173. assert len(result["permissions"]) > 0
  174. @pytest.mark.asyncio
  175. @pytest.mark.integration
  176. async def test_me_with_api_key_header(self, async_client: AsyncClient, db_session):
  177. """Verify /me returns synthetic admin user when using X-API-Key header."""
  178. from backend.app.core.auth import generate_api_key
  179. from backend.app.models.api_key import APIKey
  180. full_key, key_hash, key_prefix = generate_api_key()
  181. api_key = APIKey(name="test-kiosk-header", key_hash=key_hash, key_prefix=key_prefix, enabled=True)
  182. db_session.add(api_key)
  183. await db_session.commit()
  184. response = await async_client.get(
  185. "/api/v1/auth/me",
  186. headers={"X-API-Key": full_key},
  187. )
  188. assert response.status_code == 200
  189. result = response.json()
  190. assert result["id"] == 0
  191. assert result["username"].startswith("api-key:")
  192. assert result["is_admin"] is True
  193. @pytest.mark.asyncio
  194. @pytest.mark.integration
  195. async def test_me_with_invalid_api_key(self, async_client: AsyncClient):
  196. """Verify /me rejects invalid API key."""
  197. response = await async_client.get(
  198. "/api/v1/auth/me",
  199. headers={"Authorization": "Bearer bb_invalid_key_value"},
  200. )
  201. assert response.status_code == 401
  202. class TestUsersAPI:
  203. """Integration tests for /api/v1/users/ endpoints."""
  204. @pytest.fixture
  205. async def auth_token(self, async_client: AsyncClient):
  206. """Setup auth and return admin token."""
  207. await async_client.post(
  208. "/api/v1/auth/setup",
  209. json={
  210. "auth_enabled": True,
  211. "admin_username": "usersadmin",
  212. "admin_password": "adminpassword123",
  213. },
  214. )
  215. login_response = await async_client.post(
  216. "/api/v1/auth/login",
  217. json={"username": "usersadmin", "password": "adminpassword123"},
  218. )
  219. return login_response.json()["access_token"]
  220. @pytest.mark.asyncio
  221. @pytest.mark.integration
  222. async def test_list_users_requires_auth(self, async_client: AsyncClient):
  223. """Verify listing users requires authentication when auth is enabled."""
  224. # First enable auth
  225. await async_client.post(
  226. "/api/v1/auth/setup",
  227. json={
  228. "auth_enabled": True,
  229. "admin_username": "authreqadmin",
  230. "admin_password": "adminpassword123",
  231. },
  232. )
  233. # Now try to list users without a token
  234. response = await async_client.get("/api/v1/users/")
  235. assert response.status_code == 401
  236. @pytest.mark.asyncio
  237. @pytest.mark.integration
  238. async def test_list_users_as_admin(self, async_client: AsyncClient, auth_token: str):
  239. """Verify admin can list users."""
  240. response = await async_client.get(
  241. "/api/v1/users/",
  242. headers={"Authorization": f"Bearer {auth_token}"},
  243. )
  244. assert response.status_code == 200
  245. result = response.json()
  246. assert isinstance(result, list)
  247. assert len(result) >= 1 # At least the admin user
  248. @pytest.mark.asyncio
  249. @pytest.mark.integration
  250. async def test_create_user(self, async_client: AsyncClient, auth_token: str):
  251. """Verify admin can create a new user."""
  252. response = await async_client.post(
  253. "/api/v1/users/",
  254. headers={"Authorization": f"Bearer {auth_token}"},
  255. json={
  256. "username": "newuser",
  257. "password": "newuserpassword",
  258. "role": "user",
  259. },
  260. )
  261. assert response.status_code == 201
  262. result = response.json()
  263. assert result["username"] == "newuser"
  264. assert result["role"] == "user"
  265. assert result["is_active"] is True
  266. @pytest.mark.asyncio
  267. @pytest.mark.integration
  268. async def test_create_user_duplicate_username(self, async_client: AsyncClient, auth_token: str):
  269. """Verify creating user with duplicate username fails."""
  270. # Create first user
  271. await async_client.post(
  272. "/api/v1/users/",
  273. headers={"Authorization": f"Bearer {auth_token}"},
  274. json={
  275. "username": "duplicateuser",
  276. "password": "password123",
  277. "role": "user",
  278. },
  279. )
  280. # Try to create duplicate
  281. response = await async_client.post(
  282. "/api/v1/users/",
  283. headers={"Authorization": f"Bearer {auth_token}"},
  284. json={
  285. "username": "duplicateuser",
  286. "password": "password456",
  287. "role": "user",
  288. },
  289. )
  290. assert response.status_code == 400
  291. assert "Username already exists" in response.json()["detail"]
  292. @pytest.mark.asyncio
  293. @pytest.mark.integration
  294. async def test_update_user(self, async_client: AsyncClient, auth_token: str):
  295. """Verify admin can update a user."""
  296. # Create user
  297. create_response = await async_client.post(
  298. "/api/v1/users/",
  299. headers={"Authorization": f"Bearer {auth_token}"},
  300. json={
  301. "username": "updateuser",
  302. "password": "password123",
  303. "role": "user",
  304. },
  305. )
  306. user_id = create_response.json()["id"]
  307. # Update user
  308. response = await async_client.patch(
  309. f"/api/v1/users/{user_id}",
  310. headers={"Authorization": f"Bearer {auth_token}"},
  311. json={"role": "admin"},
  312. )
  313. assert response.status_code == 200
  314. assert response.json()["role"] == "admin"
  315. @pytest.mark.asyncio
  316. @pytest.mark.integration
  317. async def test_delete_user(self, async_client: AsyncClient, auth_token: str):
  318. """Verify admin can delete a user."""
  319. # Create user
  320. create_response = await async_client.post(
  321. "/api/v1/users/",
  322. headers={"Authorization": f"Bearer {auth_token}"},
  323. json={
  324. "username": "deleteuser",
  325. "password": "password123",
  326. "role": "user",
  327. },
  328. )
  329. user_id = create_response.json()["id"]
  330. # Delete user
  331. response = await async_client.delete(
  332. f"/api/v1/users/{user_id}",
  333. headers={"Authorization": f"Bearer {auth_token}"},
  334. )
  335. assert response.status_code == 204
  336. class TestAuthDisableAPI:
  337. """Integration tests for /api/v1/auth/disable endpoint."""
  338. @pytest.mark.asyncio
  339. @pytest.mark.integration
  340. async def test_disable_auth(self, async_client: AsyncClient):
  341. """Verify admin can disable authentication."""
  342. # Setup auth
  343. await async_client.post(
  344. "/api/v1/auth/setup",
  345. json={
  346. "auth_enabled": True,
  347. "admin_username": "disableadmin",
  348. "admin_password": "adminpassword123",
  349. },
  350. )
  351. # Login to get token
  352. login_response = await async_client.post(
  353. "/api/v1/auth/login",
  354. json={"username": "disableadmin", "password": "adminpassword123"},
  355. )
  356. token = login_response.json()["access_token"]
  357. # Disable auth
  358. response = await async_client.post(
  359. "/api/v1/auth/disable",
  360. headers={"Authorization": f"Bearer {token}"},
  361. )
  362. assert response.status_code == 200
  363. assert response.json()["auth_enabled"] is False
  364. # Verify auth is now disabled
  365. status_response = await async_client.get("/api/v1/auth/status")
  366. assert status_response.json()["auth_enabled"] is False
  367. class TestGroupsAPI:
  368. """Integration tests for /api/v1/groups/ endpoints."""
  369. @pytest.fixture
  370. async def auth_token(self, async_client: AsyncClient):
  371. """Setup auth and return admin token."""
  372. await async_client.post(
  373. "/api/v1/auth/setup",
  374. json={
  375. "auth_enabled": True,
  376. "admin_username": "groupsadmin",
  377. "admin_password": "adminpassword123",
  378. },
  379. )
  380. login_response = await async_client.post(
  381. "/api/v1/auth/login",
  382. json={"username": "groupsadmin", "password": "adminpassword123"},
  383. )
  384. return login_response.json()["access_token"]
  385. @pytest.mark.asyncio
  386. @pytest.mark.integration
  387. async def test_list_groups(self, async_client: AsyncClient, auth_token: str):
  388. """Verify listing groups returns default groups."""
  389. response = await async_client.get(
  390. "/api/v1/groups/",
  391. headers={"Authorization": f"Bearer {auth_token}"},
  392. )
  393. assert response.status_code == 200
  394. groups = response.json()
  395. assert isinstance(groups, list)
  396. # Should have default groups: Administrators, Operators, Viewers
  397. group_names = [g["name"] for g in groups]
  398. assert "Administrators" in group_names
  399. assert "Operators" in group_names
  400. assert "Viewers" in group_names
  401. @pytest.mark.asyncio
  402. @pytest.mark.integration
  403. async def test_get_permissions(self, async_client: AsyncClient, auth_token: str):
  404. """Verify getting available permissions."""
  405. response = await async_client.get(
  406. "/api/v1/groups/permissions",
  407. headers={"Authorization": f"Bearer {auth_token}"},
  408. )
  409. assert response.status_code == 200
  410. permissions = response.json()
  411. assert isinstance(permissions, dict)
  412. # Should have permission categories
  413. assert "Printers" in permissions or len(permissions) > 0
  414. @pytest.mark.asyncio
  415. @pytest.mark.integration
  416. async def test_create_group(self, async_client: AsyncClient, auth_token: str):
  417. """Verify creating a new group."""
  418. response = await async_client.post(
  419. "/api/v1/groups/",
  420. headers={"Authorization": f"Bearer {auth_token}"},
  421. json={
  422. "name": "Custom Group",
  423. "description": "A custom test group",
  424. "permissions": ["printers:read", "archives:read"],
  425. },
  426. )
  427. assert response.status_code == 201
  428. group = response.json()
  429. assert group["name"] == "Custom Group"
  430. assert group["description"] == "A custom test group"
  431. assert "printers:read" in group["permissions"]
  432. assert group["is_system"] is False
  433. @pytest.mark.asyncio
  434. @pytest.mark.integration
  435. async def test_update_group(self, async_client: AsyncClient, auth_token: str):
  436. """Verify updating a group."""
  437. # Create a group first
  438. create_response = await async_client.post(
  439. "/api/v1/groups/",
  440. headers={"Authorization": f"Bearer {auth_token}"},
  441. json={
  442. "name": "Update Test Group",
  443. "permissions": ["printers:read"],
  444. },
  445. )
  446. group_id = create_response.json()["id"]
  447. # Update the group
  448. response = await async_client.patch(
  449. f"/api/v1/groups/{group_id}",
  450. headers={"Authorization": f"Bearer {auth_token}"},
  451. json={
  452. "description": "Updated description",
  453. "permissions": ["printers:read", "printers:control"],
  454. },
  455. )
  456. assert response.status_code == 200
  457. group = response.json()
  458. assert group["description"] == "Updated description"
  459. assert "printers:control" in group["permissions"]
  460. @pytest.mark.asyncio
  461. @pytest.mark.integration
  462. async def test_cannot_delete_system_group(self, async_client: AsyncClient, auth_token: str):
  463. """Verify system groups cannot be deleted."""
  464. # Get the Administrators group
  465. list_response = await async_client.get(
  466. "/api/v1/groups/",
  467. headers={"Authorization": f"Bearer {auth_token}"},
  468. )
  469. admin_group = next(g for g in list_response.json() if g["name"] == "Administrators")
  470. # Try to delete it
  471. response = await async_client.delete(
  472. f"/api/v1/groups/{admin_group['id']}",
  473. headers={"Authorization": f"Bearer {auth_token}"},
  474. )
  475. assert response.status_code == 400
  476. assert "system group" in response.json()["detail"].lower()
  477. @pytest.mark.asyncio
  478. @pytest.mark.integration
  479. async def test_delete_custom_group(self, async_client: AsyncClient, auth_token: str):
  480. """Verify custom groups can be deleted."""
  481. # Create a group
  482. create_response = await async_client.post(
  483. "/api/v1/groups/",
  484. headers={"Authorization": f"Bearer {auth_token}"},
  485. json={"name": "Delete Test Group"},
  486. )
  487. group_id = create_response.json()["id"]
  488. # Delete it
  489. response = await async_client.delete(
  490. f"/api/v1/groups/{group_id}",
  491. headers={"Authorization": f"Bearer {auth_token}"},
  492. )
  493. assert response.status_code == 204
  494. class TestUserGroupsAPI:
  495. """Integration tests for user-group assignments."""
  496. @pytest.fixture
  497. async def auth_token(self, async_client: AsyncClient):
  498. """Setup auth and return admin token."""
  499. await async_client.post(
  500. "/api/v1/auth/setup",
  501. json={
  502. "auth_enabled": True,
  503. "admin_username": "usergroupadmin",
  504. "admin_password": "adminpassword123",
  505. },
  506. )
  507. login_response = await async_client.post(
  508. "/api/v1/auth/login",
  509. json={"username": "usergroupadmin", "password": "adminpassword123"},
  510. )
  511. return login_response.json()["access_token"]
  512. @pytest.mark.asyncio
  513. @pytest.mark.integration
  514. async def test_create_user_with_groups(self, async_client: AsyncClient, auth_token: str):
  515. """Verify creating a user with group assignments."""
  516. # Get Operators group ID
  517. groups_response = await async_client.get(
  518. "/api/v1/groups/",
  519. headers={"Authorization": f"Bearer {auth_token}"},
  520. )
  521. operators_group = next(g for g in groups_response.json() if g["name"] == "Operators")
  522. # Create user with group
  523. response = await async_client.post(
  524. "/api/v1/users/",
  525. headers={"Authorization": f"Bearer {auth_token}"},
  526. json={
  527. "username": "groupuser",
  528. "password": "password123",
  529. "group_ids": [operators_group["id"]],
  530. },
  531. )
  532. assert response.status_code == 201
  533. user = response.json()
  534. assert any(g["name"] == "Operators" for g in user["groups"])
  535. @pytest.mark.asyncio
  536. @pytest.mark.integration
  537. async def test_add_user_to_group(self, async_client: AsyncClient, auth_token: str):
  538. """Verify adding a user to a group."""
  539. # Create a user
  540. user_response = await async_client.post(
  541. "/api/v1/users/",
  542. headers={"Authorization": f"Bearer {auth_token}"},
  543. json={"username": "addtogroup", "password": "password123"},
  544. )
  545. user_id = user_response.json()["id"]
  546. # Get Viewers group
  547. groups_response = await async_client.get(
  548. "/api/v1/groups/",
  549. headers={"Authorization": f"Bearer {auth_token}"},
  550. )
  551. viewers_group = next(g for g in groups_response.json() if g["name"] == "Viewers")
  552. # Add user to group
  553. response = await async_client.post(
  554. f"/api/v1/groups/{viewers_group['id']}/users/{user_id}",
  555. headers={"Authorization": f"Bearer {auth_token}"},
  556. )
  557. assert response.status_code == 204
  558. # Verify user is in group
  559. user_check = await async_client.get(
  560. f"/api/v1/users/{user_id}",
  561. headers={"Authorization": f"Bearer {auth_token}"},
  562. )
  563. assert any(g["name"] == "Viewers" for g in user_check.json()["groups"])
  564. class TestChangePasswordAPI:
  565. """Integration tests for /api/v1/users/me/change-password endpoint."""
  566. @pytest.fixture
  567. async def user_token(self, async_client: AsyncClient):
  568. """Setup auth and return regular user token."""
  569. # Enable auth with admin
  570. await async_client.post(
  571. "/api/v1/auth/setup",
  572. json={
  573. "auth_enabled": True,
  574. "admin_username": "pwchangeadmin",
  575. "admin_password": "adminpassword123",
  576. },
  577. )
  578. admin_login = await async_client.post(
  579. "/api/v1/auth/login",
  580. json={"username": "pwchangeadmin", "password": "adminpassword123"},
  581. )
  582. admin_token = admin_login.json()["access_token"]
  583. # Create a regular user
  584. await async_client.post(
  585. "/api/v1/users/",
  586. headers={"Authorization": f"Bearer {admin_token}"},
  587. json={"username": "pwchangeuser", "password": "oldpassword123"},
  588. )
  589. # Login as regular user
  590. user_login = await async_client.post(
  591. "/api/v1/auth/login",
  592. json={"username": "pwchangeuser", "password": "oldpassword123"},
  593. )
  594. return user_login.json()["access_token"]
  595. @pytest.mark.asyncio
  596. @pytest.mark.integration
  597. async def test_change_password_success(self, async_client: AsyncClient, user_token: str):
  598. """Verify user can change their own password."""
  599. response = await async_client.post(
  600. "/api/v1/users/me/change-password",
  601. headers={"Authorization": f"Bearer {user_token}"},
  602. json={
  603. "current_password": "oldpassword123",
  604. "new_password": "newpassword456",
  605. },
  606. )
  607. assert response.status_code == 200
  608. assert "success" in response.json()["message"].lower()
  609. # Verify can login with new password
  610. login_response = await async_client.post(
  611. "/api/v1/auth/login",
  612. json={"username": "pwchangeuser", "password": "newpassword456"},
  613. )
  614. assert login_response.status_code == 200
  615. @pytest.mark.asyncio
  616. @pytest.mark.integration
  617. async def test_change_password_wrong_current(self, async_client: AsyncClient, user_token: str):
  618. """Verify changing password fails with wrong current password."""
  619. response = await async_client.post(
  620. "/api/v1/users/me/change-password",
  621. headers={"Authorization": f"Bearer {user_token}"},
  622. json={
  623. "current_password": "wrongpassword",
  624. "new_password": "newpassword456",
  625. },
  626. )
  627. assert response.status_code == 400
  628. assert "incorrect" in response.json()["detail"].lower()
  629. @pytest.mark.asyncio
  630. @pytest.mark.integration
  631. async def test_change_password_requires_auth(self, async_client: AsyncClient):
  632. """Verify changing password requires authentication."""
  633. response = await async_client.post(
  634. "/api/v1/users/me/change-password",
  635. json={
  636. "current_password": "oldpassword",
  637. "new_password": "newpassword",
  638. },
  639. )
  640. assert response.status_code == 401
  641. class TestAuthMiddlewarePublicRoutes:
  642. """Tests for auth middleware public route configuration.
  643. These routes must be accessible without authentication, even when auth is enabled,
  644. because browser elements like <img src> and <video src> don't send Authorization headers.
  645. """
  646. @pytest.fixture
  647. async def enabled_auth(self, async_client: AsyncClient):
  648. """Enable auth for testing middleware behavior."""
  649. await async_client.post(
  650. "/api/v1/auth/setup",
  651. json={
  652. "auth_enabled": True,
  653. "admin_username": "middlewareadmin",
  654. "admin_password": "adminpassword123",
  655. },
  656. )
  657. @pytest.mark.asyncio
  658. @pytest.mark.integration
  659. async def test_auth_status_is_public(self, async_client: AsyncClient, enabled_auth):
  660. """Verify /api/v1/auth/status is accessible without auth."""
  661. response = await async_client.get("/api/v1/auth/status")
  662. assert response.status_code == 200
  663. assert "auth_enabled" in response.json()
  664. @pytest.mark.asyncio
  665. @pytest.mark.integration
  666. async def test_auth_login_is_public(self, async_client: AsyncClient, enabled_auth):
  667. """Verify /api/v1/auth/login is accessible without auth."""
  668. response = await async_client.post(
  669. "/api/v1/auth/login",
  670. json={"username": "middlewareadmin", "password": "adminpassword123"},
  671. )
  672. # Should not return 401 (unauthorized) - it should either succeed or return
  673. # a different error (like 400 for wrong credentials)
  674. assert response.status_code != 401 or "token" in response.json()
  675. @pytest.mark.asyncio
  676. @pytest.mark.integration
  677. async def test_auth_setup_is_public(self, async_client: AsyncClient):
  678. """Verify /api/v1/auth/setup is accessible without auth (needed for setup/recovery)."""
  679. # Don't enable auth first - test that setup endpoint itself is accessible
  680. response = await async_client.post(
  681. "/api/v1/auth/setup",
  682. json={"auth_enabled": False},
  683. )
  684. # Should not be 401
  685. assert response.status_code != 401
  686. @pytest.mark.asyncio
  687. @pytest.mark.integration
  688. async def test_updates_version_is_public(self, async_client: AsyncClient, enabled_auth):
  689. """Verify /api/v1/updates/version is accessible without auth."""
  690. response = await async_client.get("/api/v1/updates/version")
  691. # Should not be 401
  692. assert response.status_code != 401
  693. @pytest.mark.asyncio
  694. @pytest.mark.integration
  695. async def test_protected_route_requires_auth(self, async_client: AsyncClient, enabled_auth):
  696. """Verify non-public routes return 401 without token."""
  697. response = await async_client.get("/api/v1/printers/")
  698. assert response.status_code == 401
  699. @pytest.mark.asyncio
  700. @pytest.mark.integration
  701. async def test_protected_route_works_with_token(self, async_client: AsyncClient, enabled_auth):
  702. """Verify non-public routes work with valid token."""
  703. # Login to get token
  704. login_response = await async_client.post(
  705. "/api/v1/auth/login",
  706. json={"username": "middlewareadmin", "password": "adminpassword123"},
  707. )
  708. token = login_response.json()["access_token"]
  709. # Access protected route
  710. response = await async_client.get(
  711. "/api/v1/printers/",
  712. headers={"Authorization": f"Bearer {token}"},
  713. )
  714. assert response.status_code == 200
  715. @pytest.mark.asyncio
  716. @pytest.mark.integration
  717. async def test_advanced_auth_status_is_public(self, async_client: AsyncClient, enabled_auth):
  718. """Verify /api/v1/auth/advanced-auth/status is accessible without auth."""
  719. response = await async_client.get("/api/v1/auth/advanced-auth/status")
  720. # Should not be 401 (must be accessible for login page)
  721. assert response.status_code != 401
  722. # Should return valid response (200 with auth status)
  723. if response.status_code == 200:
  724. result = response.json()
  725. assert "advanced_auth_enabled" in result
  726. assert "smtp_configured" in result
  727. @pytest.mark.asyncio
  728. @pytest.mark.integration
  729. async def test_forgot_password_is_public(self, async_client: AsyncClient, enabled_auth):
  730. """Verify /api/v1/auth/forgot-password is accessible without auth."""
  731. response = await async_client.post(
  732. "/api/v1/auth/forgot-password",
  733. json={"email": "test@example.com"},
  734. )
  735. # Should not be 401 (must be accessible for password reset from login page)
  736. assert response.status_code != 401
  737. # Will likely be 400 (advanced auth not enabled) but that's okay -
  738. # the important thing is it's not blocked by auth middleware
  739. assert response.status_code in [200, 400]