Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
 public LGFeatureController(LGFeatureService lGFeatureService, Serilog.ILogger logger) : base()
 {
     _lGFeatureService = lGFeatureService;
     _logger           = logger;
 }
Ejemplo n.º 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 lGFeatureRepository = new LGFeatureRepository(databaseManager, new LGFeatureRepositorySql(), logger);

            Assert.NotNull(lGFeatureRepository);

            // - service
            var lGFeatureService = new LGFeatureService(lGFeatureRepository, logger);

            Assert.NotNull(lGFeatureService);

            // faker
            BrashActionResult <LGFeature> serviceResult = null;
            var lGFeatureFaker = new LGFeatureFaker(databaseManager, logger);

            Assert.NotNull(lGFeatureFaker);

            // create
            var lGFeatureCreateModel = lGFeatureFaker.GetOne();

            serviceResult = lGFeatureService.Create(lGFeatureCreateModel);
            Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);
            Assert.True(serviceResult.Model.LGFeatureId > 0);

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

            // update
            var lGFeatureUpdateModel = lGFeatureFaker.GetOne();

            lGFeatureUpdateModel.LGFeatureId = lGFeatureCreateModel.LGFeatureId;
            serviceResult = lGFeatureService.Update(lGFeatureUpdateModel);
            Assert.True(serviceResult.Status == BrashActionStatus.SUCCESS, serviceResult.Message);

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

            // fetch

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

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

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

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

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

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