public async Task Multicast_With201Ids_CallsApi2Times()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            string[] ids = new string[151];
            for (int i = 0; i < ids.Length; i++)
            {
                ids[i] = "id" + i;
            }

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(ids, new TextMessage("Test"));

            string postedData = @"{""to"":[""id0"",""id1"",""id2"",""id3"",""id4"",""id5"",""id6"",""id7"",""id8"",""id9"",""id10"",""id11"",""id12"",""id13"",""id14"",""id15"",""id16"",""id17"",""id18"",""id19"",""id20"",""id21"",""id22"",""id23"",""id24"",""id25"",""id26"",""id27"",""id28"",""id29"",""id30"",""id31"",""id32"",""id33"",""id34"",""id35"",""id36"",""id37"",""id38"",""id39"",""id40"",""id41"",""id42"",""id43"",""id44"",""id45"",""id46"",""id47"",""id48"",""id49"",""id50"",""id51"",""id52"",""id53"",""id54"",""id55"",""id56"",""id57"",""id58"",""id59"",""id60"",""id61"",""id62"",""id63"",""id64"",""id65"",""id66"",""id67"",""id68"",""id69"",""id70"",""id71"",""id72"",""id73"",""id74"",""id75"",""id76"",""id77"",""id78"",""id79"",""id80"",""id81"",""id82"",""id83"",""id84"",""id85"",""id86"",""id87"",""id88"",""id89"",""id90"",""id91"",""id92"",""id93"",""id94"",""id95"",""id96"",""id97"",""id98"",""id99"",""id100"",""id101"",""id102"",""id103"",""id104"",""id105"",""id106"",""id107"",""id108"",""id109"",""id110"",""id111"",""id112"",""id113"",""id114"",""id115"",""id116"",""id117"",""id118"",""id119"",""id120"",""id121"",""id122"",""id123"",""id124"",""id125"",""id126"",""id127"",""id128"",""id129"",""id130"",""id131"",""id132"",""id133"",""id134"",""id135"",""id136"",""id137"",""id138"",""id139"",""id140"",""id141"",""id142"",""id143"",""id144"",""id145"",""id146"",""id147"",""id148"",""id149""],""messages"":[{""type"":""text"",""text"":""Test""}]}";

            HttpRequestMessage[] requests = httpClient.Requests.ToArray();
            Assert.AreEqual(2, requests.Length);

            Assert.AreEqual("/message/multicast", requests[0].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[0].GetPostedData());

            postedData = @"{""to"":[""id150""],""messages"":[{""type"":""text"",""text"":""Test""}]}";

            Assert.AreEqual("/message/multicast", requests[1].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[1].GetPostedData());
        }
        public async Task Multicast_With6Messages_CallsApi2Times()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            TextMessage[] messages = new TextMessage[6];
            for (int i = 0; i < messages.Length; i++)
            {
                messages[i] = new TextMessage("Test" + i);
            }

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new string[] { "id1", "id2" }, messages);

            HttpRequestMessage[] requests = httpClient.Requests.ToArray();
            Assert.AreEqual(2, requests.Length);

            string postedData = @"{""to"":[""id1"",""id2""],""messages"":[{""type"":""text"",""text"":""Test0""},{""type"":""text"",""text"":""Test1""},{""type"":""text"",""text"":""Test2""},{""type"":""text"",""text"":""Test3""},{""type"":""text"",""text"":""Test4""}]}";

            Assert.AreEqual("/message/multicast", requests[0].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[0].GetPostedData());

            postedData = @"{""to"":[""id1"",""id2""],""messages"":[{""type"":""text"",""text"":""Test5""}]}";

            Assert.AreEqual("/message/multicast", requests[1].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[1].GetPostedData());
        }
예제 #3
0
        public async Task Push_With11Messages_CallsApi3Times()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            TextMessage[] messages = new TextMessage[11];
            for (int i = 0; i < messages.Length; i++)
            {
                messages[i] = new TextMessage("Test" + i);
            }

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Push("id", messages);

            HttpRequestMessage[] requests = httpClient.Requests.ToArray();
            Assert.AreEqual(3, requests.Length);

            string postedData = @"{""to"":""id"",""messages"":[{""type"":""text"",""text"":""Test0""},{""type"":""text"",""text"":""Test1""},{""type"":""text"",""text"":""Test2""},{""type"":""text"",""text"":""Test3""},{""type"":""text"",""text"":""Test4""}]}";

            Assert.AreEqual("/message/push", requests[0].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[0].GetPostedData());

            postedData = @"{""to"":""id"",""messages"":[{""type"":""text"",""text"":""Test5""},{""type"":""text"",""text"":""Test6""},{""type"":""text"",""text"":""Test7""},{""type"":""text"",""text"":""Test8""},{""type"":""text"",""text"":""Test9""}]}";

            Assert.AreEqual("/message/push", requests[1].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[1].GetPostedData());

            postedData = @"{""to"":""id"",""messages"":[{""type"":""text"",""text"":""Test10""}]}";

            Assert.AreEqual("/message/push", requests[2].RequestUri.PathAndQuery);
            Assert.AreEqual(postedData, requests[2].GetPostedData());
        }
예제 #4
0
        public async Task ResourcesToAuthenticateAccess_Received()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated().SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var expectedService = "resource-owner";
            var scope1          = "repository:test/my-app:pull,push";
            var scope2          = "repository:test/my-app2:pull";

            var url = $@"api/boondocks/authentication?service={expectedService}&scope={scope1}&scope={scope2}";

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(url, credentials);

            // Assert:
            Assert.True(mockMessaging.ReceivedMessages.Count() == 1);

            var receivedCommand = (AuthenticateCaller)mockMessaging.ReceivedMessages.First();

            Assert.NotNull(receivedCommand.Context);

            // Assert Owning Service Submitted:
            Assert.Equal(receivedCommand.Context.ResourceOwner, expectedService);

            // Assert Resources to Authenticate Access:
            Assert.NotNull(receivedCommand.Context.Resources);
            Assert.True(receivedCommand.Context.Resources.Length == 2);

            // Assert First Resource Scope:
            var firstScope = receivedCommand.Context.Resources[0];

            Assert.Equal("repository", firstScope.Type);
            Assert.Equal("test/my-app", firstScope.Name);
            Assert.True(firstScope.Actions.Length == 2);
            Assert.Equal("pull", firstScope.Actions[0]);
            Assert.Equal("push", firstScope.Actions[1]);


            // Assert Second Resource Scope:
            var secondScope = receivedCommand.Context.Resources[1];

            Assert.Equal("repository", secondScope.Type);
            Assert.Equal("test/my-app2", secondScope.Name);
            Assert.True(secondScope.Actions.Length == 1);
            Assert.Equal("pull", firstScope.Actions[0]);
        }
        public async Task Multicast_WithUser_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new TestUser[] { new TestUser() }, new TestTextMessage());

            string postedData = @"{""to"":[""testUser""],""messages"":[{""type"":""text"",""text"":""TestTextMessage""}]}";

            Assert.AreEqual("/message/multicast", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task Multicast_CorrectInput_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new string[] { "id1", "id2" }, new TextMessage("Test"));

            string postedData = @"{""to"":[""id1"",""id2""],""messages"":[{""type"":""text"",""text"":""Test""}]}";

            Assert.AreEqual("/message/multicast", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
예제 #7
0
        public async Task Push_WithRoom_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Push(new TestRoom(), new TestTextMessage());

            string postedData = @"{""to"":""testRoom"",""messages"":[{""type"":""text"",""text"":""TestTextMessage""}]}";

            Assert.AreEqual("/message/push", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
예제 #8
0
        public async Task Push_WithUserAndEnumerable_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            IEnumerable <TestTextMessage> messages = Enumerable.Repeat(new TestTextMessage(), 2);

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Push(new TestUser(), messages);

            string postedData = @"{""to"":""testUser"",""messages"":[{""type"":""text"",""text"":""TestTextMessage""},{""type"":""text"",""text"":""TestTextMessage""}]}";

            Assert.AreEqual("/message/push", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task Multicast_WithEnumerable_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            IEnumerable <TestTextMessage> messages = Enumerable.Repeat(new TestTextMessage(), 2);

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new string[] { "id" }, messages);

            string postedData = @"{""to"":[""id""],""messages"":[{""type"":""text"",""text"":""TestTextMessage""},{""type"":""text"",""text"":""TestTextMessage""}]}";

            Assert.AreEqual("/message/multicast", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
예제 #10
0
        public void SetUp()
        {
            WebServerUrl = Resources.GetServerAddress();
            if (_useTestWebServer)
            {
                var testWebServer = new TestWebServer(WebServerUrl);
                Server = testWebServer;
                Client = testWebServer.Client;
            }
            else
            {
                Server = new WebServer(WebServerUrl);
                Client = TestHttpClient.Create(WebServerUrl);
            }

            OnSetUp();
            Server.Start();
        }
예제 #11
0
        public async Task InvalidSpecifiedCredentials_UnAuthorized()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.SetAuthenticated(false);
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
        }
예제 #12
0
        public async Task ClientCredentialsReceived_WhenSent()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated().SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentialModle = new AuthCredentialModel
            {
                Credentials = new Dictionary <string, string>
                {
                    { "CertKey1", "CertValue1" },
                    { "CertKey2", "CertValue2" }
                }
            };

            var result = await httpClient.AuthenticateAsync(credentialModle);

            // Assert:
            Assert.True(mockMessaging.ReceivedMessages.Count() == 1);

            var receivedCommand = (AuthenticateCaller)mockMessaging.ReceivedMessages.First();

            Assert.NotNull(receivedCommand.Context);

            var receivedCredentials = receivedCommand.Context.Credentials;

            Assert.NotNull(receivedCredentials);

            Assert.True(receivedCredentials.ContainsKey("CertKey1"));
            Assert.True(receivedCredentials.ContainsKey("CertKey2"));
            Assert.Equal("CertValue1", receivedCredentials["CertKey1"]);
            Assert.Equal("CertValue2", receivedCredentials["CertKey2"]);
        }
예제 #13
0
        public async Task ValidCredentials_ResourceAccess_Returned()
        {
            // Arrange:
            var allowedResourceAccess = new ResourcePermission[] {
                new ResourcePermission(type: "ResourceType", name: "ResourceName", actions: new string[] { "action1", "action2" })
            };

            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated(allowedResourceAccess)
                                     .SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            var responseValue = await result.Content.ReadAsStringAsync();

            var resource = JsonConvert.DeserializeObject <AuthResultResource>(responseValue);
            var resourcesGrantedAccess = resource.GetEmbeddedCollection <AuthAccessResource>("resource-access");

            Assert.NotNull(resourcesGrantedAccess);
            Assert.True(resourcesGrantedAccess.Count() == 1);

            var access = resourcesGrantedAccess.First();

            Assert.Equal("ResourceType", access.Type);
            Assert.Equal("ResourceName", access.Name);
            Assert.True(access.Actions.Length == 2);
            Assert.Equal("action1", access.Actions[0]);
            Assert.Equal("action2", access.Actions[1]);
        }
예제 #14
0
        public async Task ValidCredentials_OkStatus_WithTokenHeader()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Authenticated()
                                     .SetSignedToken("MOCK_TOKEN");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            result.Headers.TryGetValues("X-Custom-Token", out IEnumerable <string> values);
            Assert.True(values.Count() == 1);
            Assert.Equal("MOCK_TOKEN", values.First());
        }
예제 #15
0
        public async Task IncorrectedOrMissingCredentials_BadRequest()
        {
            // Arrange:
            var mockMessaging = MockMessagingService.Setup(results => {
                var expectedResult = AuthResult.Failed("INVALID_CONTEXT");
                results.RegisterResponse <AuthenticateCaller, AuthResult>(expectedResult);
            });

            var plugin     = new MockAppHostPlugin();
            var httpClient = TestHttpClient.Create(plugin, mockMessaging);

            // Act:
            var credentials = new AuthCredentialModel {
            };
            var result      = await httpClient.AuthenticateAsync(credentials);

            // Assert:
            Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
            Assert.NotNull(result.Content);

            var content = await result.Content.ReadAsStringAsync();

            Assert.Equal("INVALID_CONTEXT", content);
        }
 public static ILineBot CreateBot(ILineBotLogger logger)
 {
     return(new LineBot(new TestConfiguration(), TestHttpClient.Create(), logger));
 }
 public static ILineBot CreateBot()
 {
     return(new LineBot(new TestConfiguration(), TestHttpClient.Create(), new EmptyLineBotLogger()));
 }