feat(api,core,infra): refactor endpoint routing and add institution account discovery
- Move login and refresh endpoints under a unified /auth route group
- Relocate institution connection logic from /accounts to /institutions
- Implement GET /institutions/{id}/available to fetch real-time Plaid
accounts
- Introduce HttpRequestBuilder utility to streamline integration testing
- Enhance PlaidService with GetAccountsAsync to support account fetching
- Clean up global usings and project structure for better domain
isolation
This commit is contained in:
@@ -10,7 +10,6 @@ internal static class AccountsExtensions
|
|||||||
.RequireAuthorization();
|
.RequireAuthorization();
|
||||||
|
|
||||||
accountsGroup.MapAddEndpoint();
|
accountsGroup.MapAddEndpoint();
|
||||||
accountsGroup.MapConnectEndpoint();
|
|
||||||
|
|
||||||
return accountsGroup;
|
return accountsGroup;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
using FiscalOS.Core.Accounts;
|
|
||||||
|
|
||||||
namespace FiscalOS.API.Accounts.Add;
|
namespace FiscalOS.API.Accounts.Add;
|
||||||
|
|
||||||
internal static class Endpoint
|
internal static class Endpoint
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
namespace FiscalOS.API.Accounts.Add;
|
namespace FiscalOS.API.Accounts.Add;
|
||||||
|
|
||||||
public record Request : IValidatableObject
|
public record Request : IValidatableObject
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
namespace FiscalOS.API.Auth;
|
||||||
|
|
||||||
|
internal static class AuthExtensions
|
||||||
|
{
|
||||||
|
private const string RouteGroupPrefix = "/auth";
|
||||||
|
|
||||||
|
public static RouteGroupBuilder MapAuthEndpoints(this WebApplication app)
|
||||||
|
{
|
||||||
|
var authGroup = app.MapGroup(RouteGroupPrefix);
|
||||||
|
|
||||||
|
authGroup.MapLoginEndpoint();
|
||||||
|
|
||||||
|
authGroup.MapRefreshEndpoint()
|
||||||
|
.RequireAuthorization(Schemes.AllowExpiredTokens);
|
||||||
|
|
||||||
|
return authGroup;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
namespace FiscalOS.API.Login;
|
namespace FiscalOS.API.Auth.Login;
|
||||||
|
|
||||||
internal static class Endpoint
|
internal static class Endpoint
|
||||||
{
|
{
|
||||||
private const string Route = "/login";
|
private const string Route = "/login";
|
||||||
|
|
||||||
public static RouteHandlerBuilder MapLoginEndpoint(this WebApplication app)
|
public static RouteHandlerBuilder MapLoginEndpoint(this RouteGroupBuilder groupBuilder)
|
||||||
{
|
{
|
||||||
return app.MapPost(Route, HandleAsync);
|
return groupBuilder.MapPost(Route, HandleAsync);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<IResult> HandleAsync(
|
private static async Task<IResult> HandleAsync(
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace FiscalOS.API.Login;
|
namespace FiscalOS.API.Auth.Login;
|
||||||
|
|
||||||
public record Request : IValidatableObject
|
public record Request : IValidatableObject
|
||||||
{
|
{
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace FiscalOS.API.Login;
|
namespace FiscalOS.API.Auth.Login;
|
||||||
|
|
||||||
internal sealed record Response
|
internal sealed record Response
|
||||||
{
|
{
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
namespace FiscalOS.API.Refresh;
|
namespace FiscalOS.API.Auth.Refresh;
|
||||||
|
|
||||||
internal static class Endpoint
|
internal static class Endpoint
|
||||||
{
|
{
|
||||||
private const string Route = "/refresh";
|
private const string Route = "/refresh";
|
||||||
|
|
||||||
public static RouteHandlerBuilder MapRefreshEndpoint(this WebApplication app)
|
public static RouteHandlerBuilder MapRefreshEndpoint(this RouteGroupBuilder groupBuilder)
|
||||||
{
|
{
|
||||||
return app.MapPost(Route, HandleAsync);
|
return groupBuilder.MapPost(Route, HandleAsync);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<IResult> HandleAsync(
|
private static async Task<IResult> HandleAsync(
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace FiscalOS.API.Refresh;
|
namespace FiscalOS.API.Auth.Refresh;
|
||||||
|
|
||||||
internal sealed record Response
|
internal sealed record Response
|
||||||
{
|
{
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
using FiscalOS.Core.Identity;
|
|
||||||
|
|
||||||
namespace FiscalOS.API.Http;
|
namespace FiscalOS.API.Http;
|
||||||
|
|
||||||
internal static class HttpContextExtensions
|
internal static class HttpContextExtensions
|
||||||
|
|||||||
+1
-3
@@ -1,6 +1,4 @@
|
|||||||
using FiscalOS.Core.Accounts;
|
namespace FiscalOS.API.Institutions.Connect;
|
||||||
|
|
||||||
namespace FiscalOS.API.Accounts.Connect;
|
|
||||||
|
|
||||||
internal static class Endpoint
|
internal static class Endpoint
|
||||||
{
|
{
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace FiscalOS.API.Accounts.Connect;
|
namespace FiscalOS.API.Institutions.Connect;
|
||||||
|
|
||||||
public record Request : IValidatableObject
|
public record Request : IValidatableObject
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
namespace FiscalOS.API.Institutions.GetAvailable;
|
||||||
|
|
||||||
|
internal static class Endpoint
|
||||||
|
{
|
||||||
|
private const string Route = "/{id}/available";
|
||||||
|
|
||||||
|
public static RouteHandlerBuilder MapGetAvailableEndpoint(this RouteGroupBuilder groupBuilder)
|
||||||
|
{
|
||||||
|
return groupBuilder.MapGet(Route, HandleAsync);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<IResult> HandleAsync(
|
||||||
|
HttpContext httpContext,
|
||||||
|
[FromRoute] Guid id,
|
||||||
|
[FromServices] AppDbContext appDbContext,
|
||||||
|
[FromServices] PlaidService plaidService,
|
||||||
|
[FromServices] IEncryptor encryptor,
|
||||||
|
CancellationToken ct
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var userId = httpContext.GetUserId();
|
||||||
|
|
||||||
|
var user = await appDbContext.Users
|
||||||
|
.Include(u => u.Institutions.Where(i => i.Id == id))
|
||||||
|
.ThenInclude(i => i.Metadata)
|
||||||
|
.SingleOrDefaultAsync(u => u.Id == userId, ct);
|
||||||
|
|
||||||
|
if (user is null)
|
||||||
|
{
|
||||||
|
return Results.Unauthorized();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.Institutions.Any() is false)
|
||||||
|
{
|
||||||
|
return Results.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var institution = user.Institutions.First();
|
||||||
|
|
||||||
|
if (institution.Metadata is not PlaidMetadata plaidMetadata)
|
||||||
|
{
|
||||||
|
return Results.BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
var accessToken = await encryptor.DecryptAsyncFor(user, plaidMetadata.EncryptedAccessToken, ct);
|
||||||
|
var accounts = await plaidService.GetAccountsAsync(accessToken);
|
||||||
|
|
||||||
|
return Results.Ok(accounts);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace FiscalOS.API.Institutions;
|
||||||
|
|
||||||
|
internal static class InstitutionsExtensions
|
||||||
|
{
|
||||||
|
private const string RouteGroupPrefix = "/institutions";
|
||||||
|
|
||||||
|
public static RouteGroupBuilder MapInstitutionsEndpoints(this WebApplication app)
|
||||||
|
{
|
||||||
|
var institutionsGroup = app.MapGroup(RouteGroupPrefix)
|
||||||
|
.RequireAuthorization();
|
||||||
|
|
||||||
|
institutionsGroup.MapConnectEndpoint();
|
||||||
|
institutionsGroup.MapGetAvailableEndpoint();
|
||||||
|
|
||||||
|
return institutionsGroup;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,11 +32,8 @@ app.UseAuthentication();
|
|||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.UseStatusCodePages();
|
app.UseStatusCodePages();
|
||||||
|
|
||||||
app.MapLoginEndpoint();
|
app.MapAuthEndpoints();
|
||||||
|
|
||||||
app.MapRefreshEndpoint()
|
|
||||||
.RequireAuthorization(Schemes.AllowExpiredTokens);
|
|
||||||
|
|
||||||
app.MapAccountsEndpoints();
|
app.MapAccountsEndpoints();
|
||||||
|
app.MapInstitutionsEndpoints();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -2,12 +2,17 @@ global using System.ComponentModel.DataAnnotations;
|
|||||||
global using System.Security.Claims;
|
global using System.Security.Claims;
|
||||||
global using System.Text.Json.Serialization;
|
global using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
global using FiscalOS.Core.Identity;
|
||||||
global using FiscalOS.API.Accounts;
|
global using FiscalOS.API.Accounts;
|
||||||
global using FiscalOS.API.Accounts.Add;
|
global using FiscalOS.API.Accounts.Add;
|
||||||
global using FiscalOS.API.Accounts.Connect;
|
global using FiscalOS.API.Auth;
|
||||||
|
global using FiscalOS.API.Auth.Login;
|
||||||
|
global using FiscalOS.API.Auth.Refresh;
|
||||||
global using FiscalOS.API.Http;
|
global using FiscalOS.API.Http;
|
||||||
global using FiscalOS.API.Login;
|
global using FiscalOS.API.Institutions;
|
||||||
global using FiscalOS.API.Refresh;
|
global using FiscalOS.API.Institutions.Connect;
|
||||||
|
global using FiscalOS.API.Institutions.GetAvailable;
|
||||||
|
global using FiscalOS.Core.Accounts;
|
||||||
global using FiscalOS.Core.Authentication;
|
global using FiscalOS.Core.Authentication;
|
||||||
global using FiscalOS.Core.Security;
|
global using FiscalOS.Core.Security;
|
||||||
global using FiscalOS.Infra.Accounts.Plaid;
|
global using FiscalOS.Infra.Accounts.Plaid;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
namespace FiscalOS.Infra.Accounts.Plaid;
|
namespace FiscalOS.Infra.Accounts.Plaid;
|
||||||
|
|
||||||
public sealed class PlaidService
|
public sealed class PlaidService
|
||||||
@@ -30,6 +31,21 @@ public sealed class PlaidService
|
|||||||
return (ptr.ItemId, ptr.AccessToken);
|
return (ptr.ItemId, ptr.AccessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<Going.Plaid.Entity.Account>> GetAccountsAsync(string accessToken)
|
||||||
|
{
|
||||||
|
var ar = await _client.AccountsGetAsync(new()
|
||||||
|
{
|
||||||
|
AccessToken = accessToken,
|
||||||
|
}).ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (ar.IsSuccessStatusCode is false)
|
||||||
|
{
|
||||||
|
throw new PlaidException("Unable to retrieve accounts");
|
||||||
|
}
|
||||||
|
|
||||||
|
return [.. ar.Accounts];
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<ItemWithConsentFields> GetItemAsync(string accessToken)
|
public async Task<ItemWithConsentFields> GetItemAsync(string accessToken)
|
||||||
{
|
{
|
||||||
var ar = await _client.ItemGetAsync(new()
|
var ar = await _client.ItemGetAsync(new()
|
||||||
@@ -44,4 +60,4 @@ public sealed class PlaidService
|
|||||||
|
|
||||||
return ar.Item;
|
return ar.Item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,12 +21,6 @@ internal sealed class HttpResponseMessageAssertions(
|
|||||||
HttpStatusCode expectedStatusCode
|
HttpStatusCode expectedStatusCode
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_chain.ForCondition(Subject.Content.Headers.ContentType?.MediaType is "application/json")
|
|
||||||
.FailWith(
|
|
||||||
"Expected response to be application/json, but found {0}",
|
|
||||||
Subject.Content.Headers.ContentType?.MediaType
|
|
||||||
);
|
|
||||||
|
|
||||||
_chain.ForCondition(Subject.StatusCode == expectedStatusCode)
|
_chain.ForCondition(Subject.StatusCode == expectedStatusCode)
|
||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected response status code to be {0}, but found {1}",
|
"Expected response status code to be {0}, but found {1}",
|
||||||
@@ -34,6 +28,13 @@ internal sealed class HttpResponseMessageAssertions(
|
|||||||
Subject.StatusCode
|
Subject.StatusCode
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_chain.ForCondition(Subject.Content.Headers.ContentType?.MediaType is "application/json")
|
||||||
|
.FailWith(
|
||||||
|
"Expected response to be application/json, but found {0}",
|
||||||
|
Subject.Content.Headers.ContentType?.MediaType
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
var content = await Subject.Content.ReadFromJsonAsync<T>();
|
var content = await Subject.Content.ReadFromJsonAsync<T>();
|
||||||
|
|
||||||
_chain.ForCondition(content is not null)
|
_chain.ForCondition(content is not null)
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
namespace FiscalOS.API.Tests.Infra;
|
||||||
|
|
||||||
|
internal sealed class HttpRequestBuilder
|
||||||
|
{
|
||||||
|
private HttpMethod _method = HttpMethod.Get;
|
||||||
|
private Uri? _uri;
|
||||||
|
private object? _body;
|
||||||
|
private string? _bearerToken;
|
||||||
|
private readonly Dictionary<string, string> _cookies = [];
|
||||||
|
private readonly Dictionary<string, string> _headers = [];
|
||||||
|
|
||||||
|
private HttpRequestBuilder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HttpRequestBuilder New() => new();
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithMethod(HttpMethod method)
|
||||||
|
{
|
||||||
|
_method = method;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithUri(Uri uri)
|
||||||
|
{
|
||||||
|
_uri = uri;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithBody<T>(T body)
|
||||||
|
{
|
||||||
|
_body = body;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithBearerToken(string token)
|
||||||
|
{
|
||||||
|
_bearerToken = token;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithUserId(Guid userId)
|
||||||
|
{
|
||||||
|
_bearerToken = JwtTokenBuilder.New()
|
||||||
|
.WithClaim(JwtRegisteredClaimNames.Sub, userId.ToString())
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithCookie(string name, string value)
|
||||||
|
{
|
||||||
|
_cookies[name] = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithRefreshCookie(string token)
|
||||||
|
{
|
||||||
|
return WithCookie("fiscalos_refresh_cookie", token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder WithHeader(string name, string value)
|
||||||
|
{
|
||||||
|
_headers[name] = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder Post(Uri uri)
|
||||||
|
{
|
||||||
|
_method = HttpMethod.Post;
|
||||||
|
_uri = uri;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder Get(Uri uri)
|
||||||
|
{
|
||||||
|
_method = HttpMethod.Get;
|
||||||
|
_uri = uri;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder Put(Uri uri)
|
||||||
|
{
|
||||||
|
_method = HttpMethod.Put;
|
||||||
|
_uri = uri;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestBuilder Delete(Uri uri)
|
||||||
|
{
|
||||||
|
_method = HttpMethod.Delete;
|
||||||
|
_uri = uri;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequestMessage Build()
|
||||||
|
{
|
||||||
|
if (_uri is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("URI must be set before building the request.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = new HttpRequestMessage(_method, _uri);
|
||||||
|
|
||||||
|
if (_body is not null)
|
||||||
|
{
|
||||||
|
var json = JsonSerializer.Serialize(_body);
|
||||||
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_bearerToken is not null)
|
||||||
|
{
|
||||||
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _bearerToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (key, value) in _cookies)
|
||||||
|
{
|
||||||
|
request.Headers.Add("Cookie", $"{key}={value}");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var (key, value) in _headers)
|
||||||
|
{
|
||||||
|
request.Headers.Add(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace FiscalOS.API.Tests.Common;
|
namespace FiscalOS.API.Tests.Infra;
|
||||||
|
|
||||||
public interface ISerializableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IXunitSerializable
|
public interface ISerializableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IXunitSerializable
|
||||||
{
|
{
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace FiscalOS.API.Tests.Infra;
|
namespace FiscalOS.API.Tests.Infra;
|
||||||
|
|
||||||
public class TestApi : WebApplicationFactory<Program>
|
public class TestApi : WebApplicationFactory<Program>
|
||||||
|
|||||||
+66
-113
@@ -1,7 +1,7 @@
|
|||||||
using Account = FiscalOS.Core.Accounts.Account;
|
using Account = FiscalOS.Core.Accounts.Account;
|
||||||
using Institution = FiscalOS.Core.Accounts.Institution;
|
using Institution = FiscalOS.Core.Accounts.Institution;
|
||||||
|
|
||||||
namespace FiscalOS.API.Tests.Integration;
|
namespace FiscalOS.API.Tests.Integration.Accounts;
|
||||||
|
|
||||||
public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
||||||
{
|
{
|
||||||
@@ -18,17 +18,12 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Add_WhenCalledWithoutInstitutionIdOrAccountIdOrAccountName_ItShouldReturn400WithProblemDetails()
|
public async Task Add_WhenCalledWithoutInstitutionIdOrAccountIdOrAccountName_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new { })
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var content = new StringContent(JsonSerializer.Serialize(new { }), Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -42,22 +37,16 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Add_WhenCalledWithoutInstitutionId_ItShouldReturn400WithProblemDetails()
|
public async Task Add_WhenCalledWithoutInstitutionId_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
plaidAccountId = "accountId",
|
||||||
|
plaidAccountName = "Some Account",
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
plaidAccountId = "accountId",
|
|
||||||
plaidAccountName = "Some Account",
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -69,22 +58,16 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Add_WhenCalledWithoutAccountId_ItShouldReturn400WithProblemDetails()
|
public async Task Add_WhenCalledWithoutAccountId_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
plaidInstitutionId = "institutionId",
|
||||||
|
plaidAccountName = "Some Account",
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
plaidInstitutionId = "institutionId",
|
|
||||||
plaidAccountName = "Some Account",
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -96,22 +79,16 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Add_WhenCalledWithoutAccountName_ItShouldReturn400WithProblemDetails()
|
public async Task Add_WhenCalledWithoutAccountName_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
plaidInstitutionId = "institutionId",
|
||||||
|
plaidAccountId = "accountId",
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
plaidInstitutionId = "institutionId",
|
|
||||||
plaidAccountId = "accountId",
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -123,23 +100,17 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Add_WhenCalledWithNonExistentUser_ItShouldReturn401WithProblemDetails()
|
public async Task Add_WhenCalledWithNonExistentUser_ItShouldReturn401WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
plaidInstitutionId = "id",
|
||||||
|
plaidAccountId = "id",
|
||||||
|
plaidAccountName = "Some Account",
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
plaidInstitutionId = "id",
|
|
||||||
plaidAccountId = "id",
|
|
||||||
plaidAccountName = "Some Account",
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
await response.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
@@ -162,23 +133,17 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return user;
|
return user;
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
plaidInstitutionId = "id",
|
||||||
|
plaidAccountId = "id",
|
||||||
|
plaidAccountName = "Some Account",
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
plaidInstitutionId = "id",
|
|
||||||
plaidAccountId = "id",
|
|
||||||
plaidAccountName = "Some Account",
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -216,23 +181,17 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, institution, account);
|
return (user, institution, account);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(AddUri)
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
plaidInstitutionId = ((PlaidMetadata)institution.Metadata!).PlaidId,
|
||||||
|
plaidAccountId = ((PlaidAccountMetadata)account.Metadata!).PlaidId,
|
||||||
|
plaidAccountName = ((PlaidAccountMetadata)account.Metadata).PlaidName,
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
plaidInstitutionId = ((PlaidMetadata)institution.Metadata!).PlaidId,
|
|
||||||
plaidAccountId = ((PlaidAccountMetadata)account.Metadata!).PlaidId,
|
|
||||||
plaidAccountName = ((PlaidAccountMetadata)account.Metadata).PlaidName,
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.Conflict);
|
await response.Should().BeProblemDetails(HttpStatusCode.Conflict);
|
||||||
@@ -261,25 +220,19 @@ public class AddTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, institution);
|
return (user, institution);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
var newAccountId = "newAccountId";
|
var newAccountId = "newAccountId";
|
||||||
var newAccountName = "New Account";
|
var newAccountName = "New Account";
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
using var request = HttpRequestBuilder.New()
|
||||||
{
|
.Post(AddUri)
|
||||||
plaidInstitutionId = ((PlaidMetadata)institution.Metadata!).PlaidId,
|
.WithUserId(user.Id)
|
||||||
plaidAccountId = newAccountId,
|
.WithBody(new
|
||||||
plaidAccountName = newAccountName,
|
{
|
||||||
});
|
plaidInstitutionId = ((PlaidMetadata)institution.Metadata!).PlaidId,
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
plaidAccountId = newAccountId,
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, AddUri)
|
plaidAccountName = newAccountName,
|
||||||
{
|
})
|
||||||
Content = content,
|
.Build();
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
+39
-29
@@ -1,22 +1,23 @@
|
|||||||
using FiscalOS.API.Tests.Common;
|
namespace FiscalOS.API.Tests.Integration.Auth;
|
||||||
|
|
||||||
namespace FiscalOS.API.Tests.Integration;
|
|
||||||
|
|
||||||
public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
|
public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
|
||||||
{
|
{
|
||||||
private static readonly Uri LoginUri = new("/login", UriKind.Relative);
|
private static readonly Uri LoginUri = new("/auth/login", UriKind.Relative);
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[ClassData<LoginValidationTestCases>]
|
[ClassData<LoginValidationTestCases>]
|
||||||
public async Task Login_WhenUserSubmitsInvalidRequest_ItShouldReturn400WithProblemDetails(LoginValidationTestCase tc)
|
public async Task Login_WhenUserSubmitsInvalidRequest_ItShouldReturn400WithProblemDetails(LoginValidationTestCase tc)
|
||||||
{
|
{
|
||||||
var req = new
|
using var request = HttpRequestBuilder.New()
|
||||||
{
|
.Post(LoginUri)
|
||||||
username = tc.Username,
|
.WithBody(new
|
||||||
password = tc.Password,
|
{
|
||||||
};
|
username = tc.Username,
|
||||||
|
password = tc.Password,
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
var res = await Client.PostAsJsonAsync(LoginUri, req, TestContext.Current.CancellationToken);
|
var res = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await res.Should().BeValidationProblemDetails(tc.ExpectedErrors);
|
await res.Should().BeValidationProblemDetails(tc.ExpectedErrors);
|
||||||
}
|
}
|
||||||
@@ -24,13 +25,16 @@ public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Login_WhenUserDoesNotExist_ItShouldReturn401WithProblemDetails()
|
public async Task Login_WhenUserDoesNotExist_ItShouldReturn401WithProblemDetails()
|
||||||
{
|
{
|
||||||
var req = new
|
using var request = HttpRequestBuilder.New()
|
||||||
{
|
.Post(LoginUri)
|
||||||
username = "Test",
|
.WithBody(new
|
||||||
password = "@Password2",
|
{
|
||||||
};
|
username = "Test",
|
||||||
|
password = "@Password2",
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
var res = await Client.PostAsJsonAsync(LoginUri, req, TestContext.Current.CancellationToken);
|
var res = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await res.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
await res.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
}
|
}
|
||||||
@@ -49,13 +53,16 @@ public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
await context.SaveChangesAsync(ct);
|
await context.SaveChangesAsync(ct);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var req = new
|
using var request = HttpRequestBuilder.New()
|
||||||
{
|
.Post(LoginUri)
|
||||||
username = "Stevan",
|
.WithBody(new
|
||||||
password = "@Password2",
|
{
|
||||||
};
|
username = "Stevan",
|
||||||
|
password = "@Password2",
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
var res = await Client.PostAsJsonAsync(LoginUri, req, TestContext.Current.CancellationToken);
|
var res = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await res.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
await res.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
}
|
}
|
||||||
@@ -74,16 +81,19 @@ public class LoginTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
await context.SaveChangesAsync(ct);
|
await context.SaveChangesAsync(ct);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var req = new
|
using var request = HttpRequestBuilder.New()
|
||||||
{
|
.Post(LoginUri)
|
||||||
username = "Stevan",
|
.WithBody(new
|
||||||
password = "@Password1",
|
{
|
||||||
};
|
username = "Stevan",
|
||||||
|
password = "@Password1",
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
var res = await Client.PostAsJsonAsync(LoginUri, req, TestContext.Current.CancellationToken);
|
var res = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
res.Should().HaveSetCookieHeader("fiscalos_refresh_cookie");
|
res.Should().HaveSetCookieHeader("fiscalos_refresh_cookie");
|
||||||
await res.Should().BeJsonContentOfType<Login.Response>(HttpStatusCode.OK);
|
await res.Should().BeJsonContentOfType<API.Auth.Login.Response>(HttpStatusCode.OK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+26
-34
@@ -1,8 +1,8 @@
|
|||||||
namespace FiscalOS.API.Tests.Integration;
|
namespace FiscalOS.API.Tests.Integration.Auth;
|
||||||
|
|
||||||
public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
||||||
{
|
{
|
||||||
private static readonly Uri RefreshUri = new("/refresh", UriKind.Relative);
|
private static readonly Uri RefreshUri = new("/auth/refresh", UriKind.Relative);
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Refresh_WhenCalledWithNoAccessToken_ItShouldReturn401WithProblemDetails()
|
public async Task Refresh_WhenCalledWithNoAccessToken_ItShouldReturn401WithProblemDetails()
|
||||||
@@ -15,14 +15,12 @@ public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Refresh_WhenCalledWithNonExistentRefreshToken_ItShouldReturn400WithProblemDetails()
|
public async Task Refresh_WhenCalledWithNonExistentRefreshToken_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(RefreshUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithRefreshCookie("nonexistenttoken")
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, RefreshUri);
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
request.Headers.Add("Cookie", "fiscalos_refresh_cookie=nonexistenttoken");
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.BadRequest);
|
await response.Should().BeProblemDetails(HttpStatusCode.BadRequest);
|
||||||
@@ -53,14 +51,12 @@ public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (new User[] { user1, user2 }, refreshToken1);
|
return (new User[] { user1, user2 }, refreshToken1);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, users[0].Id.ToString())
|
.Post(RefreshUri)
|
||||||
|
.WithUserId(users[0].Id)
|
||||||
|
.WithRefreshCookie(refreshToken.Token)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, RefreshUri);
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
request.Headers.Add("Cookie", $"fiscalos_refresh_cookie={refreshToken.Token}");
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.Forbidden);
|
await response.Should().BeProblemDetails(HttpStatusCode.Forbidden);
|
||||||
@@ -98,14 +94,12 @@ public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, refreshToken);
|
return (user, refreshToken);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(RefreshUri)
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.WithRefreshCookie(refreshToken.Token)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, RefreshUri);
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
request.Headers.Add("Cookie", $"fiscalos_refresh_cookie={refreshToken.Token}");
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.BadRequest);
|
await response.Should().BeProblemDetails(HttpStatusCode.BadRequest);
|
||||||
@@ -132,14 +126,12 @@ public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, refreshToken);
|
return (user, refreshToken);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(RefreshUri)
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.WithRefreshCookie(refreshToken.Token)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, RefreshUri);
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
request.Headers.Add("Cookie", $"fiscalos_refresh_cookie={refreshToken.Token}");
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.BadRequest);
|
await response.Should().BeProblemDetails(HttpStatusCode.BadRequest);
|
||||||
@@ -168,19 +160,19 @@ public class RefreshTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, refreshToken);
|
return (user, refreshToken);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(RefreshUri)
|
||||||
.WithExpiresAt(DateTime.UtcNow.AddMinutes(accessTokenExpiresAtOffset))
|
.WithBearerToken(JwtTokenBuilder.New()
|
||||||
|
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
||||||
|
.WithExpiresAt(DateTime.UtcNow.AddMinutes(accessTokenExpiresAtOffset))
|
||||||
|
.Build())
|
||||||
|
.WithRefreshCookie(refreshToken.Token)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, RefreshUri);
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
request.Headers.Add("Cookie", $"fiscalos_refresh_cookie={refreshToken.Token}");
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
response.Should().HaveSetCookieHeader("fiscalos_refresh_cookie");
|
response.Should().HaveSetCookieHeader("fiscalos_refresh_cookie");
|
||||||
await response.Should().BeJsonContentOfType<Refresh.Response>(HttpStatusCode.OK);
|
await response.Should().BeJsonContentOfType<API.Auth.Refresh.Response>(HttpStatusCode.OK);
|
||||||
|
|
||||||
var oldRefreshTokenInDb = await ExecuteAsync(
|
var oldRefreshTokenInDb = await ExecuteAsync(
|
||||||
async (context, ct) => await context.Set<RefreshToken>()
|
async (context, ct) => await context.Set<RefreshToken>()
|
||||||
+44
-74
@@ -1,15 +1,20 @@
|
|||||||
using Institution = FiscalOS.Core.Accounts.Institution;
|
using Institution = FiscalOS.Core.Accounts.Institution;
|
||||||
|
|
||||||
namespace FiscalOS.API.Tests.Integration;
|
namespace FiscalOS.API.Tests.Integration.Institutions;
|
||||||
|
|
||||||
public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
||||||
{
|
{
|
||||||
private static readonly Uri ConnectUri = new("/accounts/connect", UriKind.Relative);
|
private static readonly Uri ConnectUri = new("/institutions/connect", UriKind.Relative);
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Connect_WhenCalledWithoutValidToken_ItShouldReturn401WithProblemDetails()
|
public async Task Connect_WhenCalledWithoutValidToken_ItShouldReturn401WithProblemDetails()
|
||||||
{
|
{
|
||||||
var res = await Client.PostAsJsonAsync(ConnectUri, new { }, TestContext.Current.CancellationToken);
|
using var request = HttpRequestBuilder.New()
|
||||||
|
.Post(ConnectUri)
|
||||||
|
.WithBody(new { })
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var res = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await res.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
await res.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
}
|
}
|
||||||
@@ -17,17 +22,12 @@ public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Connect_WhenCalledWithoutPublicTokenOrPlaidInstitutionId_ItShouldReturn400WithProblemDetails()
|
public async Task Connect_WhenCalledWithoutPublicTokenOrPlaidInstitutionId_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(ConnectUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new { })
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
using var content = new StringContent(JsonSerializer.Serialize(new { }), Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, ConnectUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -40,18 +40,12 @@ public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Connect_WhenCalledWithoutPublicToken_ItShouldReturn400WithProblemDetails()
|
public async Task Connect_WhenCalledWithoutPublicToken_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(ConnectUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new { plaidInstitutionId = "id" })
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new { plaidInstitutionId = "id" });
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, ConnectUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -63,18 +57,12 @@ public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Connect_WhenCalledWithoutPlaidInstitutionId_ItShouldReturn400WithProblemDetails()
|
public async Task Connect_WhenCalledWithoutPlaidInstitutionId_ItShouldReturn400WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(ConnectUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new { publicToken = "token" })
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new { publicToken = "token" });
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, ConnectUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
await response.Should().BeValidationProblemDetails(new Dictionary<string, string[]>()
|
||||||
@@ -86,22 +74,16 @@ public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Connect_WhenCalledWithNonExistentUser_ItShouldReturn401WithProblemDetails()
|
public async Task Connect_WhenCalledWithNonExistentUser_ItShouldReturn401WithProblemDetails()
|
||||||
{
|
{
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, Guid.NewGuid().ToString())
|
.Post(ConnectUri)
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
publicToken = "token",
|
||||||
|
plaidInstitutionId = "id",
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
publicToken = "token",
|
|
||||||
plaidInstitutionId = "id",
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, ConnectUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
await response.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
@@ -129,22 +111,16 @@ public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, institution);
|
return (user, institution);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(ConnectUri)
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
publicToken = "token",
|
||||||
|
plaidInstitutionId = ((PlaidMetadata)institution.Metadata!).PlaidId,
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
publicToken = "token",
|
|
||||||
plaidInstitutionId = ((PlaidMetadata)institution.Metadata!).PlaidId,
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, ConnectUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
await response.Should().BeProblemDetails(HttpStatusCode.Conflict);
|
await response.Should().BeProblemDetails(HttpStatusCode.Conflict);
|
||||||
@@ -176,22 +152,16 @@ public class ConnectTests(TestApi testApi) : IntegrationTest(testApi)
|
|||||||
return (user, publicTokenResponse.PublicToken);
|
return (user, publicTokenResponse.PublicToken);
|
||||||
}, TestContext.Current.CancellationToken);
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var jwt = JwtTokenBuilder.New()
|
using var request = HttpRequestBuilder.New()
|
||||||
.WithClaim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
|
.Post(ConnectUri)
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.WithBody(new
|
||||||
|
{
|
||||||
|
publicToken,
|
||||||
|
plaidInstitutionId,
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(new
|
|
||||||
{
|
|
||||||
publicToken,
|
|
||||||
plaidInstitutionId,
|
|
||||||
});
|
|
||||||
using var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, ConnectUri)
|
|
||||||
{
|
|
||||||
Content = content,
|
|
||||||
};
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
||||||
|
|
||||||
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
using Institution = FiscalOS.Core.Accounts.Institution;
|
||||||
|
|
||||||
|
namespace FiscalOS.API.Tests.Integration.Institutions;
|
||||||
|
|
||||||
|
public class GetAvailableTests(TestApi testApi) : IntegrationTest(testApi)
|
||||||
|
{
|
||||||
|
private static Uri GetAvailableUri(Guid id) => new($"/institutions/{id}/available", UriKind.Relative);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAvailable_WhenCalledAndUnauthenticated_ItShouldReturn401WithProblemDetails()
|
||||||
|
{
|
||||||
|
using var request = HttpRequestBuilder.New()
|
||||||
|
.WithUri(GetAvailableUri(Guid.NewGuid()))
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
await response.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAvailable_WhenCalledByNonExistentUser_ItShouldReturn401WithProblemDetails()
|
||||||
|
{
|
||||||
|
using var request = HttpRequestBuilder.New()
|
||||||
|
.WithUri(GetAvailableUri(Guid.NewGuid()))
|
||||||
|
.WithUserId(Guid.NewGuid())
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
await response.Should().BeProblemDetails(HttpStatusCode.Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAvailable_WhenCalledWithNonExistentInstitutionId_ItShouldReturn404WithProblemDetails()
|
||||||
|
{
|
||||||
|
var user = await ExecuteAsync(static async (context, ct, sp) =>
|
||||||
|
{
|
||||||
|
var passwordHasher = sp.GetRequiredService<IPasswordHasher>();
|
||||||
|
var encryptor = sp.GetRequiredService<IEncryptor>();
|
||||||
|
|
||||||
|
var userEncryptionKey = await encryptor.GenerateEncryptedKeyAsync(ct);
|
||||||
|
var user = User.From("Stevan", passwordHasher.Hash("@Password1"), userEncryptionKey);
|
||||||
|
|
||||||
|
context.Add(user);
|
||||||
|
await context.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
using var request = HttpRequestBuilder.New()
|
||||||
|
.WithUri(GetAvailableUri(Guid.NewGuid()))
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
await response.Should().BeProblemDetails(HttpStatusCode.NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAvailable_WhenCalledWithConnectedInstitution_ItShouldReturn200WithAvailableAccounts()
|
||||||
|
{
|
||||||
|
var (user, institution, expectedAccounts) = await ExecuteAsync(static async (context, ct, sp) =>
|
||||||
|
{
|
||||||
|
var passwordHasher = sp.GetRequiredService<IPasswordHasher>();
|
||||||
|
var encryptor = sp.GetRequiredService<IEncryptor>();
|
||||||
|
var plaidClient = sp.GetRequiredService<PlaidClient>();
|
||||||
|
|
||||||
|
var userEncryptionKey = await encryptor.GenerateEncryptedKeyAsync(ct);
|
||||||
|
var user = User.From("Stevan", passwordHasher.Hash("@Password1"), userEncryptionKey);
|
||||||
|
|
||||||
|
var institutionId = "ins_109508";
|
||||||
|
|
||||||
|
var publicTokenResponse = await plaidClient.SandboxPublicTokenCreateAsync(new()
|
||||||
|
{
|
||||||
|
InstitutionId = institutionId,
|
||||||
|
InitialProducts = [Products.Transactions],
|
||||||
|
});
|
||||||
|
|
||||||
|
var exchangeTokenResponse = await plaidClient.ItemPublicTokenExchangeAsync(new()
|
||||||
|
{
|
||||||
|
PublicToken = publicTokenResponse.PublicToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
var accountsResponse = await plaidClient.AccountsGetAsync(new()
|
||||||
|
{
|
||||||
|
AccessToken = exchangeTokenResponse.AccessToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
var encryptedAccessToken = await encryptor.EncryptAsyncFor(user, exchangeTokenResponse.AccessToken, ct);
|
||||||
|
var institutionMetadata = PlaidMetadata.From(institutionId, "Some Bank", encryptedAccessToken);
|
||||||
|
var institution = Institution.From("Some Bank", institutionMetadata);
|
||||||
|
|
||||||
|
user.AddInstitution(institution);
|
||||||
|
|
||||||
|
context.Add(user);
|
||||||
|
context.Add(institution);
|
||||||
|
await context.SaveChangesAsync(ct);
|
||||||
|
|
||||||
|
return (user, institution, accountsResponse.Accounts);
|
||||||
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
using var request = HttpRequestBuilder.New()
|
||||||
|
.WithUri(GetAvailableUri(institution.Id))
|
||||||
|
.WithUserId(user.Id)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = await Client.SendAsync(request, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
(await response.Should().BeJsonContentOfType<List<Account>>(HttpStatusCode.OK))
|
||||||
|
.Which.Should().BeEquivalentTo(expectedAccounts);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@ global using Microsoft.AspNetCore.Mvc;
|
|||||||
global using Microsoft.AspNetCore.Mvc.Testing;
|
global using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
global using Microsoft.AspNetCore.TestHost;
|
global using Microsoft.AspNetCore.TestHost;
|
||||||
global using Microsoft.EntityFrameworkCore;
|
global using Microsoft.EntityFrameworkCore;
|
||||||
|
global using Microsoft.Extensions.Configuration;
|
||||||
global using Microsoft.Extensions.DependencyInjection;
|
global using Microsoft.Extensions.DependencyInjection;
|
||||||
global using Microsoft.Extensions.Logging;
|
global using Microsoft.Extensions.Logging;
|
||||||
global using Microsoft.Extensions.Options;
|
global using Microsoft.Extensions.Options;
|
||||||
|
|||||||
Reference in New Issue
Block a user