// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Web; using System.Web.TestUtil; using Microsoft.TestCommon; using Moq; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace Microsoft.Web.Helpers.Test { public class FacebookTest { public FacebookTest() { Facebook.AppId = "myapp'"; Facebook.AppSecret = "myappsecret"; Facebook.Language = "french"; } [Fact] public void GetFacebookCookieInfoReturnsEmptyStringIfCookieIsNotPresent() { // Arrange var context = CreateHttpContext(); // Act var info = Facebook.GetFacebookCookieInfo(context, "foo"); // Assert Assert.Equal("", info); } [Fact] public void GetFacebookCookieInfoThrowsIfCookieIsNotValid() { // Arrange var context = CreateHttpContext(new Dictionary<string, string> { {"fbs_myapp'", "sig=malformed-signature&name=foo&val=bar&uid=MyFacebookName"}, {"fbs_uid", "MyFacebookName"}, }); // Act and Assert Assert.Throws<InvalidOperationException>(() => Facebook.GetFacebookCookieInfo(context, "uid"), "Invalid Facebook cookie."); } [Fact] public void GetFacebookCookieReturnsUserIdIfCookieIsValid() { // Arrange var context = CreateHttpContext(new Dictionary<string, string> { {"fbs_myapp'", "sig=B2E6B3A21D0C9FA72E612BD6C3084807&name=foo&val=bar&uid=MyFacebookName"}, }); // Act var info = Facebook.GetFacebookCookieInfo(context, "uid"); // Assert Assert.Equal("MyFacebookName", info); } [Fact] public void GetInitScriptsJSEncodesParameters() { // Arrange var expectedText = @" <div id=""fb-root""></div> <script type=""text/javascript""> window.fbAsyncInit = function () { FB.init({ appId: 'MyApp\u0027', status: true, cookie: true, xfbml: true }); }; (function () { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/French/all.js'; document.getElementById('fb-root').appendChild(e); } ()); function loginRedirect(url) { window.location = url; } </script> "; // Act var actualText = Facebook.GetInitializationScripts(); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void LoginButtonTest() { // Arrange var expected40 = @"<fb:login-button autologoutlink=""True"" size=""extra-small"" length=""extra-long"" onlogin=""loginRedirect('http://www.test.com/facebook/?registerUrl=http%3a%2f%2fwww.test.com%2f&returnUrl=http%3a%2f%2fww.test.com%2fLogin.cshtml')"" show-faces=""True"" perms=""email,none""">Awesome "button text"</fb:login-button>"; var expected45 = @"<fb:login-button autologoutlink=""True"" size=""extra-small"" length=""extra-long"" onlogin=""loginRedirect('http://www.test.com/facebook/?registerUrl=http%3a%2f%2fwww.test.com%2f\u0026returnUrl=http%3a%2f%2fww.test.com%2fLogin.cshtml')"" show-faces=""True"" perms=""email,none""">Awesome "button text"</fb:login-button>"; // Act var actualText = Facebook.LoginButton("http://www.test.com", "http://ww.test.com/Login.cshtml", "http://www.test.com/facebook/", "Awesome \"button text\"", true, "extra-small", "extra-long", true, "none\""); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(RuntimeEnvironment.IsVersion45Installed ? expected45 : expected40, actualText.ToString()); } [Fact] public void LoginButtonOnlyTagTest() { // Arrange var expectedText = @"<fb:login-button autologoutlink=""True"" size=""small"" length=""medium"" onlogin=""foobar();"" show-faces=""True"" perms=""none""">"Awesome button text"</fb:login-button>"; // Act var actualText = Facebook.LoginButtonTagOnly("\"Awesome button text\"", true, "small", "medium", "foobar();", true, "none\""); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void LikeButtonTest() { // Arrange var expectedText = @"<iframe src=""http://www.facebook.com/plugins/like.php?href=http%3a%2f%2fsomewebsite&layout=modern&show_faces=false&width=300&action=hop&colorscheme=lighter&height=30&font=Comic+Sans&locale=French&ref=foo+bar"" scrolling=""no"" frameborder=""0"" style=""border:none; overflow:hidden; width:300px; height:30px;"" allowTransparency=""true""></iframe>"; // Act var actualText = Facebook.LikeButton("http://somewebsite", "modern", false, 300, 30, "hop", "Comic Sans", "lighter", "foo bar"); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void CommentsWithNoXidTest() { // Arrange var expectedText = @"<fb:comments numposts=""3"" width=""300"" reverse=""true"" simple=""true"" ></fb:comments>"; // Act var actualText = Facebook.Comments(null, 300, 3, true, true); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void CommentsWithXidTest() { // Arrange var expectedText = @"<fb:comments xid=""bar"" numposts=""3"" width=""300"" reverse=""true"" simple=""true"" ></fb:comments>"; // Act var actualText = Facebook.Comments("bar", 300, 3, true, true); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void RecommendationsTest() { // Arrange var expectedText = @"<iframe src=""http://www.facebook.com/plugins/recommendations.php?site=http%3a%2f%2fsomesite&width=100&height=200&header=False&colorscheme=blue&font=none&border_color=black&filter=All+posts&ref=ref+label&locale=french"" scrolling=""no"" frameborder=""0"" style=""border:none; overflow:hidden; width:100px; height:200px;"" allowTransparency=""true""></iframe>"; // Act var actualText = Facebook.Recommendations("http://somesite", 100, 200, false, "blue", "none", "black", "All posts", "ref label"); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void LikeBoxTest() { // Arrange var expectedText = @"<iframe src=""http://www.facebook.com/plugins/recommendations.php?href=http%3a%2f%2fsomesite&width=100&height=200&header=False&colorscheme=blue&connections=5&stream=True&header=False&locale=french"" scrolling=""no"" frameborder=""0"" style=""border:none; overflow:hidden; width:100px; height:200px;"" allowTransparency=""true""></iframe>"; // Act var actualText = Facebook.LikeBox("http://somesite", 100, 200, "blue", 5, true, false); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void FacepileTest() { // Arrange var expectedText = @"<fb:facepile max-rows=""3"" width=""100""></fb:facepile>"; // Act var actualText = Facebook.Facepile(3, 100); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void LiveStreamWithEmptyXidTest() { // Arrange var expectedText = @"<iframe src=""http://www.facebook.com/plugins/live_stream_box.php?app_id=myapp%27&width=100&height=100&always_post_to_friends=True&locale=french"" scrolling=""no"" frameborder=""0"" style=""border:none; overflow:hidden; width:100px; height:100px;"" allowTransparency=""true""></iframe>"; // Act var actualText = Facebook.LiveStream(100, 100, "", "", true); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void LiveStreamWithXidValueTest() { // Arrange var expectedText = @"<iframe src=""http://www.facebook.com/plugins/live_stream_box.php?app_id=myapp%27&width=100&height=100&always_post_to_friends=True&locale=french&xid=some-val&via_url=http%3a%2f%2fmysite"" scrolling=""no"" frameborder=""0"" style=""border:none; overflow:hidden; width:100px; height:100px;"" allowTransparency=""true""></iframe>"; // Act var actualText = Facebook.LiveStream(100, 100, "some-val", "http://mysite", true); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void ActivityStreamTest() { // Arrange var expectedText = @"<iframe src=""http://www.facebook.com/plugins/activity.php?site=http%3a%2f%2fmysite&width=100&height=120&header=False&colorscheme=gray&font=Arial&border_color=blue&recommendations=True&locale=french"" scrolling=""no"" frameborder=""0"" style=""border:none; overflow:hidden; width:300px; height:300px;"" allowTransparency=""true""></iframe>"; // Act var actualText = Facebook.ActivityFeed("http://mysite", 100, 120, false, "gray", "Arial", "blue", true); // Assert UnitTestHelper.AssertEqualsIgnoreWhitespace(expectedText, actualText.ToString()); } [Fact] public void FbmlNamespacesTest() { // Arrange var expectedText = @"xmlns:fb=""http://www.facebook.com/2008/fbml"" xmlns:og=""http://opengraphprotocol.org/schema/"""; // Act var actualText = Facebook.FbmlNamespaces(); // Assert Assert.Equal(expectedText, actualText.ToString()); } private static HttpContextBase CreateHttpContext(IDictionary<string, string> cookieValues = null) { var context = new Mock<HttpContextBase>(); var httpRequest = new Mock<HttpRequestBase>(); var cookies = new HttpCookieCollection(); httpRequest.Setup(c => c.Cookies).Returns(cookies); context.Setup(c => c.Request).Returns(httpRequest.Object); if (cookieValues != null) { foreach (var item in cookieValues) { cookies.Add(new HttpCookie(item.Key, item.Value)); } } return context.Object; } } }