Ejemplo n.º 1
0
        public async Task <IActionResult> GetEntitlementInternal([FromBody] EntitlementRequestModel request)
        {
            Logger.LogInformation($"Internal Entitlement Request: {JsonConvert.SerializeObject(request)}");

            if (request == null)
            {
                return(BadRequest());
            }

            var validationResult = request.Validate(User.Identity.Name);

            if (validationResult.Code == ContractExecutionStatesEnum.ValidationError)
            {
                Logger.LogWarning(validationResult.Message);
                return(BadRequest(validationResult.Message));
            }

            var isEntitled = false;

            if (!string.IsNullOrEmpty(request.UserEmail))
            {
                if (AcceptedEmails == null)
                {
                    Logger.LogInformation($"Loading testing entitlement accepted emails");
                    LoadTestingEmails();
                }
                isEntitled = AcceptedEmails.Contains(request.UserEmail.ToLower());
                _logger.LogInformation($"{request.UserEmail} {(isEntitled ? "is an accepted email" : "is not in the allowed list")}");
            }

            if (!isEntitled)
            {
                var enableEntitlementCheck = ConfigStore.GetValueBool(ConfigConstants.ENABLE_ENTITLEMENTS_CONFIG_KEY, false);
                if (enableEntitlementCheck)
                {
                    var statusCode = await EmsClient.GetEntitlements(Guid.Parse(request.UserUid), Guid.Parse(request.OrganizationIdentifier), request.Sku, request.Feature, CustomHeaders);

                    isEntitled = statusCode == HttpStatusCode.OK;
                }
                else
                {
                    Logger.LogInformation($"Entitlement checking is disabled, allowing the request.");
                    isEntitled = true;
                }
            }

            var response = new EntitlementResponseModel
            {
                Feature                = request.Feature,
                Sku                    = request.Sku,
                IsEntitled             = isEntitled,
                OrganizationIdentifier = request.OrganizationIdentifier,
                UserUid                = request.UserUid,
                UserEmail              = request.UserEmail
            };

            Logger.LogInformation($"Generated Entitlements Response: {JsonConvert.SerializeObject(response)}");

            return(Json(response));
        }
Ejemplo n.º 2
0
        public void ShouldCallIfDisabled()
        {
            // We should still get an OK Result when entitlements are checking
            // So we at least hit the entitlements service everytime
            var response = new EntitlementResponseModel {
                Feature = "test-feature", UserUid = "test-uuid", UserEmail = "test-email", OrganizationIdentifier = "test-org", IsEntitled = true
            };

            _mockConfiguration
            .Setup(m => m.GetValueBool(It.Is <string>(s => s == ConfigConstants.ENABLE_ENTITLEMENTS_CONFIG_KEY), It.IsAny <bool>()))
            .Returns(false);

            _mockWebRequest.Setup(m => m.ExecuteRequest <EntitlementResponseModel>(It.IsAny <string>(),
                                                                                   It.IsAny <Stream>(),
                                                                                   It.IsAny <IHeaderDictionary>(),
                                                                                   It.Is <HttpMethod>(m => m == HttpMethod.Post),
                                                                                   It.IsAny <int?>(),
                                                                                   It.IsAny <int>(),
                                                                                   It.IsAny <bool>()))
            .Returns(Task.FromResult(response));

            var proxy  = new EntitlementProxy(_mockWebRequest.Object, _mockConfiguration.Object, _loggerFactory, _dataCache, _mockServiceResolution.Object);
            var result = proxy.IsEntitled(new EntitlementRequestModel
            {
                Feature = "test-feature", Sku = "test-sku", OrganizationIdentifier = "test-org", UserUid = "test-uuid", UserEmail = "test-email"
            }).Result;

            result.Should().NotBeNull();
            result.IsEntitled.Should().BeTrue();
            result.UserUid.Should().Be("test-uuid");
            result.UserEmail.Should().Be("test-email");
            result.Feature.Should().Be("test-feature");
            result.Sku.Should().Be("test-sku");
            result.OrganizationIdentifier.Should().Be("test-org");
        }
Ejemplo n.º 3
0
        public IActionResult GetMockEntitlement([FromBody] EntitlementRequestModel request)
        {
            Logger.LogInformation($"{nameof(GetMockEntitlement)}: UserUid={request.UserUid}");

            var response = new EntitlementResponseModel
            {
                Feature                = request.Feature,
                Sku                    = request.Sku,
                IsEntitled             = true,
                OrganizationIdentifier = request.OrganizationIdentifier,
                UserUid                = request.UserUid,
                UserEmail              = request.UserEmail
            };

            return(Json(response));
        }
Ejemplo n.º 4
0
        public void ShouldCallIfEnabled()
        {
            var response = new EntitlementResponseModel();

            _mockConfiguration
            .Setup(m => m.GetValueBool(It.Is <string>(s => s == ConfigConstants.ENABLE_ENTITLEMENTS_CONFIG_KEY), It.IsAny <bool>()))
            .Returns(true);

            _mockWebRequest.Setup(m => m.ExecuteRequest <EntitlementResponseModel>(It.IsAny <string>(),
                                                                                   It.IsAny <Stream>(),
                                                                                   It.IsAny <IHeaderDictionary>(),
                                                                                   It.Is <HttpMethod>(m => m == HttpMethod.Post),
                                                                                   It.IsAny <int?>(),
                                                                                   It.IsAny <int>(),
                                                                                   It.IsAny <bool>()))
            .Returns(Task.FromResult(response));

            var proxy  = new EntitlementProxy(_mockWebRequest.Object, _mockConfiguration.Object, _loggerFactory, _dataCache, _mockServiceResolution.Object);
            var result = proxy.IsEntitled(new EntitlementRequestModel
            {
                Feature = "test-feature",
                Sku     = "test-sku",
                OrganizationIdentifier = "test-org",
                UserUid   = "test-uuid",
                UserEmail = "test-email"
            }).Result;

            result.Should().NotBeNull();

            _mockWebRequest.Verify(m => m.ExecuteRequest <EntitlementResponseModel>(It.IsAny <string>(),
                                                                                    It.IsAny <Stream>(),
                                                                                    It.IsAny <IHeaderDictionary>(),
                                                                                    It.Is <HttpMethod>(m => m == HttpMethod.Post),
                                                                                    It.IsAny <int?>(),
                                                                                    It.IsAny <int>(),
                                                                                    It.IsAny <bool>()),
                                   Times.Once);
        }