Exemplo n.º 1
0
        public async Task IgnoresAdminCommandsFromNonAdmin()
        {
            // Arrange
            BotMock
            .Setup(b => b.GetChatAdministratorsAsync(It.IsAny <ChatId>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(Array.Empty <ChatMember>());
            var builder = new BotFrameworkBuilder(BotMock.Object);
            var command = new AdminTestCommand();

            builder.AddCommand <AdminTestCommand>(command);
            var bot = (BotFramework)builder.Build();

            var update = new Update
            {
                Message = new Message
                {
                    Text = "/admin",
                    From = new User {
                        Id = 123
                    },
                    Chat = new Chat {
                        Id = 456
                    }
                },
            };

            // Act
            await bot.HandleUpdate(update);

            // Assert
            Assert.False(command.Called);
        }
Exemplo n.º 2
0
        public async Task HandlesAdminCommandsFromAdmin()
        {
            var chat = new Chat {
                Id = 456
            };

            // Arrange
            BotMock
            .Setup(b => b.GetChatAdministratorsAsync(
                       It.Is <ChatId>(c => c.Identifier == chat.Id),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync((ChatId id, CancellationToken token) => {
                return(new[]
                {
                    new ChatMember
                    {
                        User = new User {
                            Id = 123
                        }
                    }
                });
            });
            var builder = new BotFrameworkBuilder(BotMock.Object);
            var command = new AdminTestCommand();

            builder.AddCommand <AdminTestCommand>(command);
            var bot = (BotFramework)builder.Build();

            var update = new Update
            {
                Message = new Message
                {
                    Text = "/admin",
                    From = new User {
                        Id = 123
                    },
                    Chat = chat
                },
            };

            // Act
            await bot.HandleUpdate(update);

            // Assert
            Assert.True(command.Called);
        }