Dan Brown

Started Social Auth Testing

...@@ -113,18 +113,6 @@ class AuthTest extends TestCase ...@@ -113,18 +113,6 @@ class AuthTest extends TestCase
113 } 113 }
114 114
115 /** 115 /**
116 - * Quickly sets an array of settings.
117 - * @param $settingsArray
118 - */
119 - private function setSettings($settingsArray)
120 - {
121 - $settings = app('BookStack\Services\SettingService');
122 - foreach ($settingsArray as $key => $value) {
123 - $settings->put($key, $value);
124 - }
125 - }
126 -
127 - /**
128 * Perform a login 116 * Perform a login
129 * @param string $email 117 * @param string $email
130 * @param string $password 118 * @param string $password
......
1 +<?php
2 +
3 +class SocialAuthTest extends TestCase
4 +{
5 +
6 + public function testSocialRegistration()
7 + {
8 + // http://docs.mockery.io/en/latest/reference/startup_methods.html
9 + $user = factory(\BookStack\User::class)->make();
10 +
11 + $this->setSettings(['registration-enabled' => 'true']);
12 + $this->setEnvironment(['GOOGLE_APP_ID' => 'abc123', 'GOOGLE_APP_SECRET' => '123abc', 'APP_URL' => 'http://localhost']);
13 +
14 + $mockSocialite = Mockery::mock('Laravel\Socialite\Contracts\Factory');
15 + $this->app['Laravel\Socialite\Contracts\Factory'] = $mockSocialite;
16 + $mockSocialDriver = Mockery::mock('Laravel\Socialite\Contracts\Provider');
17 + $mockSocialUser = Mockery::mock('\Laravel\Socialite\Contracts\User');
18 +
19 + $mockSocialite->shouldReceive('driver')->twice()->with('google')->andReturn($mockSocialDriver);
20 + $mockSocialDriver->shouldReceive('redirect')->once()->andReturn(redirect('/'));
21 + $mockSocialDriver->shouldReceive('user')->once()->andReturn($mockSocialUser);
22 +
23 + $mockSocialUser->shouldReceive('getId')->twice()->andReturn(1);
24 + $mockSocialUser->shouldReceive('getEmail')->twice()->andReturn($user->email);
25 + $mockSocialUser->shouldReceive('getName')->once()->andReturn($user->name);
26 + $mockSocialUser->shouldReceive('getAvatar')->once()->andReturn('avatar_placeholder');
27 +
28 + $this->visit('/register/service/google');
29 + $this->visit('/login/service/google/callback');
30 + $this->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
31 + $user = $user->whereEmail($user->email)->first();
32 + $this->seeInDatabase('social_accounts', ['user_id' => $user->id]);
33 + }
34 +
35 + protected function setEnvironment($array)
36 + {
37 + foreach ($array as $key => $value) {
38 + putenv("$key=$value");
39 + }
40 + }
41 +
42 +}
...@@ -36,4 +36,16 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase ...@@ -36,4 +36,16 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
36 } 36 }
37 return $this->actingAs($this->admin); 37 return $this->actingAs($this->admin);
38 } 38 }
39 +
40 + /**
41 + * Quickly sets an array of settings.
42 + * @param $settingsArray
43 + */
44 + protected function setSettings($settingsArray)
45 + {
46 + $settings = app('BookStack\Services\SettingService');
47 + foreach ($settingsArray as $key => $value) {
48 + $settings->put($key, $value);
49 + }
50 + }
39 } 51 }
......