Passed
Push — master ( f15488...242b86 )
by Arthur
05:00
created

HttpTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 68.28%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 82
ccs 28
cts 41
cp 0.6828
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getHttpUser() 0 6 1
A httpNoAuth() 0 3 1
A sendRequest() 0 5 2
A getUserTokenData() 0 23 2
A http() 0 3 1
A decodeHttpContent() 0 5 2
A sendRequestWithToken() 0 5 2
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;
0 ignored issues
show
Bug introduced by
The type Auth0\Login\Contract\Auth0UserRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Cache;
13
use Foundation\Exceptions\Exception;
14
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use GuzzleHttp\Exception\ClientException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Exception\ClientException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Illuminate\Contracts\Auth\Authenticatable;
17
use Modules\User\Entities\User;
18
19
abstract class HttpTest extends \Foundation\Abstracts\Tests\TestCase
20
{
21
22
    /**
23
     * @var Auth0UserRepository
24
     */
25
    protected $service;
26
27 4
    public function setUp()
28
    {
29 4
        parent::setUp();
30 4
        $this->service = $this->app->make(Auth0UserRepository::class);
31 4
    }
32
33
34
    /**
35
     * @return User | Authenticatable
36
     */
37 2
    protected function getHttpUser()
38
    {
39 2
        $auth0 = \App::make('auth0');
40 2
        $tokenInfo = $auth0->decodeJWT($this->getUserTokenData()->id_token);
41
42
        return $this->service->getUserByDecodedJWT($tokenInfo);
43
    }
44
45 2
    private function getUserTokenData(): \stdClass
46
    {
47
        return Cache::remember('testing:http_access_token', 60, function () {
48
            try {
49 2
                $httpClient = new Client();
50 2
                $domain = 'https://astral.eu.auth0.com/';
51 2
                $test = env('AUTH0_DOMAIN');
52 2
                $clientId = env('AUTH0_CLIENT_ID');
53 2
                $username = env('AUTH0_TEST_USER_NAME');
54 2
                $password = env('AUTH0_TEST_USER_PASS');
55
56 2
                $response = $httpClient->post($domain . 'oauth/token', [
57
                    'form_params' => [
58 2
                        'grant_type' => 'password',
59 2
                        'client_id' => $clientId,
60 2
                        'username' => $username,
61 2
                        'password' => $password,
62 2
                        'scope' => 'openid profile email offline_access',
63
                    ],
64
                ]);
65
                return json_decode($response->getBody()->getContents());
66 2
            } catch (ClientException $exception) {
67 2
                throw new Exception("Could not obtain token from Auth0 for testing from $test $domain $clientId $username $password" . $exception->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $domain does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $test does not seem to be defined for all execution paths leading up to this point.
Loading history...
68
            }
69 2
        });
70
    }
71
72
    protected function decodeHttpContent($content, $unwrap = true)
73
    {
74
        if ($unwrap)
75
            return json_decode($content, true)['data'];
76
        return json_decode($content, true);
77
    }
78
79
    protected function http(string $method, string $route, array $payload = [])
80
    {
81
        return $this->sendRequest($method, $route, $payload, true);
82
    }
83
84 1
    private function sendRequest(string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
85
    {
86 1
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
87
            'Authorization' => 'Bearer ' . $this->getUserTokenData()->id_token,
88 1
        ] : []);
89
    }
90
91
    protected function sendRequestWithToken($token, string $method, string $route, array $payload = [], $authenticated = true): \Illuminate\Foundation\Testing\TestResponse
92
    {
93
        return $this->json($method, env('API_URL') . '/' . $route, $payload, $authenticated ? [
94
            'Authorization' => 'Bearer ' . $token,
95
        ] : []);
96
    }
97
98 1
    protected function httpNoAuth(string $method, string $route, array $payload = [])
99
    {
100 1
        return $this->sendRequest($method, $route, $payload, false);
101
    }
102
}
103