-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: Queues | ||
--- | ||
|
||
When using `MsGraph` within jobs, commands, or other queued processes, you need to authenticate the user explicitly. Ensure the user model has `access_token` and `refresh_token` stored in the database. Use the `login` method to authenticate the user before making any API calls: | ||
|
||
```php | ||
MsGraph::login(User::find(1)); | ||
MsGraph::get('me'); | ||
``` | ||
|
||
Here's an example of how to structure a job that uses `MsGraph`: | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\User; | ||
use Dcblogdev\MsGraph\Facades\MsGraph; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ExampleMsGraphJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
protected $user; | ||
|
||
public function __construct(User $user) | ||
{ | ||
$this->user = $user; | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
MsGraph::login($this->user); | ||
$userData = MsGraph::get('me'); | ||
// Process $userData as needed | ||
} | ||
} | ||
``` | ||
|
||
Dispatch this job with a user instance: | ||
|
||
```php | ||
ExampleMsGraphJob::dispatch($user); | ||
``` | ||
|
||
This approach ensures that the Microsoft Graph API calls are made with the correct user context, even in background processes. |
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
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 |
---|---|---|
|
@@ -3,8 +3,11 @@ | |
use Dcblogdev\MsGraph\Facades\MsGraph as MsGraphFacade; | ||
use Dcblogdev\MsGraph\Models\MsGraphToken; | ||
use Dcblogdev\MsGraph\MsGraph; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Routing\Redirector; | ||
use Illuminate\Support\Facades\Auth; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use League\OAuth2\Client\Provider\GenericProvider; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use Mockery\MockInterface; | ||
|
@@ -170,3 +173,79 @@ | |
'refresh_token' => $refreshToken, | ||
]); | ||
}); | ||
|
||
class TestUser extends Authenticatable | ||
{ | ||
protected $fillable = ['id', 'email']; | ||
} | ||
|
||
test('can login and set user', function () { | ||
$user = new TestUser(['id' => 1, 'email' => '[email protected]']); | ||
|
||
MsGraphFacade::login($user); | ||
|
||
expect(MsGraphFacade::getUser())->toBe($user); | ||
}); | ||
|
||
test('getUser returns null when no user is logged in', function () { | ||
MsGraphFacade::login(null); // Reset the user | ||
expect(MsGraphFacade::getUser())->toBeNull(); | ||
}); | ||
|
||
test('getAccessToken uses logged in user when available', function () { | ||
$user = new TestUser(['id' => 1, 'email' => '[email protected]']); | ||
|
||
MsGraphFacade::login($user); | ||
|
||
MsGraphToken::create([ | ||
'user_id' => $user->id, | ||
'access_token' => 'test_token', | ||
'refresh_token' => 'refresh_token', | ||
'expires' => strtotime('+1 day'), | ||
]); | ||
|
||
$token = MsGraphFacade::getAccessToken(); | ||
|
||
expect($token)->toBe('test_token'); | ||
}); | ||
|
||
test('getUserId returns logged in user id when available', function () { | ||
$user = new TestUser(['id' => 1, 'email' => '[email protected]']); | ||
|
||
MsGraphFacade::login($user); | ||
|
||
$reflection = new ReflectionClass(MsGraphFacade::getFacadeRoot()); | ||
$method = $reflection->getMethod('getUserId'); | ||
$method->setAccessible(true); | ||
|
||
$userId = $method->invoke(MsGraphFacade::getFacadeRoot()); | ||
|
||
expect($userId)->toBe('1'); | ||
}); | ||
|
||
test('getUserId falls back to auth id when no user is logged in', function () { | ||
MsGraphFacade::login(null); // Reset the user | ||
|
||
$user = new TestUser(['id' => 2, 'email' => '[email protected]']); | ||
Auth::shouldReceive('id')->andReturn(2); | ||
|
||
$reflection = new ReflectionClass(MsGraphFacade::getFacadeRoot()); | ||
$method = $reflection->getMethod('getUserId'); | ||
$method->setAccessible(true); | ||
|
||
$userId = $method->invoke(MsGraphFacade::getFacadeRoot()); | ||
|
||
expect($userId)->toBe('2'); | ||
}); | ||
|
||
test('getAccessToken redirects when user is not connected and redirectWhenNotConnected is true', function () { | ||
config(['msgraph.redirectUri' => 'http://example.com/redirect']); | ||
|
||
MsGraphFacade::login(null); // Reset the user | ||
Auth::shouldReceive('id')->andReturn(null); | ||
|
||
$response = MsGraphFacade::getAccessToken(null, true); | ||
|
||
expect($response)->toBeInstanceOf(RedirectResponse::class) | ||
->and($response->getTargetUrl())->toBe('http://example.com/redirect'); | ||
}); |