Example #1
0
        public async void SendActivitiesAsyncShouldSucceedAndNoActivityReturnedWithActivityTypeNotMessage()
        {
            const string testResponse          = "Test Response";
            var          facebookClientWrapper = new Mock <FacebookClientWrapper>(_testOptions);
            var          facebookAdapter       = new FacebookAdapter(facebookClientWrapper.Object);
            var          attachments           = new List <Attachment>();
            var          activity = new Activity
            {
                Type         = ActivityTypes.Trace,
                Text         = "Test text",
                Conversation = new ConversationAccount()
                {
                    Id = "Test id",
                },
                ChannelData = new FacebookMessage("recipientId", new Message(), "messagingtype"),
                Attachments = attachments,
            };

            Activity[]         activities = { activity };
            ResourceResponse[] responses  = null;

            attachments.Add(new Attachment("text/html", "http://contoso.com"));
            facebookClientWrapper.Setup(api => api.SendMessageAsync(It.IsAny <string>(), It.IsAny <FacebookMessage>(), It.IsAny <HttpMethod>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(testResponse));

            using (var turnContext = new TurnContext(facebookAdapter, activity))
            {
                responses = await facebookAdapter.SendActivitiesAsync(turnContext, activities, default);
            }

            Assert.True(responses.Length == 0);
        }
Example #2
0
        public async void ContinueConversationAsyncShouldFailWithNullLogic()
        {
            var facebookAdapter       = new FacebookAdapter(new Mock <FacebookClientWrapper>(_testOptions).Object);
            var conversationReference = new ConversationReference();

            await Assert.ThrowsAsync <ArgumentNullException>(async() => { await facebookAdapter.ContinueConversationAsync(conversationReference, null, default); });
        }
Example #3
0
        public async void SendActivitiesAsyncShouldPostToFacebookOnRequestThreadControl()
        {
            const string testResponse          = "Test Response";
            var          facebookClientWrapper = new Mock <FacebookClientWrapper>(_testOptions);
            var          facebookAdapter       = new FacebookAdapter(facebookClientWrapper.Object);
            var          activity = new Activity
            {
                Type         = ActivityTypes.Event,
                Text         = "Test text",
                Name         = "request_thread_control",
                Conversation = new ConversationAccount()
                {
                    Id = "Test id",
                },
                ChannelData = new FacebookMessage("recipientId", new Message(), "messagingtype"),
            };

            Activity[]         activities = { activity };
            ResourceResponse[] responses  = null;

            facebookClientWrapper.Setup(api => api.SendMessageAsync(It.IsAny <string>(), It.IsAny <FacebookMessage>(), It.IsAny <HttpMethod>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(testResponse));
            facebookClientWrapper.Setup(api => api.RequestThreadControlAsync(It.IsAny <string>(), It.IsAny <string>(), default)).Returns(Task.FromResult(true));

            using (var turnContext = new TurnContext(facebookAdapter, activity))
            {
                responses = await facebookAdapter.SendActivitiesAsync(turnContext, activities, default);
            }

            Assert.Equal(testResponse, responses[0].Id);
            facebookClientWrapper.Verify(api => api.RequestThreadControlAsync(It.IsAny <string>(), It.IsAny <string>(), default), Times.Once);
        }
        public async Task ProcessAsyncShouldThrowExceptionWithUnverifiedSignature()
        {
            var testOptionsVerifyEnabled = new FacebookAdapterOptions()
            {
                VerifyIncomingRequests = true
            };

            var facebookClientWrapper = new Mock <FacebookClientWrapper>(_testOptions);
            var facebookAdapter       = new FacebookAdapter(facebookClientWrapper.Object, testOptionsVerifyEnabled);
            var payload      = File.ReadAllText(Directory.GetCurrentDirectory() + @"/Files/Payload.json");
            var stream       = new MemoryStream(Encoding.UTF8.GetBytes(payload));
            var httpRequest  = new Mock <HttpRequest>();
            var httpResponse = new Mock <HttpResponse>();
            var bot          = new Mock <IBot>();

            httpRequest.SetupGet(req => req.Query[It.IsAny <string>()]).Returns("test");
            httpRequest.SetupGet(req => req.Body).Returns(stream);

            httpResponse.Setup(_ => _.Body.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .Callback((byte[] data, int offset, int length, CancellationToken token) =>
            {
                if (length > 0)
                {
                    var actual = Encoding.UTF8.GetString(data);
                }
            });

            await Assert.ThrowsAsync <AuthenticationException>(() => facebookAdapter.ProcessAsync(httpRequest.Object, httpResponse.Object, bot.Object, default(CancellationToken)));
        }
Example #5
0
        public async Task UpdateActivityAsyncShouldThrowNotImplementedException()
        {
            var facebookAdapter = new FacebookAdapter(new FacebookClientWrapper(_testOptions));
            var activity        = new Activity();

            using (var turnContext = new TurnContext(facebookAdapter, activity))
            {
                await Assert.ThrowsAsync <NotImplementedException>(() => facebookAdapter.UpdateActivityAsync(turnContext, activity, default));
            }
        }
Example #6
0
        public async void ContinueConversationAsyncShouldFailWithNullConversationReference()
        {
            var facebookAdapter = new FacebookAdapter(new Mock <FacebookClientWrapper>(_testOptions).Object);

            Task BotsLogic(ITurnContext turnContext, CancellationToken cancellationToken)
            {
                return(Task.CompletedTask);
            }

            await Assert.ThrowsAsync <ArgumentNullException>(async() => { await facebookAdapter.ContinueConversationAsync(null, BotsLogic, default); });
        }
        public async Task DeleteActivityAsyncShouldThrowNotImplementedException()
        {
            var facebookAdapter       = new FacebookAdapter(new FacebookClientWrapper(_testOptions), _adapterOptions);
            var activity              = new Activity();
            var conversationReference = new ConversationReference();

            using (var turnContext = new TurnContext(facebookAdapter, activity))
            {
                await Assert.ThrowsAsync <NotImplementedException>(() => facebookAdapter.DeleteActivityAsync(turnContext, conversationReference, default));
            }
        }
        public static void Main(string[] args)
        {
            IAdapter vkAdapter            = new VkAdapter(new Vk("Вася пупкин", 20, 25));
            IAdapter facebookAdapter      = new FacebookAdapter(new Facebook("Joseph", 150, 18));
            IAdapter odnoklassnikiAdapter = new OdnoklassnikiAdapter(new Odnoklassniki("Зинаида Ивановна", 2, 66));

            PrintData.PrintUserInfo(vkAdapter);
            PrintData.PrintUserInfo(facebookAdapter);
            PrintData.PrintUserInfo(odnoklassnikiAdapter);

            Console.ReadKey();
        }
Example #9
0
        public async void ContinueConversationAsyncShouldSucceed()
        {
            var callbackInvoked       = false;
            var facebookAdapter       = new FacebookAdapter(new Mock <FacebookClientWrapper>(_testOptions).Object);
            var conversationReference = new ConversationReference();

            Task BotsLogic(ITurnContext turnContext, CancellationToken cancellationToken)
            {
                callbackInvoked = true;
                return(Task.CompletedTask);
            }

            await facebookAdapter.ContinueConversationAsync(conversationReference, BotsLogic, default);

            Assert.True(callbackInvoked);
        }
Example #10
0
        public async void ProcessAsyncShouldVerifyWebhookOnHubModeSubscribe()
        {
            var facebookClientWrapper = new Mock <FacebookClientWrapper>(_testOptions);
            var facebookAdapter       = new FacebookAdapter(facebookClientWrapper.Object);
            var httpRequest           = new Mock <HttpRequest>();
            var httpResponse          = new Mock <HttpResponse>();
            var bot = new Mock <IBot>();

            facebookClientWrapper.Setup(api => api.VerifyWebhookAsync(It.IsAny <HttpRequest>(), It.IsAny <HttpResponse>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask);

            httpRequest.SetupGet(req => req.Query[It.IsAny <string>()]).Returns("subscribe");

            await facebookAdapter.ProcessAsync(httpRequest.Object, httpResponse.Object, bot.Object, default(CancellationToken));

            bot.Verify(b => b.OnTurnAsync(It.IsAny <TurnContext>(), It.IsAny <CancellationToken>()), Times.Never);
        }
        public async void ProcessAsyncShouldSucceedWithCorrectData()
        {
            var payload = File.ReadAllText(Directory.GetCurrentDirectory() + @"/Files/Payload.json");
            var facebookClientWrapper = new Mock <FacebookClientWrapper>(_testOptions);
            var facebookAdapter       = new FacebookAdapter(facebookClientWrapper.Object, _adapterOptions);
            var stream       = new MemoryStream(Encoding.UTF8.GetBytes(payload));
            var httpRequest  = new Mock <HttpRequest>();
            var httpResponse = new Mock <HttpResponse>();
            var bot          = new Mock <IBot>();

            httpRequest.SetupGet(req => req.Query[It.IsAny <string>()]).Returns("test");
            httpRequest.SetupGet(req => req.Body).Returns(stream);

            await facebookAdapter.ProcessAsync(httpRequest.Object, httpResponse.Object, bot.Object, default(CancellationToken));

            bot.Verify(b => b.OnTurnAsync(It.IsAny <TurnContext>(), It.IsAny <CancellationToken>()), Times.Once);
        }
Example #12
0
        public async void SendActivitiesAsyncShouldFailWithActivityTypeNotMessage()
        {
            var facebookAdapter = new FacebookAdapter(new Mock <FacebookClientWrapper>(_testOptions).Object);
            var activity        = new Activity
            {
                Type = ActivityTypes.Event,
            };

            Activity[] activities = { activity };

            using (var turnContext = new TurnContext(facebookAdapter, activity))
            {
                await Assert.ThrowsAsync <Exception>(async() =>
                {
                    await facebookAdapter.SendActivitiesAsync(turnContext, activities, default);
                });
            }
        }
 public FacebookController(FacebookAdapter adapter, IBot bot)
 {
     _adapter = adapter;
     _bot     = bot;
 }