-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: profile need test too, should hide WeChat service account on web #…
- Loading branch information
Showing
8 changed files
with
155 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace sinkcup\LaravelUiSocialite\Socialite\Controllers\Settings; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Support\ViewErrorBag; | ||
use Illuminate\Validation\Rule; | ||
use sinkcup\LaravelUiSocialite\SocialAccount; | ||
use sinkcup\LaravelUiSocialite\Socialite\Controllers\Controller; | ||
use sinkcup\LaravelUiSocialite\Socialite\Controllers\SocialiteLoginController; | ||
|
||
class ProfileController extends Controller | ||
{ | ||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth'); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function edit() | ||
{ | ||
$user = auth()->user(); | ||
$social_login_providers = self::formatProviders(config('auth.social_login.providers'), request()); | ||
$linked_providers = SocialAccount::where('user_id', $user->id)->select(['provider'])->pluck('provider')->all(); | ||
return view('settings.profile', compact('user', 'social_login_providers', 'linked_providers') + | ||
['errors' => session('errors', new ViewErrorBag())] // HACK: only for test | ||
); | ||
} | ||
|
||
/** | ||
* Update the user's profile. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request) | ||
{ | ||
$user = $request->user(); | ||
$validated_data = Validator::make($request->all(), [ | ||
'email' => [ | ||
Rule::unique('users')->ignore($user->id), | ||
], | ||
'name' => 'string|max:255', | ||
])->validate(); | ||
$user->update($request->all()); | ||
return redirect(route('profile.edit')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
src/Socialite/stubs/tests/Feature/ProfileControllerTest.stub
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace sinkcup\LaravelUiSocialite\Tests\Feature; | ||
|
||
use App\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use sinkcup\LaravelUiSocialite\SocialAccount; | ||
use sinkcup\LaravelUiSocialite\Tests\TestCase; | ||
|
||
class ProfileControllerTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testEdit() | ||
{ | ||
$user = factory(User::class)->create(); | ||
$social_account = factory(SocialAccount::class)->create(['user_id' => $user->id]); | ||
$response = $this->actingAs($user)->get('/settings/profile'); | ||
|
||
$response->assertViewIs('settings.profile'); | ||
$response->assertViewHas('user', $user); | ||
$response->assertViewHas('social_login_providers', config('auth.social_login.providers')); | ||
$response->assertViewHas('linked_providers', [$social_account->provider]); | ||
} | ||
|
||
public function testEditShouldHideWeChatWebWhenVisitFromWeChatApp() | ||
{ | ||
$providers = ['github', 'wechat_service_account']; | ||
$this->app['config']->set('auth.social_login.providers', array_merge($providers, ['wechat_web'])); | ||
$user = factory(User::class)->create(); | ||
$social_account = factory(SocialAccount::class)->create(['user_id' => $user->id]); | ||
$response = $this->actingAs($user) | ||
->withHeader( | ||
'user-agent', | ||
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.5(0x17000523) NetType/WIFI Language/zh_CN' | ||
)->get('/settings/profile'); | ||
|
||
$response->assertViewIs('settings.profile'); | ||
$response->assertViewHas('user', $user); | ||
$response->assertViewHas('social_login_providers', $providers); | ||
$response->assertViewHas('linked_providers', [$social_account->provider]); | ||
} | ||
|
||
public function testEditShouldHideWeChatServiceAccountWhenVisitFromWeb() | ||
{ | ||
$providers = ['github', 'wechat_web']; | ||
$this->app['config']->set('auth.social_login.providers', array_merge($providers, ['wechat_service_account'])); | ||
$user = factory(User::class)->create(); | ||
$response = $this->actingAs($user)->get('/settings/profile'); | ||
|
||
$response->assertViewIs('settings.profile'); | ||
$response->assertViewHas('user', $user); | ||
$response->assertViewHas('social_login_providers', $providers); | ||
$response->assertViewHas('linked_providers', []); | ||
} | ||
|
||
public function testUpdate() | ||
{ | ||
$user = factory(User::class)->create(); | ||
$data = [ | ||
'email' => $this->faker->safeEmail, | ||
'name' => $this->faker->name, | ||
]; | ||
$response = $this->actingAs($user)->put('/settings/profile', $data); | ||
|
||
$response->assertRedirect(route('profile.edit')); | ||
$user->refresh(); | ||
$this->assertEquals($data['email'], $user->email); | ||
$this->assertEquals($data['name'], $user->name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters