public async Task RevokesAGivenConsent(NpgsqlConsentStore store, Consent consent)
            {
                // Given
                await store.UpdateAsync(consent);

                // When
                await store.RevokeAsync(consent.Subject, consent.ClientId);

                // Then
                var fromDb = await store.LoadAsync(consent.Subject, consent.ClientId);

                fromDb.ShouldBe(null);
            }
            public async Task InsertsConsentIfItDoesNotExist(NpgsqlConsentStore store, Consent consent)
            {
                // Given
                var before = await store.LoadAsync(consent.Subject, consent.ClientId);
                before.ShouldBe(null);

                // When
                await store.UpdateAsync(consent);

                // Then
                var fromDb = await store.LoadAsync(consent.Subject, consent.ClientId);

                fromDb.ShouldNotBe(null);
                fromDb.ClientId.ShouldBe(consent.ClientId);
                fromDb.Subject.ShouldBe(consent.Subject);
            }
        public Task UpdateConsentAsync(Client client, ClaimsPrincipal user, IEnumerable<string> scopes)
        {
            if (client.AllowRememberConsent)
            {
                var consent = new Consent
                {
                    ClientId = client.ClientId,
                    Subject = user.GetSubjectId(),
                    Scopes = string.Join(" ", scopes.OrderBy(s => s).ToArray())
                };

                _consents.Add(consent);
            }

            return Task.FromResult(0);
        }
            public async Task UpdatesTheExistingConsentIfItExists(NpgsqlConsentStore store, Consent consent, IEnumerable<string> newScopes)
            {
                // Given
                await store.UpdateAsync(consent);

                // When
                consent.Scopes = newScopes;
                await store.UpdateAsync(consent);

                // Then
                var fromDb = await store.LoadAsync(consent.Subject, consent.ClientId);

                fromDb.ShouldNotBe(null);
                fromDb.ClientId.ShouldBe(consent.ClientId);
                fromDb.Subject.ShouldBe(consent.Subject);
                fromDb.Scopes.All(newScopes.Contains).ShouldBe(true);
            }
        public Task UpdateAsync(Consent consent)
        {
            // makes a snapshot as a DB would
            consent.Scopes = consent.Scopes.ToArray();

            var query =
                from c in _consents
                where c.Subject == consent.Subject && c.ClientId == consent.ClientId
                select c;
            var item = query.SingleOrDefault();
            if (item != null)
            {
                item.Scopes = consent.Scopes;
            }
            else
            {
                _consents.Add(consent);
            }
            return Task.FromResult(0);
        }
        public async Task UpdateConsentAsync(Client client, ClaimsPrincipal user, IEnumerable<string> scopes)
        {
            if (client == null) throw new ArgumentNullException("client");
            if (user == null) throw new ArgumentNullException("user");

            if (client.AllowRememberConsent)
            {
                var subject = user.GetSubjectId();
                var clientId = client.ClientId;

                if (scopes != null && scopes.Any())
                {
                    var consent = new Consent
                    {
                        Subject = subject,
                        ClientId = clientId,
                        Scopes = scopes
                    };
                    await _store.UpdateAsync(consent);
                }
                else
                {
                    await _store.RevokeAsync(subject, clientId);
                }
            }
        }
            public async Task LoadsAllConsentsForASubject(NpgsqlConsentStore store, 
                                                          Consent consent1, 
                                                          Consent consent2)
            {
                // Given
                consent2.Subject = consent1.Subject;
                await store.UpdateAsync(consent1);
                await store.UpdateAsync(consent2);

                // When
                var fromDb = await store.LoadAllAsync(consent1.Subject);

                // Then
                fromDb.Count().ShouldBe(2);
                fromDb.ShouldContain(x => x.ClientId == consent1.ClientId);
                fromDb.ShouldContain(x => x.ClientId == consent2.ClientId);
                fromDb.ShouldContain(x => x.Scopes.All(y => consent1.Scopes.Contains(y)));
                fromDb.ShouldContain(x => x.Scopes.All(y => consent2.Scopes.Contains(y)));
            }
            public async Task LoadsOnlyTheConsentsGivenForTheSubject(NpgsqlConsentStore store,
                                                                    Consent consent1,
                                                                    Consent consent2)
            {
                // Given
                await store.UpdateAsync(consent1);
                await store.UpdateAsync(consent2);

                // When
                var fromDb = await store.LoadAllAsync(consent1.Subject);

                // Then
                fromDb.Count().ShouldBe(1);
                fromDb.Single().Subject.ShouldBe(consent1.Subject);
            }