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