Exemple #1
0
        public async Task GetTokenWithSubstitution()
        {
            var options = new DbContextOptionsBuilder <WCADbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetTokenWithSubstitution))
                          .Options;

            var clock   = FakeClock.FromUtc(2019, 10, 1, 8, 0, 0);
            var user1Id = string.Empty;

            var serviceScopeFactory = new Mock <IServiceScopeFactory>();

            serviceScopeFactory.Setup(f => f.CreateScope()).Returns(() =>
            {
                var serviceScope = new Mock <IServiceScope>();
                serviceScope.Setup(s => s.ServiceProvider.GetService(It.IsAny <Type>())).Returns(new WCADbContextTransient(options));
                return(serviceScope.Object);
            });

            // Run the test against one instance of the context
            using (var context = new WCADbContextTransient(options))
            {
                var entityEntry1 = context.Users.Add(new WCAUser()
                {
                    Email = "user1@domain"
                });
                var entityEntry2 = context.Users.Add(new WCAUser()
                {
                    Email = "user2@domain"
                });
                await context.SaveChangesAsync();

                user1Id = entityEntry1.Entity.Id;
                var startTime = DateTime.UtcNow;

                context.ActionstepCredentialSubstitutions.Add(new ActionstepCredentialSubstitution()
                {
                    CreatedBy           = entityEntry1.Entity,
                    ForOwner            = entityEntry1.Entity,
                    SubstituteWithOwner = entityEntry2.Entity
                });
                context.SaveChanges();

                // Will be disposed of in implementation
                var service1 = new TokenSetRepository(new TestTelemetryLogger(), serviceScopeFactory.Object, clock);

                // Create tokens for each user
                await service1.AddOrUpdateTokenSet(new TokenSet("at", "testToken0", 3600, new Uri("https://uri/"), "org", "rt", Instant.FromUtc(2019, 10, 1, 8, 0), entityEntry1.Entity.Id));

                await service1.AddOrUpdateTokenSet(new TokenSet("at", "testToken1", 3600, new Uri("https://uri/"), "org", "rt", Instant.FromUtc(2019, 10, 1, 8, 0), entityEntry2.Entity.Id));
            }

            // Use a separate instance of the context to verify correct data was saved to database
            var service2         = new TokenSetRepository(new TestTelemetryLogger(), serviceScopeFactory.Object, clock);
            var substitutedToken = await service2.GetTokenSet(new TokenSetQuery(user1Id, "org"));

            // Should return second token which was substituted instead of first
            Assert.Equal("testToken1", substitutedToken?.TokenType);
        }
Exemple #2
0
        public async Task GetTokenReturnsNullIfNotFound()
        {
            var options = new DbContextOptionsBuilder <WCADbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetTokenReturnsNullIfNotFound))
                          .Options;

            var clock = FakeClock.FromUtc(2019, 10, 1, 8, 0, 0);

            var serviceScopeFactory = new Mock <IServiceScopeFactory>();

            serviceScopeFactory.Setup(f => f.CreateScope()).Returns(() =>
            {
                var serviceScope = new Mock <IServiceScope>();
                serviceScope.Setup(s => s.ServiceProvider.GetService(It.IsAny <Type>())).Returns(new WCADbContextTransient(options));
                return(serviceScope.Object);
            });

            var service = new TokenSetRepository(new TestTelemetryLogger(), serviceScopeFactory.Object, clock);
            var tokenShouldntBeFound = await service.GetTokenSet(new TokenSetQuery("User doesn't exist", "Org doesn't exist"));

            Assert.Null(tokenShouldntBeFound);
        }