ProxyRefreshTest::createTestingKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
rs 9.7998
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Containers\User\UI\API\Tests\Functional;
4
5
use App\Containers\Authentication\Exceptions\RefreshTokenMissedException;
6
use App\Containers\Authentication\Tests\ApiTestCase;
7
use Config;
8
use Illuminate\Support\Facades\DB;
9
10
/**
11
 * Class ProxyRefreshTest
12
 *
13
 * @group authorization
14
 * @group api
15
 *
16
 * @author  Mahmoud Zalt  <[email protected]>
17
 */
18
class ProxyRefreshTest extends ApiTestCase
19
{
20
21
    protected $endpoint = '[email protected]/clients/web/admin/refresh';
22
23
    protected $access = [
24
        'permissions' => '',
25
        'roles'       => '',
26
    ];
27
28
    private $testingFilesCreated = false;
29
30
    /**
31
     * @test
32
     */
33
    public function testRefreshWithoutBeingCreatedOrPassed()
34
    {
35
        $user = $this->getTestingUser([
36
            'email'    => '[email protected]',
37
            'password' => 'testingpass'
38
        ]);
39
40
        $this->actingAs($user, 'api');
41
42
        $clientId = '100';
43
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
44
45
        // create client
46
        DB::table('oauth_clients')->insert([
47
            [
48
                'id'                     => $clientId,
49
                'secret'                 => $clientSecret,
50
                'name'                   => 'Testing',
51
                'redirect'               => 'http://localhost',
52
                'password_client'        => '1',
53
                'personal_access_client' => '0',
54
                'revoked'                => '0',
55
            ],
56
        ]);
57
58
        // make the clients credentials available as env variables
59
        Config::set('authentication-container.clients.web.admin.id', $clientId);
60
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
61
62
        // create testing oauth keys files
63
        $publicFilePath = $this->createTestingKey('oauth-public.key');
64
        $privateFilePath = $this->createTestingKey('oauth-private.key');
65
66
        $data = [
67
            'refresh_token' => null
68
        ];
69
70
        $response = $this->makeCall($data);
71
72
        $response->assertStatus(400);
73
74
        $message = (new RefreshTokenMissedException())->getMessage();
75
        $this->assertResponseContainKeyValue(['message' => $message]);
76
77
        // delete testing keys files if they were created for this test
78
        if ($this->testingFilesCreated) {
79
            unlink($publicFilePath);
80
            unlink($privateFilePath);
81
        }
82
    }
83
84
    /**
85
     * @param $fileName
86
     *
87
     * @return  string
88
     */
89 View Code Duplication
    private function createTestingKey($fileName)
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $filePath = storage_path($fileName);
92
93
        if (!file_exists($filePath)) {
94
            $keysStubDirectory = __DIR__ . '/Stubs/';
95
96
            copy($keysStubDirectory . $fileName, $filePath);
97
98
            $this->testingFilesCreated = true;
99
        }
100
101
        return $filePath;
102
    }
103
}
104