public async Task SetResponseToCache()
        {
            _options.PdpCacheDuration = 60;
            var cacheEntry  = new TestCacheEntry();
            var mockedCache = CreateEmptyMockedCache();

            mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId)))
            .Returns(cacheEntry);

            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { requestedresource })
            };

            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);

            var result = await provider.GetPermissionsAsync(_userId, _application);

            Assert.Equal(pdpResponse.applicationId, ((PdpResponse)cacheEntry.Value).applicationId);
            Assert.Equal(pdpResponse.userId, ((PdpResponse)cacheEntry.Value).userId);
            Assert.Equal(pdpResponse.permissions, ((PdpResponse)cacheEntry.Value).permissions);
        }
        public async Task ApiKeyIsSetInHeader()
        {
            var mockedCache = CreateEmptyMockedCache();

            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { requestedresource })
            };

            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);
            var result      = await provider.GetPermissionsAsync(_userId, _application);

            Assert.Equal(_apiKey, mockHandler.Headers.GetValues(HeaderKeys.Apikey).FirstOrDefault());
        }
        public async Task GetCachedPdpResponse()
        {
            _options.PdpCacheDuration = 60;
            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { requestedresource })
            };

            var mockedCache = CreateMockedCache(BuildCacheKey(_userId), pdpResponse);
            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.NotFound, null);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);

            var result = await provider.GetPermissionsAsync(_userId, _application);

            Assert.Equal(pdpResponse, result);
        }
        public async Task ReturnsNullIfUserUnknown()
        {
            var mockedCache = CreateEmptyMockedCache();

            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { requestedresource })
            };

            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.NotFound, null);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);
            var result      = await provider.GetPermissionsAsync("otherUser", _application);

            Assert.Null(result);
            Assert.NotEmpty(_logger.LoggedMessages);
        }
        public async Task GetResponse()
        {
            var mockedCache = CreateEmptyMockedCache();

            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { requestedresource })
            };

            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);
            var result      = await provider.GetPermissionsAsync(_userId, _application);

            Assert.Equal(pdpResponse.applicationId, result.applicationId);
            Assert.Equal(pdpResponse.userId, result.userId);
            Assert.Equal(pdpResponse.permissions, result.permissions);
        }
        public async Task CacheDurationFromOptionsIsUsed()
        {
            _options.PdpCacheDuration = 60;
            var cacheEntry  = new TestCacheEntry();
            var mockedCache = CreateEmptyMockedCache();

            mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId)))
            .Returns(cacheEntry);

            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { requestedresource })
            };

            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);

            var result = await provider.GetPermissionsAsync(_userId, _application);

            Assert.True(cacheEntry.AbsoluteExpirationRelativeToNow.Value == new TimeSpan(0, _options.PdpCacheDuration, 0));
        }
        public async Task ShouldNotCacheResponseWithoutPermissions()
        {
            _options.PdpCacheDuration = 60;
            var cacheEntry  = new TestCacheEntry();
            var mockedCache = CreateEmptyMockedCache();

            mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId)))
            .Returns(cacheEntry);

            var pdpResponse = new PdpResponse
            {
                applicationId = _application,
                userId        = _userId,
                permissions   = new List <String>(new string[] { })
            };

            var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse);
            var provider    = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger);

            var result = await provider.GetPermissionsAsync(_userId, _application);

            mockedCache.Verify(c => c.CreateEntry(It.IsAny <object>()), Times.Never);
            Assert.Null(cacheEntry.Value);
        }