|
| 1 | +using System.Text.Json; |
| 2 | +using Microsoft.Playwright.NUnit; |
| 3 | +using Microsoft.Playwright; |
| 4 | + |
| 5 | +namespace PlaywrightTests; |
| 6 | + |
| 7 | +[TestFixture] |
| 8 | +public class EndpointTests : PlaywrightTest |
| 9 | +{ |
| 10 | + private IAPIRequestContext? Request; |
| 11 | + static string PROTOCOL = Environment.GetEnvironmentVariable("PROTOCOL") ?? "http"; |
| 12 | + static string SERVER = Environment.GetEnvironmentVariable("SERVER") ?? "localhost"; |
| 13 | + static string PORT = Environment.GetEnvironmentVariable("PORT") ?? ":5000"; |
| 14 | + static string API = Environment.GetEnvironmentVariable("API") ?? "scim"; |
| 15 | + private static string _baseUrl = $"{PROTOCOL}://{SERVER}{PORT}/{API}/"; |
| 16 | + |
| 17 | + [Test] |
| 18 | + public async Task SchemaIsReturned() |
| 19 | + { |
| 20 | + var schemaResponse = await Request.GetAsync("Schemas"); |
| 21 | + Assert.True(schemaResponse.Ok); |
| 22 | + |
| 23 | + var schemaJsonResponse = await schemaResponse.TextAsync(); |
| 24 | + |
| 25 | + Assert.IsFalse(string.IsNullOrWhiteSpace(schemaJsonResponse)); |
| 26 | + Assert.IsTrue(schemaJsonResponse.Contains("User Account")); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task GetEmptyUsers() |
| 31 | + { |
| 32 | + var emptyUsersResponse = await Request.GetAsync("Users"); |
| 33 | + Assert.True(emptyUsersResponse.Ok); |
| 34 | + |
| 35 | + var emptyUsersJson = emptyUsersResponse.JsonAsync().Result; |
| 36 | + Assert.True(emptyUsersJson.HasValue); |
| 37 | + Assert.True(emptyUsersJson!.Value.TryGetProperty("totalResults", out var totalResults)); |
| 38 | + Assert.That(totalResults.GetInt32(), Is.EqualTo(0)); |
| 39 | + Assert.True(emptyUsersJson.Value.TryGetProperty("itemsPerPage", out var itemsPerPage)); |
| 40 | + Assert.That(itemsPerPage.GetInt32(), Is.EqualTo(0)); |
| 41 | + Assert.True(emptyUsersJson.Value.TryGetProperty("schemas", out var schemas)); |
| 42 | + Assert.That(schemas.GetArrayLength(), Is.EqualTo(1)); |
| 43 | + Assert.True(emptyUsersJson.Value.TryGetProperty("Resources", out var resources)); |
| 44 | + Assert.That(resources.GetArrayLength(), Is.EqualTo(0)); |
| 45 | + Assert.That(schemas[0].ValueKind, Is.EqualTo(JsonValueKind.String)); |
| 46 | + Assert.That(schemas[0].GetString(), Is.EqualTo("urn:ietf:params:scim:api:messages:2.0:ListResponse")); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public async Task GetEmptyGroups() |
| 51 | + { |
| 52 | + var emptyGroupsResponse = await Request.GetAsync("Groups"); |
| 53 | + Assert.True(emptyGroupsResponse.Ok); |
| 54 | + |
| 55 | + var emptyGroupsJson = emptyGroupsResponse.JsonAsync().Result; |
| 56 | + Assert.True(emptyGroupsJson.HasValue); |
| 57 | + Assert.True(emptyGroupsJson!.Value.TryGetProperty("totalResults", out var totalResults)); |
| 58 | + Assert.That(totalResults.GetInt32(), Is.EqualTo(0)); |
| 59 | + Assert.True(emptyGroupsJson.Value.TryGetProperty("itemsPerPage", out var itemsPerPage)); |
| 60 | + Assert.That(itemsPerPage.GetInt32(), Is.EqualTo(0)); |
| 61 | + Assert.True(emptyGroupsJson.Value.TryGetProperty("schemas", out var schemas)); |
| 62 | + Assert.That(schemas.GetArrayLength(), Is.EqualTo(1)); |
| 63 | + Assert.True(emptyGroupsJson.Value.TryGetProperty("Resources", out var resources)); |
| 64 | + Assert.That(resources.GetArrayLength(), Is.EqualTo(0)); |
| 65 | + Assert.That(schemas[0].ValueKind, Is.EqualTo(JsonValueKind.String)); |
| 66 | + Assert.That(schemas[0].GetString(), Is.EqualTo("urn:ietf:params:scim:api:messages:2.0:ListResponse")); |
| 67 | + |
| 68 | + foreach (var resource in resources.EnumerateArray()) |
| 69 | + { |
| 70 | + Assert.True(resource.TryGetProperty("endpoint", out _)); |
| 71 | + Assert.True(resource.TryGetProperty("name", out _)); |
| 72 | + Assert.True(resource.TryGetProperty("meta", out _)); |
| 73 | + Assert.True(resource.TryGetProperty("schema", out _)); |
| 74 | + Assert.True(resource.TryGetProperty("schemas", out _)); |
| 75 | + Assert.True(resource.TryGetProperty("id", out _)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public async Task GetResourceTypes() |
| 81 | + { |
| 82 | + var resourceTypesResponse = await Request.GetAsync("ResourceTypes"); |
| 83 | + Assert.True(resourceTypesResponse.Ok); |
| 84 | + |
| 85 | + var resourceTypesResponseJson = resourceTypesResponse.JsonAsync().Result; |
| 86 | + Assert.True(resourceTypesResponseJson.HasValue); |
| 87 | + Assert.True(resourceTypesResponseJson!.Value.TryGetProperty("totalResults", out var totalResults)); |
| 88 | + Assert.That(totalResults.GetInt32(), Is.EqualTo(2)); |
| 89 | + Assert.True(resourceTypesResponseJson.Value.TryGetProperty("itemsPerPage", out var itemsPerPage)); |
| 90 | + Assert.That(itemsPerPage.GetInt32(), Is.EqualTo(2)); |
| 91 | + Assert.True(resourceTypesResponseJson.Value.TryGetProperty("startIndex", out var startIndex)); |
| 92 | + Assert.That(startIndex.GetInt32(), Is.EqualTo(1)); |
| 93 | + Assert.True(resourceTypesResponseJson.Value.TryGetProperty("schemas", out var schemas)); |
| 94 | + Assert.That(schemas.GetArrayLength(), Is.EqualTo(1)); |
| 95 | + Assert.True(resourceTypesResponseJson.Value.TryGetProperty("Resources", out var resources)); |
| 96 | + Assert.That(resources.GetArrayLength(), Is.EqualTo(2)); |
| 97 | + Assert.That(schemas[0].ValueKind, Is.EqualTo(JsonValueKind.String)); |
| 98 | + Assert.That(schemas[0].GetString(), Is.EqualTo("urn:ietf:params:scim:api:messages:2.0:ListResponse")); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public async Task GetServiceProviderConfig() |
| 103 | + { |
| 104 | + var serviceProviderConfigResponse = await Request.GetAsync("ServiceProviderConfig"); |
| 105 | + Assert.True(serviceProviderConfigResponse.Ok); |
| 106 | + |
| 107 | + var serviceProviderConfigResponseJson = serviceProviderConfigResponse.JsonAsync().Result; |
| 108 | + Assert.True(serviceProviderConfigResponseJson.HasValue); |
| 109 | + Assert.True(serviceProviderConfigResponseJson!.Value.TryGetProperty("authenticationSchemes", out var authenticationSchemes)); |
| 110 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("meta", out var meta)); |
| 111 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("bulk", out var bulk)); |
| 112 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("eTag", out var eTag)); |
| 113 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("filter", out var filter)); |
| 114 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("patch", out var patch)); |
| 115 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("sort", out var sort)); |
| 116 | + Assert.True(serviceProviderConfigResponseJson.Value.TryGetProperty("schemas", out var xmlDataFormat)); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + [SetUp] |
| 122 | + public async Task SetUpAPITesting() |
| 123 | + { |
| 124 | + await CreateAPIRequestContext(); |
| 125 | + await GetAccessToken(); |
| 126 | + } |
| 127 | + |
| 128 | + private async Task CreateAPIRequestContext() |
| 129 | + { |
| 130 | + // Assuming personal access token available in the environment. |
| 131 | + |
| 132 | + Request = await this.Playwright.APIRequest.NewContextAsync(new() |
| 133 | + { |
| 134 | + // All requests we send go to this API endpoint. |
| 135 | + BaseURL = _baseUrl, |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + private async Task GetAccessToken() |
| 140 | + { |
| 141 | + var headers = new Dictionary<string, string>(); |
| 142 | + var response = await Request.GetAsync("Token"); |
| 143 | + Assert.True(response.Ok); |
| 144 | + |
| 145 | + var tokenJsonResponse = response.JsonAsync().Result; |
| 146 | + if (!tokenJsonResponse.HasValue) |
| 147 | + { |
| 148 | + throw new Exception("No token found in response."); |
| 149 | + } |
| 150 | + |
| 151 | + if (tokenJsonResponse.Value.TryGetProperty("token", out var token) == true) |
| 152 | + { |
| 153 | + if (token.ValueKind == JsonValueKind.String) |
| 154 | + { |
| 155 | + // Add authorization token to all requests. |
| 156 | + // Assuming personal access token available in the environment. |
| 157 | + headers.Add("Authorization", $"Bearer {token}"); |
| 158 | + await Request.DisposeAsync(); |
| 159 | + Request = await this.Playwright.APIRequest.NewContextAsync(new() |
| 160 | + { |
| 161 | + // All requests we send go to this API endpoint. |
| 162 | + BaseURL = _baseUrl, |
| 163 | + ExtraHTTPHeaders = headers |
| 164 | + }); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + [TearDown] |
| 170 | + public async Task TearDownAPITesting() |
| 171 | + { |
| 172 | + await Request.DisposeAsync(); |
| 173 | + } |
| 174 | +} |
0 commit comments