Пример #1
0
        private async Task SetRostersFromRolesForGoogleSheetsWithBadUrlFails(
            GoogleSheetsType type, Func <string, Task> setRosters)
        {
            Mock <IGoogleSheetsGenerator> mockGenerator = new Mock <IGoogleSheetsGenerator>();

            mockGenerator
            .Setup(generator => generator.TryUpdateRosters(It.IsAny <IByRoleTeamManager>(), It.IsAny <Uri>()))
            .Returns(Task.FromResult <IResult <string> >(new SuccessResult <string>(string.Empty)));

            Mock <IGoogleSheetsGeneratorFactory> mockFactory = new Mock <IGoogleSheetsGeneratorFactory>();

            mockFactory
            .Setup(factory => factory.Create(type))
            .Returns(mockGenerator.Object);

            this.InitializeHandler(googleSheetsGeneratorFactory: mockFactory.Object);

            using (BotConfigurationContext context = this.botConfigurationfactory.Create())
                using (DatabaseAction action = new DatabaseAction(context))
                {
                    await action.SetTeamRolePrefixAsync(DefaultGuildId, TeamRolePrefix);
                }

            await setRosters("this URL does not parse");

            this.MessageStore.VerifyChannelMessages(
                "The link to the Google Sheet wasn't understandable. Be sure to copy the full URL from the address bar.");
            mockFactory.Verify(factory => factory.Create(It.IsAny <GoogleSheetsType>()), Times.Never);
        }
Пример #2
0
        private async Task SetRostersFromRolesForGoogleSheetsSucceeds(
            GoogleSheetsType type, Func <string, Task> setRosters)
        {
            Mock <IGoogleSheetsGenerator> mockGenerator = new Mock <IGoogleSheetsGenerator>();

            mockGenerator
            .Setup(generator => generator.TryUpdateRosters(It.IsAny <IByRoleTeamManager>(), It.IsAny <Uri>()))
            .Returns(Task.FromResult <IResult <string> >(new SuccessResult <string>(string.Empty)))
            .Verifiable();

            Mock <IGoogleSheetsGeneratorFactory> mockFactory = new Mock <IGoogleSheetsGeneratorFactory>();

            mockFactory
            .Setup(factory => factory.Create(type))
            .Returns(mockGenerator.Object);

            this.InitializeHandler(googleSheetsGeneratorFactory: mockFactory.Object);

            using (BotConfigurationContext context = this.botConfigurationfactory.Create())
                using (DatabaseAction action = new DatabaseAction(context))
                {
                    await action.SetTeamRolePrefixAsync(DefaultGuildId, TeamRolePrefix);
                }

            await setRosters("http://localhost/sheetsUrl");

            mockFactory.Verify();
            this.MessageStore.VerifyChannelMessages("Rosters updated.");
        }
Пример #3
0
 public IGoogleSheetsGenerator Create(GoogleSheetsType sheetsType)
 {
     return(sheetsType switch
     {
         GoogleSheetsType.UCSD => new UCSDGoogleSheetsGenerator(this.SheetsApi),
         GoogleSheetsType.TJ => new TJSheetsGenerator(this.SheetsApi),
         _ => throw new ArgumentException(
             $"Cannot create a generator for type {Enum.GetName(typeof(GoogleSheetsType), sheetsType)}"),
     });
        private async Task SetRostersFromRolesForSheets(string sheetsUrl, GoogleSheetsType type)
        {
            if (!(this.Context.Channel is IGuildChannel guildChannel))
            {
                return;
            }

            if (!Uri.TryCreate(sheetsUrl, UriKind.Absolute, out Uri sheetsUri))
            {
                await this.Context.Channel.SendMessageAsync(
                    "The link to the Google Sheet wasn't understandable. Be sure to copy the full URL from the address bar.");

                return;
            }

            Logger.Information($"User {this.Context.User.Id} attempting to export a {type.ToString()} scoresheet");

            // TODO: Figure out an limitation story (count as export? Separate DB field?)
            string teamRolePrefix;

            using (DatabaseAction action = this.DatabaseActionFactory.Create())
            {
                teamRolePrefix = await action.GetTeamRolePrefixAsync(this.Context.Guild.Id);
            }

            if (string.IsNullOrEmpty(teamRolePrefix))
            {
                await this.Context.Channel.SendMessageAsync(
                    "Couldn't export to the rosters sheet. This server is not using the team role prefix. Use !setTeamRolePrefix to set the prefix for role names to use for teams.");

                return;
            }

            IByRoleTeamManager     teamManager = new ByRoleTeamManager(guildChannel, teamRolePrefix);
            IGoogleSheetsGenerator generator   = this.GoogleSheetsGeneratorFactory.Create(type);
            IResult <string>       result      = await generator.TryUpdateRosters(teamManager, sheetsUri);

            if (!result.Success)
            {
                await this.Context.Channel.SendMessageAsync(result.ErrorMessage);

                return;
            }

            await this.Context.Channel.SendMessageAsync("Rosters updated.");
        }
Пример #5
0
        private async Task SetRostersFromRolesForGoogleSheetsWithoutByRoleTeamsFails(
            GoogleSheetsType type, Func <string, Task> setRosters)
        {
            Mock <IGoogleSheetsGenerator> mockGenerator = new Mock <IGoogleSheetsGenerator>();

            mockGenerator
            .Setup(generator => generator.TryUpdateRosters(It.IsAny <IByRoleTeamManager>(), It.IsAny <Uri>()))
            .Returns(Task.FromResult <IResult <string> >(new SuccessResult <string>(string.Empty)))
            .Verifiable();

            Mock <IGoogleSheetsGeneratorFactory> mockFactory = new Mock <IGoogleSheetsGeneratorFactory>();

            mockFactory
            .Setup(factory => factory.Create(type))
            .Returns(mockGenerator.Object);

            this.InitializeHandler(googleSheetsGeneratorFactory: mockFactory.Object);

            await setRosters("http://localhost/sheetsUrl");

            this.MessageStore.VerifyChannelMessages(
                "Couldn't export to the rosters sheet. This server is not using the team role prefix. Use !setTeamRolePrefix to set the prefix for role names to use for teams.");
            mockFactory.Verify(factory => factory.Create(It.IsAny <GoogleSheetsType>()), Times.Never);
        }