OSDN Git Service

API v1.1 に対応
[opentween/open-tween.git] / OpenTween.Tests / Api / TwitterApiStatusTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 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.Text;
26 using NUnit.Framework;
27
28 namespace OpenTween.Api
29 {
30     [TestFixture]
31     class TwitterApiStatusTest
32     {
33         [Test]
34         public void ResetTest()
35         {
36             var apiStatus = new TwitterApiStatus();
37
38             apiStatus.AccessLimit = new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0));
39             apiStatus.MediaUploadLimit = new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0));
40             apiStatus.AccessLevel = TwitterApiAccessLevel.ReadWriteAndDirectMessage;
41
42             apiStatus.Reset();
43
44             Assert.That(apiStatus.AccessLimit, Is.Null);
45             Assert.That(apiStatus.MediaUploadLimit, Is.Null);
46             Assert.That(apiStatus.AccessLevel, Is.EqualTo(TwitterApiAccessLevel.Anonymous));
47         }
48
49         public static object[] ParseRateLimit_TestCase =
50         {
51             new object[] {
52                 new Dictionary<string, string> {
53                     {"X-RateLimit-Limit", "150"},
54                     {"X-RateLimit-Remaining", "100"},
55                     {"X-RateLimit-Reset", "1356998400"},
56                 },
57                 new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()),
58             },
59             new object[] {
60                 new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
61                     {"x-ratelimit-limit", "150"},
62                     {"x-ratelimit-remaining", "100"},
63                     {"x-ratelimit-reset", "1356998400"},
64                 },
65                 new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()),
66             },
67             new object[] {
68                 new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
69                     {"X-RateLimit-Limit", "150"},
70                     {"X-RateLimit-Remaining", "100"},
71                     {"X-RateLimit-Reset", "hogehoge"},
72                 },
73                 null,
74             },
75             new object[] {
76                 new Dictionary<string, string> {
77                     {"X-RateLimit-Limit", "150"},
78                     {"X-RateLimit-Remaining", "100"},
79                 },
80                 null,
81             },
82         };
83         [TestCaseSource("ParseRateLimit_TestCase")]
84         public void ParseRateLimitTest(IDictionary<string, string> header, ApiLimit expect)
85         {
86             var limit = TwitterApiStatus.ParseRateLimit(header, "X-RateLimit");
87             Assert.That(limit, Is.EqualTo(expect));
88         }
89
90         public static object[] ParseMediaRateLimit_TestCase =
91         {
92             new object[] {
93                 new Dictionary<string, string> {
94                     {"X-MediaRateLimit-Limit", "30"},
95                     {"X-MediaRateLimit-Remaining", "20"},
96                     {"X-MediaRateLimit-Reset", "1234567890"},
97                 },
98                 new ApiLimit(30, 20, new DateTime(2009, 2, 13, 23, 31, 30, DateTimeKind.Utc).ToLocalTime()),
99             },
100             new object[] {
101                 new Dictionary<string, string> {
102                     {"X-MediaRateLimit-Limit", "30"},
103                     {"X-MediaRateLimit-Remaining", "20"},
104                     {"X-MediaRateLimit-Reset", "hogehoge"},
105                 },
106                 null,
107             },
108             new object[] {
109                 new Dictionary<string, string> {
110                     {"X-MediaRateLimit-Limit", "30"},
111                     {"X-MediaRateLimit-Remaining", "20"},
112                 },
113                 null,
114             },
115         };
116         [TestCaseSource("ParseMediaRateLimit_TestCase")]
117         public void ParseMediaRateLimitTest(IDictionary<string, string> header, ApiLimit expect)
118         {
119             var limit = TwitterApiStatus.ParseRateLimit(header, "X-MediaRateLimit-");
120             Assert.That(limit, Is.EqualTo(expect));
121         }
122
123         public static object[] ParseAccessLevel_TestCase =
124         {
125             new object[] {
126                 new Dictionary<string, string> { {"X-Access-Level", "read"} },
127                 TwitterApiAccessLevel.Read,
128             },
129             new object[] {
130                 new Dictionary<string, string> { {"X-Access-Level", "read-write"} },
131                 TwitterApiAccessLevel.ReadWrite,
132             },
133             new object[] {
134                 new Dictionary<string, string> { {"X-Access-Level", "read-write-directmessages"} },
135                 TwitterApiAccessLevel.ReadWriteAndDirectMessage,
136             },
137             new object[] {
138                 new Dictionary<string, string> { {"X-Access-Level", ""} }, // 何故かたまに出てくるやつ
139                 null,
140             },
141             new object[] {
142                 new Dictionary<string, string> { },
143                 null,
144             },
145         };
146         [TestCaseSource("ParseAccessLevel_TestCase")]
147         public void ParseAccessLevelTest(IDictionary<string, string> header, TwitterApiAccessLevel? expect)
148         {
149             var accessLevel = TwitterApiStatus.ParseAccessLevel(header, "X-Access-Level");
150             Assert.That(accessLevel, Is.EqualTo(expect));
151         }
152
153         [Test]
154         public void UpdateFromHeaderTest()
155         {
156             var status = new TwitterApiStatus();
157
158             var eventCalled = false;
159             status.AccessLimitUpdated += (s, e) => eventCalled = true;
160
161             var header = new Dictionary<string, string>
162             {
163                 {"X-RateLimit-Limit", "150"},
164                 {"X-RateLimit-Remaining", "100"},
165                 {"X-RateLimit-Reset", "1356998400"},
166                 {"X-MediaRateLimit-Limit", "30"},
167                 {"X-MediaRateLimit-Remaining", "20"},
168                 {"X-MediaRateLimit-Reset", "1357084800"},
169                 {"X-Access-Level", "read-write-directmessages"},
170             };
171
172             status.UpdateFromHeader(header);
173
174             var rateLimit = status.AccessLimit;
175             Assert.That(rateLimit.AccessLimitCount, Is.EqualTo(150));
176             Assert.That(rateLimit.AccessLimitRemain, Is.EqualTo(100));
177             Assert.That(rateLimit.AccessLimitResetDate, Is.EqualTo(new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()));
178
179             var mediaLimit = status.MediaUploadLimit;
180             Assert.That(mediaLimit.AccessLimitCount, Is.EqualTo(30));
181             Assert.That(mediaLimit.AccessLimitRemain, Is.EqualTo(20));
182             Assert.That(mediaLimit.AccessLimitResetDate, Is.EqualTo(new DateTime(2013, 1, 2, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()));
183
184             Assert.That(status.AccessLevel, Is.EqualTo(TwitterApiAccessLevel.ReadWriteAndDirectMessage));
185
186             Assert.That(eventCalled, Is.True);
187         }
188
189         [Test]
190         public void UpdateFromApiTest()
191         {
192             var status = new TwitterApiStatus();
193
194             var eventCalled = false;
195             status.AccessLimitUpdated += (s, e) => eventCalled = true;
196
197             var apiResponse = new TwitterDataModel.RateLimitStatus
198             {
199                 HourlyLimit = 150,
200                 RemainingHits = 100,
201                 ResetTime = "Tue Jan 01 00:00:00 +0000 2013",
202                 ResetTimeInSeconds = 1356998400,
203                 Photos = new TwitterDataModel.MediaRateLimitStatus
204                 {
205                     DailyLimit = 30,
206                     RemainingHits = 20,
207                     ResetTime = "Wed Jan 02 00:00:00 +0000 2013",
208                     RestTimeInSeconds = 1357084800,
209                 },
210             };
211
212             status.UpdateFromApi(apiResponse);
213
214             var rateLimit = status.AccessLimit;
215             Assert.That(rateLimit.AccessLimitCount, Is.EqualTo(150));
216             Assert.That(rateLimit.AccessLimitRemain, Is.EqualTo(100));
217             Assert.That(rateLimit.AccessLimitResetDate, Is.EqualTo(new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()));
218
219             var mediaLimit = status.MediaUploadLimit;
220             Assert.That(mediaLimit.AccessLimitCount, Is.EqualTo(30));
221             Assert.That(mediaLimit.AccessLimitRemain, Is.EqualTo(20));
222             Assert.That(mediaLimit.AccessLimitResetDate, Is.EqualTo(new DateTime(2013, 1, 2, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()));
223
224             Assert.That(eventCalled, Is.True);
225         }
226
227         [Test]
228         public void UpdateFromApiTest2()
229         {
230             var status = new TwitterApiStatus();
231
232             var eventCalled = false;
233             status.AccessLimitUpdated += (s, e) => eventCalled = true;
234
235             var apiResponse = new TwitterDataModel.RateLimitStatus
236             {
237                 HourlyLimit = 150,
238                 RemainingHits = 100,
239                 ResetTime = "Tue Jan 01 00:00:00 +0000 2013",
240                 ResetTimeInSeconds = 1356998400,
241                 Photos = null,
242             };
243
244             status.UpdateFromApi(apiResponse);
245
246             var rateLimit = status.AccessLimit;
247             Assert.That(rateLimit.AccessLimitCount, Is.EqualTo(150));
248             Assert.That(rateLimit.AccessLimitRemain, Is.EqualTo(100));
249             Assert.That(rateLimit.AccessLimitResetDate, Is.EqualTo(new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()));
250
251             Assert.That(status.MediaUploadLimit, Is.Null);
252
253             Assert.That(eventCalled, Is.True);
254         }
255
256         [Test]
257         public void UpdateFromApiTest3()
258         {
259             var status = new TwitterApiStatus();
260
261             var eventCalled = false;
262             status.AccessLimitUpdated += (s, e) => eventCalled = true;
263
264             Assert.That(() => status.UpdateFromApi(null), Throws.TypeOf<ArgumentNullException>());
265
266             Assert.That(status.AccessLimit, Is.Null);
267             Assert.That(status.MediaUploadLimit, Is.Null);
268
269             Assert.That(eventCalled, Is.False);
270         }
271
272         [Test]
273         public void AccessLimitUpdatedTest()
274         {
275             var apiStatus = new TwitterApiStatus();
276
277             var eventCount = 0;
278             apiStatus.AccessLimitUpdated += (s, e) => eventCount++;
279
280             Assert.That(eventCount, Is.EqualTo(0));
281
282             apiStatus.AccessLimit = new ApiLimit(150, 100, new DateTime(2013, 1, 1, 0, 0, 0));
283             Assert.That(eventCount, Is.EqualTo(1));
284
285             apiStatus.Reset();
286             Assert.That(eventCount, Is.EqualTo(2));
287         }
288     }
289 }