Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Call Services as a Super User to pass RequiredRole restrictions
using (var appHost = new BasicAppHost
{
ConfigureAppHost = host =>
{
host.Config.AdminAuthSecret = "allowAdmin";
},
...
}.Init())
{
// Arrange
UserAuth userAuth;
AssignRolesResponse assignRolesResponse;
var newRegistration = CreateNewUserRegistration();
var request = new BasicRequest(newRegistration);
request.QueryString["authSecret"] = "allowAdmin"; // Treat request from Admin Super User
}
//Rewrite Service to call `IAuthRepository` directly instead of Service
using (var appHost = new BasicAppHost
{
ConfigureContainer = container =>
{
container.Register<IDbConnectionFactory>(DbConnFactory);
container.Register<IAuthRepository>(c => new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
}
}.Init())
{
// Arrange
UserAuth userAuth;
var newRegistration = CreateNewUserRegistration();
var request = new BasicRequest(newRegistration);
var response = (RegisterResponse)appHost.ExecuteService(newRegistration, request);
// Test #1: Check role and permission assignment
// ---------------------------------------------
// Act
using (var db = DbConnFactory.Open())
{
// Hydrate userAuth
userAuth = db.SingleById<UserAuth>(response.UserId);
}
var assignRoleRequest =
new AssignRoles
{
UserName = userAuth.UserName,
Roles = { TestRoleName },
Permissions = { TestPermissionName },
};
// Assert #1.1:
// Check AssignRoles response to contain roles and permissions
var authRepo = appHost.Resolve<IAuthRepository>();
userAuth = (UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
authRepo.AssignRoles(userAuth, assignRoleRequest.Roles, assignRoleRequest.Permissions);
Assert.That(authRepo.GetRoles(userAuth).First(), Is.EqualTo(TestRoleName));
Assert.That(authRepo.GetPermissions(userAuth).First(), Is.EqualTo(TestPermissionName));
// Assert #1.2:
// Check UserAuth to contain roles and permissions
using (var db = DbConnFactory.Open())
{
// Hydrate userAuth
userAuth = db.SingleById<UserAuth>(response.UserId);
}
Assert.That(userAuth.Roles[0], Is.EqualTo(TestRoleName));
Assert.That(userAuth.Permissions[0], Is.EqualTo(TestPermissionName));
// Test #2: Check role and permission un-assignment
// ------------------------------------------------
// Act
var unassignRolesRequest =
new UnAssignRoles
{
UserName = userAuth.UserName,
Roles = { TestRoleName },
Permissions = { TestPermissionName },
};
authRepo.UnAssignRoles(userAuth, unassignRolesRequest.Roles, unassignRolesRequest.Permissions);
// Assert #2.1:
// Check UserAuth not to contain roles and permissions above
using (var db = DbConnFactory.Open())
{
// Hydrate userAuth
userAuth = db.SingleById<UserAuth>(response.UserId);
}
Assert.That(authRepo.GetRoles(userAuth).Count, Is.EqualTo(0));
Assert.That(authRepo.GetPermissions(userAuth).Count, Is.EqualTo(0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment