SocialiteControllerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 Exception;
  7. use Illuminate\Support\Facades\DB;
  8. use Laravel\Socialite\Facades\Socialite;
  9. use PHPUnit\Framework\Attributes\CoversClass;
  10. use PHPUnit\Framework\Attributes\DataProvider;
  11. use PHPUnit\Framework\Attributes\Test;
  12. use Tests\FeatureTestCase;
  13. /**
  14. * SocialiteControllerTest test class
  15. */
  16. #[CoversClass(SocialiteController::class)]
  17. class SocialiteControllerTest extends FeatureTestCase
  18. {
  19. /**
  20. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  21. */
  22. protected $user;
  23. /**
  24. * @var \Laravel\Socialite\Two\User
  25. */
  26. protected $socialiteUser;
  27. private const USER_OAUTH_ID = '12345';
  28. private const USER_OAUTH_PROVIDER = 'github';
  29. private const USER_NAME = 'John';
  30. private const USER_NICKNAME = 'Jo';
  31. private const USER_EMAIL = 'john@provider.com';
  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. #[Test]
  51. public function test_redirect_redirects_to_provider_url()
  52. {
  53. Settings::set('enableSso', true);
  54. $response = $this->get('/socialite/redirect/github');
  55. $response->assertRedirectContains('https://github.com/login/oauth/authorize');
  56. }
  57. #[Test]
  58. public function test_redirect_returns_error_when_registrations_are_disabled()
  59. {
  60. Settings::set('enableSso', false);
  61. $response = $this->get('/socialite/redirect/github');
  62. $response->assertRedirect('/error?err=sso_disabled');
  63. }
  64. #[Test]
  65. public function test_redirect_returns_error_when_sso_provider_client_id_is_missing()
  66. {
  67. //Settings::set('enableSso', true);
  68. config(['services.github.client_id' => null], true);
  69. $response = $this->get('/socialite/redirect/github');
  70. $response->assertRedirect('/error?err=sso_bad_provider_setup');
  71. }
  72. #[Test]
  73. public function test_redirect_returns_error_when_sso_provider_client_secret_is_missing()
  74. {
  75. config(['services.github.client_secret' => null]);
  76. $response = $this->get('/socialite/redirect/github');
  77. $response->assertRedirect('/error?err=sso_bad_provider_setup');
  78. }
  79. #[Test]
  80. #[DataProvider('ssoConfigVarProvider')]
  81. public function test_redirect_returns_error_when_openid_provider_client_secret_is_missing($ssoConfigVar)
  82. {
  83. config(['services.openid.' . $ssoConfigVar => null]);
  84. $response = $this->get('/socialite/redirect/openid');
  85. $response->assertRedirect('/error?err=sso_bad_provider_setup');
  86. }
  87. public static function ssoConfigVarProvider()
  88. {
  89. return [
  90. 'TOKEN_URL' => [
  91. 'token_url',
  92. ],
  93. 'AUTHORIZE_URL' => [
  94. 'authorize_url',
  95. ],
  96. 'USERINFO_URL' => [
  97. 'userinfo_url',
  98. ],
  99. ];
  100. }
  101. #[Test]
  102. public function test_callback_authenticates_the_user()
  103. {
  104. Socialite::shouldReceive('driver->user')
  105. ->andReturn($this->socialiteUser);
  106. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  107. $this->assertAuthenticatedAs($this->user, 'web-guard');
  108. }
  109. #[Test]
  110. public function test_callback_redirects_authenticated_user_to_accounts()
  111. {
  112. Socialite::shouldReceive('driver->user')
  113. ->andReturn($this->socialiteUser);
  114. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  115. $response->assertRedirect('/accounts');
  116. }
  117. #[Test]
  118. public function test_callback_updates_user_informations()
  119. {
  120. $socialiteUpdatedUser = new \Laravel\Socialite\Two\User;
  121. $socialiteUpdatedUser->id = self::USER_OAUTH_ID;
  122. $socialiteUpdatedUser->email = 'new_email';
  123. $socialiteUpdatedUser->nickname = 'new_nickname';
  124. Socialite::shouldReceive('driver->user')
  125. ->andReturn($socialiteUpdatedUser);
  126. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  127. $this->assertDatabaseHas('users', [
  128. 'oauth_id' => self::USER_OAUTH_ID,
  129. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  130. 'email' => 'new_email',
  131. ]);
  132. }
  133. #[Test]
  134. public function test_callback_updates_username_with_fallback_value()
  135. {
  136. $socialiteUpdatedUser = new \Laravel\Socialite\Two\User;
  137. $socialiteUpdatedUser->id = self::USER_OAUTH_ID;
  138. $socialiteUpdatedUser->name = 'new_name';
  139. $socialiteUpdatedUser->email = 'new_email';
  140. Socialite::shouldReceive('driver->user')
  141. ->andReturn($socialiteUpdatedUser);
  142. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  143. $this->assertDatabaseHas('users', [
  144. 'oauth_id' => self::USER_OAUTH_ID,
  145. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  146. 'email' => 'new_email',
  147. ]);
  148. }
  149. #[Test]
  150. public function test_callback_registers_new_user()
  151. {
  152. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  153. $newSocialiteUser->id = 'new_id';
  154. $newSocialiteUser->name = 'jane';
  155. $newSocialiteUser->email = 'jane@provider.com';
  156. Socialite::shouldReceive('driver->user')
  157. ->andReturn($newSocialiteUser);
  158. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  159. $this->assertDatabaseHas('users', [
  160. 'oauth_id' => 'new_id',
  161. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  162. 'email' => 'jane@provider.com',
  163. 'is_admin' => 0,
  164. ]);
  165. }
  166. #[Test]
  167. public function test_callback_registers_new_user_with_existing_name()
  168. {
  169. $socialiteUserWithSameName = new \Laravel\Socialite\Two\User;
  170. $socialiteUserWithSameName->id = 'socialiteUserWithSameNameId';
  171. $socialiteUserWithSameName->name = self::USER_NAME;
  172. $socialiteUserWithSameName->email = 'socialiteuserwithsamename@example.com';
  173. $socialiteUserWithSameName->nickname = self::USER_NICKNAME;
  174. Socialite::shouldReceive('driver->user')
  175. ->andReturn($socialiteUserWithSameName);
  176. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  177. $this->assertDatabaseHas('users', [
  178. 'oauth_id' => 'socialiteUserWithSameNameId',
  179. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  180. 'email' => 'socialiteuserwithsamename@example.com',
  181. ]);
  182. }
  183. #[Test]
  184. public function test_callback_always_registers_first_user_as_admin()
  185. {
  186. DB::table('users')->delete();
  187. Settings::set('disableRegistration', true);
  188. Settings::set('enableSso', false);
  189. Socialite::shouldReceive('driver->user')
  190. ->andReturn($this->socialiteUser);
  191. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  192. $this->assertDatabaseHas('users', [
  193. 'oauth_id' => self::USER_OAUTH_ID,
  194. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  195. 'is_admin' => 1,
  196. ]);
  197. }
  198. #[Test]
  199. public function test_callback_returns_error_when_email_is_already_used()
  200. {
  201. $userWithSameEmail = User::factory()->create([
  202. 'name' => 'userWithSameEmail',
  203. 'email' => 'other@example.com',
  204. 'password' => 'password',
  205. ]);
  206. $socialiteUserWithSameEmail = new \Laravel\Socialite\Two\User;
  207. $socialiteUserWithSameEmail->id = '666';
  208. $socialiteUserWithSameEmail->name = 'socialiteUserWithSameEmail';
  209. $socialiteUserWithSameEmail->email = 'other@example.com';
  210. $socialiteUserWithSameEmail->nickname = self::USER_NICKNAME;
  211. Socialite::shouldReceive('driver->user')
  212. ->andReturn($socialiteUserWithSameEmail);
  213. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  214. $response->assertRedirect('/error?err=sso_email_already_used');
  215. $this->assertDatabaseMissing('users', [
  216. 'oauth_id' => '666',
  217. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  218. ]);
  219. }
  220. #[Test]
  221. public function test_callback_redirects_to_error_when_sso_provider_reject_auth()
  222. {
  223. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  224. $newSocialiteUser->id = 'rejected_id';
  225. $newSocialiteUser->name = 'jane';
  226. $newSocialiteUser->email = 'jane@provider.com';
  227. Socialite::shouldReceive('driver->user')
  228. ->andThrow(new Exception);
  229. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  230. $response->assertRedirect('/error?err=sso_failed');
  231. }
  232. #[Test]
  233. public function test_callback_redirects_to_error_when_registrations_are_closed()
  234. {
  235. Settings::set('disableRegistration', true);
  236. Settings::set('keepSsoRegistrationEnabled', false);
  237. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  238. $newSocialiteUser->id = 'rejected_id';
  239. $newSocialiteUser->name = 'jane';
  240. $newSocialiteUser->email = 'jane@provider.com';
  241. Socialite::shouldReceive('driver->user')
  242. ->andReturn($newSocialiteUser);
  243. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  244. $response->assertRedirect('/error?err=sso_no_register');
  245. }
  246. #[Test]
  247. public function test_callback_skips_registration_when_all_registrations_are_closed()
  248. {
  249. Settings::set('disableRegistration', true);
  250. Settings::set('keepSsoRegistrationEnabled', false);
  251. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  252. $newSocialiteUser->id = 'rejected_id';
  253. $newSocialiteUser->name = 'jane';
  254. $newSocialiteUser->email = 'jane@provider.com';
  255. Socialite::shouldReceive('driver->user')
  256. ->andReturn($newSocialiteUser);
  257. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  258. $this->assertDatabaseMissing('users', [
  259. 'oauth_id' => 'rejected_id',
  260. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  261. ]);
  262. }
  263. #[Test]
  264. public function test_callback_registers_new_user_when_sso_registrations_are_enabled()
  265. {
  266. Settings::set('disableRegistration', true);
  267. Settings::set('keepSsoRegistrationEnabled', true);
  268. $newSocialiteUser = new \Laravel\Socialite\Two\User;
  269. $newSocialiteUser->id = 'new_id';
  270. $newSocialiteUser->name = 'jane';
  271. $newSocialiteUser->email = 'jane@provider.com';
  272. Socialite::shouldReceive('driver->user')
  273. ->andReturn($newSocialiteUser);
  274. $response = $this->get('/socialite/callback/github', ['driver' => 'github']);
  275. $this->assertDatabaseHas('users', [
  276. 'oauth_id' => 'new_id',
  277. 'oauth_provider' => self::USER_OAUTH_PROVIDER,
  278. 'email' => 'jane@provider.com',
  279. 'is_admin' => 0,
  280. ]);
  281. }
  282. }