Skip to content

Commit f1b6c5a

Browse files
committed
Use async methods
RoleStore:async methods+code cleaning+unit tests UserStore: async+code cleaning+unit tests
1 parent 95fd614 commit f1b6c5a

File tree

8 files changed

+334
-193
lines changed

8 files changed

+334
-193
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
using NHibernate.AspNet.Identity;
1+
using NHibernate.AspNet.Identity;
22

33
namespace NHibernate.AspNet.Identity.Tests.Models
44
{
55
public class ApplicationUser : IdentityUser
66
{
77
}
8+
9+
public class ApplicationRole : IdentityRole
10+
{
11+
}
812
}

source/NHibernate.AspNet.Identity.Tests/NHibernate.AspNet.Identity.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<ItemGroup>
105105
<Compile Include="Models\Foo.cs" />
106106
<Compile Include="Models\IdentityModels.cs" />
107+
<Compile Include="RoleStoreTest.cs" />
107108
<Compile Include="UserStoreTest.cs" />
108109
<Compile Include="MapTest.cs" />
109110
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using Microsoft.AspNet.Identity;
2+
using NHibernate.AspNet.Identity.Tests.Models;
3+
using NUnit.Framework;
4+
using System;
5+
using System.Linq;
6+
using System.Security.Claims;
7+
using System.Transactions;
8+
using TestClass = NUnit.Framework.TestFixtureAttribute;
9+
using TestCleanup = NUnit.Framework.TearDownAttribute;
10+
using TestInitialize = NUnit.Framework.SetUpAttribute;
11+
using TestMethod = NUnit.Framework.TestAttribute;
12+
13+
namespace NHibernate.AspNet.Identity.Tests
14+
{
15+
[TestClass]
16+
public class RoleStoreTest
17+
{
18+
ISession _session;
19+
20+
[TestInitialize]
21+
public void Initialize()
22+
{
23+
var factory = SessionFactoryProvider.Instance.SessionFactory;
24+
_session = factory.OpenSession();
25+
SessionFactoryProvider.Instance.BuildSchema();
26+
}
27+
28+
[TestCleanup]
29+
public void Cleanup()
30+
{
31+
_session.Close();
32+
_session.Dispose();
33+
}
34+
35+
[TestMethod]
36+
public void WhenCreateRole()
37+
{
38+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
39+
var role = new ApplicationRole() { Name = "RealRoleName" };
40+
41+
using (var transaction = new TransactionScope())
42+
{
43+
var result = roleManager.CreateAsync(role).GetAwaiter().GetResult();
44+
transaction.Complete();
45+
Assert.AreEqual(0, result.Errors.Count());
46+
}
47+
48+
var actual = _session.Query<ApplicationRole>().FirstOrDefault(x => x.Name == role.Name);
49+
50+
Assert.IsNotNull(actual);
51+
Assert.AreEqual(role.Name, actual.Name);
52+
}
53+
54+
[TestMethod]
55+
public void WhenDeleteRole()
56+
{
57+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
58+
var role = new ApplicationRole() { Name = "RealRoleName" };
59+
60+
using (var transaction = new TransactionScope())
61+
{
62+
var result = roleManager.CreateAsync(role).GetAwaiter().GetResult();
63+
transaction.Complete();
64+
Assert.AreEqual(0, result.Errors.Count());
65+
}
66+
67+
using (var transaction = new TransactionScope())
68+
{
69+
var result = roleManager.DeleteAsync(role).GetAwaiter().GetResult();
70+
transaction.Complete();
71+
Assert.AreEqual(0, result.Errors.Count());
72+
}
73+
74+
var actual = _session.Query<ApplicationRole>().FirstOrDefault(x => x.Name == role.Name);
75+
76+
Assert.IsNull(actual);
77+
}
78+
79+
[TestMethod]
80+
public void FindByName()
81+
{
82+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
83+
roleManager.Create(new ApplicationRole() { Name = "test" });
84+
var x = roleManager.FindByName("test");
85+
Assert.IsNotNull(x);
86+
}
87+
88+
[TestMethod]
89+
public void FindByNameIsCaseInsensitive()
90+
{
91+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
92+
roleManager.Create(new ApplicationRole() { Name = "test" });
93+
var x = roleManager.FindByName("TeSt");
94+
Assert.IsNotNull(x);
95+
}
96+
97+
[TestMethod]
98+
public void FindByNameNotExisting()
99+
{
100+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
101+
var x = roleManager.FindByName("xxx");
102+
Assert.IsNull(x);
103+
}
104+
105+
[TestMethod]
106+
public void FindById()
107+
{
108+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
109+
var role = new ApplicationRole() { Name = "test" };
110+
roleManager.Create(role);
111+
var x = roleManager.FindById(role.Id);
112+
Assert.IsNotNull(x);
113+
}
114+
115+
[TestMethod]
116+
public void FindByIdNotExisting()
117+
{
118+
var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_session));
119+
var x = roleManager.FindById(new Guid().ToString());
120+
Assert.IsNull(x);
121+
}
122+
}
123+
}

source/NHibernate.AspNet.Identity.Tests/SessionFactoryProvider.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Linq;
44
using NHibernate.AspNet.Identity.DomainModel;
@@ -12,7 +12,7 @@ namespace NHibernate.AspNet.Identity.Tests
1212
public sealed class SessionFactoryProvider
1313
{
1414
private static volatile SessionFactoryProvider _instance;
15-
private static object _syncRoot = new Object();
15+
private static object _syncRoot = new object();
1616

1717
private Configuration _configuration;
1818

@@ -26,19 +26,20 @@ private SessionFactoryProvider()
2626
{
2727
Name = "NHibernate.AspNet.Identity";
2828

29-
var baseEntityToIgnore = new[] {
30-
typeof(SharpArch.Domain.DomainModel.Entity),
31-
typeof(EntityWithTypedId<int>),
32-
typeof(EntityWithTypedId<string>),
29+
var baseEntityToIgnore = new[] {
30+
typeof(SharpArch.Domain.DomainModel.Entity),
31+
typeof(EntityWithTypedId<int>),
32+
typeof(EntityWithTypedId<string>),
3333
};
3434

35-
var allEntities = new[] {
36-
typeof(IdentityUser),
37-
typeof(ApplicationUser),
38-
typeof(IdentityRole),
39-
typeof(IdentityUserLogin),
40-
typeof(IdentityUserClaim),
41-
typeof(Foo),
35+
var allEntities = new[] {
36+
typeof(IdentityUser),
37+
typeof(ApplicationUser),
38+
typeof(ApplicationRole),
39+
typeof(IdentityRole),
40+
typeof(IdentityUserLogin),
41+
typeof(IdentityUserClaim),
42+
typeof(Foo),
4243
};
4344

4445
var mapper = new ConventionModelMapper();

0 commit comments

Comments
 (0)