Esempio n. 1
0
        public async Task CreateConnection_BillingSyncType_InvalidLicense_Throws(OrganizationConnectionRequestModel model,
                                                                                 BillingSyncConfig config, Guid cloudOrgId, OrganizationLicense organizationLicense,
                                                                                 SutProvider <OrganizationConnectionsController> sutProvider)
        {
            model.Type             = OrganizationConnectionType.CloudBillingSync;
            organizationLicense.Id = cloudOrgId;

            model.Config = JsonDocumentFromObject(config);
            var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);

            typedModel.ParsedConfig.CloudOrganizationId = cloudOrgId;

            sutProvider.GetDependency <ICurrentContext>()
            .OrganizationOwner(model.OrganizationId)
            .Returns(true);

            sutProvider.GetDependency <ILicensingService>()
            .ReadOrganizationLicenseAsync(model.OrganizationId)
            .Returns(organizationLicense);

            sutProvider.GetDependency <ILicensingService>()
            .VerifyLicense(organizationLicense)
            .Returns(false);

            await Assert.ThrowsAsync <BadRequestException>(async() => await sutProvider.Sut.CreateConnection(model));
        }
        public async Task <OrganizationConnectionResponseModel> CreateConnection([FromBody] OrganizationConnectionRequestModel model)
        {
            if (!await HasPermissionAsync(model?.OrganizationId))
            {
                throw new BadRequestException("Only the owner of an organization can create a connection.");
            }

            if (await HasConnectionTypeAsync(model))
            {
                throw new BadRequestException($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.");
            }

            switch (model.Type)
            {
            case OrganizationConnectionType.CloudBillingSync:
                var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);
                var license    = await _licensingService.ReadOrganizationLicenseAsync(model.OrganizationId);

                typedModel.ParsedConfig.CloudOrganizationId = license.Id;
                var connection = await _createOrganizationConnectionCommand.CreateAsync(typedModel.ToData());

                return(new OrganizationConnectionResponseModel(connection, typeof(BillingSyncConfig)));

            default:
                throw new BadRequestException($"Unknown Organization connection Type: {model.Type}");
            }
        }
        private async Task <bool> HasConnectionTypeAsync(OrganizationConnectionRequestModel model, Guid?connectionId,
                                                         OrganizationConnectionType type)
        {
            var existingConnections = await GetConnectionsAsync(model.OrganizationId, type);

            return(existingConnections.Any(c => c.Type == model.Type && (!connectionId.HasValue || c.Id != connectionId.Value)));
        }
        public async Task CreateConnection_Success(OrganizationConnectionRequestModel model, BillingSyncConfig config,
                                                   Guid cloudOrgId, SutProvider <OrganizationConnectionsController> sutProvider)
        {
            model.Config = JsonDocumentFromObject(config);
            var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);

            typedModel.ParsedConfig.CloudOrganizationId = cloudOrgId;

            sutProvider.GetDependency <ICreateOrganizationConnectionCommand>().CreateAsync <BillingSyncConfig>(default)
Esempio n. 5
0
        public async Task CreateConnection_CloudBillingSync_RequiresOwnerPermissions(SutProvider <OrganizationConnectionsController> sutProvider)
        {
            var model = new OrganizationConnectionRequestModel
            {
                Type = OrganizationConnectionType.CloudBillingSync,
            };
            var exception = await Assert.ThrowsAsync <BadRequestException>(() => sutProvider.Sut.CreateConnection(model));

            Assert.Contains($"You do not have permission to create a connection of type", exception.Message);
        }
Esempio n. 6
0
        public async Task CreateConnection_Success(OrganizationConnectionRequestModel model, BillingSyncConfig config,
                                                   Guid cloudOrgId, OrganizationLicense organizationLicense, SutProvider <OrganizationConnectionsController> sutProvider)
        {
            organizationLicense.Id = cloudOrgId;

            model.Config = JsonDocumentFromObject(config);
            var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);

            typedModel.ParsedConfig.CloudOrganizationId = cloudOrgId;

            sutProvider.GetDependency <IGlobalSettings>().SelfHosted.Returns(true);
            sutProvider.GetDependency <ICreateOrganizationConnectionCommand>().CreateAsync <BillingSyncConfig>(default)
Esempio n. 7
0
        public async Task CreateConnection_OnlyOneConnectionOfEachType(OrganizationConnectionType type,
                                                                       OrganizationConnectionRequestModel model, BillingSyncConfig config, Guid existingEntityId,
                                                                       SutProvider <OrganizationConnectionsController> sutProvider)
        {
            model.Type   = type;
            model.Config = JsonDocumentFromObject(config);
            var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);
            var existing   = typedModel.ToData(existingEntityId).ToEntity();

            sutProvider.GetDependency <ICurrentContext>().OrganizationOwner(model.OrganizationId).Returns(true);

            sutProvider.GetDependency <IOrganizationConnectionRepository>().GetByOrganizationIdTypeAsync(model.OrganizationId, type).Returns(new[] { existing });

            var exception = await Assert.ThrowsAsync <BadRequestException>(() => sutProvider.Sut.CreateConnection(model));

            Assert.Contains($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.", exception.Message);
        }
        public async Task <OrganizationConnectionResponseModel> UpdateConnection(Guid organizationConnectionId, [FromBody] OrganizationConnectionRequestModel model)
        {
            if (!await HasPermissionAsync(model?.OrganizationId))
            {
                throw new BadRequestException("Only the owner of an organization can update a connection.");
            }

            if (await HasConnectionTypeAsync(model, organizationConnectionId))
            {
                throw new BadRequestException($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.");
            }

            switch (model.Type)
            {
            case OrganizationConnectionType.CloudBillingSync:
                var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);
                var connection = await _updateOrganizationConnectionCommand.UpdateAsync(typedModel.ToData(organizationConnectionId));

                return(new OrganizationConnectionResponseModel(connection, typeof(BillingSyncConfig)));

            default:
                throw new BadRequestException($"Unkown Organization connection Type: {model.Type}");
            }
        }
        public async Task <OrganizationConnectionResponseModel> CreateConnection([FromBody] OrganizationConnectionRequestModel model)
        {
            if (!await HasPermissionAsync(model?.OrganizationId))
            {
                throw new BadRequestException($"You do not have permission to create a connection of type {model.Type}.");
            }

            if (await HasConnectionTypeAsync(model, null, model.Type))
            {
                throw new BadRequestException($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.");
            }

            switch (model.Type)
            {
            case OrganizationConnectionType.CloudBillingSync:
                return(await CreateOrUpdateOrganizationConnectionAsync <BillingSyncConfig>(null, model, ValidateBillingSyncConfig));

            case OrganizationConnectionType.Scim:
                return(await CreateOrUpdateOrganizationConnectionAsync <ScimConfig>(null, model));

            default:
                throw new BadRequestException($"Unknown Organization connection Type: {model.Type}");
            }
        }
        public async Task <OrganizationConnectionResponseModel> UpdateConnection(Guid organizationConnectionId, [FromBody] OrganizationConnectionRequestModel model)
        {
            var existingOrganizationConnection = await _organizationConnectionRepository.GetByIdAsync(organizationConnectionId);

            if (existingOrganizationConnection == null)
            {
                throw new NotFoundException();
            }

            if (!await HasPermissionAsync(model?.OrganizationId, model?.Type))
            {
                throw new BadRequestException("You do not have permission to update this connection.");
            }

            if (await HasConnectionTypeAsync(model, organizationConnectionId, model.Type))
            {
                throw new BadRequestException($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.");
            }

            switch (model.Type)
            {
            case OrganizationConnectionType.CloudBillingSync:
                return(await CreateOrUpdateOrganizationConnectionAsync <BillingSyncConfig>(organizationConnectionId, model));

            case OrganizationConnectionType.Scim:
                return(await CreateOrUpdateOrganizationConnectionAsync <ScimConfig>(organizationConnectionId, model));

            default:
                throw new BadRequestException($"Unkown Organization connection Type: {model.Type}");
            }
        }