Exemplo n.º 1
0
        public async Task AutoConfigureGuildAsync_GuildContainsAnyClaimMappings_DoesNotCreateClaimMappings()
        {
            var testContext = new AutoConfigureGuildAsyncTestContext()
            {
                AnyClaimMappings = true
            };

            using (testContext)
            {
                testContext.Setup();

                var uut = testContext.CreateInstance <AuthorizationService>();

                await uut.AutoConfigureGuildAsync(testContext.Get <IGuild>(), testContext.CancellationToken);

                testContext.ClaimMappingRepositoryShouldHaveReceivedAnyAsync();

                testContext.GetMock <ISelfUserProvider>()
                .Invocations.ShouldBeEmpty();

                testContext.GetMock <IUserService>()
                .Invocations.ShouldBeEmpty();

                testContext.GetMock <IClaimMappingRepository>()
                .ShouldNotHaveReceived(x => x.BeginCreateTransactionAsync());

                testContext.GetMock <IClaimMappingRepository>()
                .ShouldNotHaveReceived(x => x.CreateAsync(It.IsAny <ClaimMappingCreationData>()));
            }
        }
Exemplo n.º 2
0
        public async Task AutoConfigureGuildAsync_GuildContainsNoClaimMappings_TracksSelfUser()
        {
            var testContext = new AutoConfigureGuildAsyncTestContext();

            using (testContext)
            {
                testContext.Setup();

                var uut = testContext.CreateInstance <AuthorizationService>();

                await uut.AutoConfigureGuildAsync(testContext.Get <IGuild>(), testContext.CancellationToken);

                testContext.ClaimMappingRepositoryShouldHaveReceivedAnyAsync();

                testContext.GetMock <IUserService>()
                .ShouldHaveReceived(x => x.TrackUserAsync(testContext.Get <IGuild>(), testContext.SelfUserId), Times.Once());
            }
        }
Exemplo n.º 3
0
        public async Task AutoConfigureGuildAsync_GuildContainsNoClaimMappings_CreatesClaimMappingsForAdministratorRoles()
        {
            var testContext = new AutoConfigureGuildAsyncTestContext();

            using (testContext)
            {
                testContext.Setup();

                var sequence = new MockSequence();

                var mockClaimMappingRepository = testContext.GetMock <IClaimMappingRepository>();

                mockClaimMappingRepository
                .InSequence(sequence)
                .Setup(x => x.BeginCreateTransactionAsync())
                .ReturnsAsync(testContext.MockClaimMappingCreateTransaction.Object);

                mockClaimMappingRepository
                .InSequence(sequence)
                .Setup(x => x.CreateAsync(It.IsAny <ClaimMappingCreationData>()))
                .ReturnsAsync(0);

                testContext.MockClaimMappingCreateTransaction
                .InSequence(sequence)
                .Setup(x => x.Commit());

                var uut = testContext.CreateInstance <AuthorizationService>();

                await uut.AutoConfigureGuildAsync(testContext.Get <IGuild>(), testContext.CancellationToken);

                testContext.ClaimMappingRepositoryShouldHaveReceivedAnyAsync();

                testContext.GetMock <IClaimMappingRepository>()
                .ShouldHaveReceived(x => x.BeginCreateTransactionAsync(), Times.Once());

                var actualClaimMappings = testContext.GetMock <IClaimMappingRepository>()
                                          .Invocations
                                          .Where(x => x.Method.Name == nameof(IClaimMappingRepository.CreateAsync))
                                          .Select(x => x.Arguments[0] as ClaimMappingCreationData)
                                          .ToArray();

                foreach (var actualClaimMapping in actualClaimMappings)
                {
                    actualClaimMapping.CreatedById.ShouldBe(testContext.SelfUserId);
                    actualClaimMapping.GuildId.ShouldBe(testContext.GuildId);
                    actualClaimMapping.RoleId.ShouldNotBeNull();
                    actualClaimMapping.Type.ShouldBe(ClaimMappingType.Granted);
                    actualClaimMapping.UserId.ShouldBeNull();
                }

                var expectedClaimMappings = Enum.GetValues(typeof(AuthorizationClaim)).Cast <AuthorizationClaim>()
                                            .SelectMany(x => testContext.AdministratorRoleIds
                                                        .Select(y => (claim: x, roleId: y)))
                                            .ToArray();

                actualClaimMappings
                .Select(x => (claim: x.Claim, roleId: x.RoleId.Value))
                .ShouldBe(expectedClaimMappings);

                testContext.MockClaimMappingCreateTransaction
                .ShouldHaveReceived(x => x.Commit(), Times.Once());
            }
        }