-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Mailgun.Models.SignedEvent.Tests/Mailgun.Models.SignedEvent.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters