-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathScimTest.cs
213 lines (187 loc) · 7.15 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
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xunit;
namespace RingCentral.Tests;
[Collection("Sequential")]
public class ScimTest
{
private const string EMAIL = "test.user@example.com";
// Get Service Provider Config: HTTP GET /scim/v2/ServiceProviderConfig
[Fact]
public async void GetServiceProviderConfig()
{
var rc = await ReusableRestClient.GetInstance();
// 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()
{
var rc = await ReusableRestClient.GetInstance();
// // "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()
{
var rc = await ReusableRestClient.GetInstance();
// 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()
{
var rc = await ReusableRestClient.GetInstance();
// delete existing user first, we don't want to create duplicate users
var searchRequest = new ScimSearchRequest
{
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();
await Task.Delay(10000); // add some delay, to workaround parallel github CI issues
// create the user
var user = new ScimUser
{
emails = new[]
{
new ScimEmail
{
type = "work",
value = EMAIL
}
},
name = new ScimName
{
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()
{
var rc = await ReusableRestClient.GetInstance();
// search user and return 10 results
var searchRequest = new ScimSearchRequest
{
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()
{
var rc = await ReusableRestClient.GetInstance();
// list 10 users and use the first one for testing
var searchRequest = new ScimSearchRequest
{
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()
{
var rc = await ReusableRestClient.GetInstance();
var searchRequest = new ScimSearchRequest
{
count = 1,
filter = "emails eq \"test.user@example.com\""
};
// 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<ScimUser>(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("test.user@example.com", 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()
{
var rc = await ReusableRestClient.GetInstance();
var searchRequest = new ScimSearchRequest
{
count = 1,
filter = "emails eq \"test.user@example.com\""
};
// 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 ScimUserPatch
{
schemas = new[]
{
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
},
Operations = new[]
{
new ScimPatchOperation
{
op = "replace",
path = "name.familyName",
value = guid
}
}
};
// patch it
var ur = await rc.Scim().Users(userResponse.id).Patch(userPatch);
Assert.Equal("test.user@example.com", ur.emails[0].value);
// make sure data patched successfully
Assert.Equal(guid, ur.name.familyName);
}
}
}