Exemplo n.º 1
0
        public void By_default_assigned_roles_are_saved_in_UserAuth_table()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host =>
                {
                    host.Plugins.Add(new AuthFeature(() => new AuthUserSession(), new [] { new BasicAuthProvider() })
                    {
                        IncludeRegistrationService = true,
                    });
                },
                ConfigureContainer = container =>
                {
                    container.Register <IDbConnectionFactory>(c =>
                                                              new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));

                    container.Register <IAuthRepository>(c =>
                                                         new OrmLiteAuthRepository(c.Resolve <IDbConnectionFactory>()));

                    container.Resolve <IAuthRepository>().InitSchema();
                }
            }.Init())
            {
                using (var db = appHost.Container.Resolve <IDbConnectionFactory>().Open())
                {
                    var register = CreateNewUserRegistration();
                    var req      = new BasicRequest(register)
                    {
                        QueryString = { ["authSecret"] = appHost.Config.AdminAuthSecret = "allow" }
                    };

                    var response = (RegisterResponse)appHost.ExecuteService(register, req);
                    var userAuth = db.SingleById <UserAuth>(response.UserId);

                    var assignResponse = (AssignRolesResponse)appHost.ExecuteService(new AssignRoles
                    {
                        UserName    = userAuth.UserName,
                        Roles       = { "TestRole" },
                        Permissions = { "TestPermission" },
                    }, req);
                    Assert.That(assignResponse.AllRoles[0], Is.EqualTo("TestRole"));
                    Assert.That(assignResponse.AllPermissions[0], Is.EqualTo("TestPermission"));

                    userAuth = db.SingleById <UserAuth>(response.UserId);
                    Assert.That(userAuth.Roles[0], Is.EqualTo("TestRole"));
                    Assert.That(userAuth.Permissions[0], Is.EqualTo("TestPermission"));

                    appHost.ExecuteService(new UnAssignRoles
                    {
                        UserName    = userAuth.UserName,
                        Roles       = { "TestRole" },
                        Permissions = { "TestPermission" },
                    }, req);

                    userAuth = db.SingleById <UserAuth>(response.UserId);
                    Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                    Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));
                }
            }
        }
Exemplo n.º 2
0
        public void Can_assign_roles_that_persist_to_UserAuthRole_table_in_DynamoDb()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host =>
                {
                    host.Plugins.Add(new AuthFeature(() => new AuthUserSession(), new[] { new BasicAuthProvider() })
                    {
                        IncludeRegistrationService = true,
                    });
                },
                ConfigureContainer = container =>
                {
                    container.Register <IPocoDynamo>(c => new PocoDynamo(DynamoConfig.CreateDynamoDBClient()));
                    //DynamoMetadata.Reset();
                    container.Resolve <IPocoDynamo>().DeleteAllTables(TimeSpan.FromMinutes(1));

                    container.Register <IAuthRepository>(c => new DynamoDbAuthRepository(c.Resolve <IPocoDynamo>()));
                    container.Resolve <IAuthRepository>().InitSchema();
                }
            }.Init())
            {
                var db = appHost.Container.Resolve <IPocoDynamo>();

                var register = CreateNewUserRegistration();
                var req      = new BasicRequest(register);
                req.QueryString["authSecret"] = appHost.Config.AdminAuthSecret = "allow";

                var response = (RegisterResponse)appHost.ExecuteService(register, req);
                var userAuth = db.GetItem <UserAuth>(response.UserId);

                var assignResponse = (AssignRolesResponse)appHost.ExecuteService(new AssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { "TestRole" },
                    Permissions = { "TestPermission" },
                }, req);
                Assert.That(assignResponse.AllRoles[0], Is.EqualTo("TestRole"));
                Assert.That(assignResponse.AllPermissions[0], Is.EqualTo("TestPermission"));

                Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));

                var manageRoles = (IManageRoles)appHost.Container.Resolve <IAuthRepository>();
                Assert.That(manageRoles.HasRole(userAuth.Id.ToString(), "TestRole"));
                Assert.That(manageRoles.HasPermission(userAuth.Id.ToString(), "TestPermission"));

                appHost.ExecuteService(new UnAssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { "TestRole" },
                    Permissions = { "TestPermission" },
                }, req);

                Assert.That(!manageRoles.HasRole(userAuth.Id.ToString(), "TestRole"));
                Assert.That(!manageRoles.HasPermission(userAuth.Id.ToString(), "TestPermission"));
            }
        }
Exemplo n.º 3
0
        public void Can_assign_roles_that_persist_to_UserAuthRole_table()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureContainer = container =>
                {
                    container.Register <IDbConnectionFactory>(c =>
                                                              new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));

                    container.Register <IAuthRepository>(c =>
                                                         new OrmLiteAuthRepository(c.Resolve <IDbConnectionFactory>())
                    {
                        UseDistinctRoleTables = true,
                    });

                    container.Resolve <IAuthRepository>().InitSchema();
                }
            }.Init())
            {
                using (var db = appHost.Container.Resolve <IDbConnectionFactory>().Open())
                {
                    var register = CreateNewUserRegistration();
                    var req      = new BasicRequest(register);
                    req.QueryString["authSecret"] = appHost.Config.AdminAuthSecret = "allow";

                    var response = (RegisterResponse)appHost.ExecuteService(register, req);
                    var userAuth = db.SingleById <UserAuth>(response.UserId);

                    var assignResponse = (AssignRolesResponse)appHost.ExecuteService(new AssignRoles
                    {
                        UserName    = userAuth.UserName,
                        Roles       = { "TestRole" },
                        Permissions = { "TestPermission" },
                    }, req);
                    Assert.That(assignResponse.AllRoles[0], Is.EqualTo("TestRole"));
                    Assert.That(assignResponse.AllPermissions[0], Is.EqualTo("TestPermission"));

                    Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                    Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));

                    var manageRoles = (IManageRoles)appHost.Container.Resolve <IAuthRepository>();
                    Assert.That(manageRoles.HasRole(userAuth.Id.ToString(), "TestRole"));
                    Assert.That(manageRoles.HasPermission(userAuth.Id.ToString(), "TestPermission"));

                    appHost.ExecuteService(new UnAssignRoles
                    {
                        UserName    = userAuth.UserName,
                        Roles       = { "TestRole" },
                        Permissions = { "TestPermission" },
                    }, req);

                    Assert.That(!manageRoles.HasRole(userAuth.Id.ToString(), "TestRole"));
                    Assert.That(!manageRoles.HasPermission(userAuth.Id.ToString(), "TestPermission"));
                }
            }
        }
Exemplo n.º 4
0
        public void By_default_assigned_roles_are_saved_in_UserAuth_table()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureContainer = container =>
                {
                    container.Register <IDbConnectionFactory>(c =>
                                                              new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));

                    container.Register <IAuthRepository>(c =>
                                                         new OrmLiteAuthRepository(c.Resolve <IDbConnectionFactory>()));

                    container.Resolve <IAuthRepository>().InitSchema();
                }
            }.Init())
            {
                using (var db = appHost.Container.Resolve <IDbConnectionFactory>().Open())
                {
                    var register = CreateNewUserRegistration();
                    var req      = new BasicRequest(register);
                    var response = (RegisterResponse)appHost.ExecuteService(register, req);
                    var userAuth = db.SingleById <UserAuth>(response.UserId);

                    var assignResponse = (AssignRolesResponse)appHost.ExecuteService(new AssignRoles
                    {
                        UserName    = userAuth.UserName,
                        Roles       = { "TestRole" },
                        Permissions = { "TestPermission" },
                    }, req);
                    Assert.That(assignResponse.AllRoles[0], Is.EqualTo("TestRole"));
                    Assert.That(assignResponse.AllPermissions[0], Is.EqualTo("TestPermission"));

                    userAuth = db.SingleById <UserAuth>(response.UserId);
                    Assert.That(userAuth.Roles[0], Is.EqualTo("TestRole"));
                    Assert.That(userAuth.Permissions[0], Is.EqualTo("TestPermission"));

                    appHost.ExecuteService(new UnAssignRoles
                    {
                        UserName    = userAuth.UserName,
                        Roles       = { "TestRole" },
                        Permissions = { "TestPermission" },
                    }, req);

                    userAuth = db.SingleById <UserAuth>(response.UserId);
                    Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                    Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));
                }
            }
        }
Exemplo n.º 5
0
        public void Can_use_nested_classes_as_Request_DTOs()
        {
            using (var appHost = new BasicAppHost(typeof(NestedService).Assembly).Init())
            {
                var root = (Root)appHost.ExecuteService(new Root {
                    Id = 1
                });
                Assert.That(root.Id, Is.EqualTo(1));

                var nested = (Root.Nested)appHost.ExecuteService(new Root.Nested {
                    Id = 2
                });
                Assert.That(nested.Id, Is.EqualTo(2));
            }
        }
Exemplo n.º 6
0
        public void Can_register_IDtoBValidator_separately()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host => {
                    host.RegisterService <DtoAService>();
                    host.Plugins.Add(new ValidationFeature());
                },
                ConfigureContainer = c => {
                    c.RegisterAs <DtoBValidator, IDtoBValidator>();
                    c.RegisterValidators(typeof(DtoARequestValidator).Assembly);
                }
            }.Init())
            {
                var c             = appHost.Container;
                var dtoAValidator = (DtoARequestValidator)c.TryResolve <IValidator <DtoA> >();
                Assert.That(dtoAValidator, Is.Not.Null);
                Assert.That(dtoAValidator.dtoBValidator, Is.Not.Null);
                Assert.That(c.TryResolve <IValidator <DtoB> >(), Is.Not.Null);
                Assert.That(c.TryResolve <IDtoBValidator>(), Is.Not.Null);

                var response = appHost.ExecuteService(new DtoA());
                response.PrintDump();
            }
        }
Exemplo n.º 7
0
        public void Can_Mock_Session_in_Container()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host => host.RegisterService(typeof(MockSessionTestService)),
                ConfigureContainer = x => x.Register <IAuthSession>(c => CreateUserSession())
            }.Init())
            {
                var response = appHost.ExecuteService(new MockSessionTest()) as AuthUserSession;

                Assert.That(response.UserAuthId, Is.EqualTo("1"));
                Assert.That(response.UserAuthName, Is.EqualTo("Mocked"));
                Assert.That(response.PrimaryEmail, Is.EqualTo("*****@*****.**"));
            }
        }
Exemplo n.º 8
0
        public void Can_Mock_Session_in_RequestFilterAttribute()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host =>
                {
                    host.RegisterService(typeof(MockSessionTestService));
                }
            }.Init())
            {
                var response = appHost.ExecuteService(new MockSessionAttributeTest()) as AuthUserSession;

                Assert.That(response.UserAuthId, Is.EqualTo("1"));
                Assert.That(response.UserAuthName, Is.EqualTo("Mocked"));
                Assert.That(response.PrimaryEmail, Is.EqualTo("*****@*****.**"));
            }
        }
Exemplo n.º 9
0
        public void CheckRoleAssignmentInUserAuthTable()
        {
            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);
                var      authRepo        = appHost.Resolve <IAuthRepository>();

                // 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 },
                };

                userAuth = (UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.AssignRoles(userAuth, assignRoleRequest.Roles, assignRoleRequest.Permissions);

                // Assert:
                // Check UserAuth to contain roles and permissions
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.EqualTo(TestRoleName));
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.EqualTo(TestPermissionName));

                // Test #2: Check role and permission un-assignment
                // ------------------------------------------------
                // Act
                using (var db = DbConnFactory.Open())
                {
                    // Hydrate userAuth
                    userAuth = db.SingleById <UserAuth>(response.UserId);
                }

                var unassignRolesRequest =
                    new UnAssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.UnAssignRoles(userAuth, unassignRolesRequest.Roles, unassignRolesRequest.Permissions);

                // Assert:
                // Check UserAuth not to contain roles and permissions above
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.Null);
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.Null);
            }
        }
Exemplo n.º 10
0
        public void CheckRoleAssignmentInUserAuthTableUsingSecretKey()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host =>
                {
                    host.Config.AdminAuthSecret = AdminAuthSecret;
                },
                ConfigureContainer = container =>
                {
                    container.Register <IUnitOfWork>(c => UnitOfWork);
                    container.Register <IAuthRepository>(c =>
                                                         new LightSpeedUserAuthRepository(c.Resolve <IUnitOfWork>()));
                }
            }.Init())
            {
                // Arrange
                LightSpeed.UserAuth userAuth;
                AssignRolesResponse assignRolesResponse;
                var newRegistration = CreateNewUserRegistration();
                var request         = new BasicRequest(newRegistration);
                request.QueryString["authSecret"] = AdminAuthSecret;
                var response = (RegisterResponse)appHost.ExecuteService(newRegistration, request);

                // Test #1: Check role and permission assignment
                // ---------------------------------------------
                // Act
                userAuth = UnitOfWork.FindById <LightSpeed.UserAuth>(response.UserId); // Hydrate userAuth

                var assignRoleRequest =
                    new AssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                // Assert #1.1:
                // Check AssignRoles response to contain roles and permissions
                assignRolesResponse = (AssignRolesResponse)appHost.ExecuteService(assignRoleRequest, request);
                Assert.That(assignRolesResponse.AllRoles[0], Is.EqualTo(TestRoleName));
                Assert.That(assignRolesResponse.AllPermissions[0], Is.EqualTo(TestPermissionName));

                // Assert #1.2:
                // Check UserAuth to contain roles and permissions
                userAuth = UnitOfWork.FindById <LightSpeed.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 },
                };
                appHost.ExecuteService(unassignRolesRequest, request);

                // Assert #2.1:
                // Check UserAuth not to contain roles and permissions above
                userAuth = UnitOfWork.FindById <LightSpeed.UserAuth>(response.UserId);
                Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));
            }
        }
Exemplo n.º 11
0
        public void CheckRoleAssignmentInUserRoleTableUsingIAuthRepository()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureContainer = container =>
                {
                    container.Register <IUnitOfWork>(c => UnitOfWork);
                    container.Register <IAuthRepository>(c =>
                                                         new LightSpeedUserAuthRepository(c.Resolve <IUnitOfWork>())
                    {
                        UseDistinctRoleTables = true
                    });
                }
            }.Init())
            {
                // Arrange
                LightSpeed.UserAuth userAuth;
                AssignRolesResponse assignRolesResponse;
                var newRegistration = CreateNewUserRegistration();
                var request         = new BasicRequest(newRegistration);
                var response        = (RegisterResponse)appHost.ExecuteService(newRegistration, request);
                var authRepo        = appHost.Resolve <IAuthRepository>();

                // Test #1: Check role and permission assignment
                // ---------------------------------------------
                // Act
                userAuth = UnitOfWork.FindById <LightSpeed.UserAuth>(response.UserId); // Hydrate userAuth

                var assignRoleRequest =
                    new AssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (LightSpeed.UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.AssignRoles(userAuth, assignRoleRequest.Roles, assignRoleRequest.Permissions);

                // Assert #1.1:
                // Check UserAuth to contain roles and permissions
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.EqualTo(TestRoleName));
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.EqualTo(TestPermissionName));

                // Assert #1.2:
                // Check that roles and permissions are not persisted to UserAuth table
                Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));

                // Assert #1.3:
                // Check UserRoles table to contain roles and permissions
                var manageRoles = (IManageRoles)appHost.Container.Resolve <IAuthRepository>();
                Assert.That(manageRoles.HasRole(userAuth.Id.ToString(), TestRoleName));
                Assert.That(manageRoles.HasPermission(userAuth.Id.ToString(), TestPermissionName));

                // Test #2: Check role and permission un-assignment
                // ------------------------------------------------
                // Act
                userAuth = UnitOfWork.FindById <LightSpeed.UserAuth>(response.UserId); // Hydrate userAuth

                var unassignRolesRequest =
                    new UnAssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };

                userAuth = (LightSpeed.UserAuth)authRepo.GetUserAuthByUserName(assignRoleRequest.UserName);
                authRepo.UnAssignRoles(userAuth, unassignRolesRequest.Roles, unassignRolesRequest.Permissions);

                // Assert #2.1:
                // Check UserAuth to contain roles and permissions
                Assert.That(authRepo.GetRoles(userAuth).FirstOrDefault(), Is.Null);
                Assert.That(authRepo.GetPermissions(userAuth).FirstOrDefault(), Is.Null);

                // Assert #2.2:
                // Check UserRole table not to contain roles and permissions above
                Assert.That(!manageRoles.HasRole(userAuth.Id.ToString(), TestRoleName));
                Assert.That(!manageRoles.HasPermission(userAuth.Id.ToString(), TestPermissionName));
            }
        }
Exemplo n.º 12
0
        public void CheckRoleAssignmentInUserRoleTable()
        {
            using (var appHost = new BasicAppHost
            {
                ConfigureAppHost = host =>
                {
                    host.Config.AdminAuthSecret = AdminAuthSecret;
                },
                ConfigureContainer = container =>
                {
                    container.Register <IDbConnectionFactory>(DbConnFactory);
                    container.Register <IAuthRepository>(c =>
                                                         new OrmLiteAuthRepository(c.Resolve <IDbConnectionFactory>())
                    {
                        UseDistinctRoleTables = true
                    });
                }
            }.Init())
            {
                // Arrange
                UserAuth            userAuth;
                AssignRolesResponse assignRolesResponse;
                var newRegistration = CreateNewUserRegistration();
                var request         = new BasicRequest(newRegistration);
                request.QueryString["authSecret"] = AdminAuthSecret;
                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
                assignRolesResponse = (AssignRolesResponse)appHost.ExecuteService(assignRoleRequest, request);
                Assert.That(assignRolesResponse.AllRoles[0], Is.EqualTo(TestRoleName));
                Assert.That(assignRolesResponse.AllPermissions[0], Is.EqualTo(TestPermissionName));

                // Assert #1.2:
                // Check that roles and permissions are not persisted to UserAuth table
                Assert.That(userAuth.Roles.Count, Is.EqualTo(0));
                Assert.That(userAuth.Permissions.Count, Is.EqualTo(0));

                // Assert #1.3:
                // Check UserRoles table to contain roles and permissions
                var manageRoles = (IManageRoles)appHost.Container.Resolve <IAuthRepository>();
                Assert.That(manageRoles.HasRole(userAuth.Id.ToString(), TestRoleName));
                Assert.That(manageRoles.HasPermission(userAuth.Id.ToString(), TestPermissionName));

                // Test #2: Check role and permission un-assignment
                // ------------------------------------------------
                // Act
                var unassignRolesRequest =
                    new UnAssignRoles
                {
                    UserName    = userAuth.UserName,
                    Roles       = { TestRoleName },
                    Permissions = { TestPermissionName },
                };
                appHost.ExecuteService(unassignRolesRequest, request);

                // Assert #2.1:
                // Check UserRole table not to contain roles and permissions above
                Assert.That(!manageRoles.HasRole(userAuth.Id.ToString(), TestRoleName));
                Assert.That(!manageRoles.HasPermission(userAuth.Id.ToString(), TestPermissionName));
            }
        }