Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #35 from stormpath/its
Browse files Browse the repository at this point in the history
Add integration tests
  • Loading branch information
nbarbettini authored Sep 13, 2016
2 parents ccf6f9a + 8feb05c commit 163ca9b
Show file tree
Hide file tree
Showing 15 changed files with 696 additions and 110 deletions.
9 changes: 8 additions & 1 deletion Stormpath.Owin.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Stormpath.Owin.Middleware", "src\Stormpath.Owin.Middleware\Stormpath.Owin.Middleware.xproj", "{BFBC0BAE-281E-40E9-9BCA-6EFBF6DBBCF6}"
EndProject
Expand All @@ -21,6 +21,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Stormpath.Owin.Views", "src
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Stormpath.Owin.Views.Precompiled", "src\Stormpath.Owin.Views.Precompiled\Stormpath.Owin.Views.Precompiled.xproj", "{763034BD-D324-4E12-B503-6C1F9E560DC1}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Stormpath.Owin.IntegrationTest", "test\Stormpath.Owin.IntegrationTest\Stormpath.Owin.IntegrationTest.xproj", "{2E00EC4B-A29F-4037-97AB-E1C9AF7EB6A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -55,6 +57,10 @@ Global
{763034BD-D324-4E12-B503-6C1F9E560DC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{763034BD-D324-4E12-B503-6C1F9E560DC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{763034BD-D324-4E12-B503-6C1F9E560DC1}.Release|Any CPU.Build.0 = Release|Any CPU
{2E00EC4B-A29F-4037-97AB-E1C9AF7EB6A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E00EC4B-A29F-4037-97AB-E1C9AF7EB6A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E00EC4B-A29F-4037-97AB-E1C9AF7EB6A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E00EC4B-A29F-4037-97AB-E1C9AF7EB6A7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -67,5 +73,6 @@ Global
{3615FE0D-5B58-4A37-AD9C-9FC32DFD84CD} = {213BB7A9-5C1C-4917-BC65-0C20F0627CF2}
{237B0525-BE8F-43EC-92EF-D8E43352368D} = {213BB7A9-5C1C-4917-BC65-0C20F0627CF2}
{763034BD-D324-4E12-B503-6C1F9E560DC1} = {213BB7A9-5C1C-4917-BC65-0C20F0627CF2}
{2E00EC4B-A29F-4037-97AB-E1C9AF7EB6A7} = {B26DCAB1-697E-47B3-9BD2-74B4B7839248}
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Stormpath.Owin.Abstractions/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"xmlDoc": true
},
"dependencies": {
"Stormpath.Configuration.Abstractions": "6.0.0-beta1",
"Stormpath.SDK.Abstractions": "0.94.0-beta1"
"Stormpath.Configuration.Abstractions": "6.0.0",
"Stormpath.SDK.Abstractions": "0.94.0"
},
"description": "Common components for the Stormpath OWIN middleware library.",
"frameworks": {
Expand Down
64 changes: 64 additions & 0 deletions src/Stormpath.Owin.Middleware/DefaultSmartComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;

namespace Stormpath.Owin.Middleware
{
internal sealed class DefaultSmartComparer : IEqualityComparer<object>
{
public new bool Equals(object x, object y)
{
if (x == null || y == null)
{
return false;
}

if (x.Equals(y))
{
return true;
}

if (x is string && y is string)
{
return CompareStringsOrdinal(x, y);
}

if (IsIntegerLike(x) && IsIntegerLike(y))
{
return IntegersEquals(x, y);
}

return false;
}

public int GetHashCode(object obj)
=> obj.GetHashCode();

/// <summary>
/// By default, always compare strings with an Ordinal comparison.
/// </summary>
/// <param name="x">The first string.</param>
/// <param name="y">The second string.</param>
/// <returns><c>true</c> if the strings are equal.</returns>
private static bool CompareStringsOrdinal(object x, object y)
=> ((string)x).Equals((string)y, StringComparison.Ordinal);

private static bool IsIntegerLike(object value)
=> value is byte || value is short || value is int || value is long;

/// <summary>
/// Compares two integer-like types, regardless of the size of the underlying type.
/// </summary>
/// <remarks>
/// This is necessary because the Stormpath SDK deserializes integers as <c>long</c> under the hood.
/// </remarks>
/// <param name="obj1">An integer-like type.</param>
/// <param name="obj2">An integer-like type.</param>
/// <returns><c>true</c> if the values are identical.</returns>
private static bool IntegersEquals(object obj1, object obj2)
{
var long1 = (long)Convert.ChangeType(obj1, typeof(long));
var long2 = (long)Convert.ChangeType(obj2, typeof(long));
return long1 == long2;
}
}
}
33 changes: 14 additions & 19 deletions src/Stormpath.Owin.Middleware/RequireCustomDataFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Stormpath.Owin.Abstractions;
using Stormpath.SDK.Account;
Expand All @@ -10,40 +11,34 @@ public sealed class RequireCustomDataFilter : IAuthorizationFilter
{
private readonly string _key;
private readonly object _value;
private readonly IEqualityComparer<object> _comparer;

public RequireCustomDataFilter(string key, object value)
: this(key, value, new DefaultSmartComparer())
{
}

public RequireCustomDataFilter(string key, object value, IEqualityComparer<object> comparer)
{
_key = key;
_value = value;
_comparer = comparer;
}

public bool IsAuthorized(IAccount account)
{
var customData = account?.GetCustomData();

if (customData?[_key] == null)
{
return false;
}

return customData[_key].Equals(_value);
return _comparer.Equals(customData?[_key], _value);
}

public async Task<bool> IsAuthorizedAsync(IAccount account, CancellationToken cancellationToken)
{
if (account == null)
{
return false;
}

var customData = await account.GetCustomDataAsync(cancellationToken).ConfigureAwait(false);

if (customData?[_key] == null)
{
return false;
}
var customData = account == null
? null
: await account.GetCustomDataAsync(cancellationToken).ConfigureAwait(false);

return customData[_key].Equals(_value);
return _comparer.Equals(customData?[_key], _value);
}
}
}
2 changes: 1 addition & 1 deletion src/Stormpath.Owin.Middleware/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"target": "project",
"version": "1.5.0-beta4"
},
"Stormpath.SDK": "0.94.0-beta1"
"Stormpath.SDK": "0.94.0"
},
"description": "Stormpath OWIN middleware for .NET.",
"frameworks": {
Expand Down
14 changes: 14 additions & 0 deletions test/Stormpath.Owin.IntegrationTest/IntegrationTestCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace Stormpath.Owin.IntegrationTest
{
[CollectionDefinition(nameof(IntegrationTestCollection))]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>
{
// Intentionally left blank. This class only serves as an anchor for CollectionDefinitionAttribute.
}
}
46 changes: 46 additions & 0 deletions test/Stormpath.Owin.IntegrationTest/IntegrationTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using Stormpath.SDK.Application;
using Stormpath.SDK.Client;
using Stormpath.SDK.Directory;
using Stormpath.SDK.Http;
using Stormpath.SDK.Resource;
using Stormpath.SDK.Serialization;

namespace Stormpath.Owin.IntegrationTest
{
public class IntegrationTestFixture : IDisposable
{
private readonly TestEnvironment _environment;

public IntegrationTestFixture()
{
Client = Clients.Builder()
.SetHttpClient(HttpClients.Create().SystemNetHttpClient())
.SetSerializer(Serializers.Create().JsonNetSerializer())
.Build();

TestInstanceKey = Guid.NewGuid().ToString();

_environment = new TestEnvironment(Client, async client =>
{
TestApplication = await client.CreateApplicationAsync($"Stormpath.Owin IT {TestInstanceKey}", true);
TestDirectory = await TestApplication.GetDefaultAccountStoreAsync() as IDirectory;
return new IResource[] {TestApplication, TestDirectory};
});
}

public IClient Client { get; }

public string TestInstanceKey { get; }

public IApplication TestApplication { get; private set; }

public IDirectory TestDirectory { get; private set; }

public void Dispose()
{
_environment.Dispose();
}
}
}
19 changes: 19 additions & 0 deletions test/Stormpath.Owin.IntegrationTest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Stormpath.Owin.IntegrationTest")]
[assembly: AssemblyTrademark("")]

// 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("2e00ec4b-a29f-4037-97ab-e1c9af7eb6a7")]
Loading

0 comments on commit 163ca9b

Please sign in to comment.