-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupdate.test.php
135 lines (101 loc) Β· 3.16 KB
/
update.test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
beforeAll(function () {
$auth = authInstance();
$auth->config(['db.table' => 'users']);
$auth->register([
'username' => 'test-user-1',
'email' => '[email protected]',
'password' => 'password',
]);
$auth->register([
'username' => 'test-user-2',
'email' => '[email protected]',
'password' => 'password',
]);
sleep(1);
});
afterAll(function () {
dbInstance()->delete('users')->execute();
});
test('update should update user data', function () {
$auth = authInstance();
$auth->config(['db.table' => 'users']);
$success = $auth->login([
'username' => 'test-user-1',
'password' => 'password'
]);
if (!$success) {
$this->fail(json_encode($auth->errors()));
}
$updateData = [
'username' => 'test-user-3',
];
$updateSuccess = $auth->update($updateData);
if (!$updateSuccess) {
$this->fail(json_encode($auth->errors()));
}
expect($auth->user()->username)->toBe($updateData['username']);
});
test('update should fail if user already exists', function () {
$auth = authInstance();
$auth->config(['db.table' => 'users']);
$success = $auth->login([
'username' => 'test-user-3',
'password' => 'password'
]);
if (!$success) {
$this->fail(json_encode($auth->errors()));
}
$updateData = [
'username' => 'test-user-2',
];
$updateSuccess = $auth->update($updateData);
expect($updateSuccess)->toBeFalse();
expect($auth->errors()['username'])->toBe('username already exists');
});
test('updatePassword should update user password', function () {
$auth = authInstance();
$auth->config(['unique' => ['username']]);
$auth->config(['db.table' => 'users']);
$success = $auth->login([
'username' => 'test-user-2',
'password' => 'password'
]);
if (!$success) {
$this->fail(json_encode($auth->errors()));
}
$oldPassword = 'password';
$newPassword = 'new-password';
$updateSuccess = $auth->updatePassword($oldPassword, $newPassword);
if (!$updateSuccess) {
$this->fail(json_encode($auth->errors()));
}
$loginSuccess = $auth->login([
'username' => 'test-user-2',
'password' => 'new-password'
]);
expect($loginSuccess)->toBeTrue();
expect($auth->user()->{$auth->config('password.key')})->not()->toBe($oldPassword);
});
test('update should regenerate session id if session => true', function () {
$auth = authInstance();
$auth->config(['session' => true]);
$auth->config(['db.table' => 'users']);
$success = $auth->login([
'username' => 'test-user-2',
'password' => 'new-password'
]);
if (!$success) {
$this->fail(json_encode($auth->errors()));
}
$updateData = [
'username' => 'test-user-5',
];
$initialSessionId = session_id();
$updateSuccess = $auth->update($updateData);
if (!$updateSuccess) {
$this->fail(json_encode($auth->errors()));
}
expect($auth->user()->username)->toBe($updateData['username']);
expect($initialSessionId)->not()->toBe(session_id());
});