Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserMerged Webhook #1219

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading