1
0

AuthEndpointsTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Auth;
  4. use App\Domain\Auth\Role;
  5. use App\Domain\Auth\TokenKind;
  6. use App\Tests\Integration\Support\AppTestCase;
  7. /**
  8. * Behavioural tests for the /api/v1/auth/* endpoints. These verify the
  9. * upsert flows and OIDC role resolution from group mappings.
  10. */
  11. final class AuthEndpointsTest extends AppTestCase
  12. {
  13. public function testUpsertLocalCreatesUserOnFirstCall(): void
  14. {
  15. $token = $this->createToken(TokenKind::Service);
  16. $adminId = $this->createUser(Role::Admin, isLocal: true);
  17. $response = $this->request(
  18. 'POST',
  19. '/api/v1/auth/users/upsert-local',
  20. [
  21. 'Authorization' => 'Bearer ' . $token,
  22. 'X-Acting-User-Id' => (string) $adminId,
  23. 'Content-Type' => 'application/json',
  24. ],
  25. json_encode(['username' => 'second']) ?: null
  26. );
  27. self::assertSame(200, $response->getStatusCode());
  28. $body = $this->decode($response);
  29. self::assertIsInt($body['user_id']);
  30. self::assertSame('admin', $body['role']);
  31. self::assertNull($body['email']);
  32. self::assertSame('second', $body['display_name']);
  33. self::assertTrue($body['is_local']);
  34. }
  35. public function testUpsertLocalIsIdempotent(): void
  36. {
  37. $token = $this->createToken(TokenKind::Service);
  38. $adminId = $this->createUser(Role::Admin, isLocal: true);
  39. $headers = [
  40. 'Authorization' => 'Bearer ' . $token,
  41. 'X-Acting-User-Id' => (string) $adminId,
  42. 'Content-Type' => 'application/json',
  43. ];
  44. $body = json_encode(['username' => 'idempotent']) ?: null;
  45. $first = $this->decode($this->request('POST', '/api/v1/auth/users/upsert-local', $headers, $body));
  46. $second = $this->decode($this->request('POST', '/api/v1/auth/users/upsert-local', $headers, $body));
  47. self::assertSame($first['user_id'], $second['user_id']);
  48. }
  49. public function testUpsertOidcResolvesRoleFromGroups(): void
  50. {
  51. $token = $this->createToken(TokenKind::Service);
  52. $adminId = $this->createUser(Role::Admin, isLocal: true);
  53. // Seed a role mapping: group "ops-group" → operator
  54. $this->db->insert('oidc_role_mappings', [
  55. 'group_id' => 'ops-group',
  56. 'role' => Role::Operator->value,
  57. ]);
  58. $this->db->insert('oidc_role_mappings', [
  59. 'group_id' => 'admin-group',
  60. 'role' => Role::Admin->value,
  61. ]);
  62. $response = $this->request(
  63. 'POST',
  64. '/api/v1/auth/users/upsert-oidc',
  65. [
  66. 'Authorization' => 'Bearer ' . $token,
  67. 'X-Acting-User-Id' => (string) $adminId,
  68. 'Content-Type' => 'application/json',
  69. ],
  70. json_encode([
  71. 'subject' => 'sub-1',
  72. 'email' => 'alice@example.com',
  73. 'display_name' => 'Alice',
  74. 'groups' => ['ops-group', 'admin-group'],
  75. ]) ?: null
  76. );
  77. self::assertSame(200, $response->getStatusCode());
  78. $body = $this->decode($response);
  79. self::assertSame('admin', $body['role'], 'highest matching role wins');
  80. self::assertSame('alice@example.com', $body['email']);
  81. self::assertSame('Alice', $body['display_name']);
  82. self::assertFalse($body['is_local']);
  83. }
  84. public function testUpsertOidcFallsBackToDefaultRoleWithNoMatchingGroup(): void
  85. {
  86. $token = $this->createToken(TokenKind::Service);
  87. $adminId = $this->createUser(Role::Admin, isLocal: true);
  88. $response = $this->request(
  89. 'POST',
  90. '/api/v1/auth/users/upsert-oidc',
  91. [
  92. 'Authorization' => 'Bearer ' . $token,
  93. 'X-Acting-User-Id' => (string) $adminId,
  94. 'Content-Type' => 'application/json',
  95. ],
  96. json_encode([
  97. 'subject' => 'sub-default',
  98. 'email' => 'b@example.com',
  99. 'display_name' => 'B',
  100. 'groups' => ['unknown-group'],
  101. ]) ?: null
  102. );
  103. self::assertSame(200, $response->getStatusCode());
  104. // Default in tests is Role::Viewer.
  105. self::assertSame('viewer', $this->decode($response)['role']);
  106. }
  107. public function testUpsertOidcRecomputesRoleOnSubsequentLogins(): void
  108. {
  109. $token = $this->createToken(TokenKind::Service);
  110. $adminId = $this->createUser(Role::Admin, isLocal: true);
  111. $this->db->insert('oidc_role_mappings', [
  112. 'group_id' => 'g1',
  113. 'role' => Role::Operator->value,
  114. ]);
  115. $headers = [
  116. 'Authorization' => 'Bearer ' . $token,
  117. 'X-Acting-User-Id' => (string) $adminId,
  118. 'Content-Type' => 'application/json',
  119. ];
  120. $first = $this->decode($this->request(
  121. 'POST',
  122. '/api/v1/auth/users/upsert-oidc',
  123. $headers,
  124. json_encode([
  125. 'subject' => 'churn',
  126. 'email' => 'c@example.com',
  127. 'display_name' => 'C',
  128. 'groups' => ['g1'],
  129. ]) ?: null
  130. ));
  131. self::assertSame('operator', $first['role']);
  132. // Subsequent login with no matching group → role drops to default viewer.
  133. $second = $this->decode($this->request(
  134. 'POST',
  135. '/api/v1/auth/users/upsert-oidc',
  136. $headers,
  137. json_encode([
  138. 'subject' => 'churn',
  139. 'email' => 'c@example.com',
  140. 'display_name' => 'C',
  141. 'groups' => [],
  142. ]) ?: null
  143. ));
  144. self::assertSame($first['user_id'], $second['user_id']);
  145. self::assertSame('viewer', $second['role']);
  146. }
  147. public function testUpsertOidcRejectsAdminToken(): void
  148. {
  149. // Even an admin token can't call /auth/* — those are service-only.
  150. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  151. $response = $this->request(
  152. 'POST',
  153. '/api/v1/auth/users/upsert-local',
  154. [
  155. 'Authorization' => 'Bearer ' . $token,
  156. 'Content-Type' => 'application/json',
  157. ],
  158. json_encode(['username' => 'admin']) ?: null
  159. );
  160. self::assertSame(403, $response->getStatusCode());
  161. }
  162. }