From d941f036a8fcf38ef019679fa69e132552c01319 Mon Sep 17 00:00:00 2001 From: Devin Rader Date: Wed, 13 May 2015 16:43:00 -0400 Subject: [PATCH] now with more tests --- JWT.Tests/DecodeTests.cs | 96 +++++++++++++++++++++++++ JWT.Tests/EncodeTests.cs | 33 +++++++++ JWT.Tests/JWT.Tests.csproj | 104 +++++++++++++++++++++++++++ JWT.Tests/Properties/AssemblyInfo.cs | 36 ++++++++++ JWT.Tests/packages.config | 4 ++ JWT.sln | 12 +++- 6 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 JWT.Tests/DecodeTests.cs create mode 100644 JWT.Tests/EncodeTests.cs create mode 100644 JWT.Tests/JWT.Tests.csproj create mode 100644 JWT.Tests/Properties/AssemblyInfo.cs create mode 100644 JWT.Tests/packages.config diff --git a/JWT.Tests/DecodeTests.cs b/JWT.Tests/DecodeTests.cs new file mode 100644 index 000000000..72e81e16e --- /dev/null +++ b/JWT.Tests/DecodeTests.cs @@ -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 expected_payload = new Dictionary() { + { "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(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(malformedtoken, "ABC", false); + } + + [TestMethod] + [ExpectedException(typeof(SignatureVerificationException))] + public void Should_Throw_On_Invalid_Key() + { + string invalidkey = "XYZ"; + + Customer decoded_payload = JWT.JsonWebToken.DecodeToObject(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(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(invalidexptoken, "ABC", true); + } + } + + public class Customer { + public string FirstName {get;set;} + public int Age {get;set;} + } +} diff --git a/JWT.Tests/EncodeTests.cs b/JWT.Tests/EncodeTests.cs new file mode 100644 index 000000000..312d1e35b --- /dev/null +++ b/JWT.Tests/EncodeTests.cs @@ -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() { {"foo", "bar"} }; + + string result = JWT.JsonWebToken.Encode(extraheaders, customer, "ABC", JwtHashAlgorithm.HS256); + + Assert.AreEqual(extraheaderstoken, result); + } + } +} diff --git a/JWT.Tests/JWT.Tests.csproj b/JWT.Tests/JWT.Tests.csproj new file mode 100644 index 000000000..e084cc71d --- /dev/null +++ b/JWT.Tests/JWT.Tests.csproj @@ -0,0 +1,104 @@ + + + + Debug + AnyCPU + {BF568781-D576-4545-A552-4DC839B1AF14} + Library + Properties + JWT.Tests + JWT.Tests + v4.5.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\FluentAssertions.3.3.0\lib\net45\FluentAssertions.dll + True + + + ..\packages\FluentAssertions.3.3.0\lib\net45\FluentAssertions.Core.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + + {a80b51b8-ddf6-4026-98a4-b59653e50b38} + JWT + + + + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/JWT.Tests/Properties/AssemblyInfo.cs b/JWT.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..b6e19a85c --- /dev/null +++ b/JWT.Tests/Properties/AssemblyInfo.cs @@ -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")] diff --git a/JWT.Tests/packages.config b/JWT.Tests/packages.config new file mode 100644 index 000000000..5b22f3d05 --- /dev/null +++ b/JWT.Tests/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/JWT.sln b/JWT.sln index 54ce7aec5..946e40e05 100644 --- a/JWT.sln +++ b/JWT.sln @@ -1,8 +1,12 @@  -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT", "JWT\JWT.csproj", "{A80B51B8-DDF6-4026-98A4-B59653E50B38}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT.Tests", "JWT.Tests\JWT.Tests.csproj", "{BF568781-D576-4545-A552-4DC839B1AF14}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -13,6 +17,10 @@ Global {A80B51B8-DDF6-4026-98A4-B59653E50B38}.Debug|Any CPU.Build.0 = Debug|Any CPU {A80B51B8-DDF6-4026-98A4-B59653E50B38}.Release|Any CPU.ActiveCfg = Release|Any CPU {A80B51B8-DDF6-4026-98A4-B59653E50B38}.Release|Any CPU.Build.0 = Release|Any CPU + {BF568781-D576-4545-A552-4DC839B1AF14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF568781-D576-4545-A552-4DC839B1AF14}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF568781-D576-4545-A552-4DC839B1AF14}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF568781-D576-4545-A552-4DC839B1AF14}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE