Skip to content

Commit 469a9d2

Browse files
committed
added middleware tests
1 parent e32498e commit 469a9d2

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraphAdmin;
4+
use Dcblogdev\MsGraph\MsGraphAdminAuthenticated;
5+
use Illuminate\Support\Facades\Route;
6+
7+
beforeEach(function () {
8+
Route::middleware(MsGraphAdminAuthenticated::class)->get('/test-route', function () {
9+
return response()->json(['message' => 'Access granted']);
10+
});
11+
});
12+
13+
test('redirects to MsGraphAdmin::connect() when not connected', function () {
14+
// Mock MsGraphAdmin::isConnected() to return false
15+
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(false);
16+
MsGraphAdmin::shouldReceive('connect')->once()->andReturn(redirect('https://login.microsoftonline.com'));
17+
18+
$response = $this->get('/test-route');
19+
20+
$response->assertRedirect('https://login.microsoftonline.com');
21+
});
22+
23+
test('allows request when MsGraphAdmin is connected', function () {
24+
// Mock MsGraphAdmin::isConnected() to return true
25+
MsGraphAdmin::shouldReceive('isConnected')->once()->andReturn(true);
26+
27+
$response = $this->get('/test-route');
28+
29+
$response->assertOk()
30+
->assertJson(['message' => 'Access granted']);
31+
});

tests/MsGraphAuthenticatedTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Dcblogdev\MsGraph\Facades\MsGraph;
4+
use Dcblogdev\MsGraph\MsGraphAuthenticated;
5+
use Illuminate\Support\Facades\Route;
6+
7+
beforeEach(function () {
8+
Route::middleware(MsGraphAuthenticated::class)->get('/test-route', function () {
9+
return response()->json(['message' => 'Access granted']);
10+
});
11+
});
12+
13+
test('redirects to MsGraph::connect() when not connected', function () {
14+
// Mock MsGraph::isConnected() to return false
15+
MsGraph::shouldReceive('isConnected')->once()->andReturn(false);
16+
MsGraph::shouldReceive('connect')->once()->andReturn(redirect('https://login.microsoftonline.com'));
17+
18+
$response = $this->get('/test-route');
19+
20+
$response->assertRedirect('https://login.microsoftonline.com');
21+
});
22+
23+
test('allows request when MsGraph is connected', function () {
24+
// Mock MsGraph::isConnected() to return true
25+
MsGraph::shouldReceive('isConnected')->once()->andReturn(true);
26+
27+
$response = $this->get('/test-route');
28+
29+
$response->assertOk()
30+
->assertJson(['message' => 'Access granted']);
31+
});

0 commit comments

Comments
 (0)