Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
belidzs committed Jun 18, 2020
1 parent cd0906f commit e919ec7
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="stylecop.json" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Mailgun.Models.SignedEvent\Mailgun.Models.SignedEvent.csproj" />
</ItemGroup>

</Project>
91 changes: 91 additions & 0 deletions Mailgun.Models.SignedEvent.Tests/MailgunSignatureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// <copyright file="MailgunSignatureTests.cs" company="Balazs Keresztury">
// Copyright (c) Balazs Keresztury. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

using System;
using NUnit.Framework;

namespace Mailgun.Models.SignedEvent.Tests
{
/// <summary>
/// Tests MailgunSignature.
/// </summary>
public class MailgunSignatureTests
{
private DateTime _originalTimestamp;
private TimeSpan _timeSinceOriginalTimestamp;
private MailgunSignature _validSignature;
private string _apiKey;

/// <summary>
/// Sets up test fixture with a know valid signature.
/// </summary>
[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,
};
}

/// <summary>
/// Checks if a valid signature returns as valid.
/// </summary>
[Test]
public void Valid()
{
Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.True);
}

/// <summary>
/// Checks if a slightly old signature returns invalid.
/// </summary>
[Test]
public void TooOld()
{
Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp - new TimeSpan(0, 1, 0)), Is.False);
}

/// <summary>
/// Checks if changing the signature invalidates it.
/// </summary>
[Test]
public void BadSignature()
{
this._validSignature.Signature += "x";

Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False);
}

/// <summary>
/// Checks if changing the token invalidates the signature.
/// </summary>
[Test]
public void BadToken()
{
this._validSignature.Token += "x";

Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False);
}

/// <summary>
/// Checks if changing the API key invalidates the signature.
/// </summary>
[Test]
public void BadApiKey()
{
this._apiKey += "x";

Assert.That(this._validSignature.IsValid(this._apiKey, this._timeSinceOriginalTimestamp + new TimeSpan(0, 1, 0)), Is.False);
}
}
}
16 changes: 16 additions & 0 deletions Mailgun.Models.SignedEvent.Tests/stylecop.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
6 changes: 6 additions & 0 deletions Mailgun.Models.SignedEvent.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e919ec7

Please sign in to comment.