コード例 #1
0
 public LGAbilityController(LGAbilityService lGAbilityService, Serilog.ILogger logger) : base()
 {
     _lGAbilityService = lGAbilityService;
     _logger           = logger;
 }
コード例 #2
0
        public void CreateUserAccounts()
        {
            // file system
            var path         = "/shop/randomsilo/modern-web/backends/LoyalGuard";
            var project      = "LoyalGuard";
            var outputPath   = $"{path}/{project}.Infrastructure.Test/TestOutput/";
            var databaseFile = GetDatabase(outputPath, MethodBase.GetCurrentMethod());

            // logger
            ILogger logger = GetLogger($"{outputPath}/{MethodBase.GetCurrentMethod()}.log");

            // database setup

            // - context
            IDatabaseContext databaseContext = new DatabaseContext(
                $"Data Source={databaseFile}"
                , "TestDb"
                , "TestSchema"
                , $"{path}/sql/sqlite/ALL.sql"
                );

            Assert.NotNull(databaseContext);

            // - manager
            IManageDatabase databaseManager = new DatabaseManager(databaseContext);

            Assert.NotNull(databaseManager);

            // - create tables
            databaseManager.CreateDatabase();

            // - repositories
            var lGAccountRepository   = new LGAccountRepository(databaseManager, new LGAccountRepositorySql(), logger);
            var lGPrivilegeRepository = new LGPrivilegeRepository(databaseManager, new LGPrivilegeRepositorySql(), logger);
            var lGFeatureRepository   = new LGFeatureRepository(databaseManager, new LGFeatureRepositorySql(), logger);
            var lGAbilityRepository   = new LGAbilityRepository(databaseManager, new LGAbilityRepositorySql(), logger);
            var lGRoleRepository      = new LGRoleRepository(databaseManager, new LGRoleRepositorySql(), logger);
            var lGTokenRepository     = new LGTokenRepository(databaseManager, new LGTokenRepositorySql(), logger);

            Assert.NotNull(lGAccountRepository);
            Assert.NotNull(lGPrivilegeRepository);
            Assert.NotNull(lGFeatureRepository);
            Assert.NotNull(lGAbilityRepository);
            Assert.NotNull(lGRoleRepository);
            Assert.NotNull(lGTokenRepository);

            // - services
            var lGAccountService   = new LGAccountService(lGAccountRepository, logger);
            var lGPrivilegeService = new LGPrivilegeService(lGPrivilegeRepository, logger);
            var lGFeatureService   = new LGFeatureService(lGFeatureRepository, logger);
            var lGAbilityService   = new LGAbilityService(lGAbilityRepository, logger);
            var lGRoleService      = new LGRoleService(lGRoleRepository, logger);
            var lGTokenService     = new LGTokenService(lGTokenRepository, logger);

            Assert.NotNull(lGAccountService);
            Assert.NotNull(lGPrivilegeService);
            Assert.NotNull(lGFeatureService);
            Assert.NotNull(lGAbilityService);
            Assert.NotNull(lGRoleService);
            Assert.NotNull(lGTokenService);

            // - authService
            var authService = new AuthService(
                lGAccountService
                , lGTokenService
                , lGPrivilegeService
                , lGFeatureService
                , lGAbilityService
                , lGRoleService
                , logger
                );

            // - get all features
            var featureModels = lGFeatureService.FindWhere("").Models;

            // - get all abilities
            var abilityModels = lGAbilityService.FindWhere("").Models;

            // - get specific roles
            var roleModels  = lGRoleService.FindWhere("").Models;
            var adminRole   = roleModels.FirstOrDefault(role => role.ChoiceName == "Administrator");
            var userRole    = roleModels.FirstOrDefault(role => role.ChoiceName == "User");
            var auditorRole = roleModels.FirstOrDefault(role => role.ChoiceName == "Auditor");

            // create admin
            LGAccount adminAccount = new LGAccount()
            {
                LastName     = "Administator"
                , FirstName  = "System"
                , MiddleName = "X"
                , UserName   = "******"
                , Email      = "*****@*****.**"
                , Password   = "******"
                , RoleIdRef  = adminRole.LGRoleId
            };

            var serviceResult = lGAccountService.Create(adminAccount);

            Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);
            Assert.True(serviceResult.Model.LGAccountId > 0);
            adminAccount = serviceResult.Model;

            foreach (var feature in featureModels)
            {
                foreach (var ability in abilityModels)
                {
                    var priviledge = new LGPrivilege()
                    {
                        AbilityIdRef   = ability.LGAbilityId
                        , FeatureIdRef = feature.LGFeatureId
                        , LGAccountId  = adminAccount.LGAccountId
                        , Starts       = DateTime.Now
                        , Ends         = DateTime.Now.AddYears(2)
                    };

                    var priviledgeCreateResult = lGPrivilegeService.Create(priviledge);
                    Assert.True(priviledgeCreateResult.Status == BrashActionStatus.SUCCESS, priviledgeCreateResult.Message);
                    Assert.True(priviledgeCreateResult.Model.LGPrivilegeId > 0);
                }
            }

            // authenticate
            var adminSignIn = new AccountSignin()
            {
                UserName   = "******"
                , Password = "******"
            };
            var adminAuthResult = authService.Authenticate(adminSignIn);

            Assert.True(adminAuthResult.Status == BrashActionStatus.SUCCESS);
            Assert.NotNull(adminAuthResult.Model);
            Assert.NotNull(adminAuthResult.Model.Account);
            Assert.NotNull(adminAuthResult.Model.Token);

            Assert.Equal("Administrator", adminAuthResult.Model.Role);
        }
コード例 #3
0
        public void CreateUpdateDeleteFetch()
        {
            // file system
            var path         = "/shop/randomsilo/modern-web/backends/LoyalGuard";
            var project      = "LoyalGuard";
            var outputPath   = $"{path}/{project}.Infrastructure.Test/TestOutput/";
            var databaseFile = GetDatabase(outputPath, MethodBase.GetCurrentMethod());

            // logger
            ILogger logger = GetLogger($"{outputPath}/{MethodBase.GetCurrentMethod()}.log");

            // database setup

            // - context
            IDatabaseContext databaseContext = new DatabaseContext(
                $"Data Source={databaseFile}"
                , "TestDb"
                , "TestSchema"
                , $"{path}/sql/sqlite/ALL.sql"
                );

            Assert.NotNull(databaseContext);

            // - manager
            IManageDatabase databaseManager = new DatabaseManager(databaseContext);

            Assert.NotNull(databaseManager);

            // - create tables
            databaseManager.CreateDatabase();

            // - repository
            var lGAbilityRepository = new LGAbilityRepository(databaseManager, new LGAbilityRepositorySql(), logger);

            Assert.NotNull(lGAbilityRepository);

            // - service
            var lGAbilityService = new LGAbilityService(lGAbilityRepository, logger);

            Assert.NotNull(lGAbilityService);

            // faker
            BrashActionResult <LGAbility> serviceResult = null;
            var lGAbilityFaker = new LGAbilityFaker(databaseManager, logger);

            Assert.NotNull(lGAbilityFaker);

            // create
            var lGAbilityCreateModel = lGAbilityFaker.GetOne();

            serviceResult = lGAbilityService.Create(lGAbilityCreateModel);
            Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);
            Assert.True(serviceResult.Model.LGAbilityId > 0);

            // use model with id
            lGAbilityCreateModel = serviceResult.Model;

            // update
            var lGAbilityUpdateModel = lGAbilityFaker.GetOne();

            lGAbilityUpdateModel.LGAbilityId = lGAbilityCreateModel.LGAbilityId;
            serviceResult = lGAbilityService.Update(lGAbilityUpdateModel);
            Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);

            // delete
            serviceResult = lGAbilityService.Delete(lGAbilityCreateModel);
            Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);

            // fetch

            // - make fakes
            var fakes = lGAbilityFaker.GetMany(10);

            // - add fakes to database
            List <int?> ids = new List <int?>();

            foreach (var f in fakes)
            {
                serviceResult = lGAbilityService.Create(f);

                Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);
                Assert.True(serviceResult.Model.LGAbilityId >= 0);
                ids.Add(serviceResult.Model.LGAbilityId);
            }

            // - get fakes from database
            foreach (var id in ids)
            {
                var model = new LGAbility()
                {
                    LGAbilityId = id
                };

                serviceResult = lGAbilityService.Fetch(model);
                Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);
                Assert.True(serviceResult.Model.LGAbilityId >= 0);
            }
        }