1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Unit\Middleware; |
5
|
|
|
|
6
|
|
|
use Longman\LaravelLodash\Middlewares\SimpleBasicAuth; |
7
|
|
|
use Tests\Unit\TestCase; |
8
|
|
|
|
9
|
|
|
use function config; |
10
|
|
|
|
11
|
|
|
class SimpleBasicAuthTest extends TestCase |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
protected function getEnvironmentSetUp($app) |
14
|
|
|
{ |
15
|
|
|
/** @var \Illuminate\Routing\Router $router */ |
16
|
|
|
$router = $app['router']; |
17
|
|
|
|
18
|
|
|
$router->get('url1', static function () { |
19
|
|
|
return 'ok'; |
20
|
|
|
})->middleware(SimpleBasicAuth::class); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** @test */ |
24
|
|
View Code Duplication |
public function it_should_return_access_denied_on_empty_credentials() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
config()->set('auth.simple', [ |
27
|
|
|
'enabled' => true, |
28
|
|
|
'user' => 'testuser', |
29
|
|
|
'password' => 'testpass', |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$response = $this->call('GET', 'url1', [], [], [], []); |
|
|
|
|
33
|
|
|
$response->assertStatus(401); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @test */ |
37
|
|
View Code Duplication |
public function it_should_return_access_denied_on_wrong_credentials() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
config()->set('auth.simple', [ |
40
|
|
|
'enabled' => true, |
41
|
|
|
'user' => 'testuser', |
42
|
|
|
'password' => 'testpass', |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$response = $this->call('GET', 'url1', [], [], [], ['PHP_AUTH_USER' => 'testuser', 'PHP_AUTH_PW' => 'wrongpass']); |
|
|
|
|
46
|
|
|
$response->assertStatus(401); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @test */ |
50
|
|
View Code Duplication |
public function it_should_return_ok_on_disabled_auth() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
config()->set('auth.simple', [ |
53
|
|
|
'enabled' => false, |
54
|
|
|
'user' => 'testuser', |
55
|
|
|
'password' => 'testpass', |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$response = $this->call('GET', 'url1', [], [], [], []); |
|
|
|
|
59
|
|
|
$response->assertStatus(200); |
|
|
|
|
60
|
|
|
$response->assertSeeText('ok'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** @test */ |
64
|
|
View Code Duplication |
public function it_should_return_ok_with_credentials() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
config()->set('auth.simple', [ |
67
|
|
|
'enabled' => true, |
68
|
|
|
'user' => 'testuser', |
69
|
|
|
'password' => 'testpass', |
70
|
|
|
]); |
71
|
|
|
$response = $this->call('GET', 'url1', [], [], [], ['PHP_AUTH_USER' => 'testuser', 'PHP_AUTH_PW' => 'testpass']); |
|
|
|
|
72
|
|
|
$response->assertStatus(200); |
|
|
|
|
73
|
|
|
$response->assertSeeText('ok'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|