Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
created

FilesTest::testAccountManipulation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 35
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 58
rs 9.36

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 SourcesTests\Files\Volume;
4
5
6
use CommonTestClass;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Data\FileCertUser;
9
use kalanis\kw_auth\Data\FileGroup;
10
use kalanis\kw_auth\Sources\Files\Volume\Files;
11
use kalanis\kw_locks\LockException;
12
13
14
class FilesTest extends CommonTestClass
15
{
16
    protected $sourcePath = '';
17
18
    protected function setUp(): void
19
    {
20
        $this->sourcePath = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data';
21
    }
22
23
    /**
24
     * @throws AuthException
25
     * @throws LockException
26
     */
27
    public function testDataOnly(): void
28
    {
29
        $lib = $this->fileSources();
30
        $this->assertEmpty($lib->getDataOnly('does not exist'));
31
        $user = $lib->getDataOnly('manager');
32
        $this->assertNotEmpty($user);
33
        $this->assertEquals('Manage', $user->getDisplayName());
34
    }
35
36
    /**
37
     * @throws AuthException
38
     * @throws LockException
39
     */
40
    public function testAuthenticate(): void
41
    {
42
        $lib = $this->fileSources();
43
        $this->assertEmpty($lib->authenticate('manager', ['password' => 'thisisnotreal']));
44
        $user = $lib->authenticate('manager', ['password' => 'valid']);
45
        $this->assertNotEmpty($user);
46
        $this->assertEquals('Manage', $user->getDisplayName());
47
    }
48
49
    /**
50
     * @throws AuthException
51
     * @throws LockException
52
     */
53
    public function testAuthenticateNoPass(): void
54
    {
55
        $lib = $this->fileSources();
56
        $this->expectException(AuthException::class);
57
        $lib->authenticate('manager', []);
58
    }
59
60
    /**
61
     * @throws AuthException
62
     * @throws LockException
63
     */
64
    public function testAccountManipulation(): void
65
    {
66
        $lib = $this->fileSources();
67
        $user = $this->wantedUser();
68
69
        // create
70
        $lib->createAccount($user, 'here to set');
71
        // check data
72
        $saved = $lib->getDataOnly($user->getAuthName());
73
        $this->assertEquals('Testing another', $saved->getDisplayName());
74
        $this->assertEquals('why_here', $saved->getDir());
75
        $this->assertEquals(3, $saved->getClass());
76
77
        // check login
78
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
79
80
        // update
81
        $user->setData(
82
            $user->getAuthId(),
83
            $user->getAuthName(),
84
            $user->getGroup(),
85
            2,
86
            'WheĐn yoĐu dđo nođt knđow',
87
            $user->getDir()
88
        );
89
        $user->addCertInfo('==public key for accessing that content==', 'hidden salt');
90
        $lib->updateAccount($user);
91
        $lib->updateCertKeys($user->getAuthName(), $user->getPubKey(), $user->getPubSalt());
92
93
        // update name
94
        $user->setData(
95
            $user->getAuthId(),
96
            'changed name',
97
            $user->getGroup(),
98
            $user->getClass(),
99
            $user->getDisplayName(),
100
            $user->getDir()
101
        );
102
        $lib->updateAccount($user);
103
104
        // check data - again with new values
105
        $saved = $lib->getCertData($user->getAuthName());
106
        $this->assertEquals('When you do not know', $saved->getDisplayName());
107
        $this->assertEquals(2, $saved->getClass());
108
        $this->assertEquals($user->getPubKey(), $saved->getPubKey());
109
        $this->assertEquals($user->getPubSalt(), $saved->getPubSalt());
110
111
112
        // update password
113
        $lib->updatePassword($user->getAuthName(), 'another pass');
114
        // check login
115
        $this->assertEmpty($lib->authenticate($user->getAuthName(), ['password' => 'here to set']));
116
        $this->assertNotEmpty($lib->authenticate($user->getAuthName(), ['password' => 'another pass']));
117
118
        // remove
119
        $lib->deleteAccount($user->getAuthName());
120
        // check for existence
121
        $this->assertEmpty($lib->getDataOnly($user->getAuthName()));
122
    }
123
124
    /**
125
     * @throws AuthException
126
     * @throws LockException
127
     * AuthId is not correct but auth name is
128
     */
129
    public function testAccountUpdateFail(): void
130
    {
131
        $lib = $this->fileSources();
132
        $user = new FileCertUser();
133
        $user->setData(600, 'worker', 0, 0, 'Die on set', 'so_here');
134
135
        $this->expectException(AuthException::class);
136
        $lib->updateAccount($user);
137
    }
138
139
    /**
140
     * @throws AuthException
141
     * @throws LockException
142
     */
143
    public function testCreateFail(): void
144
    {
145
        $lib = $this->fileSources();
146
        $user = $this->wantedUser();
147
        $this->expectException(AuthException::class);
148
        $lib->createAccount($user, '');
149
    }
150
151
    /**
152
     * @throws AuthException
153
     * @throws LockException
154
     */
155
    public function testAllUsers(): void
156
    {
157
        $lib = $this->fileSources();
158
        $data = $lib->readAccounts();
159
        $this->assertEquals(1, $data[0]->getClass());
160
        $this->assertEquals('manager', $data[1]->getAuthName());
161
    }
162
163
    /**
164
     * Contains a full comedy/tragedy of work with locks
165
     * @throws LockException
166
     * @return Files
167
     */
168
    protected function fileSources(): Files
169
    {
170
        return new Files(
171
            new \MockModes(),
172
            $this->getLockPath(),
173
            $this->sourcePath
174
        );
175
    }
176
177
    protected function wantedUser(): FileCertUser
178
    {
179
        $user = new FileCertUser();
180
        $user->setData(1003, 'another', 0, 0, 'Testing another', 'why_here');
181
        return $user;
182
    }
183
184
    /**
185
     * @throws AuthException
186
     * @throws LockException
187
     */
188
    public function testGroupManipulation(): void
189
    {
190
        $lib = $this->fileSources();
191
        $group = $this->wantedGroup();
192
193
        // create
194
        $lib->createGroup($group);
195
        // check data
196
        $saved = $lib->getGroupDataOnly($group->getGroupId());
197
        $this->assertEquals('another', $saved->getGroupName());
198
        $this->assertEquals('Testing group', $saved->getGroupDesc());
199
        $this->assertEquals(1001, $saved->getGroupAuthorId());
200
201
        // update
202
        $group->setData(
203
            $group->getGroupId(),
204
            $group->getGroupName(),
205
            1002,
206
            'WheĐn yoĐu dđo nođt knđow'
207
        );
208
        $lib->updateGroup($group);
209
210
        // check data - again with new values
211
        $saved = $lib->getGroupDataOnly($group->getGroupId());
212
        $this->assertEquals('When you do not know', $saved->getGroupDesc()); // overwrite this
213
        $this->assertEquals(1001, $saved->getGroupAuthorId()); // cannot overwrite this
214
215
        // remove
216
        $lib->deleteGroup($group->getGroupId());
217
        // check for existence
218
        $this->assertEmpty($lib->getGroupDataOnly($group->getGroupId()));
219
    }
220
221
    /**
222
     * @throws AuthException
223
     * @throws LockException
224
     */
225
    public function testCreateGroupFail(): void
226
    {
227
        $lib = $this->fileSources();
228
        $group = $this->wantedGroup('');
229
        $this->expectException(AuthException::class);
230
        $lib->createGroup($group);
231
    }
232
233
    /**
234
     * @throws AuthException
235
     * @throws LockException
236
     */
237
    public function testDeleteGroupFail(): void
238
    {
239
        $lib = $this->fileSources();
240
        $this->expectException(AuthException::class);
241
        $lib->deleteGroup(1);
242
    }
243
244
    /**
245
     * @throws AuthException
246
     * @throws LockException
247
     */
248
    public function testAllGroups(): void
249
    {
250
        $lib = $this->fileSources();
251
        $data = $lib->readGroup();
252
        $this->assertEquals('Maintainers', $data[0]->getGroupDesc());
253
        $this->assertEquals(1000, $data[1]->getGroupAuthorId());
254
    }
255
256
    protected function wantedGroup($name = 'another'): FileGroup
257
    {
258
        $user = new FileGroup();
259
        $user->setData(3, $name, 1001, 'Testing group');
260
        return $user;
261
    }
262
}
263