mediawiki_api 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b76c6bc6c035d11d6b3690aba25961df0472eac
4
- data.tar.gz: bf35918e838839955bc6e3897ce8bfe865c26d70
3
+ metadata.gz: 176f86e086fb5b9c2a275cade16f2199c7a662b8
4
+ data.tar.gz: da273be34eff6481902b5e30621f0c08ba481af1
5
5
  SHA512:
6
- metadata.gz: 33b30d02eb1ab3cf1044875a5903a516e0d55638e5c60c93b29545ef9cfd34270ee32e50a8c0c67e55fadc6451048b084c6d8c44e4107934ca917ace7f62d6df
7
- data.tar.gz: 899a279b3499c720ddd38d3c6ce883613d54c4c9f32cb67e4e40098aff9bc7bf8b09d43e1cbddf7b7058c4fe4a162e8a1e1a01335deaafb8c4ca42dbbc75b4fc
6
+ metadata.gz: 5a76cf068332b572df322dfd3d98a76c6a018f1213aee4585f1cc94e787e15edc5678e6df2b2002d41fd37f61baf3601c569397a6c210cf1d261317e878702b8
7
+ data.tar.gz: cc667e15712a3acf66b26f88f366ee468606e50081dc18f72909b65ec6c23d9cd5093db8c8b3cf214512f20262b80307d026c739ca636b0829de5d48b2d81d0b
data/README.md CHANGED
@@ -65,6 +65,10 @@ See https://www.mediawiki.org/wiki/Gerrit
65
65
 
66
66
  ## Release notes
67
67
 
68
+ ### 0.6.0 2016-05-25
69
+ - Update account creation code for AuthManager. This change updates the gem to test which API
70
+ flavor is in use, then send requests accordingly.
71
+
68
72
  ### 0.5.0 2015-09-04
69
73
  - Client cookies can now be read and modified via MediawikiApi::Client#cookies.
70
74
  - Logging in will recurse upon a `NeedToken` API error only once to avoid
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # pre-flight
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
4
+
5
+ require 'rspec/core/rake_task'
6
+ require 'rubocop/rake_task'
7
+ require 'yard'
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ RuboCop::RakeTask.new(:rubocop)
11
+ YARD::Rake::YardocTask.new(:yard)
12
+
13
+ task default: [:test]
14
+
15
+ desc 'Run all build/tests commands (CI entry point)'
16
+ task test: [:build, :rubocop, :spec, :yard]
17
+
18
+ desc 'Generate all documentations'
19
+ task doc: [:yard]
@@ -41,7 +41,44 @@ module MediawikiApi
41
41
  end
42
42
  end
43
43
 
44
- def create_account(username, password, token = nil)
44
+ def create_account(username, password)
45
+ params = { modules: 'createaccount', token_type: false }
46
+ d = action(:paraminfo, params).data
47
+ params = d['modules'] && d['modules'][0] && d['modules'][0]['parameters']
48
+ if !params || !params.map
49
+ raise CreateAccountError, 'unexpected API response format'
50
+ end
51
+ params = params.map{ |o| o['name'] }
52
+
53
+ if params.include? 'requests'
54
+ create_account_new(username, password)
55
+ else
56
+ create_account_old(username, password)
57
+ end
58
+ end
59
+
60
+ def create_account_new(username, password)
61
+ # post-AuthManager
62
+ data = action(:query, { meta: 'tokens', type: 'createaccount', token_type: false }).data
63
+ token = data['tokens'] && data['tokens']['createaccounttoken']
64
+ unless token
65
+ raise CreateAccountError, 'failed to get createaccount API token'
66
+ end
67
+
68
+ data = action(:createaccount, {
69
+ username: username,
70
+ password: password,
71
+ retype: password,
72
+ createreturnurl: 'http://example.com', # won't be used but must be a valid URL
73
+ createtoken: token,
74
+ token_type: false
75
+ }).data
76
+ raise CreateAccountError, data['message'] if data['status'] != 'PASS'
77
+ data
78
+ end
79
+
80
+ def create_account_old(username, password, token = nil)
81
+ # pre-AuthManager
45
82
  params = { name: username, password: password, token_type: false }
46
83
  params[:token] = token unless token.nil?
47
84
 
@@ -52,7 +89,7 @@ module MediawikiApi
52
89
  @logged_in = true
53
90
  @tokens.clear
54
91
  when 'NeedToken'
55
- data = create_account(username, password, data['token'])
92
+ data = create_account_old(username, password, data['token'])
56
93
  else
57
94
  raise CreateAccountError, data['result']
58
95
  end
@@ -1,4 +1,4 @@
1
1
  # MediaWiki Ruby API
2
2
  module MediawikiApi
3
- VERSION = '0.5.0'
3
+ VERSION = '0.6.0'
4
4
  end
data/spec/client_spec.rb CHANGED
@@ -335,57 +335,126 @@ describe MediawikiApi::Client do
335
335
  end
336
336
 
337
337
  describe '#create_account' do
338
- it 'creates an account when API returns Success' do
339
- stub_request(:post, api_url).
340
- with(body: { format: 'json', action: 'createaccount', name: 'Test', password: 'qwe123' }).
341
- to_return(body: { createaccount: body_base.merge(result: 'Success') }.to_json)
342
-
343
- expect(subject.create_account('Test', 'qwe123')).to include('result' => 'Success')
344
- end
345
-
346
- context 'when API returns NeedToken' do
338
+ context 'when the old createaccount API is used' do
347
339
  before do
348
340
  stub_request(:post, api_url).
349
- with(body: { format: 'json', action: 'createaccount',
350
- name: 'Test', password: 'qwe123' }).
351
- to_return(
352
- body: { createaccount: body_base.merge(result: 'NeedToken', token: '456') }.to_json,
353
- headers: { 'Set-Cookie' => 'prefixSession=789; path=/; domain=localhost; HttpOnly' }
354
- )
341
+ with(body: { action: 'paraminfo', format: 'json', modules: 'createaccount' }).
342
+ to_return(body: { paraminfo: body_base.merge(modules: [{ parameters: [] }]) }.to_json)
343
+ end
355
344
 
356
- @success_req = stub_request(:post, api_url).
357
- with(body: { format: 'json', action: 'createaccount',
358
- name: 'Test', password: 'qwe123', token: '456' }).
359
- with(headers: { 'Cookie' => 'prefixSession=789' }).
345
+ it 'creates an account when API returns Success' do
346
+ stub_request(:post, api_url).
347
+ with(body: { format: 'json', action: 'createaccount', name: 'Test', password: 'qwe123' }).
360
348
  to_return(body: { createaccount: body_base.merge(result: 'Success') }.to_json)
361
- end
362
349
 
363
- it 'creates an account' do
364
350
  expect(subject.create_account('Test', 'qwe123')).to include('result' => 'Success')
365
351
  end
366
352
 
367
- it 'sends second request with token and cookies' do
368
- subject.create_account 'Test', 'qwe123'
369
- expect(@success_req).to have_been_requested
353
+ context 'when API returns NeedToken' do
354
+ before do
355
+ stub_request(:post, api_url).
356
+ with(body: { format: 'json', action: 'createaccount',
357
+ name: 'Test', password: 'qwe123' }).
358
+ to_return(
359
+ body: { createaccount: body_base.merge(result: 'NeedToken', token: '456') }.to_json,
360
+ headers: { 'Set-Cookie' => 'prefixSession=789; path=/; domain=localhost; HttpOnly' }
361
+ )
362
+
363
+ @success_req = stub_request(:post, api_url).
364
+ with(body: { format: 'json', action: 'createaccount',
365
+ name: 'Test', password: 'qwe123', token: '456' }).
366
+ with(headers: { 'Cookie' => 'prefixSession=789' }).
367
+ to_return(body: { createaccount: body_base.merge(result: 'Success') }.to_json)
368
+ end
369
+
370
+ it 'creates an account' do
371
+ expect(subject.create_account('Test', 'qwe123')).to include('result' => 'Success')
372
+ end
373
+
374
+ it 'sends second request with token and cookies' do
375
+ subject.create_account 'Test', 'qwe123'
376
+ expect(@success_req).to have_been_requested
377
+ end
378
+ end
379
+
380
+ # docs don't specify other results, but who knows
381
+ # http://www.mediawiki.org/wiki/API:Account_creation
382
+ context 'when API returns neither Success nor NeedToken' do
383
+ before do
384
+ stub_request(:post, api_url).
385
+ with(body: { format: 'json', action: 'createaccount',
386
+ name: 'Test', password: 'qwe123' }).
387
+ to_return(body: { createaccount: body_base.merge(result: 'WhoKnows') }.to_json)
388
+ end
389
+
390
+ it 'raises error with proper message' do
391
+ expect { subject.create_account 'Test', 'qwe123' }.to raise_error(
392
+ MediawikiApi::CreateAccountError,
393
+ 'WhoKnows'
394
+ )
395
+ end
370
396
  end
371
397
  end
372
398
 
373
- # docs don't specify other results, but who knows
374
- # http://www.mediawiki.org/wiki/API:Account_creation
375
- context 'when API returns neither Success nor NeedToken' do
399
+ context 'when the new createaccount API is used' do
376
400
  before do
377
401
  stub_request(:post, api_url).
378
- with(body: { format: 'json', action: 'createaccount',
379
- name: 'Test', password: 'qwe123' }).
380
- to_return(body: { createaccount: body_base.merge(result: 'WhoKnows') }.to_json)
402
+ with(body: { action: 'paraminfo', format: 'json', modules: 'createaccount' }).
403
+ to_return(body: { paraminfo: body_base.merge(
404
+ modules: [{ parameters: [{ name: 'requests' }] }]
405
+ ) }.to_json)
381
406
  end
382
407
 
383
- it 'raises error with proper message' do
408
+ it 'raises an error when fetching a token fails' do
409
+ stub_request(:post, api_url).
410
+ with(body: { action: 'query', format: 'json', meta: 'tokens', type: 'createaccount' }).
411
+ to_return(body: { tokens: body_base.merge(foo: '12345\\+') }.to_json)
384
412
  expect { subject.create_account 'Test', 'qwe123' }.to raise_error(
385
413
  MediawikiApi::CreateAccountError,
386
- 'WhoKnows'
414
+ 'failed to get createaccount API token'
387
415
  )
388
416
  end
417
+
418
+ context 'when fetching a token succeeds' do
419
+ before do
420
+ stub_request(:post, api_url).
421
+ with(body: { format: 'json', action: 'query', meta: 'tokens', type: 'createaccount' }).
422
+ to_return(body: { tokens: body_base.merge(createaccounttoken: '12345\\+') }.to_json)
423
+ end
424
+
425
+ it 'creates an account when the API returns success' do
426
+ stub_request(:post, api_url).
427
+ with(body: { format: 'json', action: 'createaccount',
428
+ createreturnurl: 'http://example.com', username: 'Test',
429
+ password: 'qwe123', retype: 'qwe123', createtoken: '12345\\+' }).
430
+ to_return(body: { createaccount: body_base.merge(status: 'PASS') }.to_json)
431
+ expect(subject.create_account('Test', 'qwe123')).to include('status' => 'PASS')
432
+ end
433
+
434
+ it 'raises an error when the API returns failure' do
435
+ stub_request(:post, api_url).
436
+ with(body: { format: 'json', action: 'createaccount',
437
+ createreturnurl: 'http://example.com', username: 'Test',
438
+ password: 'qwe123', retype: 'qwe123', createtoken: '12345\\+' }).
439
+ to_return(body: { createaccount: body_base.merge(
440
+ status: 'FAIL', message: 'User exists!'
441
+ ) }.to_json)
442
+ expect { subject.create_account 'Test', 'qwe123' }.to raise_error(
443
+ MediawikiApi::CreateAccountError,
444
+ 'User exists!'
445
+ )
446
+ end
447
+ end
448
+ end
449
+
450
+ it 'raises an error when the paraminfo query result is weird' do
451
+ stub_request(:post, api_url).
452
+ with(body: { action: 'paraminfo', format: 'json', modules: 'createaccount' }).
453
+ to_return(body: { paraminfo: body_base.merge(modules: []) }.to_json)
454
+ expect { subject.create_account 'Test', 'qwe123' }.to raise_error(
455
+ MediawikiApi::CreateAccountError,
456
+ 'unexpected API response format'
457
+ )
389
458
  end
390
459
  end
391
460
 
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.5.0
4
+ version: 0.6.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-09-04 00:00:00.000000000 Z
17
+ date: 2016-05-25 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: faraday
@@ -188,6 +188,7 @@ files:
188
188
  - Gemfile
189
189
  - LICENSE.txt
190
190
  - README.md
191
+ - Rakefile
191
192
  - lib/mediawiki_api.rb
192
193
  - lib/mediawiki_api/client.rb
193
194
  - lib/mediawiki_api/exceptions.rb
@@ -219,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
220
  version: '0'
220
221
  requirements: []
221
222
  rubyforge_project:
222
- rubygems_version: 2.4.3
223
+ rubygems_version: 2.5.1
223
224
  signing_key:
224
225
  specification_version: 4
225
226
  summary: A library for interacting with MediaWiki API from Ruby.