Skip to content

Commit

Permalink
Fix: Webhooks: exception during payload deserialization (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
innomaxx committed May 10, 2023
1 parent aae2862 commit c22a5e0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Crowdin.Api/Webhooks/Webhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Webhook
public IDictionary<string, string> Headers { get; set; }

[JsonProperty("payload")]
public IDictionary<string, IDictionary<string, string>> Payload { get; set; }
public object Payload { get; set; }

[JsonProperty("isActive")]
public bool IsActive { get; set; }
Expand Down
55 changes: 41 additions & 14 deletions tests/Crowdin.Api.Tests/Core/Resources/Webhooks.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions tests/Crowdin.Api.Tests/Core/Resources/Webhooks.resx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,64 @@
"createdAt": "2019-09-23T09:19:07+00:00",
"updatedAt": "2019-09-23T09:19:07+00:00"
}
}</value>
</data>
<data name="GetWebhook_Response" xml:space="preserve">
<value>{
"data": {
"id": 9,
"projectId": 7,
"name": "File proofreading complete",
"url": "https://something.com",
"events": [
"project.approved"
],
"headers": {
"x-api-key": "secret"
},
"payload": {
"project.approved": {
"event": "{{event}}",
"project": {
"id": "{{projectId}}",
"userId": "{{projectUserId}}",
"sourceLanguageId": "{{projectSourceLanguageId}}",
"targetLanguageIds": "{{projectTargetLanguageIds}}",
"identifier": "{{projectIdentifier}}",
"name": "{{projectName}}",
"createdAt": "{{projectCreatedAt}}",
"updatedAt": "{{projectUpdatedAt}}",
"lastActivity": "{{projectLastActivity}}",
"description": "{{projectDescription}}",
"url": "{{projectUrl}}",
"cname": "{{projectCname}}",
"logo": "{{projectLogo}}",
"isExternal": "{{projectIsExternal}}",
"externalType": "{{projectExternalType}}",
"hasCrowdsourcing": "{{projectHasCrowdsourcing}}"
},
"targetLanguage": {
"id": "{{targetLanguageId}}",
"name": "{{targetLanguageName}}",
"editorCode": "{{targetLanguageEditorCode}}",
"twoLettersCode": "{{targetLanguageTwoLettersCode}}",
"threeLettersCode": "{{targetLanguageThreeLettersCode}}",
"locale": "{{targetLanguageLocale}}",
"androidCode": "{{targetLanguageAndroidCode}}",
"osxCode": "{{targetLanguageOsxCode}}",
"osxLocale": "{{targetLanguageOsxLocale}}",
"textDirection": "{{targetLanguageTextDirection}}",
"dialectOf": "{{targetLanguageDialectOf}}"
}
}
},
"isActive": true,
"batchingEnabled": false,
"requestType": "POST",
"contentType": "application/json",
"createdAt": "2023-05-05T10:22:40+02:00",
"updatedAt": "2023-05-05T10:22:40+02:00"
}
}</value>
</data>
</root>
40 changes: 40 additions & 0 deletions tests/Crowdin.Api.Tests/Webhooks/WebhooksApiTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -126,5 +127,44 @@ public async Task EditWebhook()
Assert.Equal(newName, response.Name);
Assert.Equal(newRequestType, response.RequestType);
}

[Fact]
public async Task GetWebhook()
{
const int projectId = 7;
const int webhookId = 9;

Mock<ICrowdinApiClient> mockClient = TestUtils.CreateMockClientWithDefaultParser();

var url = $"/projects/{projectId}/webhooks/{webhookId}";

mockClient
.Setup(client => client.SendGetRequest(url, null))
.ReturnsAsync(new CrowdinApiResult
{
StatusCode = HttpStatusCode.OK,
JsonObject = JObject.Parse(Core.Resources.Webhooks.GetWebhook_Response)
});

var executor = new WebhooksApiExecutor(mockClient.Object);
Webhook? response = await executor.GetWebhook(projectId, webhookId);

Assert.NotNull(response);

Assert.Single(response.Events);
Assert.Equal(EventType.ProjectApproved, response.Events[0]);

Assert.Single(response.Headers);
Assert.Equal("secret", response.Headers["x-api-key"]);

var payload = response.Payload as JObject;
Assert.NotNull(payload);
Assert.NotNull(payload!["project.approved"]);

Assert.Equal(ContentType.ApplicationJson, response.ContentType);
Assert.Equal(RequestType.POST, response.RequestType);
Assert.Equal(DateTimeOffset.Parse("2023-05-05T10:22:40+02:00"), response.CreatedAt);
Assert.Equal(DateTimeOffset.Parse("2023-05-05T10:22:40+02:00"), response.UpdatedAt);
}
}
}

0 comments on commit c22a5e0

Please sign in to comment.