-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathwiki_api_spec.rb
More file actions
239 lines (207 loc) · 8.32 KB
/
wiki_api_spec.rb
File metadata and controls
239 lines (207 loc) · 8.32 KB
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
# frozen_string_literal: true
require 'rails_helper'
require "#{Rails.root}/lib/wiki_api"
class UnexpectedError < StandardError; end
describe WikiApi do
describe 'handles errors by calling ApiErrorHandling method and raising a PageFetchError' do
let(:subject) { described_class.new.get_page_content('Ragesoss') }
it 'handles mediawiki 503 errors gracefully' do
stub_wikipedia_503_error
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: 503/
)
end
it 'handles timeout errors gracefully' do
allow_any_instance_of(MediawikiApi::Client).to receive(:send)
.and_raise(Faraday::TimeoutError)
expect_any_instance_of(described_class).to receive(:log_error).once
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: nil/
)
end
it 'handles API errors gracefully' do
allow_any_instance_of(MediawikiApi::Client).to receive(:send)
.and_raise(MediawikiApi::ApiError)
expect_any_instance_of(described_class).to receive(:log_error).once
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: nil/
)
end
it 'handles HTTP errors gracefully' do
allow_any_instance_of(MediawikiApi::Client).to receive(:send)
.and_raise(MediawikiApi::HttpError, '')
expect_any_instance_of(described_class).to receive(:log_error).once
expect { subject }.to raise_error(
WikiApi::PageFetchError,
/Failed to fetch content for Ragesoss with response status: nil/
)
end
end
describe '#get_page_content' do
it 'returns the content of a page' do
VCR.use_cassette 'wiki/course_list' do
title = 'Wikipedia:Education program/Dashboard/test_ids'
response = described_class.new.get_page_content(title)
expect(response).to eq("439\n456\n351")
end
end
it 'returns a UTF-8 string' do
VCR.use_cassette 'wiki/utf8' do
title = 'Montreal'
response = described_class.new.get_page_content(title)
expect(response.encoding.name).to eq('UTF-8')
expect(response).to include('Montréal')
end
end
# This is really a test that User#talk_page is returning the format
# expected by the API, and not a URL-encoded page title.
it 'gets the content of a user talk page with special characters' do
user = build(:user, username: 'Julie209!')
VCR.use_cassette 'wiki/user_talk' do
response = described_class.new.get_page_content(user.talk_page)
expect(response).not_to be_blank
end
end
end
describe '#fetch_all' do
it 'returns the same data as a single complete query would' do
VCR.use_cassette 'wiki/continue_response' do
titles = %(apple Fruit ecosystem Pear)
# With a low palimit, this query will need to continue
continue_query = { titles:,
prop: 'pageassessments',
redirects: 'true',
palimit: 2 }
# With a high palimit, this query will not need to continue
complete_query = continue_query.merge(palimit: 50)
complete = described_class.new.send(:fetch_all, complete_query)
continue = described_class.new.send(:fetch_all, continue_query)
expect(complete).to eq(continue)
end
end
end
describe '#get_article_ratings' do
it 'returns the ratings of articles' do
VCR.use_cassette 'wiki/article_ratings' do
# A single article
response = described_class.new.get_article_rating('History_of_biology')
expect(response['History_of_biology']).to eq('fa')
# A single non-existant article
response = described_class.new.get_article_rating('THIS_IS_NOT_A_REAL_ARTICLE_TITLE')
expect(response['THIS_IS_NOT_A_REAL_ARTICLE_TITLE']).to eq(nil)
# A mix of existing and non-existant, including ones with niche ratings.
# Some of these ratings may change over time.
articles = [
'History_of_biology', # fa
'A_Clash_of_Kings', # c
'Ecology', # ga
'Fast_inverse_square_root', # ga
'Latericaecum', # unassessed
'List_of_Oregon_ballot_measures', # list
'The_American_Monomyth', # stub
'Drug_Trafficking_Safe_Harbor_Elimination_Act', # start
'Energy_policy_of_the_United_States', # b
'List_of_camouflage_methods', # fl
'THIS_IS_NOT_A_REAL_ARTICLE_TITLE', # does not exist
'1804_Snow_hurricane', # a
'Barton_S._Alexander', # a
'Bell_number', # b, formerly bplus
'List_of_Canadian_plants_by_family_S', # sl
'Antarctica_(disambiguation)', # disambig
'2015_Pacific_typhoon_season', # start
'Sex_trafficking', # c
'American_Civil_War_prison_camps' # cl, but reported as c
]
response = described_class.new.get_article_rating(articles)
expect(response['History_of_biology']).to eq('fa')
expect(response['THIS_IS_NOT_A_REAL_ARTICLE_TITLE']).to eq(nil)
expect(response['Bell_number']).to eq('b')
end
end
end
describe '#get_user_id' do
context 'for an English Wikipedia users' do
let(:wiki) { Wiki.new(language: 'en', project: 'wikipedia') }
it 'returns the correct user_id for all types of usernames' do
usernames = { 'Ragesoss' => 319203,
'LiAnna (Wiki Ed)' => 21102089, # spaces and parens
'ערן' => 7201119, # Hebrew characters
'JRicker,PhD' => 17137867, # comma
'Evol&Glass' => 22403865, # ampersand
"Jack's nomadic mind" => 26211578, # apostrophe
'!!Aaapplesauce' => 11274650 } # exclamation
VCR.use_cassette 'wiki/get_user_id_en_wiki' do
usernames.each do |username, id|
result = described_class.new(wiki).get_user_id(username)
expect(result).to eq(id)
end
end
end
end
context 'for a Spanish Wikipedia user' do
let(:wiki) { Wiki.new(language: 'es', project: 'wikipedia') }
let(:username) { 'Ragesoss' }
it 'returns the correct user_id' do
VCR.use_cassette 'wiki/get_user_id_es_wiki' do
result = described_class.new(wiki).get_user_id(username)
expect(result).to eq(772153)
end
end
end
it 'returns nil for usernames that do not exist' do
VCR.use_cassette 'wiki/get_user_id_nonexistent' do
username = 'RagesossRagesossRagesoss'
user_id = described_class.new.get_user_id(username)
expect(user_id).to be_nil
end
end
end
describe '#get_user_info' do
let(:wiki) { Wiki.new(language: 'en', project: 'wikipedia') }
context 'when mediawiki query returns nil' do
it 'returns early without raising an error' do
allow_any_instance_of(described_class).to receive(:mediawiki).and_return(nil)
expect { described_class.new.get_user_info('Ragesoss') }.not_to raise_error
expect(described_class.new.get_user_info('Ragesoss')).to be_nil
end
end
end
describe '#redirect?' do
let(:wiki) { Wiki.new(language: 'en', project: 'wikipedia') }
let(:subject) { described_class.new(wiki).redirect?(title) }
context 'when title is a redirect' do
let(:title) { 'Athletics_in_Epic_Poetry' }
it 'returns true' do
VCR.use_cassette 'wiki/redirect' do
expect(subject).to eq(true)
end
end
end
context 'when title is not a redirect' do
let(:title) { 'Selfie' }
it 'returns false' do
VCR.use_cassette 'wiki/redirect' do
expect(subject).to eq(false)
end
end
end
context 'when title does not exist' do
let(:title) { 'THIS_PAGE_DOES_NOT_EXIST' }
it 'returns false' do
VCR.use_cassette 'wiki/redirect' do
expect(subject).to eq(false)
end
end
end
context 'when no data is returned' do
let(:title) { 'Athletics_in_Epic_Poetry' }
it 'returns false' do
stub_request(:any, /.*/).to_return(status: 200, body: '{}', headers: {})
expect(subject).to eq(false)
end
end
end
end