-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathUnitTestExample.cs
More file actions
84 lines (76 loc) · 3.73 KB
/
UnitTestExample.cs
File metadata and controls
84 lines (76 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2023 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using AuthPermissions;
using AuthPermissions.AdminCode;
using AuthPermissions.AspNetCore;
using AuthPermissions.BaseCode.DataLayer.Classes.SupportTypes;
using AuthPermissions.BaseCode.DataLayer.EfCode;
using AuthPermissions.BaseCode.SetupCode;
using Example4.MvcWebApp.IndividualAccounts.PermissionsCode;
using Microsoft.Extensions.DependencyInjection;
using Test.StubClasses;
using Xunit;
using Xunit.Extensions.AssertExtensions;
namespace Test.UnitTests.TestExamples
{
public class UnitTestExample
{
[Fact]
public async Task ExampleUseOfSetupForUnitTestingAsyncForUnitTesting()
{
//SETUP
var services = new ServiceCollection();
services.AddLogging();
var serviceProvider = await services.RegisterAuthPermissions<Example4Permissions>(options =>
{
options.TenantType = TenantTypes.HierarchicalTenant;
})
.UsingInMemoryDatabase()
.AddRolesPermissionsIfEmpty(Example4AppAuthSetupData.RolesDefinition)
.AddTenantsIfEmpty(Example4AppAuthSetupData.TenantDefinition)
.AddAuthUsersIfEmpty(Example4AppAuthSetupData.UsersRolesDefinition)
.RegisterFindUserInfoService<StubIFindUserInfoFactory.StubIFindUserInfo>()
.SetupForUnitTestingAsync();
var adminUserService = serviceProvider.GetRequiredService<IAuthUsersAdminService>();
var userId = "admin@4uInc.com";
//ATTEMPT
var status = await adminUserService.FindAuthUserByUserIdAsync(userId);
//VERIFY
status.IsValid.ShouldBeTrue(status.GetAllErrors());
var rereadUser = status.Result;
rereadUser.Email.ShouldEqual(userId.ToLower());
rereadUser.UserName.ShouldEqual(userId);
rereadUser.UserRoles.OrderBy(x => x.RoleName)
.Select(x => x.RoleName).ShouldEqual(new List<string> { "Area Manager", "Tenant Admin" });
rereadUser.UserTenant.TenantFullName.ShouldEqual("4U Inc.");
}
[Fact]
public async Task Example4RolesMarkedAsHidden()
{
//SETUP
var services = new ServiceCollection();
var serviceProvider = await services.RegisterAuthPermissions<Example4Permissions>(options =>
{
options.TenantType = TenantTypes.HierarchicalTenant;
})
.UsingInMemoryDatabase()
.AddRolesPermissionsIfEmpty(Example4AppAuthSetupData.RolesDefinition)
.AddTenantsIfEmpty(Example4AppAuthSetupData.TenantDefinition)
.AddAuthUsersIfEmpty(Example4AppAuthSetupData.UsersRolesDefinition)
.RegisterFindUserInfoService<StubIFindUserInfoFactory.StubIFindUserInfo>()
.SetupForUnitTestingAsync();
//ATTEMPT
using var context = serviceProvider.GetRequiredService<AuthPermissionsDbContext>();
var roles = context.RoleToPermissions.ToList();
//VERIFY
roles.Count.ShouldEqual(7);
roles.Where(x => x.RoleType == RoleTypes.HiddenFromTenant)
.OrderBy(x => x.RoleName)
.Select(x => x.RoleName).ShouldEqual(new[] { "App Admin", "SuperAdmin" });
roles.Where(x => x.RoleType == RoleTypes.Normal)
.OrderBy(x => x.RoleName)
.Select(x => x.RoleName)
.ShouldEqual(new[] { "Area Manager", "Sales Assistant", "Store Manager", "Tenant Admin", "Tenant Director" });
}
}
}