protected override void Act()
                {
                    var oauthAccessTokenClient = new OAuthTokenClient();
                    var input = new[] { oauthAccessTokenClient };

                    Result = ApiClientDetails.Create(input);
                }
                protected override void Act()
                {
                    var input = new List <OAuthTokenClient> {
                        TokenClient
                    };

                    Result = ApiClientDetails.Create(input);
                }
                protected override void Act()
                {
                    var input = new[]
                    {
                        Record1,
                        Record2,
                        Record3
                    };

                    Result = ApiClientDetails.Create(input);
                }
Exemplo n.º 4
0
            protected override void Act()
            {
                // Execute code under test
                var validator = new CachingOAuthTokenValidatorDecorator(
                    _decoratedValidator,
                    _cacheProvider,
                    _configValueProvider);

                _actualDetails = validator.GetClientDetailsForTokenAsync(_suppliedApiToken)
                                 .GetResultSafely();
            }
 public static void AssertIsDefaultObject(ApiClientDetails input)
 {
     input.ShouldSatisfyAllConditions(
         () => input.ApiKey.ShouldBeNull(),
         () => input.ApplicationId.ShouldBe(0),
         () => input.ClaimSetName.ShouldBeNull(),
         () => input.EducationOrganizationIds.ShouldBeEmpty(),
         () => input.IsSandboxClient.ShouldBeFalse(),
         () => input.IsTokenValid.ShouldBeFalse(),
         () => input.NamespacePrefixes.ShouldBeEmpty(),
         () => input.Profiles.ShouldBeEmpty(),
         () => input.StudentIdentificationSystemDescriptor.ShouldBeNull()
         );
 }
            /// <summary>
            /// Prepares the state of the scenario (creating stubs, test data, etc.).
            /// </summary>
            protected override void Arrange()
            {
                Supplied(Guid.NewGuid());

                A.CallTo(() =>
                         Given <IGetEntityById <Student> >().GetByIdAsync(Supplied <Guid>(), A <CancellationToken> ._))
                .Returns(Task.FromResult(Supplied(new Student())));

                A.CallTo(() =>
                         Given <IAuthorizationContextProvider>().GetAction())
                .Returns("Action");

                A.CallTo(() =>
                         Given <IAuthorizationContextProvider>().GetResourceUris())
                .Returns(new[] { "Resource" });

                var claimsIdentityProvider = new ClaimsIdentityProvider(
                    new ApiKeyContextProvider(new CallContextStorage()),
                    new StubSecurityRepository());

                var apiClientDetails = new ApiClientDetails
                {
                    ApiKey = Guid.NewGuid()
                             .ToString("n"),
                    ApplicationId     = 999,
                    ClaimSetName      = "SomeClaimSet",
                    NamespacePrefixes = new List <string> {
                        "Namespace"
                    },
                    EducationOrganizationIds = new List <int>
                    {
                        123,
                        234
                    },
                    OwnershipTokenIds = new List <short?> {
                        1
                    }
                };

                var claimsIdentity = claimsIdentityProvider.GetClaimsIdentity(
                    apiClientDetails.EducationOrganizationIds,
                    apiClientDetails.ClaimSetName,
                    apiClientDetails.NamespacePrefixes,
                    apiClientDetails.Profiles.ToList(),
                    apiClientDetails.OwnershipTokenIds.ToList());

                ClaimsPrincipal.ClaimsPrincipalSelector = () => new ClaimsPrincipal(claimsIdentity);
            }
        public async Task When_requesting_API_client_details_that_are_NOT_already_cached_for_a_token_retrieve_and_cache_the_details()
        {
            // Arrange
            var suppliedTokenString      = Guid.NewGuid().ToString("n");
            var suppliedApiClientDetails = new ApiClientDetails()
            {
                ExpiresUtc = DateTime.UtcNow.AddMinutes(30)
            };

            var cacheKeyProvider = A.Fake <IApiClientDetailsCacheKeyProvider>();

            A.CallTo(() => cacheKeyProvider.GetCacheKey(suppliedTokenString)).Returns("theCacheKey");

            var    cacheProvider = A.Fake <ICacheProvider>();
            object outObject;

            A.CallTo(() => cacheProvider.TryGetCachedObject("theCacheKey", out outObject))
            .Returns(false);

            var apiClientDetailsProvider = A.Fake <IApiClientDetailsProvider>();

            A.CallTo(() => apiClientDetailsProvider.GetClientDetailsForTokenAsync(suppliedTokenString))
            .Returns(suppliedApiClientDetails);

            // Act
            CachingApiClientDetailsProviderDecorator decorator = new(apiClientDetailsProvider, cacheProvider, cacheKeyProvider);
            var actualApiClientDetails = await decorator.GetClientDetailsForTokenAsync(suppliedTokenString);

            // Assert
            decorator.ShouldSatisfyAllConditions(
                () => actualApiClientDetails.ShouldBeSameAs(suppliedApiClientDetails),

                // Should call through to underlying store
                () => A.CallTo(() => apiClientDetailsProvider.GetClientDetailsForTokenAsync(suppliedTokenString))
                .MustHaveHappened(),

                // Should insert a new cache entry with expiration date 15 minutes after the expiration of the token
                () => A.CallTo(() => cacheProvider.Insert("theCacheKey", suppliedApiClientDetails,
                                                          A <DateTime> .That.Matches(expirationUtc => expirationUtc > suppliedApiClientDetails.ExpiresUtc.AddMinutes(14) && expirationUtc < suppliedApiClientDetails.ExpiresUtc.AddMinutes(16)),
                                                          TimeSpan.Zero))
                .MustHaveHappened()
                );
        }
Exemplo n.º 8
0
            protected override void Arrange()
            {
                // Initialize dependencies

                // Create details for an invalid token (no API key assigned)
                _suppliedInvalidClientDetails = new ApiClientDetails();

                _decoratedValidator = Stub <IOAuthTokenValidator>();

                _decoratedValidator
                .Stub(x => x.GetClientDetailsForTokenAsync(_suppliedInvalidApiToken))
                .Return(Task.FromResult(_suppliedInvalidClientDetails));

                _cacheProvider = Stub <ICacheProvider>();

                // Mock config file to return duration
                _configValueProvider = Stub <IConfigValueProvider>();

                _configValueProvider.Stub(x => x.GetValue(Arg <string> .Is.Anything))
                .Return(_suppliedDurationMinutes.ToString());
            }
Exemplo n.º 9
0
            protected override void Arrange()
            {
                // Initialize dependencies
                _suppliedClientDetails = new ApiClientDetails
                {
                    ApiKey = "resolvedApiKey"
                };

                _decoratedValidator = Stub <IOAuthTokenValidator>();

                A.CallTo(() => _decoratedValidator.GetClientDetailsForTokenAsync(_suppliedApiToken))
                .Returns(Task.FromResult(_suppliedClientDetails));

                _cacheProvider = Stub <ICacheProvider>();
                _apiSettings   = new ApiSettings {
                    Engine = ApiConfigurationConstants.SqlServer, Mode = ApiConfigurationConstants.Sandbox
                };
                _configuration = Stub <IConfigurationRoot>();

                A.CallTo(() => _configuration.GetSection("BearerTokenTimeoutMinutes").Value)
                .Returns(_suppliedDurationMinutes.ToString());
            }
Exemplo n.º 10
0
            protected override void Arrange()
            {
                // Initialize dependencies
                _suppliedCachedClientDetails = new ApiClientDetails();

                _decoratedValidator = Stub <IOAuthTokenValidator>();

                // Fake the cache to return the details
                _cacheProvider = Stub <ICacheProvider>();
                object outobject = _suppliedCachedClientDetails;

                A.CallTo(() => _cacheProvider.TryGetCachedObject(A <string> ._, out outobject)).Returns(true);

                // Mock config file to return duration
                _apiSettings = new ApiSettings {
                    Engine = ApiConfigurationConstants.SqlServer, Mode = ApiConfigurationConstants.Sandbox
                };
                _configuration = Stub <IConfigurationRoot>();

                A.CallTo(() => _configuration.GetSection("BearerTokenTimeoutMinutes").Value)
                .Returns(_suppliedDurationMinutes.ToString());
            }
Exemplo n.º 11
0
            protected override void Arrange()
            {
                // Initialize dependencies

                // Create details for an invalid token (no API key assigned)
                _suppliedInvalidClientDetails = new ApiClientDetails();

                _decoratedValidator = Stub <IOAuthTokenValidator>();

                A.CallTo(() => _decoratedValidator.GetClientDetailsForTokenAsync(_suppliedInvalidApiToken))
                .Returns(Task.FromResult(_suppliedInvalidClientDetails));

                _cacheProvider = Stub <ICacheProvider>();

                // Mock config file to return duration
                _apiSettings = new ApiSettings {
                    Engine = ApiConfigurationConstants.SqlServer, Mode = ApiConfigurationConstants.Sandbox
                };
                _configuration = Stub <IConfigurationRoot>();

                A.CallTo(() => _configuration.GetSection("BearerTokenTimeoutMinutes").Value)
                .Returns(_suppliedDurationMinutes.ToString());
            }
        public async Task When_requesting_API_client_details_that_are_already_cached_for_a_token_should_return_the_cached_instance()
        {
            // Arrange
            var suppliedTokenString      = Guid.NewGuid().ToString("n");
            var suppliedApiClientDetails = new ApiClientDetails();

            var apiClientDetailsProvider = A.Fake <IApiClientDetailsProvider>();

            var cacheKeyProvider = A.Fake <IApiClientDetailsCacheKeyProvider>();

            A.CallTo(() => cacheKeyProvider.GetCacheKey(suppliedTokenString)).Returns("theCacheKey");

            var    cacheProvider = A.Fake <ICacheProvider>();
            object outObject;

            A.CallTo(() => cacheProvider.TryGetCachedObject("theCacheKey", out outObject))
            .Returns(true)
            .AssignsOutAndRefParameters(suppliedApiClientDetails);

            // Act
            CachingApiClientDetailsProviderDecorator decorator = new(apiClientDetailsProvider, cacheProvider, cacheKeyProvider);
            var actualApiClientDetails = await decorator.GetClientDetailsForTokenAsync(suppliedTokenString);

            // Assert
            decorator.ShouldSatisfyAllConditions(
                () => actualApiClientDetails.ShouldBeSameAs(suppliedApiClientDetails, "Cached item was not returned."),

                // Should not call through to underlying store
                () => A.CallTo(() => apiClientDetailsProvider.GetClientDetailsForTokenAsync(suppliedTokenString))
                .MustNotHaveHappened(),

                // Should not try to insert a new cache entry
                () => A.CallTo(() => cacheProvider.Insert(A <string> .Ignored, A <object> .Ignored, A <DateTime> .Ignored, A <TimeSpan> .Ignored))
                .MustNotHaveHappened()
                );
        }
Exemplo n.º 13
0
            protected override void Arrange()
            {
                // Initialize dependencies
                _suppliedCachedClientDetails = new ApiClientDetails();

                _decoratedValidator = Stub <IOAuthTokenValidator>();

                // Fake the cache to return the details
                _cacheProvider = Stub <ICacheProvider>();

                _cacheProvider.Stub(
                    x =>
                    x.TryGetCachedObject(
                        Arg <string> .Is.Anything,
                        out Arg <object> .Out(_suppliedCachedClientDetails)
                        .Dummy))
                .Return(true);

                // Mock config file to return duration
                _configValueProvider = Stub <IConfigValueProvider>();

                _configValueProvider.Stub(x => x.GetValue(Arg <string> .Is.Anything))
                .Return(_suppliedDurationMinutes.ToString());
            }
Exemplo n.º 14
0
 protected void Act(string token)
 {
     _response = _systemUnderTest.GetClientDetailsForTokenAsync(token).Result;
 }
Exemplo n.º 15
0
            /// <summary>
            /// Prepares the state of the scenario (creating stubs, test data, etc.).
            /// </summary>
            protected override void Arrange()
            {
                Given <IEdFiAuthorizationProvider>(
                    new FakeAuthorizationProvider(
                        SuppliedFilterName,
                        SuppliedParameterName,
                        SuppliedParameterValue));

                var suppliedFilterText = $"TheField = :{SuppliedParameterName}";

                A.CallTo(() =>
                         Given <INHibernateFilterTextProvider>()
                         .TryGetHqlFilterText(
                             A <Type> .Ignored,
                             A <string> .Ignored,
                             out suppliedFilterText))
                .Returns(true);

                Supplied("ResourceUriValue", "uri://some-value");

                A.CallTo(() =>
                         Given <IResourceClaimUriProvider>()
                         .GetResourceClaimUris(A <Resource> .Ignored))
                .Returns(new[] { Supplied <string>("ResourceUriValue") });

                var claimsIdentityProvider = new ClaimsIdentityProvider(
                    new ApiKeyContextProvider(new CallContextStorage()),
                    new StubSecurityRepository());

                var apiClientDetails = new ApiClientDetails
                {
                    ApiKey = Guid.NewGuid()
                             .ToString("n"),
                    ApplicationId     = 999,
                    ClaimSetName      = "SomeClaimSet",
                    NamespacePrefixes = new List <string> {
                        "Namespace"
                    },
                    EducationOrganizationIds = new List <int>
                    {
                        123,
                        234
                    }
                };

                var claimsIdentity = claimsIdentityProvider.GetClaimsIdentity(
                    apiClientDetails.EducationOrganizationIds,
                    apiClientDetails.ClaimSetName,
                    apiClientDetails.NamespacePrefixes,
                    apiClientDetails.Profiles.ToList());

                _expectedClaimsPrincipal = new ClaimsPrincipal(claimsIdentity);

                ClaimsPrincipal.ClaimsPrincipalSelector = () => _expectedClaimsPrincipal;

                Resource resource = CreateStudentResource();

                // Create the builder context
                _hqlBuilderContext = new HqlBuilderContext(
                    new StringBuilder(),
                    new StringBuilder(),
                    new StringBuilder(),
                    new StringBuilder(),
                    new Dictionary <string, object>(),
                    null,
                    0,
                    new Dictionary <string, CompositeSpecificationParameter>(),
                    new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase),
                    new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase),
                    new AliasGenerator());

                // Create the processor context
                _processorContext = new CompositeDefinitionProcessorContext(
                    null,
                    null,
                    new XElement("BaseResource"),
                    resource,
                    null,
                    null,
                    null,
                    int.MinValue,
                    null);
            }
            /// <summary>
            /// Prepares the state of the scenario (creating stubs, test data, etc.).
            /// </summary>
            protected override void Arrange()
            {
                Given <IAuthorizationFilteringProvider>(
                    new FakeAuthorizationFilteringProvider(
                        SuppliedFilterName,
                        SuppliedParameterName,
                        SuppliedParameterValue));

                var suppliedFilterText = $"TheField = :{SuppliedParameterName}";

                A.CallTo(() =>
                         Given <IEducationOrganizationIdNamesProvider>()
                         .GetAllNames())
                .Returns(Array.Empty <string>());

                AuthorizationFilterDefinition ignored;

                A.CallTo(
                    () => Given <IAuthorizationFilterDefinitionProvider>()
                    .TryGetAuthorizationFilterDefinition(SuppliedFilterName, out ignored))
                .Returns(true)
                .AssignsOutAndRefParameters(
                    new AuthorizationFilterDefinition(
                        SuppliedFilterName,
                        // This is how the HQL filter text is now obtained (with elimination of the INHibernateFilterTextProvider)
                        suppliedFilterText,
                        (criteria, junction, arg3, arg4) => { },
                        (ctx1, ctx2) => null,
                        (t, props) => false));

                Supplied("ResourceUriValue", "uri://some-value");

                A.CallTo(() =>
                         Given <IResourceClaimUriProvider>()
                         .GetResourceClaimUris(A <Resource> .Ignored))
                .Returns(new[] { Supplied <string>("ResourceUriValue") });

                var claimsIdentityProvider = new ClaimsIdentityProvider(
                    new ApiKeyContextProvider(new CallContextStorage()),
                    new StubSecurityRepository());

                var apiClientDetails = new ApiClientDetails
                {
                    ApiKey = Guid.NewGuid()
                             .ToString("n"),
                    ApplicationId     = 999,
                    ClaimSetName      = "SomeClaimSet",
                    NamespacePrefixes = new List <string> {
                        "Namespace"
                    },
                    EducationOrganizationIds = new[] { 123, 234 },
                    OwnershipTokenIds        = new List <short?> {
                        1
                    }
                };

                var claimsIdentity = claimsIdentityProvider.GetClaimsIdentity(
                    apiClientDetails.EducationOrganizationIds,
                    apiClientDetails.ClaimSetName,
                    apiClientDetails.NamespacePrefixes,
                    apiClientDetails.Profiles.ToList(),
                    apiClientDetails.OwnershipTokenIds.ToList());

                _expectedClaimsPrincipal = new ClaimsPrincipal(claimsIdentity);

                ClaimsPrincipal.ClaimsPrincipalSelector = () => _expectedClaimsPrincipal;

                Resource resource = CreateStudentResource();

                // Create the builder context
                _hqlBuilderContext = new HqlBuilderContext(
                    new StringBuilder(),
                    new StringBuilder(),
                    new StringBuilder(),
                    new StringBuilder(),
                    new Dictionary <string, object>(),
                    null,
                    0,
                    new Dictionary <string, CompositeSpecificationParameter>(),
                    new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase),
                    new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase),
                    new AliasGenerator());

                // Create the processor context
                _processorContext = new CompositeDefinitionProcessorContext(
                    null,
                    null,
                    new XElement("BaseResource"),
                    resource,
                    null,
                    null,
                    null,
                    int.MinValue,
                    null);
            }
Exemplo n.º 17
0
 protected override void Act()
 {
     var _ = ApiClientDetails.Create(null);
 }
Exemplo n.º 18
0
            protected override void Act()
            {
                var input = new OAuthTokenClient[0];

                Result = ApiClientDetails.Create(input);
            }