Ejemplo n.º 1
0
        public ApiUser(
            IHttpContextAccessor httpContextAccessor,
            TenantManagementApi <TenantDto> tenantManagementApi,
            IMemoryCache memoryCache
            )
        {
            this.httpContext         = httpContextAccessor.HttpContext;
            this.tenantManagementApi = tenantManagementApi;
            this.memoryCache         = memoryCache;

            IsAuthenticated = httpContext.User.Identity.IsAuthenticated;

            if (IsAuthenticated)
            {
                var tenantId = GetTenantIdByHostname().Result;

                //parse claims list
                var claims = httpContext
                             .User
                             .Claims
                             .Select(x => new { x.Type, x.Value });

                UserId      = Guid.Parse(claims.First(x => x.Type == "userId").Value);
                TenantId    = tenantId;
                TenantRoles = claims
                              .Where(x => x.Type == TenantClaimsSchema.TenantRolesData)
                              .Select(x => x.Value.DeserializeToTenantRolesClaimData());

                //Extract roles for current tenant
                Roles = TenantRoles.First(x => x.TenantId == tenantId).Roles.Select(x => x.NormalizedName);
            }
            ;
        }
        public async Task Fetching_nonexisting_hostname_should_fail_gracefully()
        {
            var resultFetched = await TenantManagementApi.Search(new TenantSearch(hostname : "host-which-does-not-exist"));

            Assert.True(resultFetched.Success);
            Assert.False(resultFetched.Data.Any());
        }
        public async Task Deleting_tenant_should_work()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var resultDeleted = await TenantManagementApi.DeleteTenant(tenantCreated.TenantId);

            Assert.True(resultCreated.Success);
            Assert.True(resultDeleted.Success);
        }
        public async Task Created_dto_should_match_original_dto()
        {
            var tenant = UniqueTestTenant;

            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(tenant).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.Equal(tenant.Name, tenantCreated.Name);
            Assert.Equal(tenant.Hostnames, tenantCreated.Hostnames);
        }
        public async Task Created_dto_should_match_fetched_dto()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(resultFetched, tenantFetched) = await TenantManagementApi.GetTenant(tenantCreated.TenantId).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.True(resultFetched.Success);
            Assert.Equal(tenantCreated, tenantFetched);
        }
        public async Task Service_should_find_tenant_by_hostname()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(resultFetched, tenantFetched) = await TenantManagementApi.Search(new TenantSearch(hostname : tenantCreated.Hostnames.First())).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.True(resultFetched.Success);
            Assert.Equal(tenantCreated.TenantId, tenantFetched.TenantId);
        }
        public async Task Updated_dto_should_match_original_dto()
        {
            var(resultCreated, tenantCreated) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            tenantCreated.Name += " UPDATED";
            tenantCreated.Hostnames.Add($"new-hostname-for-tenant-{Guid.NewGuid()}");

            var(resultUpdated, tenantUpdated) = await TenantManagementApi.UpdateTenant(tenantCreated).UnpackSingleOrDefault();

            Assert.True(resultCreated.Success);
            Assert.True(resultUpdated.Success);
            Assert.Equal(tenantCreated, tenantUpdated);
        }
        public async Task Creating_tenant_with_already_assigned_host_should_fail_gracefully()
        {
            var tenantDto1 = UniqueTestTenant;
            var tenantDto2 = UniqueTestTenant;

            tenantDto2.Hostnames = tenantDto1.Hostnames;

            var resultCreated1 = await TenantManagementApi.AddTenant(tenantDto1);

            var resultCreated2 = await TenantManagementApi.AddTenant(tenantDto2);

            Assert.True(resultCreated1.Success);
            Assert.False(resultCreated2.Success);
        }
        public async Task Service_should_return_all_created_tenants()
        {
            var(_, tenantCreated1) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(_, tenantCreated2) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var tenants = await TenantManagementApi.GetTenants();

            var count = tenants
                        .Data
                        .Where(x => x.TenantId == tenantCreated1.TenantId || x.TenantId == tenantCreated2.TenantId)
                        .Count();

            Assert.Equal(2, count);
        }
Ejemplo n.º 10
0
        public async Task Adding_already_assigned_hostname_should_fail_gracefully()
        {
            var(createdResult1, tenant1) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            var(createdResult2, tenant2) = await TenantManagementApi.AddTenant(UniqueTestTenant).UnpackSingleOrDefault();

            foreach (var host in tenant2.Hostnames)
            {
                tenant1.Hostnames.Add(host);
            }

            var tenantUpdatedResult = await TenantManagementApi.UpdateTenant(tenant1);

            Assert.True(createdResult1.Success);
            Assert.True(createdResult2.Success);
            Assert.False(tenantUpdatedResult.Success);
        }
        private async Task LoadTenantsAndRoles()
        {
            var rolesResult = await RoleManagementApi.GetRoles();

            var tenantsResult = await TenantManagementApi.GetTenants();

            var roles   = rolesResult.Data;
            var tenants = tenantsResult.Data;

            if (!roles.Any(x => x.Name == Role1.Name))
            {
                var result = await RoleManagementApi.AddRole(Role1);

                Role1 = result.Data.Single();
            }

            if (!roles.Any(x => x.Name == Role2.Name))
            {
                var result = await RoleManagementApi.AddRole(Role2);

                Role2 = result.Data.Single();
            }

            if (!tenants.Any(x => x.Name == Tenant1.Name))
            {
                var result = await TenantManagementApi.AddTenant(Tenant1);

                Tenant1 = result.Data.Single();
            }

            if (!tenants.Any(x => x.Name == Tenant2.Name))
            {
                var result = await TenantManagementApi.AddTenant(Tenant2);

                Tenant2 = result.Data.Single();
            }
        }
Ejemplo n.º 12
0
        public async Task Deleting_nonexisting_tenant_should_fail_gracefully()
        {
            var resultDeleted = await TenantManagementApi.DeleteTenant(Guid.NewGuid());

            Assert.False(resultDeleted.Success);
        }
Ejemplo n.º 13
0
        public async Task Creating_tenant_with_invalid_parameters_should_fail_gracefully()
        {
            var resultCreated = await TenantManagementApi.AddTenant(new TenantDto());

            Assert.False(resultCreated.Success);
        }
Ejemplo n.º 14
0
        public async Task Fetching_missing_ID_should_fail_gracefully()
        {
            var resultFetched = await TenantManagementApi.GetTenant(Guid.NewGuid());

            Assert.False(resultFetched.Success);
        }