Skip to content

Commit

Permalink
Split REST controller into multiple files as preparation (#182)
Browse files Browse the repository at this point in the history
* Split REST controller into multiple files
  • Loading branch information
MartinSchmidt authored Jan 3, 2024
1 parent e7736e3 commit 7319172
Show file tree
Hide file tree
Showing 9 changed files with 784 additions and 632 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"/v1/certificates": {
"get": {
"tags": [
"Wallet"
"Certificates"
],
"summary": "Gets all certificates in the wallet that are <b>available</b> for use.",
"parameters": [
Expand Down Expand Up @@ -48,13 +48,29 @@
}
}
},
"/v1/claims": {
"/v1/aggregate-certificates": {
"get": {
"tags": [
"Wallet"
"Certificates"
],
"summary": "Gets all claims in the wallet",
"summary": "Returns aggregates certificates that are <b>available</b> to use, based on the specified time zone and time range.",
"parameters": [
{
"name": "timeAggregate",
"in": "query",
"description": "The size of each bucket in the aggregation",
"schema": {
"$ref": "#/components/schemas/TimeAggregate"
}
},
{
"name": "timeZone",
"in": "query",
"description": "The time zone. See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a list of valid time zones.",
"schema": {
"type": "string"
}
},
{
"name": "start",
"in": "query",
Expand All @@ -72,31 +88,42 @@
"type": "integer",
"format": "int64"
}
},
{
"name": "type",
"in": "query",
"description": "Filter the type of certificates to return.",
"schema": {
"$ref": "#/components/schemas/CertificateType"
}
}
],
"responses": {
"200": {
"description": "Returns all the indiviual claims.",
"description": "Returns the aggregated claims.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ClaimResultList"
"$ref": "#/components/schemas/AggregatedCertificatesResultList"
}
}
}
},
"400": {
"description": "If the time zone is invalid."
},
"401": {
"description": "If the user is not authenticated."
}
}
}
},
"/v1/transfers": {
"/v1/claims": {
"get": {
"tags": [
"Wallet"
"Claims"
],
"summary": "Gets detailed list of all of the transfers that have been made to other wallets.",
"summary": "Gets all claims in the wallet",
"parameters": [
{
"name": "start",
Expand All @@ -119,11 +146,11 @@
],
"responses": {
"200": {
"description": "Returns the individual transferes within the filter.",
"description": "Returns all the indiviual claims.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TransferResultList"
"$ref": "#/components/schemas/ClaimResultList"
}
}
}
Expand All @@ -134,12 +161,12 @@
}
}
},
"/v1/aggregate-certificates": {
"/v1/aggregate-claims": {
"get": {
"tags": [
"Wallet"
"Claims"
],
"summary": "Returns aggregates certificates that are <b>available</b> to use, based on the specified time zone and time range.",
"summary": "Returns a list of aggregates claims for the authenticated user based on the specified time zone and time range.",
"parameters": [
{
"name": "timeAggregate",
Expand Down Expand Up @@ -174,14 +201,6 @@
"type": "integer",
"format": "int64"
}
},
{
"name": "type",
"in": "query",
"description": "Filter the type of certificates to return.",
"schema": {
"$ref": "#/components/schemas/CertificateType"
}
}
],
"responses": {
Expand All @@ -190,7 +209,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AggregatedCertificatesResultList"
"$ref": "#/components/schemas/AggregatedClaimsResultList"
}
}
}
Expand All @@ -204,29 +223,13 @@
}
}
},
"/v1/aggregate-claims": {
"/v1/transfers": {
"get": {
"tags": [
"Wallet"
"Transfers"
],
"summary": "Returns a list of aggregates claims for the authenticated user based on the specified time zone and time range.",
"summary": "Gets detailed list of all of the transfers that have been made to other wallets.",
"parameters": [
{
"name": "timeAggregate",
"in": "query",
"description": "The size of each bucket in the aggregation",
"schema": {
"$ref": "#/components/schemas/TimeAggregate"
}
},
{
"name": "timeZone",
"in": "query",
"description": "The time zone. See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a list of valid time zones.",
"schema": {
"type": "string"
}
},
{
"name": "start",
"in": "query",
Expand All @@ -248,18 +251,15 @@
],
"responses": {
"200": {
"description": "Returns the aggregated claims.",
"description": "Returns the individual transferes within the filter.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/AggregatedClaimsResultList"
"$ref": "#/components/schemas/TransferResultList"
}
}
}
},
"400": {
"description": "If the time zone is invalid."
},
"401": {
"description": "If the user is not authenticated."
}
Expand All @@ -269,7 +269,7 @@
"/v1/aggregate-transfers": {
"get": {
"tags": [
"Wallet"
"Transfers"
],
"summary": "Returns a list of aggregates transfers, for all certificates transferred to another wallet for the authenticated user based.",
"parameters": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using AutoFixture;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProjectOrigin.WalletSystem.IntegrationTests.TestClassFixtures;
using ProjectOrigin.WalletSystem.IntegrationTests.TestExtensions;
using ProjectOrigin.WalletSystem.Server.Database;
using ProjectOrigin.WalletSystem.Server.Services.REST.v1;
using Xunit;

namespace ProjectOrigin.WalletSystem.IntegrationTests;

public class CertificatesControllerTests : IClassFixture<PostgresDatabaseFixture>
{
private readonly Fixture _fixture;
private readonly PostgresDatabaseFixture _dbFixture;
private readonly IUnitOfWork _unitOfWork;

public CertificatesControllerTests(PostgresDatabaseFixture postgresDatabaseFixture)
{
_fixture = new Fixture();
_dbFixture = postgresDatabaseFixture;
_unitOfWork = _dbFixture.CreateUnitOfWork();
}

[Fact]
public async Task Verify_Unauthorized()
{
// Arrange
var controller = new CertificatesController();

// Act
var result = await controller.GetCertificates(
_unitOfWork,
null,
null);

// Assert
result.Result.Should().BeOfType<UnauthorizedResult>();
}

[Theory]
[InlineData("Europe/Copenhagen", new long[] { 1000, 2400, 1400 }, CertificateType.Production)]
[InlineData("Europe/London", new long[] { 110, 240, 130 }, CertificateType.Consumption)]
[InlineData("America/Toronto", new long[] { 1600, 2400, 800 }, CertificateType.Production)]
public async Task Test_AggregateCertificates(string timezone, long[] values, CertificateType type)
{
// Arrange
var issuestartDate = new DateTimeOffset(2020, 6, 1, 12, 0, 0, TimeSpan.Zero);
var issueEndDate = new DateTimeOffset(2020, 6, 30, 12, 0, 0, TimeSpan.Zero);
var queryStartDate = new DateTimeOffset(2020, 6, 8, 12, 0, 0, TimeSpan.Zero);
var queryEndDate = new DateTimeOffset(2020, 6, 10, 12, 0, 0, TimeSpan.Zero);

var subject = _fixture.Create<string>();
var controller = new CertificatesController
{
ControllerContext = CreateContextWithUser(subject)
};

var wallet = await _dbFixture.CreateWallet(subject);
var endpoint = await _dbFixture.CreateWalletEndpoint(wallet);

for (DateTimeOffset i = issuestartDate; i < issueEndDate; i = i.AddHours(1))
{
var prodCert = await _dbFixture.CreateCertificate(
Guid.NewGuid(),
_fixture.Create<string>(),
Server.Models.GranularCertificateType.Production,
start: i,
end: i.AddHours(1));
var prodSlice = await _dbFixture.CreateSlice(endpoint, prodCert, new PedersenCommitment.SecretCommitmentInfo(100));

var consCert = await _dbFixture.CreateCertificate(
Guid.NewGuid(),
_fixture.Create<string>(),
Server.Models.GranularCertificateType.Consumption,
start: i,
end: i.AddHours(1));
var consSlice = await _dbFixture.CreateSlice(endpoint, consCert, new PedersenCommitment.SecretCommitmentInfo(10));
}

// Act
var result = await controller.AggregateCertificates(
_unitOfWork,
TimeAggregate.Day,
timezone,
queryStartDate.ToUnixTimeSeconds(),
queryEndDate.ToUnixTimeSeconds(),
type);

// Assert
result.Value.Should().NotBeNull();
var resultList = result.Value!.Result;

resultList.Should().HaveCount(3);
resultList.Select(x => x.Quantity).Should().ContainInOrder(values);
}

[Fact]
public async Task AggregateCertificates_Invalid_TimeZone()
{
// Arrange
var subject = _fixture.Create<string>();
var controller = new CertificatesController
{
ControllerContext = CreateContextWithUser(subject)
};

// Act
var result = await controller.AggregateCertificates(
_unitOfWork,
TimeAggregate.Day,
"invalid-time-zone",
null,
null,
null);

// Assert
result.Result.Should().BeOfType<BadRequestObjectResult>();
}



private static ControllerContext CreateContextWithUser(string subject)
{
return new ControllerContext
{
HttpContext = new DefaultHttpContext
{
User = new ClaimsPrincipal(new ClaimsIdentity(new System.Security.Claims.Claim[]
{
new(ClaimTypes.NameIdentifier, subject),
})),
}
};
}
}
Loading

0 comments on commit 7319172

Please sign in to comment.