Skip to content

Commit

Permalink
UserMerged Webhook (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKevJoy authored Mar 5, 2024
1 parent b0f3e47 commit 4a10fa2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ IDistributedLockProvider distributedLockProvider
{
user = userCreatedMessage.User;
}
else if (notification.Message is UserMergedMessage userMergeMessage)
{
await dataverseAdapter.ClearTeacherIdentityInfo(userMergeMessage.MergedUserId, notification.TimeUtc);
return CreateResult();
}
else
{
return CreateResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ namespace TeachingRecordSystem.Api.Endpoints.IdentityWebHooks.Messages;
public class UserMergedMessage : INotificationMessage
{
public const string MessageTypeName = "UserMerged";

public required Guid MergedUserId { get; init; }
public required User MasterUser { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,50 @@ public async Task Post_WithInvalidMessageFormat_ThrowsJsonException()
await Assert.ThrowsAsync<JsonException>(() => httpClient.SendAsync(request));
}

[Fact]
public async Task Post_WithValidUserMergedMessage_ReturnsNoContent()
{
// Arrange
var now = DateTime.UtcNow;
var masterContactId = Guid.NewGuid();
var mergedContactId = Guid.NewGuid();
var content = new
{
NotificationId = Guid.NewGuid(),
TimeUtc = now,
MessageType = UserMergedMessage.MessageTypeName,
Message = new
{
MasterUser = new
{
UserId = masterContactId,
EmailAddress = Faker.Internet.Email(),
Trn = "7654321",
MobileNumber = "07968987654"
},
MergedUserId = mergedContactId,
Changes = new { }
}
};

var jsonContent = JsonSerializer.Serialize(content, GetAnIdentityEndpoints.SerializerOptions);
var signature = GenerateSignature(GetAnIdentityOptions.Value.WebHookClientSecret, jsonContent);
var httpClient = HostFixture.CreateClient();
httpClient.DefaultRequestHeaders.Add("X-Hub-Signature-256", signature);

var request = new HttpRequestMessage(HttpMethod.Post, "/webhooks/identity")
{
Content = CreateJsonContent(jsonContent)
};

// Act
var response = await httpClient.SendAsync(request);

// Assert
Assert.Equal(StatusCodes.Status204NoContent, (int)response.StatusCode);
DataverseAdapterMock.Verify(mock => mock.ClearTeacherIdentityInfo(mergedContactId, now));
}

[Fact]
public async Task Post_WithValidUserUpdatedMessage_ReturnsNoContent()
{
Expand Down

0 comments on commit 4a10fa2

Please sign in to comment.