ProxyLoginTest::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
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Containers\Authentication\UI\API\Tests\Functional;
4
5
use App\Containers\Authentication\Tests\ApiTestCase;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\Facades\DB;
8
9
/**
10
 * Class ProxyLoginTest
11
 *
12
 * @group authorization
13
 * @group api
14
 *
15
 * @author  Mahmoud Zalt  <[email protected]>
16
 */
17
class ProxyLoginTest extends ApiTestCase
18
{
19
    protected $endpoint = '[email protected]/clients/web/admin/login';
20
21
    protected $access = [
22
        'permissions' => '',
23
        'roles'       => '',
24
    ];
25
26
    private $testingFilesCreated = false;
27
28
    /**
29
     * @test
30
     */
31
    public function testClientWebAdminProxyLogin_()
32
    {
33
        // create data to be used for creating the testing user and to be sent with the post request
34
        $data = [
35
            'email'    => '[email protected]',
36
            'password' => 'testingpass'
37
        ];
38
39
        $user = $this->getTestingUser($data);
40
        $this->actingAs($user, 'web');
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
        $response = $this->makeCall($data);
67
68
        $response->assertStatus(200);
69
70
        $response->assertCookie('refreshToken');
71
72
        $this->assertResponseContainKeyValue([
73
            'token_type' => 'Bearer',
74
        ]);
75
76
        $this->assertResponseContainKeys(['expires_in', 'access_token']);
77
78
        // delete testing keys files if they were created for this test
79
        if ($this->testingFilesCreated) {
80
            unlink($publicFilePath);
81
            unlink($privateFilePath);
82
        }
83
    }
84
85
    /**
86
     * @test
87
     */
88
    public function testClientWebAdminProxyUnconfirmedLogin_()
89
    {
90
        // create data to be used for creating the testing user and to be sent with the post request
91
        $data = [
92
            'email'     => '[email protected]',
93
            'password'  => 'testingpass',
94
            'confirmed' => false,
95
        ];
96
97
        $user = $this->getTestingUser($data);
98
        $this->actingAs($user, 'web');
99
100
        $clientId = '100';
101
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
102
103
        // create client
104
        DB::table('oauth_clients')->insert([
105
            [
106
                'id'                     => $clientId,
107
                'secret'                 => $clientSecret,
108
                'name'                   => 'Testing',
109
                'redirect'               => 'http://localhost',
110
                'password_client'        => '1',
111
                'personal_access_client' => '0',
112
                'revoked'                => '0',
113
            ],
114
        ]);
115
116
        // make the clients credentials available as env variables
117
        Config::set('authentication-container.clients.web.admin.id', $clientId);
118
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
119
120
        // create testing oauth keys files
121
        $publicFilePath = $this->createTestingKey('oauth-public.key');
122
        $privateFilePath = $this->createTestingKey('oauth-private.key');
123
124
        $response = $this->makeCall($data);
125
126
        if (Config::get('authentication-container.require_email_confirmation')) {
127
            $response->assertStatus(409);
128
        } else {
129
            $response->assertStatus(200);
130
        }
131
132
        // delete testing keys files if they were created for this test
133
        if ($this->testingFilesCreated) {
134
            unlink($publicFilePath);
135
            unlink($privateFilePath);
136
        }
137
    }
138
139
    public function testLoginWithNameAttribute()
140
    {
141
        // create data to be used for creating the testing user and to be sent with the post request
142
        $data = [
143
            'email'    => '[email protected]',
144
            'password' => 'testingpass',
145
            'name'     => 'username',
146
        ];
147
148
        $user = $this->getTestingUser($data);
149
        $this->actingAs($user, 'web');
150
151
        $clientId = '100';
152
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
153
154
        // create client
155
        DB::table('oauth_clients')->insert([
156
            [
157
              'id'                     => $clientId,
158
              'secret'                 => $clientSecret,
159
              'name'                   => 'Testing',
160
              'redirect'               => 'http://localhost',
161
              'password_client'        => '1',
162
              'personal_access_client' => '0',
163
              'revoked'                => '0',
164
            ],
165
        ]);
166
167
        // make the clients credentials available as env variables
168
        Config::set('authentication-container.clients.web.admin.id', $clientId);
169
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
170
171
        // specifically allow to login with "name" attribute
172
        Config::set('authentication-container.login.attributes',
173
            [
174
              'email' => ['email'],
175
              'name' => [],
176
            ]
177
        );
178
179
        // create testing oauth keys files
180
        $publicFilePath = $this->createTestingKey('oauth-public.key');
181
        $privateFilePath = $this->createTestingKey('oauth-private.key');
182
183
        $request = [
184
            'password' => 'testingpass',
185
            'name'     => 'username',
186
        ];
187
188
        $response = $this->makeCall($request);
189
190
        $response->assertStatus(200);
191
192
        $this->assertResponseContainKeyValue([
193
            'token_type' => 'Bearer',
194
        ]);
195
196
        $this->assertResponseContainKeys(['expires_in', 'access_token', 'refresh_token']);
197
198
        // delete testing keys files if they were created for this test
199
        if ($this->testingFilesCreated) {
200
          unlink($publicFilePath);
201
          unlink($privateFilePath);
202
        }
203
    }
204
205
    /**
206
     * @param $fileName
207
     *
208
     * @return  string
209
     */
210 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...
211
    {
212
        $filePath = storage_path($fileName);
213
214
        if (!file_exists($filePath)) {
215
            $keysStubDirectory = __DIR__ . '/Stubs/';
216
217
            copy($keysStubDirectory . $fileName, $filePath);
218
219
            $this->testingFilesCreated = true;
220
        }
221
222
        return $filePath;
223
    }
224
}
225