|
3 | 3 | use Dcblogdev\MsGraph\Facades\MsGraph as MsGraphFacade;
|
4 | 4 | use Dcblogdev\MsGraph\Models\MsGraphToken;
|
5 | 5 | use Dcblogdev\MsGraph\MsGraph;
|
| 6 | +use Illuminate\Foundation\Auth\User as Authenticatable; |
6 | 7 | use Illuminate\Http\RedirectResponse;
|
7 | 8 | use Illuminate\Routing\Redirector;
|
| 9 | +use Illuminate\Support\Facades\Auth; |
8 | 10 | use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
|
9 | 11 | use League\OAuth2\Client\Provider\GenericProvider;
|
10 | 12 | use League\OAuth2\Client\Token\AccessToken;
|
|
171 | 173 | 'refresh_token' => $refreshToken,
|
172 | 174 | ]);
|
173 | 175 | });
|
| 176 | + |
| 177 | +class TestUser extends Authenticatable |
| 178 | +{ |
| 179 | + protected $fillable = ['id', 'email']; |
| 180 | +} |
| 181 | + |
| 182 | +test('can login and set user', function () { |
| 183 | + $user = new TestUser([ 'id' => 1, 'email' => '[email protected]']); |
| 184 | + |
| 185 | + MsGraphFacade::login($user); |
| 186 | + |
| 187 | + expect(MsGraphFacade::getUser())->toBe($user); |
| 188 | +}); |
| 189 | + |
| 190 | +test('getUser returns null when no user is logged in', function () { |
| 191 | + MsGraphFacade::login(null); // Reset the user |
| 192 | + expect(MsGraphFacade::getUser())->toBeNull(); |
| 193 | +}); |
| 194 | + |
| 195 | +test('getAccessToken uses logged in user when available', function () { |
| 196 | + $user = new TestUser([ 'id' => 1, 'email' => '[email protected]']); |
| 197 | + |
| 198 | + MsGraphFacade::login($user); |
| 199 | + |
| 200 | + MsGraphToken::create([ |
| 201 | + 'user_id' => $user->id, |
| 202 | + 'access_token' => 'test_token', |
| 203 | + 'refresh_token' => 'refresh_token', |
| 204 | + 'expires' => strtotime('+1 day'), |
| 205 | + ]); |
| 206 | + |
| 207 | + $token = MsGraphFacade::getAccessToken(); |
| 208 | + |
| 209 | + expect($token)->toBe('test_token'); |
| 210 | +}); |
| 211 | + |
| 212 | +test('getUserId returns logged in user id when available', function () { |
| 213 | + $user = new TestUser([ 'id' => 1, 'email' => '[email protected]']); |
| 214 | + |
| 215 | + MsGraphFacade::login($user); |
| 216 | + |
| 217 | + $reflection = new ReflectionClass(MsGraphFacade::getFacadeRoot()); |
| 218 | + $method = $reflection->getMethod('getUserId'); |
| 219 | + $method->setAccessible(true); |
| 220 | + |
| 221 | + $userId = $method->invoke(MsGraphFacade::getFacadeRoot()); |
| 222 | + |
| 223 | + expect($userId)->toBe('1'); |
| 224 | +}); |
| 225 | + |
| 226 | +test('getUserId falls back to auth id when no user is logged in', function () { |
| 227 | + MsGraphFacade::login(null); // Reset the user |
| 228 | + |
| 229 | + $user = new TestUser([ 'id' => 2, 'email' => '[email protected]']); |
| 230 | + Auth::shouldReceive('id')->andReturn(2); |
| 231 | + |
| 232 | + $reflection = new ReflectionClass(MsGraphFacade::getFacadeRoot()); |
| 233 | + $method = $reflection->getMethod('getUserId'); |
| 234 | + $method->setAccessible(true); |
| 235 | + |
| 236 | + $userId = $method->invoke(MsGraphFacade::getFacadeRoot()); |
| 237 | + |
| 238 | + expect($userId)->toBe('2'); |
| 239 | +}); |
| 240 | + |
| 241 | +test('getAccessToken redirects when user is not connected and redirectWhenNotConnected is true', function () { |
| 242 | + config(['msgraph.redirectUri' => 'http://example.com/redirect']); |
| 243 | + |
| 244 | + MsGraphFacade::login(null); // Reset the user |
| 245 | + Auth::shouldReceive('id')->andReturn(null); |
| 246 | + |
| 247 | + $response = MsGraphFacade::getAccessToken(null, true); |
| 248 | + |
| 249 | + expect($response)->toBeInstanceOf(RedirectResponse::class) |
| 250 | + ->and($response->getTargetUrl())->toBe('http://example.com/redirect'); |
| 251 | +}); |
0 commit comments