1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-13 15:36:45 +01:00
bitwarden-server/test/IntegrationTestCommon/Factories/WebApplicationFactoryExtensions.cs

83 lines
3.3 KiB
C#
Raw Normal View History

using System.Net;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Primitives;
2022-08-29 22:06:55 +02:00
namespace Bit.IntegrationTestCommon.Factories;
public static class WebApplicationFactoryExtensions
{
2022-08-29 22:06:55 +02:00
private static async Task<HttpContext> SendAsync(this TestServer server,
HttpMethod method,
string requestUri,
HttpContent content = null,
Action<HttpContext> extraConfiguration = null)
{
2022-08-29 22:06:55 +02:00
return await server.SendAsync(httpContext =>
{
2022-08-29 22:06:55 +02:00
// Automatically set the whitelisted IP so normal tests do not run into rate limit issues
// to test rate limiter, use the extraConfiguration parameter to set Connection.RemoteIpAddress
// it runs after this so it will take precedence.
httpContext.Connection.RemoteIpAddress = IPAddress.Parse(FactoryConstants.WhitelistedIp);
2022-08-29 22:06:55 +02:00
httpContext.Request.Path = new PathString(requestUri);
httpContext.Request.Method = method.Method;
2022-08-29 22:06:55 +02:00
if (content != null)
{
foreach (var header in content.Headers)
{
httpContext.Request.Headers.Append(header.Key, new StringValues(header.Value.ToArray()));
}
2022-08-29 22:06:55 +02:00
httpContext.Request.Body = content.ReadAsStream();
}
2022-08-29 22:06:55 +02:00
extraConfiguration?.Invoke(httpContext);
});
}
public static Task<HttpContext> PostAsync(this TestServer server,
string requestUri,
HttpContent content,
Action<HttpContext> extraConfiguration = null)
=> SendAsync(server, HttpMethod.Post, requestUri, content, extraConfiguration);
public static Task<HttpContext> GetAsync(this TestServer server,
string requestUri,
Action<HttpContext> extraConfiguration = null)
=> SendAsync(server, HttpMethod.Get, requestUri, content: null, extraConfiguration);
[EC-394] SCIM server integration tests (#2197) * [EC-394] Added ScimApplicationFactory to handle tests for Scim controllers * [EC-394] Added Scim.IntegrationTest project with GroupsControllerTests * [EC-394] Fixed getting Guid Id from Operation Path * [EC-394] Added tests for GroupsController Patch action * [EC-394] Moved tests mock data setup to ScimApplicationFactory * [EC-394] Updated IntegrationTestCommon packages.lock.json * [EC-394] Updated ScimApplicationFactory and GroupsControllerTests; Added UsersController Tests * [EC-394] dotnet format * [EC-394] Updated Identity.IntegrationTest packages.lock.json * [EC-394] Updated Scim.IntegrationTest packages.lock.json * [EC-394] dotnet format * [EC-394] Reverted change on getting GUID from GetOperationPathId * [EC-394] Fixed sending userId on Patch_AddSingleMember_Success and Patch_RemoveSingleMember_Success * [EC-394] Updated test to send request with two operations * [EC-394] Removed Scim dependency from IntegrationTestCommon * [EC-394] Reverted changes to packages.lock.json.
Ran dotnet format * [EC-394] Updated Scim.IntegrationTest packages.lock.json * [EC-394] Updated GroupsControllerTests and UsersControllerTests to implement IAsyncLifetime to cleanup database before each test * [EC-394] Declared variables for GetList parameters * [EC-394] Updated AssertHelper.AssertPropertyEqual to compare each item in an IEnumerable property * [EC-394] Updated AssertHelper.AssertPropertyEqual to check if type is comparable * [EC-394] Removed unused variable from ScimApplicationFactory * Apply suggestions from code review Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> * [EC-394] Changed test users emails to domain example.com * [EC-394] Restore solution file * [EC-394] Added Scim.IntegrationTest to sln * [EC-394] Updated integration tests to be clearer and check responses in detail * [EC-394] Using NoopMailService to mock sending email invitations in tests * [EC-394] Removed multiple references to the same variable ScimApplicationFactory.TestOrganizationId1 * [EC-394] Updated const variable names * [EC-394] Using AssertPropertyEqualPredicate for IEnumerable properties Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com>
2022-09-08 10:00:59 +02:00
public static Task<HttpContext> PutAsync(this TestServer server,
string requestUri,
HttpContent content,
Action<HttpContext> extraConfiguration = null)
=> SendAsync(server, HttpMethod.Put, requestUri, content, extraConfiguration);
public static Task<HttpContext> PatchAsync(this TestServer server,
string requestUri,
HttpContent content,
Action<HttpContext> extraConfiguration = null)
=> SendAsync(server, HttpMethod.Patch, requestUri, content, extraConfiguration);
public static Task<HttpContext> DeleteAsync(this TestServer server,
string requestUri,
HttpContent content,
Action<HttpContext> extraConfiguration = null)
=> SendAsync(server, HttpMethod.Delete, requestUri, content: content, extraConfiguration);
2022-08-29 22:06:55 +02:00
public static HttpContext SetAuthEmail(this HttpContext context, string username)
{
context.Request.Headers.Append("Auth-Email", CoreHelpers.Base64UrlEncodeString(username));
2022-08-29 22:06:55 +02:00
return context;
}
2022-08-29 20:53:16 +02:00
2022-08-29 22:06:55 +02:00
public static HttpContext SetIp(this HttpContext context, string ip)
{
context.Connection.RemoteIpAddress = IPAddress.Parse(ip);
return context;
}
public static async Task<string> ReadBodyAsStringAsync(this HttpContext context)
{
using var sr = new StreamReader(context.Response.Body);
return await sr.ReadToEndAsync();
}
}