OSDN Git Service

HttpTwitter.FollowerIdsメソッドをTwitterApiクラスに置き換え
[opentween/open-tween.git] / OpenTween.Tests / Api / TwitterApiTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Net.Http;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
28 using System.Text;
29 using System.Threading.Tasks;
30 using Moq;
31 using OpenTween.Api.DataModel;
32 using OpenTween.Connection;
33 using Xunit;
34
35 namespace OpenTween.Api
36 {
37     public class TwitterApiTest
38     {
39         public TwitterApiTest()
40         {
41             this.MyCommonSetup();
42         }
43
44         public void MyCommonSetup()
45         {
46             var mockAssembly = new Mock<_Assembly>();
47             mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
48
49             MyCommon.EntryAssembly = mockAssembly.Object;
50         }
51
52         [Fact]
53         public void Initialize_Test()
54         {
55             using (var twitterApi = new TwitterApi())
56             {
57                 Assert.Null(twitterApi.apiConnection);
58
59                 twitterApi.Initialize("*** AccessToken ***", "*** AccessSecret ***", userId: 100L, screenName: "hogehoge");
60
61                 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
62
63                 var apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
64                 Assert.Equal("*** AccessToken ***", apiConnection.AccessToken);
65                 Assert.Equal("*** AccessSecret ***", apiConnection.AccessSecret);
66
67                 Assert.Equal(100L, twitterApi.CurrentUserId);
68                 Assert.Equal("hogehoge", twitterApi.CurrentScreenName);
69
70                 // 複数回 Initialize を実行した場合は新たに TwitterApiConnection が生成される
71                 twitterApi.Initialize("*** AccessToken2 ***", "*** AccessSecret2 ***", userId: 200L, screenName: "foobar");
72
73                 var oldApiConnection = apiConnection;
74                 Assert.True(oldApiConnection.IsDisposed);
75
76                 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
77
78                 apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
79                 Assert.Equal("*** AccessToken2 ***", apiConnection.AccessToken);
80                 Assert.Equal("*** AccessSecret2 ***", apiConnection.AccessSecret);
81
82                 Assert.Equal(200L, twitterApi.CurrentUserId);
83                 Assert.Equal("foobar", twitterApi.CurrentScreenName);
84             }
85         }
86
87         [Fact]
88         public async Task StatusesShow_Test()
89         {
90             using (var twitterApi = new TwitterApi())
91             {
92                 var mock = new Mock<IApiConnection>();
93                 mock.Setup(x =>
94                     x.GetAsync<TwitterStatus>(
95                         new Uri("statuses/show.json", UriKind.Relative),
96                         new Dictionary<string, string> {
97                             { "id", "100" },
98                             { "include_entities", "true" },
99                             { "include_ext_alt_text", "true" },
100                         },
101                         "/statuses/show/:id")
102                 )
103                 .ReturnsAsync(new TwitterStatus { Id = 100L });
104
105                 twitterApi.apiConnection = mock.Object;
106
107                 await twitterApi.StatusesShow(statusId: 100L)
108                     .ConfigureAwait(false);
109
110                 mock.VerifyAll();
111             }
112         }
113
114         [Fact]
115         public async Task StatusesUpdate_Test()
116         {
117             using (var twitterApi = new TwitterApi())
118             {
119                 var mock = new Mock<IApiConnection>();
120                 mock.Setup(x =>
121                     x.PostLazyAsync<TwitterStatus>(
122                         new Uri("statuses/update.json", UriKind.Relative),
123                         new Dictionary<string, string> {
124                             { "status", "hogehoge" },
125                             { "include_entities", "true" },
126                             { "include_ext_alt_text", "true" },
127                             { "in_reply_to_status_id", "100" },
128                             { "media_ids", "10,20" },
129                         })
130                 )
131                 .ReturnsAsync(LazyJson.Create(new TwitterStatus()));
132
133                 twitterApi.apiConnection = mock.Object;
134
135                 await twitterApi.StatusesUpdate("hogehoge", replyToId: 100L, mediaIds: new[] { 10L, 20L })
136                     .IgnoreResponse()
137                     .ConfigureAwait(false);
138
139                 mock.VerifyAll();
140             }
141         }
142
143         [Fact]
144         public async Task StatusesDestroy_Test()
145         {
146             using (var twitterApi = new TwitterApi())
147             {
148                 var mock = new Mock<IApiConnection>();
149                 mock.Setup(x =>
150                     x.PostLazyAsync<TwitterStatus>(
151                         new Uri("statuses/destroy.json", UriKind.Relative),
152                         new Dictionary<string, string> { { "id", "100" } })
153                 )
154                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
155
156                 twitterApi.apiConnection = mock.Object;
157
158                 await twitterApi.StatusesDestroy(statusId: 100L)
159                     .IgnoreResponse()
160                     .ConfigureAwait(false);
161
162                 mock.VerifyAll();
163             }
164         }
165
166         [Fact]
167         public async Task DirectMessagesNew_Test()
168         {
169             using (var twitterApi = new TwitterApi())
170             {
171                 var mock = new Mock<IApiConnection>();
172                 mock.Setup(x =>
173                     x.PostLazyAsync<TwitterDirectMessage>(
174                         new Uri("direct_messages/new.json", UriKind.Relative),
175                         new Dictionary<string, string> {
176                             { "text", "hogehoge" },
177                             { "screen_name", "opentween" },
178                         })
179                 )
180                 .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));
181
182                 twitterApi.apiConnection = mock.Object;
183
184                 await twitterApi.DirectMessagesNew("hogehoge", "opentween")
185                     .IgnoreResponse()
186                     .ConfigureAwait(false);
187
188                 mock.VerifyAll();
189             }
190         }
191
192         [Fact]
193         public async Task DirectMessagesDestroy_Test()
194         {
195             using (var twitterApi = new TwitterApi())
196             {
197                 var mock = new Mock<IApiConnection>();
198                 mock.Setup(x =>
199                     x.PostLazyAsync<TwitterDirectMessage>(
200                         new Uri("direct_messages/destroy.json", UriKind.Relative),
201                         new Dictionary<string, string> { { "id", "100" } })
202                 )
203                 .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage { Id = 100L }));
204
205                 twitterApi.apiConnection = mock.Object;
206
207                 await twitterApi.DirectMessagesDestroy(statusId: 100L)
208                     .IgnoreResponse()
209                     .ConfigureAwait(false);
210
211                 mock.VerifyAll();
212             }
213         }
214
215         [Fact]
216         public async Task UsersShow_Test()
217         {
218             using (var twitterApi = new TwitterApi())
219             {
220                 var mock = new Mock<IApiConnection>();
221                 mock.Setup(x =>
222                     x.GetAsync<TwitterUser>(
223                         new Uri("users/show.json", UriKind.Relative),
224                         new Dictionary<string, string> {
225                             { "screen_name", "twitterapi" },
226                             { "include_entities", "true" },
227                             { "include_ext_alt_text", "true" },
228                         },
229                         "/users/show/:id")
230                 )
231                 .ReturnsAsync(new TwitterUser { ScreenName = "twitterapi" });
232
233                 twitterApi.apiConnection = mock.Object;
234
235                 await twitterApi.UsersShow(screenName: "twitterapi")
236                     .ConfigureAwait(false);
237
238                 mock.VerifyAll();
239             }
240         }
241
242         [Fact]
243         public async Task UsersReportSpam_Test()
244         {
245             using (var twitterApi = new TwitterApi())
246             {
247                 var mock = new Mock<IApiConnection>();
248                 mock.Setup(x =>
249                     x.PostLazyAsync<TwitterUser>(
250                         new Uri("users/report_spam.json", UriKind.Relative),
251                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
252                 )
253                 .ReturnsAsync(LazyJson.Create(new TwitterUser { ScreenName = "twitterapi" }));
254
255                 twitterApi.apiConnection = mock.Object;
256
257                 await twitterApi.UsersReportSpam(screenName: "twitterapi")
258                     .IgnoreResponse()
259                     .ConfigureAwait(false);
260
261                 mock.VerifyAll();
262             }
263         }
264
265         [Fact]
266         public async Task FavoritesCreate_Test()
267         {
268             using (var twitterApi = new TwitterApi())
269             {
270                 var mock = new Mock<IApiConnection>();
271                 mock.Setup(x =>
272                     x.PostLazyAsync<TwitterStatus>(
273                         new Uri("favorites/create.json", UriKind.Relative),
274                         new Dictionary<string, string> { { "id", "100" } })
275                 )
276                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
277
278                 twitterApi.apiConnection = mock.Object;
279
280                 await twitterApi.FavoritesCreate(statusId: 100L)
281                     .IgnoreResponse()
282                     .ConfigureAwait(false);
283
284                 mock.VerifyAll();
285             }
286         }
287
288         [Fact]
289         public async Task FavoritesDestroy_Test()
290         {
291             using (var twitterApi = new TwitterApi())
292             {
293                 var mock = new Mock<IApiConnection>();
294                 mock.Setup(x =>
295                     x.PostLazyAsync<TwitterStatus>(
296                         new Uri("favorites/destroy.json", UriKind.Relative),
297                         new Dictionary<string, string> { { "id", "100" } })
298                 )
299                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
300
301                 twitterApi.apiConnection = mock.Object;
302
303                 await twitterApi.FavoritesDestroy(statusId: 100L)
304                     .IgnoreResponse()
305                     .ConfigureAwait(false);
306
307                 mock.VerifyAll();
308             }
309         }
310
311         [Fact]
312         public async Task FriendshipsShow_Test()
313         {
314             using (var twitterApi = new TwitterApi())
315             {
316                 var mock = new Mock<IApiConnection>();
317                 mock.Setup(x =>
318                     x.GetAsync<TwitterFriendship>(
319                         new Uri("friendships/show.json", UriKind.Relative),
320                         new Dictionary<string, string> { { "source_screen_name", "twitter" }, { "target_screen_name", "twitterapi" } },
321                         "/friendships/show")
322                 )
323                 .ReturnsAsync(new TwitterFriendship());
324
325                 twitterApi.apiConnection = mock.Object;
326
327                 await twitterApi.FriendshipsShow(sourceScreenName: "twitter", targetScreenName: "twitterapi")
328                     .ConfigureAwait(false);
329
330                 mock.VerifyAll();
331             }
332         }
333
334         [Fact]
335         public async Task FriendshipsCreate_Test()
336         {
337             using (var twitterApi = new TwitterApi())
338             {
339                 var mock = new Mock<IApiConnection>();
340                 mock.Setup(x =>
341                     x.PostLazyAsync<TwitterFriendship>(
342                         new Uri("friendships/create.json", UriKind.Relative),
343                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
344                 )
345                 .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));
346
347                 twitterApi.apiConnection = mock.Object;
348
349                 await twitterApi.FriendshipsCreate(screenName: "twitterapi")
350                     .IgnoreResponse()
351                     .ConfigureAwait(false);
352
353                 mock.VerifyAll();
354             }
355         }
356
357         [Fact]
358         public async Task FriendshipsDestroy_Test()
359         {
360             using (var twitterApi = new TwitterApi())
361             {
362                 var mock = new Mock<IApiConnection>();
363                 mock.Setup(x =>
364                     x.PostLazyAsync<TwitterFriendship>(
365                         new Uri("friendships/destroy.json", UriKind.Relative),
366                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
367                 )
368                 .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));
369
370                 twitterApi.apiConnection = mock.Object;
371
372                 await twitterApi.FriendshipsDestroy(screenName: "twitterapi")
373                     .IgnoreResponse()
374                     .ConfigureAwait(false);
375
376                 mock.VerifyAll();
377             }
378         }
379
380         [Fact]
381         public async Task FollowersIds_Test()
382         {
383             using (var twitterApi = new TwitterApi())
384             {
385                 var mock = new Mock<IApiConnection>();
386                 mock.Setup(x =>
387                     x.GetAsync<TwitterIds>(
388                         new Uri("followers/ids.json", UriKind.Relative),
389                         new Dictionary<string, string> { { "cursor", "-1" } },
390                         "/followers/ids")
391                 )
392                 .ReturnsAsync(new TwitterIds());
393
394                 twitterApi.apiConnection = mock.Object;
395
396                 await twitterApi.FollowersIds(cursor: -1L)
397                     .ConfigureAwait(false);
398
399                 mock.VerifyAll();
400             }
401         }
402
403         [Fact]
404         public async Task BlocksCreate_Test()
405         {
406             using (var twitterApi = new TwitterApi())
407             {
408                 var mock = new Mock<IApiConnection>();
409                 mock.Setup(x =>
410                     x.PostLazyAsync<TwitterUser>(
411                         new Uri("blocks/create.json", UriKind.Relative),
412                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
413                 )
414                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
415
416                 twitterApi.apiConnection = mock.Object;
417
418                 await twitterApi.BlocksCreate(screenName: "twitterapi")
419                     .IgnoreResponse()
420                     .ConfigureAwait(false);
421
422                 mock.VerifyAll();
423             }
424         }
425
426         [Fact]
427         public async Task BlocksDestroy_Test()
428         {
429             using (var twitterApi = new TwitterApi())
430             {
431                 var mock = new Mock<IApiConnection>();
432                 mock.Setup(x =>
433                     x.PostLazyAsync<TwitterUser>(
434                         new Uri("blocks/destroy.json", UriKind.Relative),
435                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
436                 )
437                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
438
439                 twitterApi.apiConnection = mock.Object;
440
441                 await twitterApi.BlocksDestroy(screenName: "twitterapi")
442                     .IgnoreResponse()
443                     .ConfigureAwait(false);
444
445                 mock.VerifyAll();
446             }
447         }
448
449         [Fact]
450         public async Task AccountUpdateProfile_Test()
451         {
452             using (var twitterApi = new TwitterApi())
453             {
454                 var mock = new Mock<IApiConnection>();
455                 mock.Setup(x =>
456                     x.PostLazyAsync<TwitterUser>(
457                         new Uri("account/update_profile.json", UriKind.Relative),
458                         new Dictionary<string, string> {
459                             { "include_entities", "true" },
460                             { "include_ext_alt_text", "true" },
461                             { "name", "Name" },
462                             { "url", "http://example.com/" },
463                             { "location", "Location" },
464                             { "description", "&lt;script&gt;alert(1)&lt;/script&gt;" },
465                         })
466                 )
467                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
468
469                 twitterApi.apiConnection = mock.Object;
470
471                 await twitterApi.AccountUpdateProfile(name: "Name", url: "http://example.com/", location: "Location", description: "<script>alert(1)</script>")
472                     .IgnoreResponse()
473                     .ConfigureAwait(false);
474
475                 mock.VerifyAll();
476             }
477         }
478
479         [Fact]
480         public async Task AccountUpdateProfileImage_Test()
481         {
482             using (var twitterApi = new TwitterApi())
483             using (var image = TestUtils.CreateDummyImage())
484             using (var media = new MemoryImageMediaItem(image))
485             {
486                 var mock = new Mock<IApiConnection>();
487                 mock.Setup(x =>
488                     x.PostLazyAsync<TwitterUser>(
489                         new Uri("account/update_profile_image.json", UriKind.Relative),
490                         new Dictionary<string, string> {
491                             { "include_entities", "true" },
492                             { "include_ext_alt_text", "true" },
493                         },
494                         new Dictionary<string, IMediaItem> { { "image", media } })
495                 )
496                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
497
498                 twitterApi.apiConnection = mock.Object;
499
500                 await twitterApi.AccountUpdateProfileImage(media)
501                     .IgnoreResponse()
502                     .ConfigureAwait(false);
503
504                 mock.VerifyAll();
505             }
506         }
507
508         [Fact]
509         public async Task MediaUpload_Test()
510         {
511             using (var twitterApi = new TwitterApi())
512             using (var image = TestUtils.CreateDummyImage())
513             using (var media = new MemoryImageMediaItem(image))
514             {
515                 var mock = new Mock<IApiConnection>();
516                 mock.Setup(x =>
517                     x.PostLazyAsync<TwitterUploadMediaResult>(
518                         new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
519                         null,
520                         new Dictionary<string, IMediaItem> { { "media", media } })
521                 )
522                 .ReturnsAsync(LazyJson.Create(new TwitterUploadMediaResult()));
523
524                 twitterApi.apiConnection = mock.Object;
525
526                 await twitterApi.MediaUpload(media)
527                     .IgnoreResponse()
528                     .ConfigureAwait(false);
529
530                 mock.VerifyAll();
531             }
532         }
533     }
534 }