Skip to content

Commit

Permalink
fix: profile need test too, should hide WeChat service account on web #…
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup committed Sep 10, 2019
1 parent ec8c7ff commit 7bc2aeb
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 117 deletions.
15 changes: 15 additions & 0 deletions src/Socialite/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@
namespace sinkcup\LaravelUiSocialite\Socialite\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public static function formatProviders($providers, Request $request)
{
// "WeChat Service Account Login" must be used in WeChat app.
if (!stripos($request->header('user-agent'), 'MicroMessenger')) {
if (in_array('wechat_service_account', $providers)) {
unset($providers[array_search('wechat_service_account', $providers)]);
}
} elseif (in_array('wechat_service_account', $providers)
&& in_array('wechat_web', $providers)) {
unset($providers[array_search('wechat_web', $providers)]);
}
return $providers;
}
}
58 changes: 58 additions & 0 deletions src/Socialite/Controllers/Settings/ProfileController.php
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'));
}
}
14 changes: 3 additions & 11 deletions src/Socialite/Controllers/SocialiteLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace sinkcup\LaravelUiSocialite\Socialite\Controllers;

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ViewErrorBag;
use Laravel\Socialite\Facades\Socialite;
Expand All @@ -22,23 +23,14 @@ class SocialiteLoginController extends Controller

use AuthenticatesUsers;

/**
/**
* Show the application's login form.
*
* @return \Illuminate\Http\Response
*/
public function showLoginForm()
{
$providers = config('auth.social_login.providers');
// "WeChat Service Account Login" must be used in WeChat app.
if (!stripos(request()->header('user-agent'), 'MicroMessenger')) {
if (in_array('wechat_service_account', $providers)) {
unset($providers[array_search('wechat_service_account', $providers)]);
}
} elseif (in_array('wechat_service_account', $providers)
&& in_array('wechat_web', $providers)) {
unset($providers[array_search('wechat_web', $providers)]);
}
$providers = self::formatProviders(config('auth.social_login.providers'), request());
// "WeChat Web Login" will failed if you:
// open URL in WeChat app and then "Scan QR Code in Image", or "Choose QR Code from Album"
if (in_array('wechat_web', $providers)) {
Expand Down
49 changes: 2 additions & 47 deletions src/Socialite/stubs/controllers/settings/ProfileController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,8 @@

namespace {{namespace}}Http\Controllers\Settings;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use sinkcup\LaravelUiSocialite\SocialAccount;
use {{namespace}}Http\Controllers\Controller;
use sinkcup\LaravelUiSocialite\Socialite\Controllers\Settings\ProfileController as PackageProfileController;

class ProfileController extends Controller
class ProfileController extends PackageProfileController
{
/**
* 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 = config('auth.social_login.providers');
$linked_providers = SocialAccount::where('user_id', $user->id)->select(['provider'])->pluck('provider')->all();
return view('settings.profile', compact('user', 'social_login_providers', 'linked_providers'));
}

/**
* 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'));
}
}
54 changes: 0 additions & 54 deletions src/Socialite/stubs/tests/Feature/ProfileControllerTest.stub

This file was deleted.

4 changes: 0 additions & 4 deletions src/SocialiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ protected function exportBackend()
__DIR__ . '/Socialite/stubs/tests/TestCase.stub',
app_path() . '/../tests/TestCase.php'
);
file_put_contents(
app_path() . '/../tests/Feature/ProfileControllerTest.php',
$this->compileStub('tests/Feature/ProfileControllerTest.stub')
);

$web_routes = file_get_contents(base_path('routes/web.php'));
foreach (explode("\n", file_get_contents(__DIR__ . '/Socialite/stubs/routes.stub')) as $line) {
Expand Down
71 changes: 71 additions & 0 deletions tests/Feature/ProfileControllerTest.php
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);
}
}
7 changes: 6 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ protected function setUp(): void
'uses' => 'App\Http\Controllers\Auth\LoginController@handleProviderCallback',
]);
Route::get('settings/profile', [
'uses' => 'App\Http\Controllers\ProfileController@edit',
'uses' => 'App\Http\Controllers\Settings\ProfileController@edit',
'as' => 'profile.edit',
]);
Route::put('settings/profile', [
'uses' => 'App\Http\Controllers\Settings\ProfileController@update',
'as' => 'profile.update',
]);
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
$laravel_path = __DIR__ . '/../vendor/orchestra/testbench-core/laravel';
@mkdir($laravel_path . '/app/Http/Controllers/', 0755, true);
@mkdir($laravel_path . '/routes/');
Expand Down

0 comments on commit 7bc2aeb

Please sign in to comment.