PageRenderTime 70ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/test/Microsoft.Web.Helpers.Test/TwitterTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 481 lines | 387 code | 48 blank | 46 comment | 0 complexity | 173152ee15ceaa8887ae0dd186f833fc MD5 | raw file
  1. using System.Web;
  2. using System.Web.Helpers;
  3. using System.Web.Helpers.Test;
  4. using System.Web.TestUtil;
  5. using Xunit;
  6. using Assert = Microsoft.TestCommon.AssertEx;
  7. namespace Microsoft.Web.Helpers.Test
  8. {
  9. public class TwitterTest
  10. {
  11. /// <summary>
  12. ///A test for Profile
  13. ///</summary>
  14. [Fact]
  15. public void Profile_ReturnsValidData()
  16. {
  17. // Arrange
  18. string twitterUserName = "my-userName";
  19. int width = 100;
  20. int height = 100;
  21. string backgroundShellColor = "my-backgroundShellColor";
  22. string shellColor = "my-shellColor";
  23. string tweetsBackgroundColor = "tweetsBackgroundColor";
  24. string tweetsColor = "tweets-color";
  25. string tweetsLinksColor = "tweets Linkcolor";
  26. bool scrollBar = false;
  27. bool loop = false;
  28. bool live = false;
  29. bool hashTags = false;
  30. bool timestamp = false;
  31. bool avatars = false;
  32. var behavior = "all";
  33. int searchInterval = 10;
  34. // Act
  35. string actual = Twitter.Profile(twitterUserName, width, height, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor,
  36. 5, scrollBar, loop, live, hashTags, timestamp, avatars, behavior, searchInterval).ToString();
  37. // Assert
  38. var json = GetTwitterJSObject(actual);
  39. Assert.Equal(json.type, "profile");
  40. Assert.Equal(json.interval, 1000 * searchInterval);
  41. Assert.Equal(json.width.ToString(), width.ToString());
  42. Assert.Equal(json.height.ToString(), height.ToString());
  43. Assert.Equal(json.theme.shell.background, backgroundShellColor);
  44. Assert.Equal(json.theme.shell.color, shellColor);
  45. Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
  46. Assert.Equal(json.theme.tweets.color, tweetsColor);
  47. Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
  48. Assert.Equal(json.features.scrollbar, scrollBar);
  49. Assert.Equal(json.features.loop, loop);
  50. Assert.Equal(json.features.live, live);
  51. Assert.Equal(json.features.hashtags, hashTags);
  52. Assert.Equal(json.features.avatars, avatars);
  53. Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
  54. Assert.True(actual.Contains(".setUser('my-userName')"));
  55. }
  56. [Fact]
  57. public void ProfileJSEncodesParameters()
  58. {
  59. // Arrange
  60. string twitterUserName = "\"my-userName\'";
  61. string backgroundShellColor = "\\foo";
  62. string shellColor = "<shellColor>\\";
  63. string tweetsBackgroundColor = "<tweetsBackgroundColor";
  64. string tweetsColor = "<tweetsColor>";
  65. string tweetsLinksColor = "<tweetsLinkColor>";
  66. // Act
  67. string actual = Twitter.Profile(twitterUserName, 100, 100, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();
  68. // Assert
  69. Assert.True(actual.Contains("background: '\\\\foo"));
  70. Assert.True(actual.Contains("color: '\\u003cshellColor\\u003e\\\\"));
  71. Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor"));
  72. Assert.True(actual.Contains("color: '\\u003ctweetsColor\\u003e"));
  73. Assert.True(actual.Contains("links: '\\u003ctweetsLinkColor\\u003e"));
  74. Assert.True(actual.Contains(".setUser('\\\"my-userName\\u0027')"));
  75. }
  76. [Fact]
  77. public void Search_ReturnsValidData()
  78. {
  79. // Arrange
  80. string search = "awesome-search-term";
  81. int width = 100;
  82. int height = 100;
  83. string title = "cust-title";
  84. string caption = "some caption";
  85. string backgroundShellColor = "background-shell-color";
  86. string shellColor = "shellColorValue";
  87. string tweetsBackgroundColor = "tweetsBackgroundColor";
  88. string tweetsColor = "tweetsColorVal";
  89. string tweetsLinksColor = "tweetsLinkColor";
  90. bool scrollBar = false;
  91. bool loop = false;
  92. bool live = true;
  93. bool hashTags = false;
  94. bool timestamp = true;
  95. bool avatars = true;
  96. bool topTweets = true;
  97. var behavior = "default";
  98. int searchInterval = 10;
  99. // Act
  100. string actual = Twitter.Search(search, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor, scrollBar,
  101. loop, live, hashTags, timestamp, avatars, topTweets, behavior, searchInterval).ToString();
  102. // Assert
  103. var json = GetTwitterJSObject(actual);
  104. Assert.Equal(json.type, "search");
  105. Assert.Equal(json.search, search);
  106. Assert.Equal(json.interval, 1000 * searchInterval);
  107. Assert.Equal(json.title, title);
  108. Assert.Equal(json.subject, caption);
  109. Assert.Equal(json.width.ToString(), width.ToString());
  110. Assert.Equal(json.height.ToString(), height.ToString());
  111. Assert.Equal(json.theme.shell.background, backgroundShellColor);
  112. Assert.Equal(json.theme.shell.color, shellColor);
  113. Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
  114. Assert.Equal(json.theme.tweets.color, tweetsColor);
  115. Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
  116. Assert.Equal(json.features.scrollbar, scrollBar);
  117. Assert.Equal(json.features.loop, loop);
  118. Assert.Equal(json.features.live, live);
  119. Assert.Equal(json.features.hashtags, hashTags);
  120. Assert.Equal(json.features.avatars, avatars);
  121. Assert.Equal(json.features.toptweets, topTweets);
  122. Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
  123. }
  124. [Fact]
  125. public void SearchJavascriptEncodesParameters()
  126. {
  127. // Arrange
  128. string search = "<script>";
  129. int width = 100;
  130. int height = 100;
  131. string title = "'title'";
  132. string caption = "<caption>";
  133. string backgroundShellColor = "\\foo";
  134. string shellColor = "<shellColor>\\";
  135. string tweetsBackgroundColor = "<tweetsBackgroundColor";
  136. string tweetsColor = "<tweetsColor>";
  137. string tweetsLinksColor = "<tweetsLinkColor>";
  138. // Act
  139. string actual = Twitter.Search(search, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();
  140. // Assert
  141. Assert.True(actual.Contains("search: '\\u003cscript\\u003e'"));
  142. Assert.True(actual.Contains("title: '\\u0027title\\u0027'"));
  143. Assert.True(actual.Contains("subject: '\\u003ccaption\\u003e'"));
  144. Assert.True(actual.Contains("background: '\\\\foo"));
  145. Assert.True(actual.Contains("color: '\\u003cshellColor\\u003e\\\\"));
  146. Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor"));
  147. Assert.True(actual.Contains("color: '\\u003ctweetsColor\\u003e"));
  148. Assert.True(actual.Contains("links: '\\u003ctweetsLinkColor\\u003e"));
  149. }
  150. [Fact]
  151. public void SearchWithInvalidArgs_ThrowsArgumentException()
  152. {
  153. Assert.ThrowsArgumentNullOrEmptyString(() => Twitter.Search(null).ToString(), "searchQuery");
  154. }
  155. [Fact]
  156. public void ProfileWithInvalidArgs_ThrowsArgumentException()
  157. {
  158. Assert.ThrowsArgumentNullOrEmptyString(() => Twitter.Profile(null).ToString(), "userName");
  159. }
  160. [Fact]
  161. public void SearchRendersRendersValidXhtml()
  162. {
  163. string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
  164. Twitter.Search("any<>term") +
  165. "\n </body> \n </html>";
  166. HtmlString htmlResult = new HtmlString(result);
  167. XhtmlAssert.Validate1_1(htmlResult);
  168. }
  169. [Fact]
  170. public void SearchEncodesSearchTerms()
  171. {
  172. // Act
  173. string result = Twitter.Search("'any term'", backgroundShellColor: "\"bad-color").ToString();
  174. // Assert
  175. Assert.True(result.Contains(@"background: '\""bad-color',"));
  176. //Assert.True(result.Contains(@"search: @"'\u0027any term\u0027'","));
  177. }
  178. [Fact]
  179. public void ProfileRendersRendersValidXhtml()
  180. {
  181. string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
  182. Twitter.Profile("any<>Name") +
  183. "\n </body> \n </html>";
  184. HtmlString htmlResult = new HtmlString(result);
  185. XhtmlAssert.Validate1_1(htmlResult);
  186. }
  187. [Fact]
  188. public void ProfileEncodesSearchTerms()
  189. {
  190. // Act
  191. string result = Twitter.Profile("'some user'", backgroundShellColor: "\"malformed-color").ToString();
  192. // Assert
  193. Assert.True(result.Contains(@"background: '\""malformed-color'"));
  194. Assert.True(result.Contains("setUser('\\u0027some user\\u0027')"));
  195. }
  196. [Fact]
  197. public void TweetButtonWithDefaultAttributes()
  198. {
  199. // Arrange
  200. string expected = @"<a href=""http://twitter.com/share"" class=""twitter-share-button"" data-count=""vertical"">Tweet</a><script type=""text/javascript"" src=""http://platform.twitter.com/widgets.js""></script>";
  201. // Act
  202. string result = Twitter.TweetButton().ToString();
  203. // Assert
  204. UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
  205. }
  206. [Fact]
  207. public void TweetButtonWithSpeicifedAttributes()
  208. {
  209. // Arrange
  210. string expected = @"<a href=""http://twitter.com/share"" class=""twitter-share-button"" data-text=""tweet-text"" data-url=""http://www.microsoft.com"" data-via=""userName"" data-related=""related-userName:rel-desc"" data-count=""none"">my-share-text</a><script type=""text/javascript"" src=""http://platform.twitter.com/widgets.js""></script>";
  211. // Act
  212. string result = Twitter.TweetButton("none", shareText: "my-share-text", tweetText: "tweet-text", url: "http://www.microsoft.com", language: "en", userName: "userName", relatedUserName: "related-userName", relatedUserDescription: "rel-desc").ToString();
  213. // Assert
  214. UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
  215. }
  216. [Fact]
  217. public void TweetButtonHtmlEncodesValues()
  218. {
  219. // Arrange
  220. string expected = @"<a href=""http://twitter.com/share"" class=""twitter-share-button"" data-text=""&lt;tweet-text>"" data-url=""&lt;http://www.microsoft.com>"" data-via=""&lt;userName>"" data-related=""&lt;related-userName>:&lt;rel-desc>"" data-count=""none"">&lt;Tweet</a><script type=""text/javascript"" src=""http://platform.twitter.com/widgets.js""></script>";
  221. // Act
  222. string result = Twitter.TweetButton("none", shareText: "<Tweet", tweetText: "<tweet-text>", url: "<http://www.microsoft.com>", language: "en", userName: "<userName>", relatedUserName: "<related-userName>", relatedUserDescription: "<rel-desc>").ToString();
  223. // Assert
  224. UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
  225. }
  226. [Fact]
  227. public void FollowButtonWithDefaultParameters()
  228. {
  229. // Arrange
  230. string expected = @"<a href=""http://www.twitter.com/my-twitter-userName""><img src=""http://twitter-badges.s3.amazonaws.com/follow_me-a.png"" alt=""Follow my-twitter-userName on Twitter""/></a>";
  231. // Act
  232. string result = Twitter.FollowButton("my-twitter-userName").ToString();
  233. // Assert
  234. UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
  235. }
  236. [Fact]
  237. public void FollowButtonWithSpecifiedParmeters()
  238. {
  239. // Arrange
  240. string expected = @"<a href=""http://www.twitter.com/my-twitter-userName""><img src=""http://twitter-badges.s3.amazonaws.com/t_logo-b.png"" alt=""Follow my-twitter-userName on Twitter""/></a>";
  241. // Act
  242. string result = Twitter.FollowButton("my-twitter-userName", "t_logo", "b").ToString();
  243. // Assert
  244. UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
  245. }
  246. [Fact]
  247. public void FollowButtonEncodesParameters()
  248. {
  249. // Arrange
  250. string expected = @"<a href=""http://www.twitter.com/http%3a%2f%2fmy-twitter-userName%3cscript""><img src=""http://twitter-badges.s3.amazonaws.com/t_logo-b.png"" alt=""Follow http://my-twitter-userName&lt;script on Twitter""/></a>";
  251. // Act
  252. string result = Twitter.FollowButton("http://my-twitter-userName<script", "t_logo", "b").ToString();
  253. // Assert
  254. UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
  255. }
  256. [Fact]
  257. public void FavesReturnsValidData()
  258. {
  259. // Arrange
  260. string twitterUserName = "my-userName";
  261. string title = "my-title";
  262. string caption = "my-caption";
  263. int width = 100;
  264. int height = 100;
  265. string backgroundShellColor = "my-backgroundShellColor";
  266. string shellColor = "my-shellColor";
  267. string tweetsBackgroundColor = "tweetsBackgroundColor";
  268. string tweetsColor = "tweets-color";
  269. string tweetsLinksColor = "tweets Linkcolor";
  270. int numTweets = 4;
  271. bool scrollBar = false;
  272. bool loop = false;
  273. bool live = false;
  274. bool hashTags = false;
  275. bool timestamp = false;
  276. bool avatars = false;
  277. var behavior = "all";
  278. int searchInterval = 10;
  279. // Act
  280. string actual = Twitter.Faves(twitterUserName, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor,
  281. numTweets, scrollBar, loop, live, hashTags, timestamp, avatars, "all", searchInterval).ToString();
  282. // Assert
  283. var json = GetTwitterJSObject(actual);
  284. Assert.Equal(json.type, "faves");
  285. Assert.Equal(json.interval, 1000 * searchInterval);
  286. Assert.Equal(json.width.ToString(), width.ToString());
  287. Assert.Equal(json.height.ToString(), height.ToString());
  288. Assert.Equal(json.title, title);
  289. Assert.Equal(json.subject, caption);
  290. Assert.Equal(json.theme.shell.background, backgroundShellColor);
  291. Assert.Equal(json.theme.shell.color, shellColor);
  292. Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
  293. Assert.Equal(json.theme.tweets.color, tweetsColor);
  294. Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
  295. Assert.Equal(json.features.scrollbar, scrollBar);
  296. Assert.Equal(json.features.loop, loop);
  297. Assert.Equal(json.features.live, live);
  298. Assert.Equal(json.features.hashtags, hashTags);
  299. Assert.Equal(json.features.avatars, avatars);
  300. Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
  301. Assert.True(actual.Contains(".setUser('my-userName')"));
  302. }
  303. [Fact]
  304. public void FavesJavascriptEncodesParameters()
  305. {
  306. // Arrange
  307. string twitterUserName = "<my-userName>";
  308. string title = "<my-title>";
  309. string caption = "<my-caption>";
  310. int width = 100;
  311. int height = 100;
  312. string backgroundShellColor = "<my-backgroundShellColor>";
  313. string shellColor = "<my-shellColor>";
  314. string tweetsBackgroundColor = "<tweetsBackgroundColor>";
  315. string tweetsColor = "<tweets-color>";
  316. string tweetsLinksColor = "<tweets Linkcolor>";
  317. // Act
  318. string actual = Twitter.Faves(twitterUserName, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();
  319. // Assert
  320. Assert.True(actual.Contains("title: '\\u003cmy-title\\u003e'"));
  321. Assert.True(actual.Contains("subject: '\\u003cmy-caption\\u003e'"));
  322. Assert.True(actual.Contains("background: '\\u003cmy-backgroundShellColor\\u003e'"));
  323. Assert.True(actual.Contains("color: '\\u003cmy-shellColor\\u003e'"));
  324. Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor\\u003e'"));
  325. Assert.True(actual.Contains("color: '\\u003ctweets-color\\u003e"));
  326. Assert.True(actual.Contains("links: '\\u003ctweets Linkcolor\\u003e'"));
  327. Assert.True(actual.Contains(".setUser('\\u003cmy-userName\\u003e')"));
  328. }
  329. [Fact]
  330. public void FavesRendersRendersValidXhtml()
  331. {
  332. string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
  333. Twitter.Faves("any<>Name") +
  334. "\n </body> \n </html>";
  335. HtmlString htmlResult = new HtmlString(result);
  336. XhtmlAssert.Validate1_1(htmlResult);
  337. }
  338. [Fact]
  339. public void ListReturnsValidData()
  340. {
  341. // Arrange
  342. string twitterUserName = "my-userName";
  343. string list = "my-list";
  344. string title = "my-title";
  345. string caption = "my-caption";
  346. int width = 100;
  347. int height = 100;
  348. string backgroundShellColor = "my-backgroundShellColor";
  349. string shellColor = "my-shellColor";
  350. string tweetsBackgroundColor = "tweetsBackgroundColor";
  351. string tweetsColor = "tweets-color";
  352. string tweetsLinksColor = "tweets Linkcolor";
  353. int numTweets = 4;
  354. bool scrollBar = false;
  355. bool loop = false;
  356. bool live = false;
  357. bool hashTags = false;
  358. bool timestamp = false;
  359. bool avatars = false;
  360. var behavior = "all";
  361. int searchInterval = 10;
  362. // Act
  363. string actual = Twitter.List(twitterUserName, list, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor,
  364. numTweets, scrollBar, loop, live, hashTags, timestamp, avatars, "all", searchInterval).ToString();
  365. // Assert
  366. var json = GetTwitterJSObject(actual);
  367. Assert.Equal(json.type, "list");
  368. Assert.Equal(json.interval, 1000 * searchInterval);
  369. Assert.Equal(json.width.ToString(), width.ToString());
  370. Assert.Equal(json.height.ToString(), height.ToString());
  371. Assert.Equal(json.title, title);
  372. Assert.Equal(json.subject, caption);
  373. Assert.Equal(json.theme.shell.background, backgroundShellColor);
  374. Assert.Equal(json.theme.shell.color, shellColor);
  375. Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
  376. Assert.Equal(json.theme.tweets.color, tweetsColor);
  377. Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
  378. Assert.Equal(json.features.scrollbar, scrollBar);
  379. Assert.Equal(json.features.loop, loop);
  380. Assert.Equal(json.features.live, live);
  381. Assert.Equal(json.features.hashtags, hashTags);
  382. Assert.Equal(json.features.avatars, avatars);
  383. Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
  384. Assert.True(actual.Contains(".setList('my-userName', 'my-list')"));
  385. }
  386. [Fact]
  387. public void ListJavascriptEncodesParameters()
  388. {
  389. // Arrange
  390. string twitterUserName = "<my-userName>";
  391. string list = "<my-list>";
  392. string title = "<my-title>";
  393. string caption = "<my-caption>";
  394. int width = 100;
  395. int height = 100;
  396. string backgroundShellColor = "<my-backgroundShellColor>";
  397. string shellColor = "<my-shellColor>";
  398. string tweetsBackgroundColor = "<tweetsBackgroundColor>";
  399. string tweetsColor = "<tweets-color>";
  400. string tweetsLinksColor = "<tweets Linkcolor>";
  401. // Act
  402. string actual = Twitter.List(twitterUserName, list, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();
  403. // Assert
  404. Assert.True(actual.Contains("title: '\\u003cmy-title\\u003e'"));
  405. Assert.True(actual.Contains("subject: '\\u003cmy-caption\\u003e'"));
  406. Assert.True(actual.Contains("background: '\\u003cmy-backgroundShellColor\\u003e'"));
  407. Assert.True(actual.Contains("color: '\\u003cmy-shellColor\\u003e'"));
  408. Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor\\u003e'"));
  409. Assert.True(actual.Contains("color: '\\u003ctweets-color\\u003e"));
  410. Assert.True(actual.Contains("links: '\\u003ctweets Linkcolor\\u003e'"));
  411. Assert.True(actual.Contains(".setList('\\u003cmy-userName\\u003e', '\\u003cmy-list\\u003e')"));
  412. }
  413. [Fact]
  414. public void ListRendersRendersValidXhtml()
  415. {
  416. string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
  417. Twitter.List("any<>Name", "my-list") +
  418. "\n </body> \n </html>";
  419. HtmlString htmlResult = new HtmlString(result);
  420. XhtmlAssert.Validate1_1(htmlResult);
  421. }
  422. private static dynamic GetTwitterJSObject(string twitterOutput)
  423. {
  424. const string startString = "Widget(";
  425. int start = twitterOutput.IndexOf(startString) + startString.Length, end = twitterOutput.IndexOf(')');
  426. return Json.Decode(twitterOutput.Substring(start, end - start));
  427. }
  428. }
  429. }