-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathtest-sync-discourse-user.php
222 lines (185 loc) · 8.24 KB
/
test-sync-discourse-user.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
<?php
/**
* Class SyncDiscourseUserTest
*
* @package WPDiscourse
*/
namespace WPDiscourse\Test;
use WPDiscourse\SyncDiscourseUser\SyncDiscourseUser;
use WPDiscourse\Test\UnitTest;
/**
* SyncDiscourseUser test case.
*/
class SyncDiscourseUserTest extends UnitTest {
/**
* Instance of SyncDiscourseUser.
*
* @access protected
* @var \WPDiscourse\SyncDiscourseUser\SyncDiscourseUser
*/
protected $sync_user;
/**
* Request
*
* @access protected
* @var WP_REST_Request
*/
protected $request;
/**
* Signaure
*
* @access protected
* @var string
*/
protected $signature;
/**
* Payload
*
* @access protected
* @var array
*/
protected $payload;
/**
* Setup each test.
*/
public function setUp(): void {
parent::setUp();
self::$plugin_options['webhook-secret'] = '1234567891011';
self::$plugin_options['use-discourse-user-webhook'] = 1;
self::$plugin_options['enable-sso'] = 1;
$this->sync_user = new SyncDiscourseUser();
$this->sync_user->setup_options( self::$plugin_options );
$this->sync_user->setup_logger();
$this->payload = $this->response_body_file( 'webhook_user' );
$this->signature = hash_hmac( 'sha256', $this->payload, self::$plugin_options['webhook-secret'] );
$this->request = new \WP_REST_Request();
$this->request->set_header( 'Content-Type', 'application/json' );
$this->request->set_header( 'X-Discourse-Event-Signature', "sha256={$this->signature}" );
$this->request->set_header( 'X-Discourse-Event-Type', 'user' );
$this->request->set_header( 'X-Discourse-Event', 'user_updated' );
$this->request->set_body( $this->payload );
}
public function tearDown(): void {
parent::tearDown();
$payload = json_decode( $this->payload );
delete_metadata( 'user', null, 'discourse_username', null, true );
delete_metadata( 'user', null, 'discourse_sso_user_id', null, true );
}
/**
* update_user handles webhook results correctly.
*/
public function test_update_user() {
// Setup the user
$payload = json_decode( $this->payload );
$discourse_user = $payload->user;
$user = wp_set_current_user( $discourse_user->external_id );
// Perform update
$result = $this->sync_user->update_user( $this->request );
// Ensure the post meta is updated correctly.
$this->assertEquals( get_user_meta( $user->ID, 'discourse_username', true ), $discourse_user->username );
$this->assertEquals( get_user_meta( $user->ID, 'discourse_sso_user_id', true ), $discourse_user->id );
}
/**
* update_user handles webhook results correctly when using discourse_sso_user_id.
*/
public function test_update_user_using_discourse_sso_user_id() {
// Setup request
$this->payload = $this->response_body_file( 'webhook_user' );
$payload = json_decode( $this->payload );
$payload->user->external_id = null;
$this->payload = json_encode( $payload );
$this->signature = hash_hmac( 'sha256', $this->payload, self::$plugin_options['webhook-secret'] );
$this->request->set_header( 'X-Discourse-Event-Signature', "sha256={$this->signature}" );
$this->request->set_body( $this->payload );
// Setup the user
$user_id = self::factory()->user->create();
$discourse_user = $payload->user;
$user = wp_set_current_user( $user_id );
add_user_meta( $user->ID, 'discourse_sso_user_id', $discourse_user->id, true );
// Perform update
$result = $this->sync_user->update_user( $this->request );
// Ensure the post meta is updated correctly.
$this->assertEquals( get_user_meta( $user->ID, 'discourse_username', true ), $discourse_user->username );
}
/**
* update_user handles webhook results correctly when using email.
*/
public function test_update_user_using_email() {
// Setup options
self::$plugin_options['webhook-match-user-email'] = 1;
$this->sync_user->setup_options( self::$plugin_options );
// Setup request
$this->payload = $this->response_body_file( 'webhook_user' );
$payload = json_decode( $this->payload );
$payload->user->external_id = null;
$this->payload = json_encode( $payload );
$this->signature = hash_hmac( 'sha256', $this->payload, self::$plugin_options['webhook-secret'] );
$this->request->set_header( 'X-Discourse-Event-Signature', "sha256={$this->signature}" );
$this->request->set_body( $this->payload );
// Setup the user
$user_id = self::factory()->user->create( array( 'user_email' => $payload->user->email ) );
$discourse_user = $payload->user;
$user = wp_set_current_user( $user_id );
// Perform update
$result = $this->sync_user->update_user( $this->request );
// Ensure the post meta is updated correctly.
$this->assertEquals( get_user_meta( $user->ID, 'discourse_username', true ), $discourse_user->username );
// Destroy the user
if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) {
wp_set_current_user( 0 );
}
wp_delete_user( $user_id );
}
/**
* update_user handles webhook results correctly without DiscourseConnect.
*/
public function test_update_user_using_email_without_discourse_connect() {
// Setup options
self::$plugin_options['enable-sso'] = 0;
self::$plugin_options['webhook-match-user-email'] = 1;
$this->sync_user->setup_options( self::$plugin_options );
// Setup request
$this->payload = $this->response_body_file( 'webhook_user' );
$payload = json_decode( $this->payload );
$payload->user->external_id = null;
$this->payload = json_encode( $payload );
$this->signature = hash_hmac( 'sha256', $this->payload, self::$plugin_options['webhook-secret'] );
$this->request->set_header( 'X-Discourse-Event-Signature', "sha256={$this->signature}" );
$this->request->set_body( $this->payload );
// Setup the user
$user_id = self::factory()->user->create( array( 'user_email' => $payload->user->email ) );
$discourse_user = $payload->user;
$user = wp_set_current_user( $user_id );
// Perform update
$result = $this->sync_user->update_user( $this->request );
// Ensure the post meta is updated correctly.
$this->assertEquals( get_user_meta( $user->ID, 'discourse_username', true ), $discourse_user->username );
// Destroy the user
if ( version_compare( get_bloginfo( 'version' ), '5.3', '<' ) ) {
wp_set_current_user( 0 );
}
wp_delete_user( $user_id );
}
/**
* update_user creates the right logs if unable to find user.
*/
public function test_update_user_unable_to_find_user() {
// Setup request
$this->payload = $this->response_body_file( 'webhook_user' );
$payload = json_decode( $this->payload );
$payload->user->external_id = 7;
$this->payload = json_encode( $payload );
$this->signature = hash_hmac( 'sha256', $this->payload, self::$plugin_options['webhook-secret'] );
$this->request->set_header( 'X-Discourse-Event-Signature', "sha256={$this->signature}" );
$this->request->set_body( $this->payload );
// Setup the user
$user_id = self::factory()->user->create();
$discourse_user = $payload->user;
$user = wp_set_current_user( $user_id );
// Perform update
$result = $this->sync_user->update_user( $this->request );
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertMatchesRegularExpression( '/webhook_user.WARNING: update_user.user_not_found/', $log );
}
}