forked from ringcentral/RingCentral.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScimTest.cs
300 lines (275 loc) · 12.5 KB
/
ScimTest.cs
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using Newtonsoft.Json;
using Xunit;
namespace RingCentral.Tests
{
public class ScimTest
{
private const string EMAIL = "[email protected]";
// Get Service Provider Config: HTTP GET /scim/v2/ServiceProviderConfig
[Fact]
public async void GetServiceProviderConfig()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
// to get the whole response as a string
var str = await rc.Get<string>("/scim/v2/ServiceProviderConfig");
Assert.Contains("urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig", str);
// to get the response as a C# object
var serviceProviderConfig = await rc.Scim().ServiceProviderConfig().Get();
Assert.Equal(new[] {"urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"},
serviceProviderConfig.schemas);
}
}
// Check Health: HTTP GET /scim/v2/health
[Fact]
public async void CheckHealth()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
// "OK" means SCIM system has no problem
var str = await rc.Scim().Health().Get();
Assert.Equal("OK", str);
// This is equivalent to above
var str2 = await rc.Scim(null).Health().Get();
Assert.Equal("OK", str2);
}
}
// List all the SCIM users: HTTP GET /scim/v2/Users
[Fact]
public async void GetUsers()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
// list all SCIM users
var userSearchResponse = await rc.Scim().Users().List();
Assert.True(userSearchResponse.Resources.Length > 0);
Assert.True(userSearchResponse.totalResults > 0);
Assert.Equal(1, userSearchResponse.startIndex);
}
}
// Create a SCIM user: HTTP POST /scim/v2/Users
[Fact]
public async void CreateUser()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
// delete existing user first, we don't want to create duplicate users
var searchRequest = new SearchRequest
{
count = 1,
filter = $"emails eq \"{EMAIL}\""
};
var userSearchResponse = await rc.Scim().Users().DotSearch().Post(searchRequest);
if (userSearchResponse.Resources.Length == 1)
{
await rc.Scim().Users(userSearchResponse.Resources[0].id).Delete();
}
// create the user
var user = new CreateUser
{
emails = new[]
{
new Email
{
type = "work",
value = EMAIL
}
},
name = new Name
{
familyName = "LastName",
givenName = "FirstName"
},
schemas = new[] {"urn:ietf:params:scim:schemas:core:2.0:User"},
userName = EMAIL
};
var userResponse = await rc.Scim().Users().Post(user);
Assert.Equal("LastName", userResponse.name.familyName);
Assert.Equal("FirstName", userResponse.name.givenName);
// don't forget to delete it after testing
var str = await rc.Scim().Users(userResponse.id).Delete();
Assert.Equal("", str);
}
}
// Search SCIM user: HTTP POST /scim/v2/Users/.search
[Fact]
public async void SearchUser()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
// search user and return 10 results
var searchRequest = new SearchRequest
{
count = 10
};
var userSearchResponse = await rc.Scim().Users().DotSearch().Post(searchRequest);
Assert.True(userSearchResponse.Resources.Length > 0);
Assert.True(userSearchResponse.totalResults > 0);
Assert.Equal(1, userSearchResponse.startIndex);
}
}
// Get SCIM user by ID: HTTP GET /scim/v2/Users/{id}
[Fact]
public async void GetUserById()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
// list 10 users and use the first one for testing
var searchRequest = new SearchRequest
{
count = 10
};
var userSearchResponse = await rc.Scim().Users().DotSearch().Post(searchRequest);
var firstUser = userSearchResponse.Resources[0];
// get user by ID
var user = await rc.Scim().Users(firstUser.id).Get();
Assert.Equal(user.id, firstUser.id);
Assert.Equal(user.name.familyName, firstUser.name.familyName);
Assert.Equal(user.name.givenName, firstUser.name.givenName);
}
}
// Update a SCIM user: HTTP PUT /scim/v2/Users/{id}
[Fact]
public async void UpdateUser()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
var searchRequest = new SearchRequest
{
count = 1,
filter = "emails eq \"[email protected]\""
};
// search for the user
var userSearchResponse = await rc.Scim().Users().DotSearch().Post(searchRequest);
if (userSearchResponse.Resources.Length == 1)
{
var userResponse = userSearchResponse.Resources[0];
// create a new use object by JSON serialization and deserialization
var user = JsonConvert.DeserializeObject<User>(JsonConvert.SerializeObject(userResponse));
var guid = Guid.NewGuid().ToString();
// update family name of the new user
user.name.familyName = guid;
var ur = await rc.Scim().Users(user.id).Put(user);
Assert.Equal("[email protected]", ur.emails[0].value);
// make sure user has new family name
Assert.Equal(guid, ur.name.familyName);
}
}
}
// Patch a SCIM user: HTTP PATCH /scim/v2/Users/{id}
// Patch is for update data fields by path
[Fact]
public async void PatchUser()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
var searchRequest = new SearchRequest
{
count = 1,
filter = "emails eq \"[email protected]\""
};
// search for the SCIM user
var userSearchResponse = await rc.Scim().Users().DotSearch().Post(searchRequest);
if (userSearchResponse.Resources.Length == 1)
{
var userResponse = userSearchResponse.Resources[0];
var guid = Guid.NewGuid().ToString();
var userPatch = new UserPatch
{
schemas = new[]
{
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
},
Operations = new[]
{
new PatchOperation
{
op = "replace",
path = "name.familyName",
value = guid
}
}
};
// patch it
var ur = await rc.Scim().Users(userResponse.id).Patch(userPatch);
Assert.Equal("[email protected]", ur.emails[0].value);
// make sure data patched successfully
Assert.Equal(guid, ur.name.familyName);
}
}
}
}
}