Completed
Push — master ( 5c2719...0841d5 )
by
unknown
03:14
created

ProxyLoginTest::testLoginWithDeviceAttribute_()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 54
rs 9.6716
cc 2
eloc 30
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Containers\User\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
20
    protected $endpoint; // testing multiple endpoints form the tests
21
22
    protected $access = [
23
        'permissions' => '',
24
        'roles'       => '',
25
    ];
26
27
    private $testingFilesCreated = false;
28
29
    /**
30
     * @test
31
     */
32
    public function testClientWebAdminProxyLogin_()
33
    {
34
        $endpoint = '[email protected]/clients/web/admin/login';
35
36
        // create data to be used for creating the testing user and to be sent with the post request
37
        $data = [
38
            'email'    => '[email protected]',
39
            'password' => 'testingpass'
40
        ];
41
42
        $user = $this->getTestingUser($data);
43
        $this->actingAs($user, 'web');
44
45
        $clientId = '100';
46
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
47
48
        // create client
49
        DB::table('oauth_clients')->insert([
50
            [
51
                'id'                     => $clientId,
52
                'secret'                 => $clientSecret,
53
                'name'                   => 'Testing',
54
                'redirect'               => 'http://localhost',
55
                'password_client'        => '1',
56
                'personal_access_client' => '0',
57
                'revoked'                => '0',
58
            ],
59
        ]);
60
61
        // make the clients credentials available as env variables
62
        Config::set('authentication-container.clients.web.admin.id', $clientId);
63
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
64
65
        // create testing oauth keys files
66
        $publicFilePath = $this->createTestingKey('oauth-public.key');
67
        $privateFilePath = $this->createTestingKey('oauth-private.key');
68
69
        $response = $this->endpoint($endpoint)->makeCall($data);
70
71
        $response->assertStatus(200);
72
73
        $response->assertCookie('refreshToken');
74
75
        $this->assertResponseContainKeyValue([
76
            'token_type' => 'Bearer',
77
        ]);
78
79
        $this->assertResponseContainKeys(['expires_in', 'access_token']);
80
81
        // delete testing keys files if they were created for this test
82
        if ($this->testingFilesCreated) {
83
            unlink($publicFilePath);
84
            unlink($privateFilePath);
85
        }
86
    }
87
88
    /**
89
     * @test
90
     */
91
    public function testLoginWithNameAttribute_()
92
    {
93
        $endpoint = '[email protected]/clients/web/admin/login';
94
95
        // create data to be used for creating the testing user and to be sent with the post request
96
        $data = [
97
            'email'    => '[email protected]',
98
            'password' => 'testingpass',
99
            'name'     => 'username',
100
        ];
101
102
        $user = $this->getTestingUser($data);
103
        $this->actingAs($user, 'web');
104
105
        $clientId = '100';
106
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
107
108
        // create client
109
        DB::table('oauth_clients')->insert([
110
            [
111
                'id'                     => $clientId,
112
                'secret'                 => $clientSecret,
113
                'name'                   => 'Testing',
114
                'redirect'               => 'http://localhost',
115
                'password_client'        => '1',
116
                'personal_access_client' => '0',
117
                'revoked'                => '0',
118
            ],
119
        ]);
120
121
        // make the clients credentials available as env variables
122
        Config::set('authentication-container.clients.web.admin.id', $clientId);
123
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
124
125
        // specifically allow to login with "name" attribute
126
        Config::set('authentication-container.login.allowed_login_attributes',
127
            [
128
                'email' => ['email'],
129
                'name' => [],
130
            ]
131
        );
132
133
        // create testing oauth keys files
134
        $publicFilePath = $this->createTestingKey('oauth-public.key');
135
        $privateFilePath = $this->createTestingKey('oauth-private.key');
136
137
        $request = [
138
            'password' => 'testingpass',
139
            'name'     => 'username',
140
        ];
141
142
        $response = $this->endpoint($endpoint)->makeCall($request);
143
144
        $response->assertStatus(200);
145
146
        $response->assertCookie('refreshToken');
147
148
        $this->assertResponseContainKeyValue([
149
            'token_type' => 'Bearer',
150
        ]);
151
152
        $this->assertResponseContainKeys(['expires_in', 'access_token']);
153
154
        // delete testing keys files if they were created for this test
155
        if ($this->testingFilesCreated) {
156
            unlink($publicFilePath);
157
            unlink($privateFilePath);
158
        }
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function testLoginWithDeviceAttribute_()
165
    {
166
        $endpoint = '[email protected]/clients/web/admin/login';
167
168
        // create data to be used for creating the testing user and to be sent with the post request
169
        $data = [
170
            'email'    => '[email protected]',
171
            'password' => 'testingpass',
172
            'name'     => 'username',
173
        ];
174
175
        $user = $this->getTestingUser($data);
176
        $this->actingAs($user, 'web');
177
178
        $clientId = '100';
179
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
180
181
        // create client
182
        DB::table('oauth_clients')->insert([
183
            [
184
                'id'                     => $clientId,
185
                'secret'                 => $clientSecret,
186
                'name'                   => 'Testing',
187
                'redirect'               => 'http://localhost',
188
                'password_client'        => '1',
189
                'personal_access_client' => '0',
190
                'revoked'                => '0',
191
            ],
192
        ]);
193
194
        // make the clients credentials available as env variables
195
        Config::set('authentication-container.clients.web.admin.id', $clientId);
196
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
197
198
        // create testing oauth keys files
199
        $publicFilePath = $this->createTestingKey('oauth-public.key');
200
        $privateFilePath = $this->createTestingKey('oauth-private.key');
201
202
        $request = [
203
            'password' => 'testingpass',
204
            'device'   => 'My Fancy Device',
205
        ];
206
207
        $response = $this->endpoint($endpoint)->makeCall($request);
208
209
        // we test for HTTP 400 because the user is not allowed to login via name attribute
210
        $response->assertStatus(400);
211
212
        // delete testing keys files if they were created for this test
213
        if ($this->testingFilesCreated) {
214
            unlink($publicFilePath);
215
            unlink($privateFilePath);
216
        }
217
    }
218
219
    /**
220
     * @test
221
     */
222
    public function testClientWebAdminProxyUnconfirmedLogin_()
223
    {
224
        $endpoint = '[email protected]/clients/web/admin/login';
225
226
        // create data to be used for creating the testing user and to be sent with the post request
227
        $data = [
228
            'email'     => '[email protected]',
229
            'password'  => 'testingpass',
230
            'confirmed' => false,
231
        ];
232
233
        $user = $this->getTestingUser($data);
234
        $this->actingAs($user, 'web');
235
236
        $clientId = '100';
237
        $clientSecret = 'XXp8x4QK7d3J9R7OVRXWrhc19XPRroHTTKIbY8XX';
238
239
        // create client
240
        DB::table('oauth_clients')->insert([
241
            [
242
                'id'                     => $clientId,
243
                'secret'                 => $clientSecret,
244
                'name'                   => 'Testing',
245
                'redirect'               => 'http://localhost',
246
                'password_client'        => '1',
247
                'personal_access_client' => '0',
248
                'revoked'                => '0',
249
            ],
250
        ]);
251
252
        // make the clients credentials available as env variables
253
        Config::set('authentication-container.clients.web.admin.id', $clientId);
254
        Config::set('authentication-container.clients.web.admin.secret', $clientSecret);
255
256
        // create testing oauth keys files
257
        $publicFilePath = $this->createTestingKey('oauth-public.key');
258
        $privateFilePath = $this->createTestingKey('oauth-private.key');
259
260
        $response = $this->endpoint($endpoint)->makeCall($data);
261
262
        if (Config::get('authentication-container.require_email_confirmation')) {
263
            $response->assertStatus(409);
264
        } else {
265
            $response->assertStatus(200);
266
        }
267
268
        // delete testing keys files if they were created for this test
269
        if ($this->testingFilesCreated) {
270
            unlink($publicFilePath);
271
            unlink($privateFilePath);
272
        }
273
    }
274
275
    /**
276
     * @param $fileName
277
     *
278
     * @return  string
279
     */
280 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...
281
    {
282
        $filePath = storage_path($fileName);
283
284
        if (!file_exists($filePath)) {
285
            $keysStubDirectory = __DIR__ . '/Stubs/';
286
287
            copy($keysStubDirectory . $fileName, $filePath);
288
289
            $this->testingFilesCreated = true;
290
        }
291
292
        return $filePath;
293
    }
294
}
295