RemoteOidcTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\UserOidcMapping;
  4. use App\Services\UserOidcService;
  5. use App\User;
  6. use Auth;
  7. use Faker\Factory as Faker;
  8. use League\OAuth2\Client\Provider\GenericResourceOwner;
  9. use League\OAuth2\Client\Token\AccessToken;
  10. use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
  11. use Mockery\MockInterface;
  12. use Tests\TestCase;
  13. class RemoteOidcTest extends TestCase
  14. {
  15. use MockeryPHPUnitIntegration;
  16. public function view_oidc_start()
  17. {
  18. config([
  19. 'remote-auth.oidc.enabled'=> true,
  20. 'remote-auth.oidc.clientId' => 'fake',
  21. 'remote-auth.oidc.clientSecret' => 'fakeSecret',
  22. 'remote-auth.oidc.authorizeURL' => 'http://fakeserver.oidc/authorizeURL',
  23. 'remote-auth.oidc.tokenURL' => 'http://fakeserver.oidc/tokenURL',
  24. 'remote-auth.oidc.profileURL' => 'http://fakeserver.oidc/profile',
  25. ]);
  26. $response = $this->withoutExceptionHandling()->get('auth/oidc/start');
  27. $state = session()->get('oauth2state');
  28. $callbackUrl = urlencode(url('auth/oidc/callback'));
  29. $response->assertRedirect("http://fakeserver.oidc/authorizeURL?scope=openid%20profile%20email&state={$state}&response_type=code&approval_prompt=auto&redirect_uri={$callbackUrl}&client_id=fake");
  30. }
  31. public function view_oidc_callback_new_user()
  32. {
  33. $originalUserCount = User::count();
  34. $this->assertDatabaseCount('users', $originalUserCount);
  35. config(['remote-auth.oidc.enabled' => true]);
  36. $oauthData = array(
  37. "sub" => str_random(10),
  38. "preferred_username" => fake()->unique()->userName,
  39. "email" => fake()->unique()->freeEmail,
  40. );
  41. $this->partialMock(UserOidcService::class, function (MockInterface $mock) use ($oauthData) {
  42. $mock->shouldReceive('getAccessToken')->once()->andReturn(new AccessToken(["access_token" => "token" ]));
  43. $mock->shouldReceive('getResourceOwner')->once()->andReturn(new GenericResourceOwner($oauthData, 'sub'));
  44. return $mock;
  45. });
  46. $response = $this->withoutExceptionHandling()->withSession([
  47. 'oauth2state' => 'abc123',
  48. ])->get('auth/oidc/callback?state=abc123&code=1');
  49. $response->assertRedirect('/');
  50. $mappedUser = UserOidcMapping::where('oidc_id', $oauthData['sub'])->first();
  51. $this->assertNotNull($mappedUser, "mapping is found");
  52. $user = $mappedUser->user;
  53. $this->assertEquals($user->username, $oauthData['preferred_username']);
  54. $this->assertEquals($user->email, $oauthData['email']);
  55. $this->assertEquals(Auth::guard()->user()->id, $user->id);
  56. $this->assertDatabaseCount('users', $originalUserCount+1);
  57. }
  58. public function view_oidc_callback_existing_user()
  59. {
  60. $user = User::create([
  61. 'name' => fake()->name,
  62. 'username' => fake()->unique()->username,
  63. 'email' => fake()->unique()->freeEmail,
  64. ]);
  65. $originalUserCount = User::count();
  66. $this->assertDatabaseCount('users', $originalUserCount);
  67. config(['remote-auth.oidc.enabled' => true]);
  68. $oauthData = array(
  69. "sub" => str_random(10),
  70. "preferred_username" => $user->username,
  71. "email" => $user->email,
  72. );
  73. UserOidcMapping::create([
  74. 'oidc_id' => $oauthData['sub'],
  75. 'user_id' => $user->id,
  76. ]);
  77. $this->partialMock(UserOidcService::class, function (MockInterface $mock) use ($oauthData) {
  78. $mock->shouldReceive('getAccessToken')->once()->andReturn(new AccessToken(["access_token" => "token" ]));
  79. $mock->shouldReceive('getResourceOwner')->once()->andReturn(new GenericResourceOwner($oauthData, 'sub'));
  80. return $mock;
  81. });
  82. $response = $this->withoutExceptionHandling()->withSession([
  83. 'oauth2state' => 'abc123',
  84. ])->get('auth/oidc/callback?state=abc123&code=1');
  85. $response->assertRedirect('/');
  86. $mappedUser = UserOidcMapping::where('oidc_id', $oauthData['sub'])->first();
  87. $this->assertNotNull($mappedUser, "mapping is found");
  88. $user = $mappedUser->user;
  89. $this->assertEquals($user->username, $oauthData['preferred_username']);
  90. $this->assertEquals($user->email, $oauthData['email']);
  91. $this->assertEquals(Auth::guard()->user()->id, $user->id);
  92. $this->assertDatabaseCount('users', $originalUserCount);
  93. }
  94. }