Browse files

web hook tests for all resource types

  • Loading branch information...
1 parent eb18f52 commit 27321e4cba850175a8e686319e7721f194619f58 Tim Iles committed Mar 24, 2012
View
10 GoCardlessSdk.Tests/GoCardlessSdk.Tests.csproj
@@ -113,10 +113,16 @@
<Content Include="Api\Data\PUT gocardless.com_api_v1_subscriptions_1580_cancel.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="WebHooks\Data\test invalid signature.txt">
+ <Content Include="WebHooks\Data\PreAuthorization.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
- <Content Include="WebHooks\Data\test valid signature.txt">
+ <Content Include="WebHooks\Data\Subscription.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ <Content Include="WebHooks\Data\Bill invalid signature.txt">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
+ <Content Include="WebHooks\Data\Bill.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
View
0 .../WebHooks/Data/test invalid signature.txt → .../WebHooks/Data/Bill invalid signature.txt
File renamed without changes.
View
2 ...ts/WebHooks/Data/test valid signature.txt → GoCardlessSdk.Tests/WebHooks/Data/Bill.txt
@@ -20,6 +20,6 @@
"uri": "https://sandbox.gocardless.com/api/v1/bills/AKJ398H8KB"
}
],
- "signature": "27eafb3a2f38bb6fae752ae6cd7a1a7b5a4df660af2a90499748d8f5d1f554ba"
+ "signature": "a68fbb8bce16dc3daa851043fd602e72955496093cd92f0bf89801a85fb7282d"
}
}
View
16 GoCardlessSdk.Tests/WebHooks/Data/PreAuthorization.txt
@@ -0,0 +1,16 @@
+{
+ "payload": {
+ "resource_type": "pre_authorization",
+ "action": "cancelled",
+ "pre_authorizations": [{
+ "id": "AKJ398H8KBOOO3",
+ "status": "cancelled",
+ "uri": "https://sandbox.gocardless.com/api/v1/pre_authorizations/AKJ398H8KBOOO3"
+ }, {
+ "id": "AKJ398H8KBOOOA",
+ "status": "cancelled",
+ "uri": "https://sandbox.gocardless.com/api/v1/pre_authorizations/AKJ398H8KBOOOA"
+ }],
+ "signature": "e8c8097028ff11b5d0fbec92443e62da0ff696f4fd03f4fc9590d502e203a4f5"
+ }
+}
View
16 GoCardlessSdk.Tests/WebHooks/Data/Subscription.txt
@@ -0,0 +1,16 @@
+{
+ "payload": {
+ "resource_type": "subscription",
+ "action": "cancelled",
+ "subscriptions": [{
+ "id": "AKJ398H8KBO122A",
+ "status": "cancelled",
+ "uri": "https://sandbox.gocardless.com/api/v1/subscriptions/AKJ398H8KBO122A"
+ },{
+ "id": "BBJ398H8KBO122A",
+ "status": "cancelled",
+ "uri": "https://sandbox.gocardless.com/api/v1/subscriptions/BBJ398H8KBO122A"
+ }],
+ "signature": "183501db0732c7615c5fad36afa492cc6cdbd1ff83c55a76fb14d2ddba760e34"
+ }
+}
View
82 GoCardlessSdk.Tests/WebHooks/WebHooksTests.cs
@@ -7,21 +7,89 @@ namespace GoCardlessSdk.Tests.WebHooks
public class WebHooksTests
{
[Test]
- public void TestInvalidSignature()
+ public void Bill_InvalidSignature_ThrowsException()
{
- var request = File.ReadAllText("./Webhooks/Data/test invalid signature.txt");
- GoCardless.AccountDetails.AppSecret = "8ifu76Qi4HMJC1zSNf93WntQzJKpSmce0SwBNTA5HEqQY61aBTH7Nsx4w_HG1vUL";
+ var request = File.ReadAllText("./Webhooks/Data/Bill invalid signature.txt");
+ GoCardless.AccountDetails.AppSecret = "test_secret";
Assert.Throws<SignatureException>(() => WebHooksClient.ParseRequest(request));
}
[Test]
- public void TestValidSignature()
+ public void Bill_PayloadDeserializesOk()
{
- var request = File.ReadAllText("./Webhooks/Data/test valid signature.txt");
- GoCardless.AccountDetails.AppSecret = "8ifu76Qi4HMJC1zSNf93WntQzJKpSmce0SwBNTA5HEqQY61aBTH7Nsx4w_HG1vUL";
- Assert.DoesNotThrow(() => WebHooksClient.ParseRequest(request));
+ var request = File.ReadAllText("./Webhooks/Data/Bill.txt");
+ GoCardless.AccountDetails.AppSecret = "test_secret";
+
var payload = WebHooksClient.ParseRequest(request);
+
+ Assert.AreEqual("bill", payload.ResourceType);
+ Assert.AreEqual("paid", payload.Action);
+
+ Assert.AreEqual(2, payload.Bills.Length);
+ Assert.IsNull(payload.PreAuthorizations);
+ Assert.IsNull(payload.Subscriptions);
+
+ Assert.AreEqual("AKJ398H8KA", payload.Bills[0].Id);
+ Assert.AreEqual("paid", payload.Bills[0].Status);
+ Assert.AreEqual("subscription", payload.Bills[0].SourceType);
+ Assert.AreEqual("KKJ398H8K8", payload.Bills[0].SourceId);
+ Assert.AreEqual("2011-12-01T12:00:00Z", payload.Bills[0].PaidAt);
+ Assert.AreEqual("https://sandbox.gocardless.com/api/v1/bills/AKJ398H8KA", payload.Bills[0].Uri);
+
+ Assert.AreEqual("AKJ398H8KB", payload.Bills[1].Id);
+ Assert.AreEqual("paid", payload.Bills[01].Status);
+ Assert.AreEqual("subscription", payload.Bills[1].SourceType);
+ Assert.AreEqual("8AKJ398H78", payload.Bills[1].SourceId);
+ Assert.AreEqual("2011-12-09T12:00:00Z", payload.Bills[1].PaidAt);
+ Assert.AreEqual("https://sandbox.gocardless.com/api/v1/bills/AKJ398H8KB", payload.Bills[1].Uri);
}
+ [Test]
+ public void PreAuthorization_PayloadDeserializesOk()
+ {
+ var request = File.ReadAllText("./Webhooks/Data/PreAuthorization.txt");
+ GoCardless.AccountDetails.AppSecret = "test_secret";
+
+ var payload = WebHooksClient.ParseRequest(request);
+
+ Assert.AreEqual("pre_authorization", payload.ResourceType);
+ Assert.AreEqual("cancelled", payload.Action);
+
+ Assert.IsNull(payload.Bills);
+ Assert.AreEqual(2, payload.PreAuthorizations.Length);
+ Assert.IsNull(payload.Subscriptions);
+
+ Assert.AreEqual("AKJ398H8KBOOO3", payload.PreAuthorizations[0].Id);
+ Assert.AreEqual("cancelled", payload.PreAuthorizations[0].Status);
+ Assert.AreEqual("https://sandbox.gocardless.com/api/v1/pre_authorizations/AKJ398H8KBOOO3", payload.PreAuthorizations[0].Uri);
+
+ Assert.AreEqual("AKJ398H8KBOOOA", payload.PreAuthorizations[1].Id);
+ Assert.AreEqual("cancelled", payload.PreAuthorizations[01].Status);
+ Assert.AreEqual("https://sandbox.gocardless.com/api/v1/pre_authorizations/AKJ398H8KBOOOA", payload.PreAuthorizations[1].Uri);
+ }
+
+ [Test]
+ public void Subscription_PayloadDeserializesOk()
+ {
+ var request = File.ReadAllText("./Webhooks/Data/Subscription.txt");
+ GoCardless.AccountDetails.AppSecret = "test_secret";
+
+ var payload = WebHooksClient.ParseRequest(request);
+
+ Assert.AreEqual("subscription", payload.ResourceType);
+ Assert.AreEqual("cancelled", payload.Action);
+
+ Assert.IsNull(payload.Bills);
+ Assert.IsNull(payload.PreAuthorizations);
+ Assert.AreEqual(2, payload.Subscriptions.Length);
+
+ Assert.AreEqual("AKJ398H8KBO122A", payload.Subscriptions[0].Id);
+ Assert.AreEqual("cancelled", payload.Subscriptions[0].Status);
+ Assert.AreEqual("https://sandbox.gocardless.com/api/v1/subscriptions/AKJ398H8KBO122A", payload.Subscriptions[0].Uri);
+
+ Assert.AreEqual("BBJ398H8KBO122A", payload.Subscriptions[1].Id);
+ Assert.AreEqual("cancelled", payload.Subscriptions[1].Status);
+ Assert.AreEqual("https://sandbox.gocardless.com/api/v1/subscriptions/BBJ398H8KBO122A", payload.Subscriptions[1].Uri);
+ }
}
}

0 comments on commit 27321e4

Please sign in to comment.