Skip to content

Commit

Permalink
Change model of eventsrececiver endpoint to CloudEventEnvelope
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny Birkeli committed Nov 6, 2022
1 parent 2c8c917 commit f21908b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Altinn.App.Api/Controllers/EventsReceiverController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public EventsReceiverController(
[ProducesResponseType(425)]
[ProducesResponseType(500)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> Post([FromQuery] string code, [FromBody] CloudEvent cloudEvent)
public async Task<ActionResult> Post([FromQuery] string code, [FromBody] CloudEventEnvelope cloudEventEnvelope)
{
if (await _secretCodeProvider.GetSecretCode() != code)
{
return Unauthorized();
}

CloudEvent cloudEvent = cloudEventEnvelope.CloudEvent;

IEventHandler eventHandler = _eventHandlerResolver.ResolveEventHandler(cloudEvent.Type);
try
Expand All @@ -62,7 +64,7 @@ public async Task<ActionResult> Post([FromQuery] string code, [FromBody] CloudEv
}
catch (NotImplementedException)
{
return BadRequest();
return BadRequest($"No eventhandler found that supports {cloudEvent.Type}");
}
catch (Exception ex)
{
Expand Down
33 changes: 33 additions & 0 deletions src/Altinn.App.Core/Models/CloudEventEnvelope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Altinn.App.Core.Models
{
/// <summary>
/// Cloud event envelope to push
/// </summary>
public class CloudEventEnvelope
{
/// <summary>
/// The Event to push
/// </summary>
public CloudEvent? CloudEvent { get; set; }

/// <summary>
/// The time the event was pushed to queue
/// </summary>
public DateTime Pushed { get; set; }

/// <summary>
/// Target URI to push event
/// </summary>
public Uri? Endpoint { get; set; }

/// <summary>
/// The consumer of the events
/// </summary>
public string? Consumer { get; set; }

/// <summary>
/// The subscription id that matched.
/// </summary>
public int SubscriptionId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public EventsReceiverControllerTests(WebApplicationFactory<Program> factory)
_factory = factory;
_secretCodeProvider = _factory.Services.GetRequiredService<IEventSecretCodeProvider>();
}

[Fact]
public async Task Post_ValidEventType_ShouldReturnOk()
{
Expand All @@ -39,13 +40,14 @@ public async Task Post_ValidEventType_ShouldReturnOk()
Time = DateTime.Parse("2022-10-13T09:33:46.6330634Z"),
AlternativeSubject = "/person/17858296439"
};
CloudEventEnvelope envelope = new() { CloudEvent = cloudEvent };

var org = "ttd";
var app = "non-existing-app";
string requestUrl = $"{org}/{app}/api/v1/eventsreceiver?code={await _secretCodeProvider.GetSecretCode()}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
{
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(cloudEvent), Encoding.UTF8, "application/json")
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(envelope), Encoding.UTF8, "application/json")
};

HttpResponseMessage response = await client.SendAsync(request);
Expand All @@ -69,13 +71,14 @@ public async Task Post_NonValidEventType_ShouldReturnBadRequest()
Time = DateTime.Parse("2022-10-13T09:33:46.6330634Z"),
AlternativeSubject = "/person/17858296439"
};
CloudEventEnvelope envelope = new() { CloudEvent = cloudEvent };

var org = "ttd";
var app = "non-existing-app";
string requestUrl = $"{org}/{app}/api/v1/eventsreceiver?code={await _secretCodeProvider.GetSecretCode()}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
{
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(cloudEvent), Encoding.UTF8, "application/json")
Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(envelope), Encoding.UTF8, "application/json")
};

HttpResponseMessage response = await client.SendAsync(request);
Expand Down

0 comments on commit f21908b

Please sign in to comment.