mediawiki_api 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/mediawiki_api/client.rb +6 -1
- data/lib/mediawiki_api/version.rb +1 -1
- data/spec/client_spec.rb +52 -23
- data/spec/support/request_helpers.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b76c6bc6c035d11d6b3690aba25961df0472eac
|
4
|
+
data.tar.gz: bf35918e838839955bc6e3897ce8bfe865c26d70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33b30d02eb1ab3cf1044875a5903a516e0d55638e5c60c93b29545ef9cfd34270ee32e50a8c0c67e55fadc6451048b084c6d8c44e4107934ca917ace7f62d6df
|
7
|
+
data.tar.gz: 899a279b3499c720ddd38d3c6ce883613d54c4c9f32cb67e4e40098aff9bc7bf8b09d43e1cbddf7b7058c4fe4a162e8a1e1a01335deaafb8c4ca42dbbc75b4fc
|
data/README.md
CHANGED
@@ -65,6 +65,11 @@ See https://www.mediawiki.org/wiki/Gerrit
|
|
65
65
|
|
66
66
|
## Release notes
|
67
67
|
|
68
|
+
### 0.5.0 2015-09-04
|
69
|
+
- Client cookies can now be read and modified via MediawikiApi::Client#cookies.
|
70
|
+
- Logging in will recurse upon a `NeedToken` API error only once to avoid
|
71
|
+
infinite recursion in cases where authentication is repeatedly unsuccessful.
|
72
|
+
|
68
73
|
### 0.4.1 2015-06-17
|
69
74
|
- Allow for response-less ApiError exceptions to make mocking in tests easier
|
70
75
|
|
data/lib/mediawiki_api/client.rb
CHANGED
@@ -10,18 +10,22 @@ module MediawikiApi
|
|
10
10
|
class Client
|
11
11
|
FORMAT = 'json'
|
12
12
|
|
13
|
+
attr_reader :cookies
|
13
14
|
attr_accessor :logged_in
|
14
15
|
|
15
16
|
alias_method :logged_in?, :logged_in
|
16
17
|
|
17
18
|
def initialize(url, log = false)
|
19
|
+
@cookies = HTTP::CookieJar.new
|
20
|
+
|
18
21
|
@conn = Faraday.new(url: url) do |faraday|
|
19
22
|
faraday.request :multipart
|
20
23
|
faraday.request :url_encoded
|
21
24
|
faraday.response :logger if log
|
22
|
-
faraday.use :cookie_jar
|
25
|
+
faraday.use :cookie_jar, jar: @cookies
|
23
26
|
faraday.adapter Faraday.default_adapter
|
24
27
|
end
|
28
|
+
|
25
29
|
@logged_in = false
|
26
30
|
@tokens = {}
|
27
31
|
end
|
@@ -89,6 +93,7 @@ module MediawikiApi
|
|
89
93
|
@logged_in = true
|
90
94
|
@tokens.clear
|
91
95
|
when 'NeedToken'
|
96
|
+
raise LoginError, "failed to log in with the returned token '#{token}'" unless token.nil?
|
92
97
|
data = log_in(username, password, data['token'])
|
93
98
|
else
|
94
99
|
raise LoginError, data['result']
|
data/spec/client_spec.rb
CHANGED
@@ -172,8 +172,28 @@ describe MediawikiApi::Client do
|
|
172
172
|
end
|
173
173
|
end
|
174
174
|
|
175
|
-
describe '#
|
175
|
+
describe '#cookies' do
|
176
|
+
subject { client.cookies }
|
177
|
+
|
178
|
+
it { is_expected.to be_a(HTTP::CookieJar) }
|
179
|
+
|
180
|
+
context 'when a new cookie is added' do
|
181
|
+
before do
|
182
|
+
client.cookies.add(HTTP::Cookie.new('cookie_name', '1', domain: 'localhost', path: '/'))
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'includes the cookie in subsequent requests' do
|
186
|
+
stub_token_request('csrf')
|
187
|
+
request = stub_action_request('foo').with(headers: { 'Cookie' => 'cookie_name=1' })
|
176
188
|
|
189
|
+
client.action(:foo)
|
190
|
+
|
191
|
+
expect(request).to have_been_requested
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe '#log_in' do
|
177
197
|
it 'logs in when API returns Success' do
|
178
198
|
stub_request(:post, api_url).
|
179
199
|
with(body: { format: 'json', action: 'login', lgname: 'Test', lgpassword: 'qwe123' }).
|
@@ -184,39 +204,48 @@ describe MediawikiApi::Client do
|
|
184
204
|
end
|
185
205
|
|
186
206
|
context 'when API returns NeedToken' do
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
207
|
+
context 'and a token was not given' do
|
208
|
+
before do
|
209
|
+
stub_login_request('Test', 'qwe123').
|
210
|
+
to_return(
|
211
|
+
body: { login: body_base.merge(result: 'NeedToken', token: '456') }.to_json,
|
212
|
+
headers: { 'Set-Cookie' => 'prefixSession=789; path=/; domain=localhost; HttpOnly' }
|
213
|
+
)
|
214
|
+
|
215
|
+
@success_req = stub_login_request('Test', 'qwe123', '456').
|
216
|
+
with(headers: { 'Cookie' => 'prefixSession=789' }).
|
217
|
+
to_return(body: { login: body_base.merge(result: 'Success') }.to_json)
|
218
|
+
end
|
194
219
|
|
195
|
-
|
196
|
-
|
197
|
-
lgname: 'Test', lgpassword: 'qwe123', lgtoken: '456' }).
|
198
|
-
with(headers: { 'Cookie' => 'prefixSession=789' }).
|
199
|
-
to_return(body: { login: body_base.merge(result: 'Success') }.to_json)
|
200
|
-
end
|
220
|
+
it 'logs in' do
|
221
|
+
response = subject.log_in('Test', 'qwe123')
|
201
222
|
|
202
|
-
|
203
|
-
|
223
|
+
expect(response).to include('result' => 'Success')
|
224
|
+
expect(subject.logged_in).to be true
|
225
|
+
end
|
204
226
|
|
205
|
-
|
206
|
-
|
227
|
+
it 'sends second request with token and cookies' do
|
228
|
+
subject.log_in('Test', 'qwe123')
|
229
|
+
|
230
|
+
expect(@success_req).to have_been_requested
|
231
|
+
end
|
207
232
|
end
|
208
233
|
|
209
|
-
|
210
|
-
subject.log_in('Test', 'qwe123')
|
234
|
+
context 'but a token was already provided' do
|
235
|
+
subject { client.log_in('Test', 'qwe123', '123') }
|
211
236
|
|
212
|
-
|
237
|
+
it 'should raise a LoginError' do
|
238
|
+
stub_login_request('Test', 'qwe123', '123').
|
239
|
+
to_return(body: { login: body_base.merge(result: 'NeedToken', token: '456') }.to_json)
|
240
|
+
|
241
|
+
expect { subject }.to raise_error(MediawikiApi::LoginError)
|
242
|
+
end
|
213
243
|
end
|
214
244
|
end
|
215
245
|
|
216
246
|
context 'when API returns neither Success nor NeedToken' do
|
217
247
|
before do
|
218
|
-
|
219
|
-
with(body: { format: 'json', action: 'login', lgname: 'Test', lgpassword: 'qwe123' }).
|
248
|
+
stub_login_request('Test', 'qwe123').
|
220
249
|
to_return(body: { login: body_base.merge(result: 'EmptyPass') }.to_json)
|
221
250
|
end
|
222
251
|
|
@@ -26,6 +26,13 @@ module MediawikiApi
|
|
26
26
|
stub_api_request(method, params.merge(action: action, token: mock_token))
|
27
27
|
end
|
28
28
|
|
29
|
+
def stub_login_request(username, password, token = nil)
|
30
|
+
params = { action: 'login', lgname: username, lgpassword: password }
|
31
|
+
params[:lgtoken] = token unless token.nil?
|
32
|
+
|
33
|
+
stub_api_request(:post, params)
|
34
|
+
end
|
35
|
+
|
29
36
|
def stub_token_request(type, warning = nil)
|
30
37
|
response = { query: { tokens: { "#{type}token" => mock_token } } }
|
31
38
|
response[:warnings] = { type => { '*' => [warning] } } unless warning.nil?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mediawiki_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amir Aharoni
|
@@ -14,7 +14,7 @@ authors:
|
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
|
-
date: 2015-
|
17
|
+
date: 2015-09-04 00:00:00.000000000 Z
|
18
18
|
dependencies:
|
19
19
|
- !ruby/object:Gem::Dependency
|
20
20
|
name: faraday
|