Skip to content

Commit

Permalink
feat(repo): implement takeScreenshot
Browse files Browse the repository at this point in the history
chore(repo): capitalise verifywebhooksignature method

chore(repo): reorder methods into categories with public/private
  • Loading branch information
AJCJ1 authored and cjroebuck committed Jan 21, 2025
1 parent 17982f5 commit b791199
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 110 deletions.
78 changes: 66 additions & 12 deletions Urlbox.MsTest/UrlboxTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.Diagnostics;
using System.Dynamic;
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Screenshots;

[TestClass]
Expand Down Expand Up @@ -157,7 +153,7 @@ public void Urlbox_createsWithWebhookValidator()
{
Urlbox urlbox = new Urlbox("key", "secret", "webhook");
// Shar of 'content' should not match 321, but method should run if 'webhook' passed.
var result = urlbox.verifyWebhookSignature("t=123,sha256=321", "content");
var result = urlbox.VerifyWebhookSignature("t=123,sha256=321", "content");
Assert.IsFalse(result);
}

Expand All @@ -166,7 +162,7 @@ public void Urlbox_createsWithoutWebhookValidator()
{
Urlbox urlbox = new Urlbox("key", "secret");
// Should throw bc no webhook set so no validator instance
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.verifyWebhookSignature("t=123,sha256=321", "content"));
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature("t=123,sha256=321", "content"));
Assert.AreEqual(result.Message, "Please set your webhook secret in the Urlbox instance before calling this method.");
}

Expand Down Expand Up @@ -426,6 +422,64 @@ public void FromCredentials_Exception()
{
Assert.ThrowsException<ArgumentException>(() => Urlbox.FromCredentials("", "", ""));
}

[TestMethod]
public async Task TakeScreenshot_Succeeds()
{
UrlboxOptions options = new UrlboxOptions(url: "https://urlbox.com")
{
Height = 125,
Width = 125,
};

var result = await urlbox.TakeScreenshot(options);
Assert.IsNotNull(result.RenderUrl);
Assert.IsNotNull(result.RenderId);
Assert.IsNotNull(result.Size);
}


[TestMethod]
public async Task TakeScreenshot_SucceedsWithLargerTimeout()
{
UrlboxOptions options = new UrlboxOptions(url: "https://urlbox.com")
{
Height = 125,
Width = 125,
};

var result = await urlbox.TakeScreenshot(options, 120000);
Assert.IsNotNull(result.RenderUrl);
Assert.IsNotNull(result.RenderId);
Assert.IsNotNull(result.Size);
}

[TestMethod]
public async Task TakeScreenshot_TimeoutTooLarge()
{
UrlboxOptions options = new UrlboxOptions(url: "https://urlbox.com")
{
Height = 125,
Width = 125,
};

var result = await Assert.ThrowsExceptionAsync<TimeoutException>(() => urlbox.TakeScreenshot(options, 1200001));
Assert.AreEqual("Invalid Timeout Length. Must be between 5000 (5 seconds) and 120000 (2 minutes).", result.Message);
}


[TestMethod]
public async Task TakeScreenshot_TimeoutTooSmall()
{
UrlboxOptions options = new UrlboxOptions(url: "https://urlbox.com")
{
Height = 125,
Width = 125,
};

var result = await Assert.ThrowsExceptionAsync<TimeoutException>(() => urlbox.TakeScreenshot(options, 4999));
Assert.AreEqual("Invalid Timeout Length. Must be between 5000 (5 seconds) and 120000 (2 minutes).", result.Message);
}
}

[TestClass]
Expand Down Expand Up @@ -485,7 +539,7 @@ public void verifyWebhookSignature_Succeeds()
{
string urlboxSignature = "t=123456,sha256=41f85178517e8e031be5771ee4951bc3f6fbd871f41b4866546803576b1c3843";
var content = "{\"event\":\"render.succeeded\",\"renderId\":\"e9617143-2a95-4962-9cc9-d72f3c413b9c\",\"result\":{\"renderUrl\":\"https://renders.urlbox.com/ub-temp-renders/renders/571f54138cd8b877077d3788/2024/1/11/e9617143-2a95-4962-9cc9-d72f3c413b9c.png\",\"size\":359081},\"meta\":{\"startTime\": \"2024-01-11T23:32:11.908Z\",\"endTime\":\"2024-01-11T23:33:32.500Z\"}}";
var result = urlbox.verifyWebhookSignature(urlboxSignature, content);
var result = urlbox.VerifyWebhookSignature(urlboxSignature, content);
Assert.IsTrue(result);
}

Expand All @@ -494,7 +548,7 @@ public void verifyWebhookSignature_FailsNoTimestamp()
{
string urlboxSignature = ",sha256=41f85178517e8e031be5771ee4951bc3f6fbd871f41b4866546803576b1c3843";
var content = "{\"event\":\"render.succeeded\",\"renderId\":\"e9617143-2a95-4962-9cc9-d72f3c413b9c\",\"result\":{\"renderUrl\":\"https://renders.urlbox.com/ub-temp-renders/renders/571f54138cd8b877077d3788/2024/1/11/e9617143-2a95-4962-9cc9-d72f3c413b9c.png\",\"size\":359081},\"meta\":{\"startTime\": \"2024-01-11T23:32:11.908Z\",\"endTime\":\"2024-01-11T23:33:32.500Z\"}}";
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.verifyWebhookSignature(urlboxSignature, content));
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature(urlboxSignature, content));
Assert.AreEqual(result.Message, "Unable to verify signature as header is empty or malformed. Please ensure you pass the `x-urlbox-signature` from the header of the webhook response.");
}

Expand All @@ -503,7 +557,7 @@ public void verifyWebhookSignature_FailsNoSha()
{
string urlboxSignature = "t=123456,";
var content = "{\"event\":\"render.succeeded\",\"renderId\":\"e9617143-2a95-4962-9cc9-d72f3c413b9c\",\"result\":{\"renderUrl\":\"https://renders.urlbox.com/ub-temp-renders/renders/571f54138cd8b877077d3788/2024/1/11/e9617143-2a95-4962-9cc9-d72f3c413b9c.png\",\"size\":359081},\"meta\":{\"startTime\": \"2024-01-11T23:32:11.908Z\",\"endTime\":\"2024-01-11T23:33:32.500Z\"}}";
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.verifyWebhookSignature(urlboxSignature, content));
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature(urlboxSignature, content));
Assert.AreEqual(result.Message, "Unable to verify signature as header is empty or malformed. Please ensure you pass the `x-urlbox-signature` from the header of the webhook response.");
}

Expand All @@ -512,7 +566,7 @@ public void verifyWebhookSignature_FailsShaEmpty()
{
string urlboxSignature = "t=123456,sha256=";
var content = "{\"event\":\"render.succeeded\",\"renderId\":\"e9617143-2a95-4962-9cc9-d72f3c413b9c\",\"result\":{\"renderUrl\":\"https://renders.urlbox.com/ub-temp-renders/renders/571f54138cd8b877077d3788/2024/1/11/e9617143-2a95-4962-9cc9-d72f3c413b9c.png\",\"size\":359081},\"meta\":{\"startTime\": \"2024-01-11T23:32:11.908Z\",\"endTime\":\"2024-01-11T23:33:32.500Z\"}}";
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.verifyWebhookSignature(urlboxSignature, content));
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature(urlboxSignature, content));
Assert.AreEqual("The signature could not be found, please ensure you are passing the x-urlbox-signature header.", result.Message);
}

Expand All @@ -521,7 +575,7 @@ public void verifyWebhookSignature_FailsTimestampEmpty()
{
string urlboxSignature = "t=,sha256=41f85178517e8e031be5771ee4951bc3f6fbd871f41b4866546803576b1c3843";
var content = "{\"event\":\"render.succeeded\",\"renderId\":\"e9617143-2a95-4962-9cc9-d72f3c413b9c\",\"result\":{\"renderUrl\":\"https://renders.urlbox.com/ub-temp-renders/renders/571f54138cd8b877077d3788/2024/1/11/e9617143-2a95-4962-9cc9-d72f3c413b9c.png\",\"size\":359081},\"meta\":{\"startTime\": \"2024-01-11T23:32:11.908Z\",\"endTime\":\"2024-01-11T23:33:32.500Z\"}}";
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.verifyWebhookSignature(urlboxSignature, content));
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature(urlboxSignature, content));
Assert.AreEqual("The timestamp could not be found, please ensure you are passing the x-urlbox-signature header.", result.Message);
}

Expand All @@ -530,7 +584,7 @@ public void verifyWebhookSignature_FailsNoComma()
{
string urlboxSignature = "t=12345sha256=41f85178517e8e031be5771ee4951bc3f6fbd871f41b4866546803576b1c3843";
var content = "{\"event\":\"render.succeeded\",\"renderId\":\"e9617143-2a95-4962-9cc9-d72f3c413b9c\",\"result\":{\"renderUrl\":\"https://renders.urlbox.com/ub-temp-renders/renders/571f54138cd8b877077d3788/2024/1/11/e9617143-2a95-4962-9cc9-d72f3c413b9c.png\",\"size\":359081},\"meta\":{\"startTime\": \"2024-01-11T23:32:11.908Z\",\"endTime\":\"2024-01-11T23:33:32.500Z\"}}";
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.verifyWebhookSignature(urlboxSignature, content));
var result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature(urlboxSignature, content));
Assert.AreEqual("Unable to verify signature as header is empty or malformed. Please ensure you pass the `x-urlbox-signature` from the header of the webhook response.", result.Message);
}
}
28 changes: 28 additions & 0 deletions Urlbox/Urlbox/Resource/IUrlbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;

namespace Screenshots;

public interface IUrlbox
{
// Screenshot and File Generation Methods
Task<AsyncUrlboxResponse> TakeScreenshot(UrlboxOptions options);
Task<AsyncUrlboxResponse> TakeScreenshot(UrlboxOptions options, int timeout);
Task<SyncUrlboxResponse> Render(UrlboxOptions options);
Task<AsyncUrlboxResponse> RenderAsync(UrlboxOptions options);

// Download and File Handling Methods
Task<string> DownloadAsBase64(UrlboxOptions options, string format = "png");
Task<string> DownloadAsBase64(string urlboxUrl);
Task<string> DownloadToFile(string urlboxUrl, string filename);
Task<string> DownloadToFile(UrlboxOptions options, string filename, string format = "png");

// URL Generation Methods
string GeneratePNGUrl(UrlboxOptions options);
string GenerateJPEGUrl(UrlboxOptions options);
string GeneratePDFUrl(UrlboxOptions options);
string GenerateUrlboxUrl(UrlboxOptions options, string format = "png");

// Status and Validation Methods
Task<AsyncUrlboxResponse> GetStatus(string statusUrl);
bool VerifyWebhookSignature(string header, string content);
}
Loading

0 comments on commit b791199

Please sign in to comment.