|
| 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 | +} |
0 commit comments