-
Notifications
You must be signed in to change notification settings - Fork 95
/
testProfileChangePassword.php
261 lines (214 loc) · 7.43 KB
/
testProfileChangePassword.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* Contains Class TestProfileChangePassword.
*
* @package WP-Auth0
*
* @since 3.8.0
*/
use PHPUnit\Framework\TestCase;
/**
* Class TestProfileChangePassword.
*/
class TestProfileChangePassword extends TestCase {
use AjaxHelpers;
use DomDocumentHelpers;
use HookHelpers;
use SetUpTestDb;
use UsersHelper;
/**
* WP_Auth0_Options instance.
*
* @var WP_Auth0_Options
*/
public static $options;
/**
* WP_Auth0_Api_Client_Credentials instance.
*
* @var WP_Auth0_Api_Client_Credentials
*/
public static $api_client_creds;
/**
* WP_Auth0_Api_Change_Password instance.
*
* @var WP_Auth0_Api_Change_Password
*/
public static $api_change_password;
/**
* WP_Auth0_Profile_Change_Password instance.
*
* @var WP_Auth0_Profile_Change_Password
*/
public static $change_password;
/**
* WP_Auth0_UsersRepo instance.
*
* @var WP_Auth0_UsersRepo
*/
protected static $users_repo;
/**
* Setup before the class starts.
*/
public static function setUpBeforeClass() {
self::$options = WP_Auth0_Options::Instance();
self::$api_client_creds = new WP_Auth0_Api_Client_Credentials( self::$options );
self::$api_change_password = new WP_Auth0_Api_Change_Password( self::$options, self::$api_client_creds );
self::$change_password = new WP_Auth0_Profile_Change_Password( self::$api_change_password );
self::$users_repo = new WP_Auth0_UsersRepo( self::$options );
}
/**
* Test that correct hooks are loaded.
*/
public function testInitHooks() {
$expect_hooked = [
'validate_new_password' => [
'priority' => 10,
'accepted_args' => 2,
],
];
// Same method hooked to all 3 actions.
$class_name = 'WP_Auth0_Profile_Change_Password';
$this->assertHooked( 'user_profile_update_errors', $class_name, $expect_hooked );
$this->assertHooked( 'validate_password_reset', $class_name, $expect_hooked );
$this->assertHooked( 'woocommerce_save_account_details_errors', $class_name, $expect_hooked );
}
/**
* Test that password update succeeds when run in the user_profile_update_errors hook.
*/
public function testSuccessfulPasswordChangeDuringProfileUpdate() {
$user_id = $this->createUser()->ID;
$errors = new WP_Error();
$password = uniqid();
// Core WP profile update fields.
$_POST['pass1'] = $password;
$_POST['pass2'] = $password;
$_POST['user_id'] = $user_id;
// API call mocked to succeed.
$change_password = $this->getStub( true );
// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user_id, 'auth0' );
$this->assertTrue( $change_password->validate_new_password( $errors, true ) );
$this->assertEquals( $password, $_POST['pass1'] );
$this->assertEquals( $password, $_POST['pass2'] );
$this->assertEmpty( $errors->get_error_messages() );
}
/**
* Test that password update succeeds when run in the validate_password_reset hook.
*/
public function testSuccessfulPasswordChangeDuringPasswordReset() {
$password = uniqid();
$user = $this->createUser();
$errors = new WP_Error();
// API call mocked to succeed.
$change_password = $this->getStub( true );
// Core WP form fields sent for password update.
$_POST['pass1'] = $password;
$_POST['pass2'] = $password;
// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'auth0' );
$this->assertTrue( $change_password->validate_new_password( $errors, $user ) );
$this->assertEquals( $password, $_POST['pass1'] );
$this->assertEquals( $password, $_POST['pass2'] );
$this->assertEmpty( $errors->get_error_messages() );
}
/**
* Test that password update succeeds when run in the woocommerce_save_account_details_errors hook.
*/
public function testSuccessfulPasswordChangeDuringWooAccountEdit() {
$user = $this->createUser();
$errors = new WP_Error();
// API call mocked to succeed.
$change_password = $this->getStub( true );
// WooCommerce form fields sent for password update.
$_POST['password_1'] = uniqid();
// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'auth0' );
$this->assertTrue( $change_password->validate_new_password( $errors, $user ) );
$this->assertEmpty( $errors->get_error_messages() );
}
/**
* Test that empty password fields will skip password update.
*/
public function testThatEmptyPasswordFieldSkipsUpdate() {
$user_id = $this->createUser()->ID;
$errors = new WP_Error();
// Provide everything except a password field.
$change_password = $this->getStub( true );
$_POST['user_id'] = $user_id;
$this->storeAuth0Data( $user_id );
$this->assertFalse( $change_password->validate_new_password( $errors, false ) );
$this->assertEmpty( $errors->get_error_messages() );
}
/**
* Test that the password update is skipped if no user record is provided.
*/
public function testThatMissingUserRecordSkipsUpdate() {
$user_id = $this->createUser()->ID;
$errors = new WP_Error();
// Provide everything except a user record.
$change_password = $this->getStub( true );
$_POST['pass1'] = uniqid();
$this->storeAuth0Data( $user_id );
$this->assertFalse( $change_password->validate_new_password( $errors, false ) );
}
/**
* Test that a user without Auth0 data skips update.
*/
public function testThatNonAuth0UserSkipsUpdate() {
$user_id = $this->createUser()->ID;
$errors = new WP_Error();
// Provide everything except Auth0 userinfo.
$change_password = $this->getStub( true );
$_POST['pass1'] = uniqid();
$_POST['user_id'] = $user_id;
$this->assertFalse( $change_password->validate_new_password( $errors, false ) );
}
/**
* Test that a user with a non-DB strategy skips update.
*/
public function testThatNonDbStrategySkipsUpdate() {
$user_id = $this->createUser()->ID;
$errors = new WP_Error();
// Provide everything except Auth0 userinfo for a DB user.
$change_password = $this->getStub( true );
$_POST['pass1'] = uniqid();
$_POST['user_id'] = $user_id;
$this->storeAuth0Data( $user_id, 'not-a-db-strategy' );
$this->assertFalse( $change_password->validate_new_password( $errors, false ) );
}
/**
* Test that an API failure will set UI banners and cancel the password change.
*/
public function testThatApiFailureSetsErrorsUnsetsPassword() {
$user_id = $this->createUser()->ID;
$errors = new WP_Error();
// API call mocked to fail.
$change_password = $this->getStub( false );
// Setup correct user data.
$_POST['pass1'] = uniqid();
$_POST['pass2'] = $_POST['pass1'];
$_POST['user_id'] = $user_id;
$this->storeAuth0Data( $user_id );
$this->assertFalse( $change_password->validate_new_password( $errors, false ) );
$this->assertEquals( 'Password could not be updated.', $errors->errors['auth0_password'][0] );
$this->assertEquals( 'pass1', $errors->error_data['auth0_password']['form-field'] );
$this->assertFalse( isset( $_POST['pass1'] ) );
$this->assertFalse( isset( $_POST['pass2'] ) );
}
/**
* Get an API stub set to pass or fail.
*
* @param boolean $success - True for the API call to succeed, false for it to fail.
*
* @return WP_Auth0_Profile_Change_Password
*/
public function getStub( $success ) {
$mock_api_test_password = $this
->getMockBuilder( WP_Auth0_Api_Change_Password::class )
->setMethods( [ 'call' ] )
->setConstructorArgs( [ self::$options, self::$api_client_creds ] )
->getMock();
$mock_api_test_password->method( 'call' )->willReturn( $success );
return new WP_Auth0_Profile_Change_Password( $mock_api_test_password );
}
}