SocialiteControllerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Tests\Feature\Http\Auth;
  3. use App\Facades\Settings;
  4. use App\Http\Controllers\Auth\SocialiteController;
  5. use App\Models\User;
  6. use Illuminate\Support\Facades\DB;
  7. use Laravel\Socialite\Facades\Socialite;
  8. use PHPUnit\Framework\Attributes\CoversClass;
  9. use Tests\FeatureTestCase;
  10. /**
  11. * SocialiteControllerTest test class
  12. */
  13. #[CoversClass(SocialiteController::class)]
  14. class SocialiteControllerTest extends FeatureTestCase
  15. {
  16. /**
  17. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  18. */
  19. protected $user;
  20. /**
  21. * @var \Laravel\Socialite\Two\User
  22. */
  23. protected $socialiteUser;
  24. private const USER_OAUTH_ID = '12345';
  25. private const USER_OAUTH_PROVIDER = 'github';
  26. private const USER_NAME = 'John';
  27. private const USER_NICKNAME = 'Jo';
  28. private const USER_EMAIL = 'john@provider.com';
  29. /**
  30. * @test
  31. */
  32. public function setUp() : void
  33. {
  34. parent::setUp();
  35. DB::table('users')->delete();
  36. $this->user = User::factory()->create([
  37. 'name' => self::USER_NAME,
  38. 'email' => self::USER_EMAIL,
  39. 'password' => 'password',
  40. 'is_admin' => 1,
  41. 'oauth_id' => self::USER_OAUTH_ID,
  42. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  43. ]);
  44. $this->socialiteUser = new \Laravel\Socialite\Two\User;
  45. $this->socialiteUser->id = self::USER_OAUTH_ID;
  46. $this->socialiteUser->name = self::USER_NAME;
  47. $this->socialiteUser->email = self::USER_EMAIL;
  48. $this->socialiteUser->nickname = self::USER_NICKNAME;
  49. }
  50. /**
  51. * @test
  52. */
  53. public function test_redirect_redirects_to_provider_url()
  54. {
  55. Settings::set('enableSso', true);
  56. $response = $this->get('/socialite/redirect/github');
  57. $response->assertRedirectContains('https://github.com/login/oauth/authorize');
  58. }
  59. /**
  60. * @test
  61. */
  62. public function test_redirect_returns_error_when_registrations_are_disabled()
  63. {
  64. Settings::set('enableSso', false);
  65. $response = $this->get('/socialite/redirect/github');
  66. $response->assertRedirect('/error?err=sso_disabled');
  67. }
  68. /**
  69. * @test
  70. */
  71. public function test_callback_authenticates_the_user()
  72. {
  73. Socialite::shouldReceive('driver->user')
  74. ->andReturn($this->socialiteUser);
  75. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  76. $this->assertAuthenticatedAs($this->user, 'web-guard');
  77. }
  78. /**
  79. * @test
  80. */
  81. public function test_callback_redirects_authenticated_user_to_accounts()
  82. {
  83. Socialite::shouldReceive('driver->user')
  84. ->andReturn($this->socialiteUser);
  85. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  86. $response->assertRedirect('/accounts');
  87. }
  88. /**
  89. * @test
  90. */
  91. public function test_callback_updates_user_informations()
  92. {
  93. $socialiteUpdatedUser = new \Laravel\Socialite\Two\User;
  94. $socialiteUpdatedUser->id = self::USER_OAUTH_ID;
  95. $socialiteUpdatedUser->email = 'new_email';
  96. $socialiteUpdatedUser->nickname = 'new_nickname';
  97. Socialite::shouldReceive('driver->user')
  98. ->andReturn($socialiteUpdatedUser);
  99. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  100. $this->assertDatabaseHas('users', [
  101. 'oauth_id' => self::USER_OAUTH_ID,
  102. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  103. 'email' => 'new_email',
  104. ]);
  105. }
  106. /**
  107. * @test
  108. */
  109. public function test_callback_updates_username_with_fallback_value()
  110. {
  111. $socialiteUpdatedUser = new \Laravel\Socialite\Two\User;
  112. $socialiteUpdatedUser->id = self::USER_OAUTH_ID;
  113. $socialiteUpdatedUser->name = 'new_name';
  114. $socialiteUpdatedUser->email = 'new_email';
  115. Socialite::shouldReceive('driver->user')
  116. ->andReturn($socialiteUpdatedUser);
  117. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  118. $this->assertDatabaseHas('users', [
  119. 'oauth_id' => self::USER_OAUTH_ID,
  120. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  121. 'email' => 'new_email',
  122. ]);
  123. }
  124. /**
  125. * @test
  126. */
  127. public function test_callback_registers_new_user()
  128. {
  129. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  130. $newSocialiteUser->id = 'new_id';
  131. $newSocialiteUser->name = 'jane';
  132. $newSocialiteUser->email = 'jane@provider.com';
  133. Socialite::shouldReceive('driver->user')
  134. ->andReturn($newSocialiteUser);
  135. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  136. $this->assertDatabaseHas('users', [
  137. 'oauth_id' => 'new_id',
  138. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  139. 'email' => 'jane@provider.com',
  140. 'is_admin' => 0,
  141. ]);
  142. }
  143. /**
  144. * @test
  145. */
  146. public function test_callback_registers_new_user_with_existing_name()
  147. {
  148. $socialiteUserWithSameName = new \Laravel\Socialite\Two\User;
  149. $socialiteUserWithSameName->id = 'socialiteUserWithSameNameId';
  150. $socialiteUserWithSameName->name = self::USER_NAME;
  151. $socialiteUserWithSameName->email = 'socialiteuserwithsamename@example.com';
  152. $socialiteUserWithSameName->nickname = self::USER_NICKNAME;
  153. Socialite::shouldReceive('driver->user')
  154. ->andReturn($socialiteUserWithSameName);
  155. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  156. $this->assertDatabaseHas('users', [
  157. 'oauth_id' => 'socialiteUserWithSameNameId',
  158. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  159. 'email' => 'socialiteuserwithsamename@example.com',
  160. ]);
  161. }
  162. /**
  163. * @test
  164. */
  165. public function test_callback_always_registers_first_user_as_admin()
  166. {
  167. DB::table('users')->delete();
  168. Settings::set('disableRegistration', true);
  169. Settings::set('enableSso', false);
  170. Socialite::shouldReceive('driver->user')
  171. ->andReturn($this->socialiteUser);
  172. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  173. $this->assertDatabaseHas('users', [
  174. 'oauth_id' => self::USER_OAUTH_ID,
  175. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  176. 'is_admin' => 1,
  177. ]);
  178. }
  179. /**
  180. * @test
  181. */
  182. public function test_callback_returns_error_when_email_is_already_used()
  183. {
  184. $userWithSameEmail = User::factory()->create([
  185. 'name' => 'userWithSameEmail',
  186. 'email' => 'other@example.com',
  187. 'password' => 'password',
  188. ]);
  189. $socialiteUserWithSameEmail = new \Laravel\Socialite\Two\User;
  190. $socialiteUserWithSameEmail->id = '666';
  191. $socialiteUserWithSameEmail->name = 'socialiteUserWithSameEmail';
  192. $socialiteUserWithSameEmail->email = 'other@example.com';
  193. $socialiteUserWithSameEmail->nickname = self::USER_NICKNAME;
  194. Socialite::shouldReceive('driver->user')
  195. ->andReturn($socialiteUserWithSameEmail);
  196. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  197. $response->assertRedirect('/error?err=sso_email_already_used');
  198. $this->assertDatabaseMissing('users', [
  199. 'oauth_id' => '666',
  200. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  201. ]);
  202. }
  203. /**
  204. * @test
  205. */
  206. public function test_callback_returns_error_when_registrations_are_closed()
  207. {
  208. Settings::set('disableRegistration', true);
  209. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  210. $newSocialiteUser->id = 'rejected_id';
  211. $newSocialiteUser->name = 'jane';
  212. $newSocialiteUser->email = 'jane@provider.com';
  213. Socialite::shouldReceive('driver->user')
  214. ->andReturn($newSocialiteUser);
  215. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  216. $response->assertRedirect('/error?err=sso_no_register');
  217. }
  218. /**
  219. * @test
  220. */
  221. public function test_callback_skips_registration_when_registrations_are_closed()
  222. {
  223. Settings::set('disableRegistration', true);
  224. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  225. $newSocialiteUser->id = 'rejected_id';
  226. $newSocialiteUser->name = 'jane';
  227. $newSocialiteUser->email = 'jane@provider.com';
  228. Socialite::shouldReceive('driver->user')
  229. ->andReturn($newSocialiteUser);
  230. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  231. $this->assertDatabaseMissing('users', [
  232. 'oauth_id' => 'rejected_id',
  233. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  234. ]);
  235. }
  236. }