From e919ec75f4547dcaa58ce6aefdc4a7d482c79ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Keresztury?= Date: Thu, 18 Jun 2020 12:28:11 +0200 Subject: [PATCH] Add tests --- .../Mailgun.Models.SignedEvent.Tests.csproj | 34 +++++++ .../MailgunSignatureTests.cs | 91 +++++++++++++++++++ .../stylecop.json | 16 ++++ Mailgun.Models.SignedEvent.sln | 6 ++ 4 files changed, 147 insertions(+) create mode 100644 Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj create mode 100644 Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs create mode 100644 Mailgun.Models.SignedEvent.Tests/stylecop.json diff --git a/Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj b/Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj new file mode 100644 index 0000000..931cdd5 --- /dev/null +++ b/Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj @@ -0,0 +1,34 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs b/Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs new file mode 100644 index 0000000..7abe5a1 --- /dev/null +++ b/Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs @@ -0,0 +1,91 @@ +// +// Copyright (c) Balazs Keresztury. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; +using NUnit.Framework; + +namespace Mailgun.Models.SignedEvent.Tests +{ + /// + /// Tests MailgunSignature. + /// + public class MailgunSignatureTests + { + private DateTime _originalTimestamp; + private TimeSpan _timeSinceOriginalTimestamp; + private MailgunSignature _validSignature; + private string _apiKey; + + /// + /// Sets up test fixture with a know valid signature. + /// + [SetUp] + public void Setup() + { + this._originalTimestamp = new DateTime(2020, 6, 18, 11, 55, 0).ToUniversalTime(); + var originalTimestampAsUnixEpoch = (this._originalTimestamp - DateTime.UnixEpoch).TotalSeconds.ToString(); + this._timeSinceOriginalTimestamp = DateTime.UtcNow - this._originalTimestamp; + this._apiKey = "ffffffffffffffffffffffffffffffff-ffffffff-ffffffff"; + + this._validSignature = new MailgunSignature() + { + Signature = "de4b938580bb4d84f710cbb8bfa7d224bb2262c8f644f558c2901c1ae645bb03", + Token = "ffffffffffffffffffffffffffffffffffffffffffffffffff", + Timestamp = originalTimestampAsUnixEpoch, + }; + } + + /// + /// Checks if a valid signature returns as valid. + /// + [Test] + public void Valid() + { + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.True); + } + + /// + /// Checks if a slightly old signature returns invalid. + /// + [Test] + public void TooOld() + { + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp - new TimeSpan(0, 1, 0)), Is.False); + } + + /// + /// Checks if changing the signature invalidates it. + /// + [Test] + public void BadSignature() + { + this._validSignature.Signature += "x"; + + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False); + } + + /// + /// Checks if changing the token invalidates the signature. + /// + [Test] + public void BadToken() + { + this._validSignature.Token += "x"; + + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False); + } + + /// + /// Checks if changing the API key invalidates the signature. + /// + [Test] + public void BadApiKey() + { + this._apiKey += "x"; + + Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False); + } + } +} \ No newline at end of file diff --git a/Mailgun.Models.SignedEvent.Tests/stylecop.json b/Mailgun.Models.SignedEvent.Tests/stylecop.json new file mode 100644 index 0000000..3f0420f --- /dev/null +++ b/Mailgun.Models.SignedEvent.Tests/stylecop.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": { + "documentationRules": { + "companyName": "Balazs Keresztury", + "copyrightText": "Copyright (c) {companyName}. All rights reserved.\nLicensed under the {licenseName} license. See {licenseFile} file in the project root for full license information.", + "variables": { + "licenseName": "MIT", + "licenseFile": "LICENSE" + } + }, + "orderingRules": { + "usingDirectivesPlacement": "outsideNamespace" + } + } +} diff --git a/Mailgun.Models.SignedEvent.sln b/Mailgun.Models.SignedEvent.sln index 7fbd06c..53a85fb 100644 --- a/Mailgun.Models.SignedEvent.sln +++ b/Mailgun.Models.SignedEvent.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30204.135 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailgun.Models.SignedEvent", "Mailgun.Models.SignedEvent\Mailgun.Models.SignedEvent.csproj", "{1BEB07E0-C800-45A1-990E-5F64962B8C9E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mailgun.Models.SignedEvent.Tests", "Mailgun.Models.SignedEvent.Tests\Mailgun.Models.SignedEvent.Tests.csproj", "{8E4EF695-2A4F-4FF9-913D-541F4AF7F841}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Debug|Any CPU.Build.0 = Debug|Any CPU {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Release|Any CPU.ActiveCfg = Release|Any CPU {1BEB07E0-C800-45A1-990E-5F64962B8C9E}.Release|Any CPU.Build.0 = Release|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E4EF695-2A4F-4FF9-913D-541F4AF7F841}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE