-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathEndpointTests.cs
174 lines (151 loc) · 7.74 KB
/
EndpointTests.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
using System.Text.Json;
using Microsoft.Playwright.NUnit;
using Microsoft.Playwright;
namespace PlaywrightTests;
[TestFixture]
public class EndpointTests : PlaywrightTest
{
private IAPIRequestContext? Request;
static string PROTOCOL = Environment.GetEnvironmentVariable("PROTOCOL") ?? "http";
static string SERVER = Environment.GetEnvironmentVariable("SERVER") ?? "localhost";
static string PORT = Environment.GetEnvironmentVariable("PORT") ?? ":5000";
static string API = Environment.GetEnvironmentVariable("API") ?? "scim";
private static string _baseUrl = $"{PROTOCOL}://{SERVER}{PORT}/{API}/";
[Test]
public async Task SchemaIsReturned()
{
var schemaResponse = await Request.GetAsync("Schemas");
Assert.True(schemaResponse.Ok);
var schemaJsonResponse = await schemaResponse.TextAsync();
Assert.IsFalse(string.IsNullOrWhiteSpace(schemaJsonResponse));
Assert.IsTrue(schemaJsonResponse.Contains("User Account"));
}
[Test]
public async Task GetEmptyUsers()
{
var emptyUsersResponse = await Request.GetAsync("Users");
Assert.True(emptyUsersResponse.Ok);
var emptyUsersJson = emptyUsersResponse.JsonAsync().Result;
Assert.True(emptyUsersJson.HasValue);
Assert.True(emptyUsersJson!.Value.TryGetProperty("totalResults", out var totalResults));
Assert.That(totalResults.GetInt32(), Is.EqualTo(0));
Assert.True(emptyUsersJson.Value.TryGetProperty("itemsPerPage", out var itemsPerPage));
Assert.That(itemsPerPage.GetInt32(), Is.EqualTo(0));
Assert.True(emptyUsersJson.Value.TryGetProperty("schemas", out var schemas));
Assert.That(schemas.GetArrayLength(), Is.EqualTo(1));
Assert.True(emptyUsersJson.Value.TryGetProperty("Resources", out var resources));
Assert.That(resources.GetArrayLength(), Is.EqualTo(0));
Assert.That(schemas[0].ValueKind, Is.EqualTo(JsonValueKind.String));
Assert.That(schemas[0].GetString(), Is.EqualTo("urn:ietf:params:scim:api:messages:2.0:ListResponse"));
}
[Test]
public async Task GetEmptyGroups()
{
var emptyGroupsResponse = await Request.GetAsync("Groups");
Assert.True(emptyGroupsResponse.Ok);
var emptyGroupsJson = emptyGroupsResponse.JsonAsync().Result;
Assert.True(emptyGroupsJson.HasValue);
Assert.True(emptyGroupsJson!.Value.TryGetProperty("totalResults", out var totalResults));
Assert.That(totalResults.GetInt32(), Is.EqualTo(0));
Assert.True(emptyGroupsJson.Value.TryGetProperty("itemsPerPage", out var itemsPerPage));
Assert.That(itemsPerPage.GetInt32(), Is.EqualTo(0));
Assert.True(emptyGroupsJson.Value.TryGetProperty("schemas", out var schemas));
Assert.That(schemas.GetArrayLength(), Is.EqualTo(1));
Assert.True(emptyGroupsJson.Value.TryGetProperty("Resources", out var resources));
Assert.That(resources.GetArrayLength(), Is.EqualTo(0));
Assert.That(schemas[0].ValueKind, Is.EqualTo(JsonValueKind.String));
Assert.That(schemas[0].GetString(), Is.EqualTo("urn:ietf:params:scim:api:messages:2.0:ListResponse"));
foreach (var resource in resources.EnumerateArray())
{
Assert.True(resource.TryGetProperty("endpoint", out _));
Assert.True(resource.TryGetProperty("name", out _));
Assert.True(resource.TryGetProperty("meta", out _));
Assert.True(resource.TryGetProperty("schema", out _));
Assert.True(resource.TryGetProperty("schemas", out _));
Assert.True(resource.TryGetProperty("id", out _));
}
}
[Test]
public async Task GetResourceTypes()
{
var resourceTypesResponse = await Request.GetAsync("ResourceTypes");
Assert.True(resourceTypesResponse.Ok);
var resourceTypesResponseJson = resourceTypesResponse.JsonAsync().Result;
Assert.True(resourceTypesResponseJson.HasValue);
Assert.True(resourceTypesResponseJson!.Value.TryGetProperty("totalResults", out var totalResults));
Assert.That(totalResults.GetInt32(), Is.EqualTo(2));
Assert.True(resourceTypesResponseJson.Value.TryGetProperty("itemsPerPage", out var itemsPerPage));
Assert.That(itemsPerPage.GetInt32(), Is.EqualTo(2));
Assert.True(resourceTypesResponseJson.Value.TryGetProperty("startIndex", out var startIndex));
Assert.That(startIndex.GetInt32(), Is.EqualTo(1));
Assert.True(resourceTypesResponseJson.Value.TryGetProperty("schemas", out var schemas));
Assert.That(schemas.GetArrayLength(), Is.EqualTo(1));
Assert.True(resourceTypesResponseJson.Value.TryGetProperty("Resources", out var resources));
Assert.That(resources.GetArrayLength(), Is.EqualTo(2));
Assert.That(schemas[0].ValueKind, Is.EqualTo(JsonValueKind.String));
Assert.That(schemas[0].GetString(), Is.EqualTo("urn:ietf:params:scim:api:messages:2.0:ListResponse"));
}
[Test]
public async Task GetServiceProviderConfig()
{
var serviceProviderConfigResponse = await Request.GetAsync("ServiceProviderConfig");
Assert.True(serviceProviderConfigResponse.Ok);
var serviceProviderConfigResponseJson = serviceProviderConfigResponse.JsonAsync().Result;
Assert.True(serviceProviderConfigResponseJson.HasValue);
Assert.True(serviceProviderConfigResponseJson!.Value.TryGetProperty("authenticationSchemes", out var authenticationSchemes));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("meta", out var meta));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("bulk", out var bulk));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("eTag", out var eTag));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("filter", out var filter));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("patch", out var patch));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("sort", out var sort));
Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("schemas", out var xmlDataFormat));
}
[SetUp]
public async Task SetUpAPITesting()
{
await CreateAPIRequestContext();
await GetAccessToken();
}
private async Task CreateAPIRequestContext()
{
// Assuming personal access token available in the environment.
Request = await this.Playwright.APIRequest.NewContextAsync(new()
{
// All requests we send go to this API endpoint.
BaseURL = _baseUrl,
});
}
private async Task GetAccessToken()
{
var headers = new Dictionary<string, string>();
var response = await Request.GetAsync("Token");
Assert.True(response.Ok);
var tokenJsonResponse = response.JsonAsync().Result;
if (!tokenJsonResponse.HasValue)
{
throw new Exception("No token found in response.");
}
if (tokenJsonResponse.Value.TryGetProperty("token", out var token) == true)
{
if (token.ValueKind == JsonValueKind.String)
{
// Add authorization token to all requests.
// Assuming personal access token available in the environment.
headers.Add("Authorization", $"Bearer {token}");
await Request.DisposeAsync();
Request = await this.Playwright.APIRequest.NewContextAsync(new()
{
// All requests we send go to this API endpoint.
BaseURL = _baseUrl,
ExtraHTTPHeaders = headers
});
}
}
}
[TearDown]
public async Task TearDownAPITesting()
{
await Request.DisposeAsync();
}
}