File: TwitterTest.cs

package info (click to toggle)
mono 5.18.0.240%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,253,216 kB
  • sloc: cs: 10,925,936; xml: 2,804,987; ansic: 643,970; cpp: 120,384; perl: 59,272; asm: 21,383; sh: 20,162; makefile: 18,157; python: 4,715; pascal: 924; sql: 859; sed: 16; php: 1
file content (483 lines) | stat: -rw-r--r-- 22,772 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Web;
using System.Web.Helpers;
using System.Web.Helpers.Test;
using System.Web.TestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace Microsoft.Web.Helpers.Test
{
    public class TwitterTest
    {
        /// <summary>
        ///A test for Profile
        ///</summary>
        [Fact]
        public void Profile_ReturnsValidData()
        {
            // Arrange
            string twitterUserName = "my-userName";
            int width = 100;
            int height = 100;
            string backgroundShellColor = "my-backgroundShellColor";
            string shellColor = "my-shellColor";
            string tweetsBackgroundColor = "tweetsBackgroundColor";
            string tweetsColor = "tweets-color";
            string tweetsLinksColor = "tweets Linkcolor";
            bool scrollBar = false;
            bool loop = false;
            bool live = false;
            bool hashTags = false;
            bool timestamp = false;
            bool avatars = false;
            var behavior = "all";
            int searchInterval = 10;

            // Act
            string actual = Twitter.Profile(twitterUserName, width, height, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor,
                                            5, scrollBar, loop, live, hashTags, timestamp, avatars, behavior, searchInterval).ToString();

            // Assert
            var json = GetTwitterJSObject(actual);
            Assert.Equal(json.type, "profile");
            Assert.Equal(json.interval, 1000 * searchInterval);
            Assert.Equal(json.width.ToString(), width.ToString());
            Assert.Equal(json.height.ToString(), height.ToString());
            Assert.Equal(json.theme.shell.background, backgroundShellColor);
            Assert.Equal(json.theme.shell.color, shellColor);
            Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
            Assert.Equal(json.theme.tweets.color, tweetsColor);
            Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
            Assert.Equal(json.features.scrollbar, scrollBar);
            Assert.Equal(json.features.loop, loop);
            Assert.Equal(json.features.live, live);
            Assert.Equal(json.features.hashtags, hashTags);
            Assert.Equal(json.features.avatars, avatars);
            Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
            Assert.True(actual.Contains(".setUser('my-userName')"));
        }

        [Fact]
        public void ProfileJSEncodesParameters()
        {
            // Arrange
            string twitterUserName = "\"my-userName\'";
            string backgroundShellColor = "\\foo";
            string shellColor = "<shellColor>\\";
            string tweetsBackgroundColor = "<tweetsBackgroundColor";
            string tweetsColor = "<tweetsColor>";
            string tweetsLinksColor = "<tweetsLinkColor>";

            // Act
            string actual = Twitter.Profile(twitterUserName, 100, 100, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();

            // Assert
            Assert.True(actual.Contains("background: '\\\\foo"));
            Assert.True(actual.Contains("color: '\\u003cshellColor\\u003e\\\\"));
            Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor"));
            Assert.True(actual.Contains("color: '\\u003ctweetsColor\\u003e"));
            Assert.True(actual.Contains("links: '\\u003ctweetsLinkColor\\u003e"));
            Assert.True(actual.Contains(".setUser('\\\"my-userName\\u0027')"));
        }

        [Fact]
        public void Search_ReturnsValidData()
        {
            // Arrange
            string search = "awesome-search-term";
            int width = 100;
            int height = 100;
            string title = "cust-title";
            string caption = "some caption";
            string backgroundShellColor = "background-shell-color";
            string shellColor = "shellColorValue";
            string tweetsBackgroundColor = "tweetsBackgroundColor";
            string tweetsColor = "tweetsColorVal";
            string tweetsLinksColor = "tweetsLinkColor";
            bool scrollBar = false;
            bool loop = false;
            bool live = true;
            bool hashTags = false;
            bool timestamp = true;
            bool avatars = true;
            bool topTweets = true;
            var behavior = "default";
            int searchInterval = 10;

            // Act
            string actual = Twitter.Search(search, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor, scrollBar,
                                           loop, live, hashTags, timestamp, avatars, topTweets, behavior, searchInterval).ToString();

            // Assert
            var json = GetTwitterJSObject(actual);
            Assert.Equal(json.type, "search");
            Assert.Equal(json.search, search);
            Assert.Equal(json.interval, 1000 * searchInterval);
            Assert.Equal(json.title, title);
            Assert.Equal(json.subject, caption);
            Assert.Equal(json.width.ToString(), width.ToString());
            Assert.Equal(json.height.ToString(), height.ToString());
            Assert.Equal(json.theme.shell.background, backgroundShellColor);
            Assert.Equal(json.theme.shell.color, shellColor);
            Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
            Assert.Equal(json.theme.tweets.color, tweetsColor);
            Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
            Assert.Equal(json.features.scrollbar, scrollBar);
            Assert.Equal(json.features.loop, loop);
            Assert.Equal(json.features.live, live);
            Assert.Equal(json.features.hashtags, hashTags);
            Assert.Equal(json.features.avatars, avatars);
            Assert.Equal(json.features.toptweets, topTweets);
            Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
        }

        [Fact]
        public void SearchJavascriptEncodesParameters()
        {
            // Arrange
            string search = "<script>";
            int width = 100;
            int height = 100;
            string title = "'title'";
            string caption = "<caption>";
            string backgroundShellColor = "\\foo";
            string shellColor = "<shellColor>\\";
            string tweetsBackgroundColor = "<tweetsBackgroundColor";
            string tweetsColor = "<tweetsColor>";
            string tweetsLinksColor = "<tweetsLinkColor>";

            // Act
            string actual = Twitter.Search(search, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();

            // Assert
            Assert.True(actual.Contains("search: '\\u003cscript\\u003e'"));
            Assert.True(actual.Contains("title: '\\u0027title\\u0027'"));
            Assert.True(actual.Contains("subject: '\\u003ccaption\\u003e'"));
            Assert.True(actual.Contains("background: '\\\\foo"));
            Assert.True(actual.Contains("color: '\\u003cshellColor\\u003e\\\\"));
            Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor"));
            Assert.True(actual.Contains("color: '\\u003ctweetsColor\\u003e"));
            Assert.True(actual.Contains("links: '\\u003ctweetsLinkColor\\u003e"));
        }

        [Fact]
        public void SearchWithInvalidArgs_ThrowsArgumentException()
        {
            Assert.ThrowsArgumentNullOrEmptyString(() => Twitter.Search(null).ToString(), "searchQuery");
        }

        [Fact]
        public void ProfileWithInvalidArgs_ThrowsArgumentException()
        {
            Assert.ThrowsArgumentNullOrEmptyString(() => Twitter.Profile(null).ToString(), "userName");
        }

        [Fact]
        public void SearchRendersRendersValidXhtml()
        {
            string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
                            Twitter.Search("any<>term") +
                            "\n </body> \n </html>";
            HtmlString htmlResult = new HtmlString(result);
            XhtmlAssert.Validate1_1(htmlResult);
        }

        [Fact]
        public void SearchEncodesSearchTerms()
        {
            // Act 
            string result = Twitter.Search("'any term'", backgroundShellColor: "\"bad-color").ToString();

            // Assert
            Assert.True(result.Contains(@"background: '\""bad-color',"));
            //Assert.True(result.Contains(@"search: @"'\u0027any term\u0027'","));
        }

        [Fact]
        public void ProfileRendersRendersValidXhtml()
        {
            string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
                            Twitter.Profile("any<>Name") +
                            "\n </body> \n </html>";
            HtmlString htmlResult = new HtmlString(result);
            XhtmlAssert.Validate1_1(htmlResult);
        }

        [Fact]
        public void ProfileEncodesSearchTerms()
        {
            // Act 
            string result = Twitter.Profile("'some user'", backgroundShellColor: "\"malformed-color").ToString();

            // Assert
            Assert.True(result.Contains(@"background: '\""malformed-color'"));
            Assert.True(result.Contains("setUser('\\u0027some user\\u0027')"));
        }

        [Fact]
        public void TweetButtonWithDefaultAttributes()
        {
            // Arrange
            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>";

            // Act
            string result = Twitter.TweetButton().ToString();

            // Assert
            UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
        }

        [Fact]
        public void TweetButtonWithSpeicifedAttributes()
        {
            // Arrange
            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>";

            // Act
            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();

            // Assert
            UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
        }

        [Fact]
        public void TweetButtonHtmlEncodesValues()
        {
            // Arrange
            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>";
            // Act
            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();

            // Assert
            UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
        }

        [Fact]
        public void FollowButtonWithDefaultParameters()
        {
            // Arrange
            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>";

            // Act
            string result = Twitter.FollowButton("my-twitter-userName").ToString();

            // Assert
            UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
        }

        [Fact]
        public void FollowButtonWithSpecifiedParmeters()
        {
            // Arrange
            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>";

            // Act
            string result = Twitter.FollowButton("my-twitter-userName", "t_logo", "b").ToString();

            // Assert
            UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
        }

        [Fact]
        public void FollowButtonEncodesParameters()
        {
            // Arrange
            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>";

            // Act
            string result = Twitter.FollowButton("http://my-twitter-userName<script", "t_logo", "b").ToString();

            // Assert
            UnitTestHelper.AssertEqualsIgnoreWhitespace(result, expected);
        }

        [Fact]
        public void FavesReturnsValidData()
        {
            // Arrange
            string twitterUserName = "my-userName";
            string title = "my-title";
            string caption = "my-caption";
            int width = 100;
            int height = 100;
            string backgroundShellColor = "my-backgroundShellColor";
            string shellColor = "my-shellColor";
            string tweetsBackgroundColor = "tweetsBackgroundColor";
            string tweetsColor = "tweets-color";
            string tweetsLinksColor = "tweets Linkcolor";
            int numTweets = 4;
            bool scrollBar = false;
            bool loop = false;
            bool live = false;
            bool hashTags = false;
            bool timestamp = false;
            bool avatars = false;
            var behavior = "all";
            int searchInterval = 10;

            // Act
            string actual = Twitter.Faves(twitterUserName, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor,
                                          numTweets, scrollBar, loop, live, hashTags, timestamp, avatars, "all", searchInterval).ToString();

            // Assert
            var json = GetTwitterJSObject(actual);
            Assert.Equal(json.type, "faves");
            Assert.Equal(json.interval, 1000 * searchInterval);
            Assert.Equal(json.width.ToString(), width.ToString());
            Assert.Equal(json.height.ToString(), height.ToString());
            Assert.Equal(json.title, title);
            Assert.Equal(json.subject, caption);
            Assert.Equal(json.theme.shell.background, backgroundShellColor);
            Assert.Equal(json.theme.shell.color, shellColor);
            Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
            Assert.Equal(json.theme.tweets.color, tweetsColor);
            Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
            Assert.Equal(json.features.scrollbar, scrollBar);
            Assert.Equal(json.features.loop, loop);
            Assert.Equal(json.features.live, live);
            Assert.Equal(json.features.hashtags, hashTags);
            Assert.Equal(json.features.avatars, avatars);
            Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
            Assert.True(actual.Contains(".setUser('my-userName')"));
        }

        [Fact]
        public void FavesJavascriptEncodesParameters()
        {
            // Arrange
            string twitterUserName = "<my-userName>";
            string title = "<my-title>";
            string caption = "<my-caption>";
            int width = 100;
            int height = 100;
            string backgroundShellColor = "<my-backgroundShellColor>";
            string shellColor = "<my-shellColor>";
            string tweetsBackgroundColor = "<tweetsBackgroundColor>";
            string tweetsColor = "<tweets-color>";
            string tweetsLinksColor = "<tweets Linkcolor>";

            // Act
            string actual = Twitter.Faves(twitterUserName, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();

            // Assert
            Assert.True(actual.Contains("title: '\\u003cmy-title\\u003e'"));
            Assert.True(actual.Contains("subject: '\\u003cmy-caption\\u003e'"));
            Assert.True(actual.Contains("background: '\\u003cmy-backgroundShellColor\\u003e'"));
            Assert.True(actual.Contains("color: '\\u003cmy-shellColor\\u003e'"));
            Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor\\u003e'"));
            Assert.True(actual.Contains("color: '\\u003ctweets-color\\u003e"));
            Assert.True(actual.Contains("links: '\\u003ctweets Linkcolor\\u003e'"));
            Assert.True(actual.Contains(".setUser('\\u003cmy-userName\\u003e')"));
        }

        [Fact]
        public void FavesRendersRendersValidXhtml()
        {
            string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
                            Twitter.Faves("any<>Name") +
                            "\n </body> \n </html>";
            HtmlString htmlResult = new HtmlString(result);
            XhtmlAssert.Validate1_1(htmlResult);
        }

        [Fact]
        public void ListReturnsValidData()
        {
            // Arrange
            string twitterUserName = "my-userName";
            string list = "my-list";
            string title = "my-title";
            string caption = "my-caption";
            int width = 100;
            int height = 100;
            string backgroundShellColor = "my-backgroundShellColor";
            string shellColor = "my-shellColor";
            string tweetsBackgroundColor = "tweetsBackgroundColor";
            string tweetsColor = "tweets-color";
            string tweetsLinksColor = "tweets Linkcolor";
            int numTweets = 4;
            bool scrollBar = false;
            bool loop = false;
            bool live = false;
            bool hashTags = false;
            bool timestamp = false;
            bool avatars = false;
            var behavior = "all";
            int searchInterval = 10;

            // Act
            string actual = Twitter.List(twitterUserName, list, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor,
                                         numTweets, scrollBar, loop, live, hashTags, timestamp, avatars, "all", searchInterval).ToString();

            // Assert
            var json = GetTwitterJSObject(actual);
            Assert.Equal(json.type, "list");
            Assert.Equal(json.interval, 1000 * searchInterval);
            Assert.Equal(json.width.ToString(), width.ToString());
            Assert.Equal(json.height.ToString(), height.ToString());
            Assert.Equal(json.title, title);
            Assert.Equal(json.subject, caption);
            Assert.Equal(json.theme.shell.background, backgroundShellColor);
            Assert.Equal(json.theme.shell.color, shellColor);
            Assert.Equal(json.theme.tweets.background, tweetsBackgroundColor);
            Assert.Equal(json.theme.tweets.color, tweetsColor);
            Assert.Equal(json.theme.tweets.links, tweetsLinksColor);
            Assert.Equal(json.features.scrollbar, scrollBar);
            Assert.Equal(json.features.loop, loop);
            Assert.Equal(json.features.live, live);
            Assert.Equal(json.features.hashtags, hashTags);
            Assert.Equal(json.features.avatars, avatars);
            Assert.Equal(json.features.behavior, behavior.ToLowerInvariant());
            Assert.True(actual.Contains(".setList('my-userName', 'my-list')"));
        }

        [Fact]
        public void ListJavascriptEncodesParameters()
        {
            // Arrange
            string twitterUserName = "<my-userName>";
            string list = "<my-list>";
            string title = "<my-title>";
            string caption = "<my-caption>";
            int width = 100;
            int height = 100;
            string backgroundShellColor = "<my-backgroundShellColor>";
            string shellColor = "<my-shellColor>";
            string tweetsBackgroundColor = "<tweetsBackgroundColor>";
            string tweetsColor = "<tweets-color>";
            string tweetsLinksColor = "<tweets Linkcolor>";

            // Act
            string actual = Twitter.List(twitterUserName, list, width, height, title, caption, backgroundShellColor, shellColor, tweetsBackgroundColor, tweetsColor, tweetsLinksColor).ToString();

            // Assert
            Assert.True(actual.Contains("title: '\\u003cmy-title\\u003e'"));
            Assert.True(actual.Contains("subject: '\\u003cmy-caption\\u003e'"));
            Assert.True(actual.Contains("background: '\\u003cmy-backgroundShellColor\\u003e'"));
            Assert.True(actual.Contains("color: '\\u003cmy-shellColor\\u003e'"));
            Assert.True(actual.Contains("background: '\\u003ctweetsBackgroundColor\\u003e'"));
            Assert.True(actual.Contains("color: '\\u003ctweets-color\\u003e"));
            Assert.True(actual.Contains("links: '\\u003ctweets Linkcolor\\u003e'"));
            Assert.True(actual.Contains(".setList('\\u003cmy-userName\\u003e', '\\u003cmy-list\\u003e')"));
        }

        [Fact]
        public void ListRendersRendersValidXhtml()
        {
            string result = "<html> <head> \n <title> </title> \n </head> \n <body> \n" +
                            Twitter.List("any<>Name", "my-list") +
                            "\n </body> \n </html>";
            HtmlString htmlResult = new HtmlString(result);
            XhtmlAssert.Validate1_1(htmlResult);
        }

        private static dynamic GetTwitterJSObject(string twitterOutput)
        {
            const string startString = "Widget(";
            int start = twitterOutput.IndexOf(startString) + startString.Length, end = twitterOutput.IndexOf(')');
            return Json.Decode(twitterOutput.Substring(start, end - start));
        }
    }
}