mediawiki-gateway 0.6.2 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +22 -0
  3. data/ChangeLog +16 -0
  4. data/README.md +80 -21
  5. data/Rakefile +28 -34
  6. data/bin/mediawiki-gateway +203 -0
  7. data/lib/media_wiki.rb +4 -9
  8. data/lib/media_wiki/exception.rb +11 -8
  9. data/lib/media_wiki/fake_wiki.rb +636 -0
  10. data/lib/media_wiki/gateway.rb +105 -940
  11. data/lib/media_wiki/gateway/files.rb +173 -0
  12. data/lib/media_wiki/gateway/pages.rb +400 -0
  13. data/lib/media_wiki/gateway/query.rb +98 -0
  14. data/lib/media_wiki/gateway/site.rb +101 -0
  15. data/lib/media_wiki/gateway/users.rb +182 -0
  16. data/lib/media_wiki/utils.rb +47 -13
  17. data/lib/media_wiki/version.rb +27 -0
  18. data/lib/mediawiki-gateway.rb +1 -0
  19. data/spec/{import-test-data.xml → data/import.xml} +0 -0
  20. data/spec/media_wiki/gateway/files_spec.rb +34 -0
  21. data/spec/media_wiki/gateway/pages_spec.rb +390 -0
  22. data/spec/media_wiki/gateway/query_spec.rb +84 -0
  23. data/spec/media_wiki/gateway/site_spec.rb +122 -0
  24. data/spec/media_wiki/gateway/users_spec.rb +171 -0
  25. data/spec/media_wiki/gateway_spec.rb +129 -0
  26. data/spec/{live_gateway_spec.rb → media_wiki/live_gateway_spec.rb} +31 -35
  27. data/spec/{utils_spec.rb → media_wiki/utils_spec.rb} +41 -39
  28. data/spec/spec_helper.rb +17 -16
  29. metadata +77 -135
  30. data/.ruby-version +0 -1
  31. data/.rvmrc +0 -34
  32. data/Gemfile +0 -19
  33. data/Gemfile.lock +0 -77
  34. data/LICENSE +0 -21
  35. data/config/hosts.yml +0 -17
  36. data/lib/media_wiki/config.rb +0 -69
  37. data/mediawiki-gateway.gemspec +0 -113
  38. data/samples/README +0 -18
  39. data/samples/create_page.rb +0 -13
  40. data/samples/delete_batch.rb +0 -14
  41. data/samples/download_batch.rb +0 -15
  42. data/samples/email_user.rb +0 -14
  43. data/samples/export_xml.rb +0 -14
  44. data/samples/get_page.rb +0 -11
  45. data/samples/import_xml.rb +0 -14
  46. data/samples/run_fake_media_wiki.rb +0 -8
  47. data/samples/search_content.rb +0 -12
  48. data/samples/semantic_query.rb +0 -17
  49. data/samples/upload_commons.rb +0 -45
  50. data/samples/upload_file.rb +0 -13
  51. data/spec/fake_media_wiki/api_pages.rb +0 -135
  52. data/spec/fake_media_wiki/app.rb +0 -360
  53. data/spec/fake_media_wiki/query_handling.rb +0 -136
  54. data/spec/gateway_spec.rb +0 -888
@@ -0,0 +1,84 @@
1
+ describe_fake MediaWiki::Gateway::Query do
2
+
3
+ describe "#search" do
4
+
5
+ before do
6
+ $fake_media_wiki.reset
7
+ @gateway.create("Search Test", "Foo KEY Blah")
8
+ @gateway.create("Search Test 2", "Zomp KEY Zorg")
9
+ @gateway.create("Book:Search Test", "Bar KEY Baz")
10
+ @gateway.create("Sandbox:Search Test", "Evil KEY Evil")
11
+ end
12
+
13
+ describe "with an empty key" do
14
+
15
+ it "should raise an error" do
16
+ lambda do
17
+ @gateway.search("")
18
+ end.should raise_error(MediaWiki::APIError)
19
+ end
20
+
21
+ end
22
+
23
+ describe "with a valid key and no namespaces" do
24
+
25
+ before do
26
+ @search = @gateway.search("KEY")
27
+ end
28
+
29
+ it "should list all matching pages in the main namespace" do
30
+ @search.should =~ [ "Search Test", "Search Test 2" ]
31
+ end
32
+
33
+ end
34
+
35
+ describe "with a valid key and a namespace string" do
36
+
37
+ before do
38
+ @search = @gateway.search("KEY", "Book")
39
+ end
40
+
41
+ it "should list all matching pages in the specified namespaces" do
42
+ @search.should == [ "Book:Search Test" ]
43
+ end
44
+
45
+ end
46
+
47
+ describe "with a valid key and a namespace array" do
48
+
49
+ before do
50
+ @search = @gateway.search("KEY", ["Book", "Sandbox"])
51
+ end
52
+
53
+ it "should list all matching pages in the specified namespaces" do
54
+ @search.should =~ [ "Sandbox:Search Test", "Book:Search Test" ]
55
+ end
56
+
57
+ end
58
+
59
+ describe "with maximum number of results" do
60
+
61
+ before do
62
+ @search = @gateway.search("KEY", nil, 2, 1)
63
+ end
64
+
65
+ it "should return at most the maximum number of results asked" do
66
+ @search.size.should == 1
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ describe "#semantic_query" do
73
+
74
+ before do
75
+ @response = @gateway.semantic_query('[[place::123]]', ['mainlabel=Page'])
76
+ end
77
+
78
+ it "should return an HTML string" do
79
+ @response.should == 'Sample <B>HTML</B> content.'
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,122 @@
1
+ describe_fake MediaWiki::Gateway::Site do
2
+
3
+ describe "#import" do
4
+
5
+ let(:import_file) { data('import.xml') }
6
+
7
+ describe "when not logged in" do
8
+
9
+ it "should raise an error" do
10
+ lambda do
11
+ @gateway.import(import_file)
12
+ end.should raise_error(MediaWiki::Unauthorized)
13
+ end
14
+
15
+ end
16
+
17
+ describe "when logged in as admin" do
18
+
19
+ def import_response
20
+ <<-XML
21
+ <api>
22
+ <import>
23
+ <page title='Main Page' ns='0' revisions='0'/>
24
+ <page title='Template:Header' ns='10' revisions='1'/>
25
+ </import>
26
+ </api>
27
+ XML
28
+ end
29
+
30
+ before do
31
+ @gateway.login("atlasmw", "wombat")
32
+ @page = @gateway.import(import_file)
33
+ end
34
+
35
+ it "should import content" do
36
+ expect(@page.to_s).to be_equivalent_to(import_response)
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ describe "#export" do
44
+
45
+ def export_response
46
+ <<-XML
47
+ <mediawiki>
48
+ <page>
49
+ <title>Main Page</title>
50
+ <id>1</id>
51
+ <revision>
52
+ <id>1</id>
53
+ <text>Content</text>
54
+ </revision>
55
+ </page>
56
+ </mediawiki>
57
+ XML
58
+ end
59
+
60
+ before do
61
+ @page = @gateway.export("Main Page")
62
+ end
63
+
64
+ it "should return export data for the page" do
65
+ expect(@page.to_s).to be_equivalent_to(export_response)
66
+ end
67
+
68
+ end
69
+
70
+ describe "#siteinfo" do
71
+
72
+ before do
73
+ $fake_media_wiki.reset
74
+ @siteinfo = @gateway.siteinfo
75
+ end
76
+
77
+ it "should get the siteinfo" do
78
+ @siteinfo.should == { 'generator' => "MediaWiki #{MediaWiki::VERSION}" }
79
+ end
80
+
81
+ end
82
+
83
+ describe "#version" do
84
+
85
+ before do
86
+ $fake_media_wiki.reset
87
+ @version = @gateway.version
88
+ end
89
+
90
+ it "should get the version" do
91
+ @version.should == MediaWiki::VERSION
92
+ end
93
+
94
+ end
95
+
96
+ describe "#namespaces_by_prefix" do
97
+
98
+ before do
99
+ $fake_media_wiki.reset
100
+ @namespaces = @gateway.namespaces_by_prefix
101
+ end
102
+
103
+ it "should list all namespaces" do
104
+ @namespaces.should == { "" => 0, "Book" => 100, "Sandbox" => 200 }
105
+ end
106
+
107
+ end
108
+
109
+ describe "#extensions" do
110
+
111
+ before do
112
+ $fake_media_wiki.reset
113
+ @extensions = @gateway.extensions
114
+ end
115
+
116
+ it "should list all extensions" do
117
+ @extensions.should == { "FooExtension" => "r1", "BarExtension" => "r2", 'Semantic MediaWiki' => '1.5' }
118
+ end
119
+
120
+ end
121
+
122
+ end
@@ -0,0 +1,171 @@
1
+ describe_fake MediaWiki::Gateway::Users do
2
+
3
+ describe '#login' do
4
+
5
+ describe "with a valid username & password" do
6
+
7
+ before do
8
+ @gateway.login('atlasmw', 'wombat')
9
+ end
10
+
11
+ it "should login successfully with the default domain" do
12
+ $fake_media_wiki.logged_in('atlasmw').should == true
13
+ end
14
+
15
+ end
16
+
17
+ describe "with a valid username, password and domain" do
18
+
19
+ before do
20
+ @gateway.login('ldapuser', 'ldappass', 'ldapdomain')
21
+ end
22
+
23
+ it "should login successfully" do
24
+ $fake_media_wiki.logged_in('ldapuser').should == true
25
+ end
26
+
27
+ end
28
+
29
+ describe "with an non-existent username" do
30
+
31
+ it "should raise an error" do
32
+ lambda do
33
+ @gateway.login('bogususer', 'sekrit')
34
+ end.should raise_error(MediaWiki::Unauthorized)
35
+ end
36
+
37
+ end
38
+
39
+ describe "with an incorrect password" do
40
+
41
+ it "should raise an error" do
42
+ lambda do
43
+ @gateway.login('atlasmw', 'sekrit')
44
+ end.should raise_error(MediaWiki::Unauthorized)
45
+ end
46
+
47
+ end
48
+
49
+ describe "with an incorrect domain" do
50
+
51
+ it "should raise an error" do
52
+ lambda do
53
+ @gateway.login('atlasmw', 'wombat', 'bogusdomain')
54
+ end.should raise_error(MediaWiki::Unauthorized)
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ describe "#create_account" do
62
+
63
+ describe "when logged in as admin" do
64
+ before do
65
+ @gateway.login("atlasmw", "wombat")
66
+ end
67
+
68
+ it 'should get expected result' do
69
+ expected = <<-XML
70
+ <api>
71
+ <createaccount result='success' token='admin_token+\\' userid='4' username='FooBar'/>
72
+ </api>
73
+ XML
74
+
75
+ expect(@gateway.create_account({ 'name' => 'FooBar', 'password' => 'BarBaz' }).to_s).to be_equivalent_to(expected)
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ describe '#options' do
83
+
84
+ describe 'when logged in' do
85
+ before do
86
+ @gateway.login("atlasmw", "wombat")
87
+ end
88
+
89
+ describe 'requesting an options token' do
90
+ before do
91
+ @token = @gateway.send(:get_options_token)
92
+ end
93
+
94
+ it "should return a token" do
95
+ @token.should_not == nil
96
+ @token.should_not == "+\\"
97
+ end
98
+
99
+ end
100
+
101
+ it 'should return the expected response' do
102
+ expected = '<api options="success" />'
103
+ expect(@gateway.options({ :realname => 'Bar Baz' }).to_s).to be_equivalent_to(expected)
104
+ end
105
+
106
+ end
107
+
108
+ end
109
+
110
+ describe "#user_rights" do
111
+
112
+ describe "when logged in as admin" do
113
+
114
+ before do
115
+ @gateway.login("atlasmw", "wombat")
116
+ end
117
+
118
+ describe "requesting a userrights token for an existing user" do
119
+
120
+ before do
121
+ @token = @gateway.send(:get_userrights_token, 'nonadmin')
122
+ end
123
+
124
+ it "should return a token" do
125
+ @token.should_not == nil
126
+ @token.should_not == "+\\"
127
+ end
128
+ end
129
+
130
+ describe "requesting a userrights token for an nonexistant user" do
131
+
132
+ it "should raise an error" do
133
+ lambda do
134
+ @gateway.send(:get_userrights_token, 'nosuchuser')
135
+ end.should raise_error(MediaWiki::APIError)
136
+ end
137
+ end
138
+
139
+ describe "changing a user's groups with a valid token" do
140
+
141
+ def userrights_response
142
+ <<-XML
143
+ <api>
144
+ <userrights user="nonadmin">
145
+ <removed>
146
+ <group>oldgroup</group>
147
+ </removed>
148
+ <added>
149
+ <group>newgroup</group>
150
+ </added>
151
+ </userrights>
152
+ </api>
153
+ XML
154
+ end
155
+
156
+ before do
157
+ @token = @gateway.send(:get_userrights_token, 'nonadmin')
158
+ @result = @gateway.send(:userrights, 'nonadmin', @token, 'newgroup', 'oldgroup', 'because I can')
159
+ end
160
+
161
+ it "should return a result matching the input" do
162
+ expect(@result.to_s).to be_equivalent_to(userrights_response)
163
+ end
164
+
165
+ end
166
+
167
+ end
168
+
169
+ end
170
+
171
+ end
@@ -0,0 +1,129 @@
1
+ describe_fake MediaWiki::Gateway do
2
+
3
+ describe 'setting the User-Agent header' do
4
+
5
+ ua = described_class::USER_AGENT
6
+
7
+ def expect_user_agent(expected, actual = default = true)
8
+ options = default ? {} : { user_agent: actual }
9
+ gateway = described_class.new('test.wiki', options)
10
+ expect(gateway.headers['User-Agent']).to eq(expected)
11
+ end
12
+
13
+ it 'should default to generic value' do
14
+ expect_user_agent ua
15
+ end
16
+
17
+ it 'should ignore nil value' do
18
+ expect_user_agent ua, nil
19
+ end
20
+
21
+ it 'should prepend given value' do
22
+ expect_user_agent "Foo/4.2 #{ua}", 'Foo/4.2'
23
+ end
24
+
25
+ describe 'with global default' do
26
+
27
+ before do
28
+ described_class.default_user_agent = 'Bar/2.3'
29
+ end
30
+
31
+ after do
32
+ described_class.default_user_agent = nil
33
+ end
34
+
35
+ it 'should default to global value' do
36
+ expect_user_agent "Bar/2.3 #{ua}"
37
+ end
38
+
39
+ it 'should accept nil value' do
40
+ expect_user_agent ua, nil
41
+ end
42
+
43
+ it 'should override with given value' do
44
+ expect_user_agent "Foo/4.2 #{ua}", 'Foo/4.2'
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ describe "#get_token" do
52
+
53
+ describe "when not logged in" do
54
+
55
+ describe "requesting an edit token" do
56
+
57
+ before do
58
+ @token = @gateway.send(:get_token, 'edit', 'Main Page')
59
+ end
60
+
61
+ it "should return a blank token" do
62
+ @token.should_not == nil
63
+ @token.should == "+\\"
64
+ end
65
+
66
+ end
67
+
68
+ describe "requesting an import token" do
69
+
70
+ it "should raise an error" do
71
+ lambda do
72
+ @gateway.send(:get_token, 'import', 'Main Page')
73
+ end.should raise_error(MediaWiki::Unauthorized)
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ describe "when logged in as admin user" do
81
+
82
+ before do
83
+ @gateway.login('atlasmw', 'wombat')
84
+ end
85
+
86
+ describe "requesting an edit token for a single page" do
87
+
88
+ before do
89
+ @token = @gateway.send(:get_token, 'edit', 'Main Page')
90
+ end
91
+
92
+ it "should return a token" do
93
+ @token.should_not == nil
94
+ @token.should_not == "+\\"
95
+ end
96
+
97
+ end
98
+
99
+ describe "requesting an edit token for multiple pages" do
100
+
101
+ before do
102
+ @token = @gateway.send(:get_token, 'edit', "Main Page|Another Page")
103
+ end
104
+
105
+ it "should return a token" do
106
+ @token.should_not == nil
107
+ @token.should_not == "+\\"
108
+ end
109
+
110
+ end
111
+
112
+ describe "requesting an import token" do
113
+
114
+ before do
115
+ @token = @gateway.send(:get_token, 'import', 'Main Page')
116
+ end
117
+
118
+ it "should return a token" do
119
+ @token.should_not == nil
120
+ @token.should_not == "+\\"
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
128
+
129
+ end