예제 #1
0
        public LGTokenFaker(IManageDatabase databaseManager, ILogger logger)
        {
            var random       = new Random();
            int randomNumber = random.Next();

            Bogus.Randomizer.Seed = new Random(randomNumber);

            var parentFaker      = new LGAccountFaker(databaseManager, logger);
            var parent           = parentFaker.GetOne();
            var parentRepository = new LGAccountRepository(databaseManager, new LGAccountRepositorySql(), logger);
            var parentAddResult  = parentRepository.Create(parent);

            parent = parentAddResult.Model;

            _faker = new Bogus.Faker <LGToken>()
                     .StrictMode(false)
                     .Rules((f, m) =>
            {
                m.LGTokenId   = null;
                m.LGAccountId = parent.LGAccountId;
                m.Token       = f.Lorem.Sentence(10);
                m.Created     = f.Date.Past();
                m.LastUsed    = f.Date.Past();
                m.Expires     = f.Date.Past();
            });
        }
예제 #2
0
        public LGPrivilegeFaker(IManageDatabase databaseManager, ILogger logger)
        {
            var random       = new Random();
            int randomNumber = random.Next();

            Bogus.Randomizer.Seed = new Random(randomNumber);

            var parentFaker      = new LGAccountFaker(databaseManager, logger);
            var parent           = parentFaker.GetOne();
            var parentRepository = new LGAccountRepository(databaseManager, new LGAccountRepositorySql(), logger);
            var parentAddResult  = parentRepository.Create(parent);

            parent = parentAddResult.Model;

            var featureFaker      = new LGFeatureFaker(databaseManager, logger);
            var featureFake       = featureFaker.GetOne();
            var featureRepository = new LGFeatureRepository(databaseManager, new LGFeatureRepositorySql(), logger);
            var featureFakeResult = featureRepository.Create(featureFake);

            featureFake = featureFakeResult.Model;

            var abilityFaker      = new LGAbilityFaker(databaseManager, logger);
            var abilityFake       = abilityFaker.GetOne();
            var abilityRepository = new LGAbilityRepository(databaseManager, new LGAbilityRepositorySql(), logger);
            var abilityFakeResult = abilityRepository.Create(abilityFake);

            abilityFake = abilityFakeResult.Model;

            _faker = new Bogus.Faker <LGPrivilege>()
                     .StrictMode(false)
                     .Rules((f, m) =>
            {
                m.LGPrivilegeId = null;
                m.LGAccountId   = parent.LGAccountId;
                m.Starts        = f.Date.Past();
                m.Ends          = f.Date.Past();
                m.FeatureIdRef  = featureFake.LGFeatureId;
                m.AbilityIdRef  = abilityFake.LGAbilityId;
            });
        }
예제 #3
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);
        }
예제 #4
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().ReflectedType.Name}_{MethodBase.GetCurrentMethod().Name}.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 lGAccountRepository = new LGAccountRepository(databaseManager, new LGAccountRepositorySql(), logger);

            Assert.NotNull(lGAccountRepository);

            // faker
            BrashActionResult <LGAccount> result = null;
            var lGAccountFaker = new LGAccountFaker(databaseManager, logger);

            Assert.NotNull(lGAccountFaker);

            // create
            var lGAccountCreateModel = lGAccountFaker.GetOne();

            result = lGAccountRepository.Create(lGAccountCreateModel);
            Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message);
            Assert.True(result.Model.LGAccountId > 0);

            // use model with id
            lGAccountCreateModel = result.Model;

            // update
            var lGAccountUpdateModel = lGAccountFaker.GetOne();

            lGAccountUpdateModel.LGAccountId = lGAccountCreateModel.LGAccountId;
            result = lGAccountRepository.Update(lGAccountUpdateModel);
            Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message);

            // delete
            result = lGAccountRepository.Delete(lGAccountCreateModel);
            Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message);

            // fetch

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

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

            foreach (var f in fakes)
            {
                result = lGAccountRepository.Create(f);

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

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

                result = lGAccountRepository.Fetch(model);
                Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message);
                Assert.True(result.Model.LGAccountId >= 0);
            }
        }