|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arthur |
|
5
|
|
|
* Date: 09.10.18 |
|
6
|
|
|
* Time: 21:56. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Foundation\Abstracts\Tests; |
|
10
|
|
|
|
|
11
|
|
|
use Auth0\Login\Contract\Auth0UserRepository; |
|
12
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
13
|
|
|
use Illuminate\Foundation\Testing\TestResponse; |
|
14
|
|
|
use Modules\Auth0\Services\Auth0Service; |
|
15
|
|
|
use Modules\Authorization\Entities\Role; |
|
16
|
|
|
use Modules\User\Entities\User; |
|
17
|
|
|
|
|
18
|
|
|
abstract class HttpTest extends \Foundation\Abstracts\Tests\TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Auth0Service |
|
22
|
|
|
*/ |
|
23
|
|
|
private $auth0; |
|
24
|
|
|
|
|
25
|
|
|
protected $roles = Role::USER; |
|
26
|
|
|
|
|
27
|
20 |
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
20 |
|
parent::setUp(); |
|
30
|
20 |
|
} |
|
31
|
|
|
|
|
32
|
18 |
|
protected function getAuth0Service() |
|
33
|
|
|
{ |
|
34
|
18 |
|
if ($this->auth0 === null) { |
|
35
|
18 |
|
$this->auth0 = $this->app->make(Auth0UserRepository::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
18 |
|
return $this->auth0; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return User | Authenticatable |
|
43
|
|
|
*/ |
|
44
|
17 |
|
protected function getHttpUser() |
|
45
|
|
|
{ |
|
46
|
17 |
|
return $this->getAuth0Service()->getTestUser($this->roles); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
protected function changeTestUserRoles($roles) |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->getAuth0Service()->getTestUser($roles); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
protected function decodeHttpResponse($content, $unwrap = true) |
|
55
|
|
|
{ |
|
56
|
6 |
|
if ($content instanceof TestResponse) { |
|
57
|
4 |
|
$content = $content->content(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
if ($unwrap) { |
|
61
|
6 |
|
return json_decode($content, true)['data'] ?? json_decode($content, true); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return json_decode($content, true); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
16 |
|
protected function http(string $method, string $route, array $payload = []) |
|
68
|
|
|
{ |
|
69
|
16 |
|
return $this->sendRequest($method, $route, $payload, true); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
17 |
|
private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse |
|
73
|
|
|
{ |
|
74
|
17 |
|
return $this->json($method, env('API_URL').'/'.$route, $payload, $authenticated ? [ |
|
75
|
16 |
|
'Authorization' => 'Bearer '.$this->getAuth0Service()->getTestUserToken()->id_token, |
|
|
|
|
|
|
76
|
17 |
|
] : []); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function sendRequestWithToken($token, string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->json($method, env('API_URL').'/'.$route, $payload, $authenticated ? [ |
|
82
|
|
|
'Authorization' => 'Bearer '.$token, |
|
83
|
|
|
] : []); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
protected function httpNoAuth(string $method, string $route, array $payload = []) |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->sendRequest($method, $route, $payload, false); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|