-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from johnsheehan/Tests
now with more tests
- Loading branch information
Showing
6 changed files
with
283 additions
and
2 deletions.
There are no files selected for viewing
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,96 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Web.Script.Serialization; | ||
using FluentAssertions; | ||
|
||
namespace JWT.Tests | ||
{ | ||
[TestClass] | ||
|
||
public class DecodeTests | ||
{ | ||
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); | ||
string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.cr0xw8c_HKzhFBMQrseSPGoJ0NPlRp_3BKzP96jwBdY"; | ||
Customer customer = new Customer() { FirstName = "Bob", Age = 37 }; | ||
|
||
[TestMethod] | ||
public void Should_Decode_Token_To_Json_Encoded_String() | ||
{ | ||
var expected_payload = jsonSerializer.Serialize(customer); | ||
|
||
string decoded_payload = JWT.JsonWebToken.Decode(token, "ABC", false); | ||
|
||
Assert.AreEqual(expected_payload, decoded_payload); | ||
} | ||
|
||
[TestMethod] | ||
public void Should_Decode_Token_To_Dictionary() | ||
{ | ||
Dictionary<string, object> expected_payload = new Dictionary<string, object>() { | ||
{ "FirstName", "Bob" }, | ||
{ "Age", 37 } | ||
}; | ||
|
||
object decoded_payload = JWT.JsonWebToken.DecodeToObject(token, "ABC", false); | ||
|
||
decoded_payload.ShouldBeEquivalentTo(expected_payload, options=>options.IncludingAllRuntimeProperties()); | ||
} | ||
|
||
[TestMethod] | ||
public void Should_Decode_Token_To_Generic_Type() | ||
{ | ||
Customer expected_payload = customer; | ||
|
||
Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(token, "ABC", false); | ||
|
||
decoded_payload.ShouldBeEquivalentTo(expected_payload); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ArgumentException))] | ||
public void Should_Throw_On_Malformed_Token() { | ||
string malformedtoken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.cr0xw8c_HKzhFBMQrseSPGoJ0NPlRp_3BKzP96jwBdY"; | ||
|
||
Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(malformedtoken, "ABC", false); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(SignatureVerificationException))] | ||
public void Should_Throw_On_Invalid_Key() | ||
{ | ||
string invalidkey = "XYZ"; | ||
|
||
Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(token, invalidkey, true); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(SignatureVerificationException))] | ||
public void Should_Throw_On_Invalid_Expiration_Claim() | ||
{ | ||
var invalidexptoken = JWT.JsonWebToken.Encode(new { exp = "asdsad" }, "ABC", JwtHashAlgorithm.HS256); | ||
|
||
Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(invalidexptoken, "ABC", true); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(SignatureVerificationException))] | ||
public void Should_Throw_On_Expired_Token() | ||
{ | ||
var anHourAgoUtc = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0)); | ||
Int32 unixTimestamp = (Int32)(anHourAgoUtc.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; | ||
|
||
var invalidexptoken = JWT.JsonWebToken.Encode(new { exp=unixTimestamp }, "ABC", JwtHashAlgorithm.HS256); | ||
|
||
Customer decoded_payload = JWT.JsonWebToken.DecodeToObject<Customer>(invalidexptoken, "ABC", true); | ||
} | ||
} | ||
|
||
public class Customer { | ||
public string FirstName {get;set;} | ||
public int Age {get;set;} | ||
} | ||
} |
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,33 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Web.Script.Serialization; | ||
using System.Collections.Generic; | ||
|
||
namespace JWT.Tests | ||
{ | ||
[TestClass] | ||
public class EncodeTests | ||
{ | ||
string token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.cr0xw8c_HKzhFBMQrseSPGoJ0NPlRp_3BKzP96jwBdY"; | ||
string extraheaderstoken = "eyJmb28iOiJiYXIiLCJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJGaXJzdE5hbWUiOiJCb2IiLCJBZ2UiOjM3fQ.slrbXF9VSrlX7LKsV-Umb_zEzWLxQjCfUOjNTbvyr1g"; | ||
Customer customer = new Customer() { FirstName = "Bob", Age = 37 }; | ||
|
||
[TestMethod] | ||
public void Should_Encode_Type() | ||
{ | ||
string result = JWT.JsonWebToken.Encode(customer, "ABC", JwtHashAlgorithm.HS256); | ||
|
||
Assert.AreEqual(token, result); | ||
} | ||
|
||
[TestMethod] | ||
public void Should_Encode_Type_With_Extra_Headers() | ||
{ | ||
var extraheaders = new Dictionary<string, object>() { {"foo", "bar"} }; | ||
|
||
string result = JWT.JsonWebToken.Encode(extraheaders, customer, "ABC", JwtHashAlgorithm.HS256); | ||
|
||
Assert.AreEqual(extraheaderstoken, result); | ||
} | ||
} | ||
} |
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,104 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{BF568781-D576-4545-A552-4DC839B1AF14}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>JWT.Tests</RootNamespace> | ||
<AssemblyName>JWT.Tests</AssemblyName> | ||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath> | ||
<IsCodedUITest>False</IsCodedUITest> | ||
<TestProjectType>UnitTest</TestProjectType> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="FluentAssertions, Version=3.3.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\FluentAssertions.3.3.0\lib\net45\FluentAssertions.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="FluentAssertions.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\FluentAssertions.3.3.0\lib\net45\FluentAssertions.Core.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Web.Extensions" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
</ItemGroup> | ||
<Choose> | ||
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'"> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> | ||
</ItemGroup> | ||
</When> | ||
<Otherwise> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" /> | ||
</ItemGroup> | ||
</Otherwise> | ||
</Choose> | ||
<ItemGroup> | ||
<Compile Include="DecodeTests.cs" /> | ||
<Compile Include="EncodeTests.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\JWT\JWT.csproj"> | ||
<Project>{a80b51b8-ddf6-4026-98a4-b59653e50b38}</Project> | ||
<Name>JWT</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<Choose> | ||
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<Private>False</Private> | ||
</Reference> | ||
</ItemGroup> | ||
</When> | ||
</Choose> | ||
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</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,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("JWT.Tests")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("JWT.Tests")] | ||
[assembly: AssemblyCopyright("Copyright © 2015")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("5dd5d960-dc7d-4dc4-97cb-1024d9184c85")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="FluentAssertions" version="3.3.0" targetFramework="net451" /> | ||
</packages> |
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