I am building an application that has features to login with google.
Here is the function to login with google with redirect
/**
* @return mixed
*/
public function loginWithGoogle () {
return Socialite::driver('google')
->scopes([
'https://www.googleapis.com/auth/userinfo.email'
])
->with([
'access_type' => 'offline',
'prompt' => 'consent select_account'
])
->redirect();
}
Below is the function to redirect from google login
/**
* @return RedirectResponse
*/
public function loginRedirectFromGoogle (): RedirectResponse
{
$user = Socialite::driver('google')->stateless()->user();
$acUser = User::where('email', $user->getEmail())->first();
if(empty($acUser)){
return redirect('/oauth/login?'.http_build_query([
'force_external_oauth_response' => 'true',
'external_oauth_response_error' => 'User account not register'
]));
}
Auth::login($acUser);
OAuthProvider::updateOrCreate(
[
'provider_name' => 'google',
'provider_id' => $user->id,
'user_id' => $acUser->id
],
[
'encrypted_token' => Crypt::encryptString($user->token),
'encrypted_refreshed_token' => Crypt::encryptString($user->refreshToken),
'expires_at' => time() + $user->expiresIn // time and expiresIn are all in seconds
]
);
return redirect('/oauth/login?'.http_build_query([
'force_external_oauth_response' => 'true',
'external_oauth_response_error' => ''
]));
}
Now I am trying to write the Unit Test for these two functions. I could not figure it out the way to do it.
Here are routes
Route::get('/oauths/redirect', [AuthController::class, 'loginWithGoogle']);
Route::get('/oauths/callback', [AuthController::class, 'loginRedirectFromGoogle']);
Here is AuthControllerTest file
public function testRedirectWithGoogle()
{
$abstractUser = Mockery::mock('Laravel\Socialite\Two\User');
$abstractUser
->shouldReceive('getId')
->andReturn(rand())
->shouldReceive('getName')
->andReturn('test user')
->shouldReceive('getEmail')
->andReturn('test.user' . '@gmail.com')
->shouldReceive('getAvatar')
->andReturn('https://en.gravatar.com/userimage');
$provider = Mockery::mock('Laravel\Socialite\Contracts\Provider');
$provider->shouldReceive('user')->andReturn($abstractUser);
Socialite::shouldReceive('driver')->with('google')
->andReturn($provider);
}