public void ReturnCollectionOfClaimsFilteredByAirline()
        {
            // Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCollectionOfClaimsFilteredByAirline));

            // Act, Assert
            using (var assertContext = new ClaimsDbContext(options))
            {
                var       myProfile     = new ClaimProfile();
                var       configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));
                IMapper   mapper        = new Mapper(configuration);
                var       claimDto      = new ClaimDto();
                var       claimDto2     = new ClaimDto();
                var       claimDto3     = new ClaimDto();
                IFormFile file          = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
                claimDto.BPImage  = file;
                claimDto2.BPImage = file;
                claimDto3.BPImage = file;
                claimDto.Airline  = "TestAir";
                claimDto2.Airline = "TestAir";
                claimDto3.Airline = "AirTest";
                var sut = new ClaimServices(assertContext, mapper);
                sut.CreateAsync(claimDto).GetAwaiter().GetResult();
                sut.CreateAsync(claimDto2).GetAwaiter().GetResult();
                sut.CreateAsync(claimDto3).GetAwaiter().GetResult();
                var results = sut.FilterByMultipleCriteriaAsync("TestAir", default, default, default).GetAwaiter().GetResult();
Ejemplo n.º 2
0
        public int GetRegistrationLimit(DiscordShardedClient client, SocketGuild guild)
        {
            var config = GetConfig();

            if (!config.Enabled)
            {
                return(int.MaxValue);
            }

            //Add legacy support
            var legacyUpgrade = Legacy.GetPremiumConfig(guild.Id);

            if (legacyUpgrade != null)
            {
                if (legacyUpgrade.IsPremium())
                {
                    return(int.MaxValue);
                }
            }

            var guildUpgrade = Database.Load <ClaimProfile>(ClaimProfile.DocumentName(guild.Id));

            if (guildUpgrade == null)
            {
                return(config.DefaultRegistrationLimit);
            }

            var patreonGuild = client.GetGuild(config.GuildId);
            var patreonUser  = patreonGuild?.GetUser(guildUpgrade.UserId);

            if (patreonUser == null)
            {
                return(config.DefaultRegistrationLimit);
            }

            var patreonRole = GetPremiumRole(patreonUser);

            if (patreonRole.Value == null)
            {
                return(config.DefaultRegistrationLimit);
            }

            return(patreonRole.Value.MaxRegistrationCount);
        }
        public void AddTheClaimToTheDBSetCorrectly()
        {
            // Arrange
            var options = TestUtilities.GetOptions(nameof(AddTheClaimToTheDBSetCorrectly));

            // Act, Assert
            using (var assertContext = new ClaimsDbContext(options))
            {
                var       myProfile     = new ClaimProfile();
                var       configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));
                IMapper   mapper        = new Mapper(configuration);
                var       claimDto      = new ClaimDto();
                IFormFile file          = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
                claimDto.BPImage = file;
                var sut        = new ClaimServices(assertContext, mapper);
                var testResult = sut.CreateAsync(claimDto).GetAwaiter().GetResult();

                Assert.IsTrue(assertContext.Claims.Count() == 1 && assertContext.Claims.Any(c => c.Id == testResult.Id));
            }
        }
        public void CreateNewInstanceOfClaimDto()
        {
            // Arrange
            var options = TestUtilities.GetOptions(nameof(CreateNewInstanceOfClaimDto));

            // Act, Assert
            using (var assertContext = new ClaimsDbContext(options))
            {
                var       myProfile     = new ClaimProfile();
                var       configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));
                IMapper   mapper        = new Mapper(configuration);
                var       claimDto      = new ClaimDto();
                IFormFile file          = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
                claimDto.BPImage = file;
                var sut        = new ClaimServices(assertContext, mapper);
                var testResult = sut.CreateAsync(claimDto).GetAwaiter().GetResult();

                Assert.IsInstanceOfType(testResult, typeof(ClaimDto));
            }
        }
Ejemplo n.º 5
0
        public void ReturnCollectionOfUpTo20NewestClaims()
        {
            // Arrange
            var options = TestUtilities.GetOptions(nameof(ReturnCollectionOfUpTo20NewestClaims));

            // Act, Assert
            using (var assertContext = new ClaimsDbContext(options))
            {
                var       myProfile     = new ClaimProfile();
                var       configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));
                IMapper   mapper        = new Mapper(configuration);
                IFormFile file          = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
                var       claimDto      = new ClaimDto();
                claimDto.BPImage = file;
                var claimDto2 = new ClaimDto();
                claimDto2.BPImage = file;
                var sut = new ClaimServices(assertContext, mapper);
                sut.CreateAsync(claimDto).GetAwaiter().GetResult();
                sut.CreateAsync(claimDto2).GetAwaiter().GetResult();
                var testResult = sut.Get20LatestClaimsAsync().GetAwaiter().GetResult();

                Assert.IsTrue(testResult.Count() == 2 && testResult[0].CreatedAt > testResult[1].CreatedAt);
            }
        }
Ejemplo n.º 6
0
        public async Task Claim(ShardedCommandContext context)
        {
            //Assumed context, claim is being applied to the server where it's being claimed in
            //TODO: Check if this fetches user from cache.
            var config = GetConfig();

            if (!config.Enabled)
            {
                return;
            }

            var patreonGuild = context.Client.GetGuild(config.GuildId);

            if (patreonGuild == null)
            {
                await context.Channel.SendMessageAsync("Unable to access patreon guild.");

                return;
            }

            var patreonUser = patreonGuild.GetUser(context.User.Id);

            if (patreonUser == null)
            {
                await context.Channel.SendMessageAsync($"You must join the premium server {config.ServerInvite} and get a patreon role {config.PageUrl} before being able to claim an upgrade.");

                return;
            }

            var currentRole = GetPremiumRole(patreonUser);

            if (currentRole.Value == null)
            {
                await context.Channel.SendMessageAsync($"You do not have a patreon role, you can receive one by becoming a patron at {config.PageUrl}");

                return;
            }

            var guildLicense = Database.Load <ClaimProfile>(ClaimProfile.DocumentName(context.Guild.Id));

            if (guildLicense != null)
            {
                if (guildLicense.UserId == context.User.Id)
                {
                    await context.Channel.SendMessageAsync("You've already claimed premium in this server.");

                    return;
                }
                else
                {
                    var guildClaimUser = patreonGuild.GetUser(guildLicense.UserId);
                    if (guildClaimUser == null)
                    {
                        //Delete the claim.
                        await context.Channel.SendMessageAsync($"An old upgrade by {MentionUtils.MentionUser(guildLicense.UserId)} was removed as they could not be found in the patreon server.");

                        Database.Remove <ClaimProfile>(ClaimProfile.DocumentName(guildLicense.GuildId));
                    }
                    else
                    {
                        var guildClaimUserRole = GetPremiumRole(guildClaimUser);
                        if (guildClaimUserRole.Value == null)
                        {
                            //User no longer is patron, delete claim.
                            await context.Channel.SendMessageAsync($"An old upgrade by {MentionUtils.MentionUser(guildLicense.UserId)} was removed as they no longer have a patreon role.");

                            Database.Remove <ClaimProfile>(ClaimProfile.DocumentName(guildLicense.GuildId));
                        }
                        else
                        {
                            if (guildClaimUserRole.Value.MaxRegistrationCount > currentRole.Value.MaxRegistrationCount)
                            {
                                //This is larger than the current that is trying to be redeemed, discard the one being redeemed.
                                await context.Channel.SendMessageAsync($"There is already a license redeemed with a higher user count ({guildClaimUserRole.Value.MaxRegistrationCount}) in this server. Your upgrade will not be applied.");

                                return;
                            }
                            else
                            {
                                await context.Channel.SendMessageAsync($"Another smaller upgrade was applied to this server, it has been replaced. The original license was for {guildClaimUserRole.Value.MaxRegistrationCount} users and was redeemed by {MentionUtils.MentionUser(guildLicense.UserId)}");

                                //Delete the smaller claim.
                                Database.Remove <ClaimProfile>(ClaimProfile.DocumentName(guildLicense.GuildId));
                            }
                        }
                    }
                }
            }

            var previousClaims = Database.Query <ClaimProfile>(x => x.UserId == context.User.Id);

            foreach (var claim in previousClaims)
            {
                Database.Remove <ClaimProfile>(ClaimProfile.DocumentName(claim.GuildId));
                await context.Channel.SendMessageAsync("You've already claimed a server, the old claim will be removed and applied to this server.");
            }

            var userLicense = new ClaimProfile
            {
                GuildId = context.Guild.Id,
                UserId  = context.User.Id
            };

            Database.Store(userLicense, ClaimProfile.DocumentName(context.Guild.Id));
            await context.Channel.SendMessageAsync($"The server has been upgraded to {currentRole.Value.MaxRegistrationCount} users");
        }