1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
25 using System.Net.Http;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
29 using System.Threading.Tasks;
31 using OpenTween.Api.DataModel;
32 using OpenTween.Connection;
35 namespace OpenTween.Api
37 public class TwitterApiTest
39 public TwitterApiTest()
44 public void MyCommonSetup()
46 var mockAssembly = new Mock<_Assembly>();
47 mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
49 MyCommon.EntryAssembly = mockAssembly.Object;
53 public void Initialize_Test()
55 using (var twitterApi = new TwitterApi())
57 Assert.Null(twitterApi.apiConnection);
59 twitterApi.Initialize("*** AccessToken ***", "*** AccessSecret ***", userId: 100L, screenName: "hogehoge");
61 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
63 var apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
64 Assert.Equal("*** AccessToken ***", apiConnection.AccessToken);
65 Assert.Equal("*** AccessSecret ***", apiConnection.AccessSecret);
67 Assert.Equal(100L, twitterApi.CurrentUserId);
68 Assert.Equal("hogehoge", twitterApi.CurrentScreenName);
70 // 複数回 Initialize を実行した場合は新たに TwitterApiConnection が生成される
71 twitterApi.Initialize("*** AccessToken2 ***", "*** AccessSecret2 ***", userId: 200L, screenName: "foobar");
73 var oldApiConnection = apiConnection;
74 Assert.True(oldApiConnection.IsDisposed);
76 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
78 apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
79 Assert.Equal("*** AccessToken2 ***", apiConnection.AccessToken);
80 Assert.Equal("*** AccessSecret2 ***", apiConnection.AccessSecret);
82 Assert.Equal(200L, twitterApi.CurrentUserId);
83 Assert.Equal("foobar", twitterApi.CurrentScreenName);
88 public async Task StatusesShow_Test()
90 using (var twitterApi = new TwitterApi())
92 var mock = new Mock<IApiConnection>();
94 x.GetAsync<TwitterStatus>(
95 new Uri("statuses/show.json", UriKind.Relative),
96 new Dictionary<string, string> {
98 { "include_entities", "true" },
99 { "include_ext_alt_text", "true" },
101 "/statuses/show/:id")
103 .ReturnsAsync(new TwitterStatus { Id = 100L });
105 twitterApi.apiConnection = mock.Object;
107 await twitterApi.StatusesShow(statusId: 100L)
108 .ConfigureAwait(false);
115 public async Task StatusesUpdate_Test()
117 using (var twitterApi = new TwitterApi())
119 var mock = new Mock<IApiConnection>();
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" },
131 .ReturnsAsync(LazyJson.Create(new TwitterStatus()));
133 twitterApi.apiConnection = mock.Object;
135 await twitterApi.StatusesUpdate("hogehoge", replyToId: 100L, mediaIds: new[] { 10L, 20L })
137 .ConfigureAwait(false);
144 public async Task StatusesDestroy_Test()
146 using (var twitterApi = new TwitterApi())
148 var mock = new Mock<IApiConnection>();
150 x.PostLazyAsync<TwitterStatus>(
151 new Uri("statuses/destroy.json", UriKind.Relative),
152 new Dictionary<string, string> { { "id", "100" } })
154 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
156 twitterApi.apiConnection = mock.Object;
158 await twitterApi.StatusesDestroy(statusId: 100L)
160 .ConfigureAwait(false);
167 public async Task DirectMessagesNew_Test()
169 using (var twitterApi = new TwitterApi())
171 var mock = new Mock<IApiConnection>();
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" },
180 .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));
182 twitterApi.apiConnection = mock.Object;
184 await twitterApi.DirectMessagesNew("hogehoge", "opentween")
186 .ConfigureAwait(false);
193 public async Task DirectMessagesDestroy_Test()
195 using (var twitterApi = new TwitterApi())
197 var mock = new Mock<IApiConnection>();
199 x.PostLazyAsync<TwitterDirectMessage>(
200 new Uri("direct_messages/destroy.json", UriKind.Relative),
201 new Dictionary<string, string> { { "id", "100" } })
203 .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage { Id = 100L }));
205 twitterApi.apiConnection = mock.Object;
207 await twitterApi.DirectMessagesDestroy(statusId: 100L)
209 .ConfigureAwait(false);
216 public async Task UsersShow_Test()
218 using (var twitterApi = new TwitterApi())
220 var mock = new Mock<IApiConnection>();
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" },
231 .ReturnsAsync(new TwitterUser { ScreenName = "twitterapi" });
233 twitterApi.apiConnection = mock.Object;
235 await twitterApi.UsersShow(screenName: "twitterapi")
236 .ConfigureAwait(false);
243 public async Task UsersReportSpam_Test()
245 using (var twitterApi = new TwitterApi())
247 var mock = new Mock<IApiConnection>();
249 x.PostLazyAsync<TwitterUser>(
250 new Uri("users/report_spam.json", UriKind.Relative),
251 new Dictionary<string, string> { { "screen_name", "twitterapi" } })
253 .ReturnsAsync(LazyJson.Create(new TwitterUser { ScreenName = "twitterapi" }));
255 twitterApi.apiConnection = mock.Object;
257 await twitterApi.UsersReportSpam(screenName: "twitterapi")
259 .ConfigureAwait(false);
266 public async Task FavoritesCreate_Test()
268 using (var twitterApi = new TwitterApi())
270 var mock = new Mock<IApiConnection>();
272 x.PostLazyAsync<TwitterStatus>(
273 new Uri("favorites/create.json", UriKind.Relative),
274 new Dictionary<string, string> { { "id", "100" } })
276 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
278 twitterApi.apiConnection = mock.Object;
280 await twitterApi.FavoritesCreate(statusId: 100L)
282 .ConfigureAwait(false);
289 public async Task FavoritesDestroy_Test()
291 using (var twitterApi = new TwitterApi())
293 var mock = new Mock<IApiConnection>();
295 x.PostLazyAsync<TwitterStatus>(
296 new Uri("favorites/destroy.json", UriKind.Relative),
297 new Dictionary<string, string> { { "id", "100" } })
299 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
301 twitterApi.apiConnection = mock.Object;
303 await twitterApi.FavoritesDestroy(statusId: 100L)
305 .ConfigureAwait(false);
312 public async Task FriendshipsShow_Test()
314 using (var twitterApi = new TwitterApi())
316 var mock = new Mock<IApiConnection>();
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" } },
323 .ReturnsAsync(new TwitterFriendship());
325 twitterApi.apiConnection = mock.Object;
327 await twitterApi.FriendshipsShow(sourceScreenName: "twitter", targetScreenName: "twitterapi")
328 .ConfigureAwait(false);
335 public async Task FriendshipsCreate_Test()
337 using (var twitterApi = new TwitterApi())
339 var mock = new Mock<IApiConnection>();
341 x.PostLazyAsync<TwitterFriendship>(
342 new Uri("friendships/create.json", UriKind.Relative),
343 new Dictionary<string, string> { { "screen_name", "twitterapi" } })
345 .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));
347 twitterApi.apiConnection = mock.Object;
349 await twitterApi.FriendshipsCreate(screenName: "twitterapi")
351 .ConfigureAwait(false);
358 public async Task FriendshipsDestroy_Test()
360 using (var twitterApi = new TwitterApi())
362 var mock = new Mock<IApiConnection>();
364 x.PostLazyAsync<TwitterFriendship>(
365 new Uri("friendships/destroy.json", UriKind.Relative),
366 new Dictionary<string, string> { { "screen_name", "twitterapi" } })
368 .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));
370 twitterApi.apiConnection = mock.Object;
372 await twitterApi.FriendshipsDestroy(screenName: "twitterapi")
374 .ConfigureAwait(false);
381 public async Task FollowersIds_Test()
383 using (var twitterApi = new TwitterApi())
385 var mock = new Mock<IApiConnection>();
387 x.GetAsync<TwitterIds>(
388 new Uri("followers/ids.json", UriKind.Relative),
389 new Dictionary<string, string> { { "cursor", "-1" } },
392 .ReturnsAsync(new TwitterIds());
394 twitterApi.apiConnection = mock.Object;
396 await twitterApi.FollowersIds(cursor: -1L)
397 .ConfigureAwait(false);
404 public async Task BlocksCreate_Test()
406 using (var twitterApi = new TwitterApi())
408 var mock = new Mock<IApiConnection>();
410 x.PostLazyAsync<TwitterUser>(
411 new Uri("blocks/create.json", UriKind.Relative),
412 new Dictionary<string, string> { { "screen_name", "twitterapi" } })
414 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
416 twitterApi.apiConnection = mock.Object;
418 await twitterApi.BlocksCreate(screenName: "twitterapi")
420 .ConfigureAwait(false);
427 public async Task BlocksDestroy_Test()
429 using (var twitterApi = new TwitterApi())
431 var mock = new Mock<IApiConnection>();
433 x.PostLazyAsync<TwitterUser>(
434 new Uri("blocks/destroy.json", UriKind.Relative),
435 new Dictionary<string, string> { { "screen_name", "twitterapi" } })
437 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
439 twitterApi.apiConnection = mock.Object;
441 await twitterApi.BlocksDestroy(screenName: "twitterapi")
443 .ConfigureAwait(false);
450 public async Task AccountUpdateProfile_Test()
452 using (var twitterApi = new TwitterApi())
454 var mock = new Mock<IApiConnection>();
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" },
462 { "url", "http://example.com/" },
463 { "location", "Location" },
464 { "description", "<script>alert(1)</script>" },
467 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
469 twitterApi.apiConnection = mock.Object;
471 await twitterApi.AccountUpdateProfile(name: "Name", url: "http://example.com/", location: "Location", description: "<script>alert(1)</script>")
473 .ConfigureAwait(false);
480 public async Task AccountUpdateProfileImage_Test()
482 using (var twitterApi = new TwitterApi())
483 using (var image = TestUtils.CreateDummyImage())
484 using (var media = new MemoryImageMediaItem(image))
486 var mock = new Mock<IApiConnection>();
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" },
494 new Dictionary<string, IMediaItem> { { "image", media } })
496 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
498 twitterApi.apiConnection = mock.Object;
500 await twitterApi.AccountUpdateProfileImage(media)
502 .ConfigureAwait(false);
509 public async Task MediaUpload_Test()
511 using (var twitterApi = new TwitterApi())
512 using (var image = TestUtils.CreateDummyImage())
513 using (var media = new MemoryImageMediaItem(image))
515 var mock = new Mock<IApiConnection>();
517 x.PostLazyAsync<TwitterUploadMediaResult>(
518 new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
520 new Dictionary<string, IMediaItem> { { "media", media } })
522 .ReturnsAsync(LazyJson.Create(new TwitterUploadMediaResult()));
524 twitterApi.apiConnection = mock.Object;
526 await twitterApi.MediaUpload(media)
528 .ConfigureAwait(false);