impostor 0.2.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +1 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/Gemfile +17 -0
  6. data/Gemfile.lock +47 -0
  7. data/History.txt +8 -0
  8. data/Manifest.txt +87 -55
  9. data/README.txt +11 -11
  10. data/Rakefile +16 -20
  11. data/lib/impostor/auth.rb +103 -0
  12. data/lib/impostor/config.rb +171 -0
  13. data/lib/impostor/errors.rb +67 -0
  14. data/lib/impostor/phpbb2.rb +202 -0
  15. data/lib/impostor/phpbb3.rb +199 -0
  16. data/lib/impostor/post.rb +111 -0
  17. data/lib/impostor/topic.rb +115 -0
  18. data/lib/impostor/wwf79.rb +186 -0
  19. data/lib/impostor/wwf80.rb +190 -0
  20. data/lib/impostor.rb +108 -5
  21. data/spec/auth_spec.rb +148 -0
  22. data/spec/base_spec_helper.rb +12 -0
  23. data/{test/test_helper.rb → spec/caged_net_http.rb} +8 -17
  24. data/spec/config_spec.rb +136 -0
  25. data/spec/fixtures/junk.html +1 -0
  26. data/{test → spec}/fixtures/phpbb2-get-new_topic-form-good-response.html +0 -0
  27. data/{test → spec}/fixtures/phpbb2-get-viewtopic-for-new-topic-good-response.html +11 -11
  28. data/{test → spec}/fixtures/phpbb2-get-viewtopic-for-new-topic-malformed-response.html +0 -0
  29. data/{test → spec}/fixtures/phpbb2-index.html +0 -0
  30. data/{test → spec}/fixtures/phpbb2-logged-in.html +0 -0
  31. data/{test → spec}/fixtures/phpbb2-login.html +0 -0
  32. data/{test → spec}/fixtures/phpbb2-not-logged-in.html +0 -0
  33. data/{test → spec}/fixtures/phpbb2-post-new_topic-good-response.html +0 -0
  34. data/{test → spec}/fixtures/phpbb2-post-reply-good-response.html +0 -0
  35. data/{test → spec}/fixtures/phpbb2-post-reply-throttled-response.html +0 -0
  36. data/{test → spec}/fixtures/phpbb2-too-many-posts.html +0 -0
  37. data/{test → spec}/fixtures/phpbb3-get-new-topic-form-good-response.html +0 -0
  38. data/{test → spec}/fixtures/phpbb3-get-reply-form-good-response.html +0 -0
  39. data/{test → spec}/fixtures/phpbb3-logged-in.html +0 -0
  40. data/{test → spec}/fixtures/phpbb3-login.html +0 -0
  41. data/{test → spec}/fixtures/phpbb3-not-logged-in.html +0 -0
  42. data/{test → spec}/fixtures/phpbb3-post-new_topic-good-response.html +0 -0
  43. data/{test → spec}/fixtures/phpbb3-post-reply-good-response.html +0 -0
  44. data/spec/fixtures/vcr_cassettes/phpbb2-should-be-overlimit-creating-topic.yml +1308 -0
  45. data/spec/fixtures/vcr_cassettes/phpbb2-should-create-topic.yml +923 -0
  46. data/spec/fixtures/vcr_cassettes/phpbb2-should-login.yml +360 -0
  47. data/spec/fixtures/vcr_cassettes/phpbb2-should-not-create-new-topic.yml +497 -0
  48. data/spec/fixtures/vcr_cassettes/phpbb2-should-not-login.yml +287 -0
  49. data/spec/fixtures/vcr_cassettes/phpbb2-should-not-post.yml +497 -0
  50. data/spec/fixtures/vcr_cassettes/phpbb2-should-overlimit-error-post.yml +1140 -0
  51. data/spec/fixtures/vcr_cassettes/phpbb2-should-post.yml +751 -0
  52. data/spec/fixtures/vcr_cassettes/phpbb3-should-be-overlimit-creating-topic.yml +995 -0
  53. data/spec/fixtures/vcr_cassettes/phpbb3-should-create-topic.yml +675 -0
  54. data/spec/fixtures/vcr_cassettes/phpbb3-should-login.yml +245 -0
  55. data/spec/fixtures/vcr_cassettes/phpbb3-should-not-create-new-topic.yml +350 -0
  56. data/spec/fixtures/vcr_cassettes/phpbb3-should-not-login.yml +253 -0
  57. data/spec/fixtures/vcr_cassettes/phpbb3-should-not-post.yml +350 -0
  58. data/spec/fixtures/vcr_cassettes/phpbb3-should-overlimit-error-post.yml +1046 -0
  59. data/spec/fixtures/vcr_cassettes/phpbb3-should-post.yml +605 -0
  60. data/{test → spec}/fixtures/wwf79-forum_posts.html +0 -0
  61. data/{test → spec}/fixtures/wwf79-general-new-topic-error.html +0 -0
  62. data/{test → spec}/fixtures/wwf79-general-posting-error.html +0 -0
  63. data/{test → spec}/fixtures/wwf79-good-post-forum_posts.html +1 -1
  64. data/{test → spec}/fixtures/wwf79-index.html +0 -0
  65. data/{test → spec}/fixtures/wwf79-logged-in.html +0 -0
  66. data/{test → spec}/fixtures/wwf79-login.html +0 -0
  67. data/{test → spec}/fixtures/wwf79-new-topic-forum_posts-response.html +0 -0
  68. data/{test → spec}/fixtures/wwf79-new-topic-post_message_form.html +0 -0
  69. data/{test → spec}/fixtures/wwf79-not-logged-in.html +0 -0
  70. data/{test → spec}/fixtures/wwf79-too-many-posts.html +0 -0
  71. data/{test → spec}/fixtures/wwf79-too-many-topics.html +0 -0
  72. data/{test → spec}/fixtures/wwf80-general-posting-error.html +0 -0
  73. data/{test → spec}/fixtures/wwf80-get-new_topic-form-good-response.html +0 -0
  74. data/{test → spec}/fixtures/wwf80-get-viewtopic-for-new-topic-good-response.html +0 -0
  75. data/{test → spec}/fixtures/wwf80-index.html +0 -0
  76. data/{test → spec}/fixtures/wwf80-logged-in.html +0 -0
  77. data/{test → spec}/fixtures/wwf80-login.html +0 -0
  78. data/{test → spec}/fixtures/wwf80-new_reply_form.html +0 -0
  79. data/{test → spec}/fixtures/wwf80-not-logged-in.html +0 -0
  80. data/{test → spec}/fixtures/wwf80-post-new_topic-good-response.html +1 -1
  81. data/{test → spec}/fixtures/wwf80-post-reply-good-response.html +0 -0
  82. data/{test → spec}/fixtures/wwf80-too-many-posts.html +0 -0
  83. data/spec/impostor_spec_helper.rb +162 -0
  84. data/spec/integration/phpbb2_spec.rb +111 -0
  85. data/spec/integration/phpbb3_spec.rb +109 -0
  86. data/spec/integration_spec_helper.rb +7 -0
  87. data/spec/phpbb2_spec.rb +346 -0
  88. data/spec/phpbb3_spec.rb +332 -0
  89. data/spec/post_spec.rb +134 -0
  90. data/spec/spec_helper.rb +2 -0
  91. data/spec/test_impostor.rb +12 -0
  92. data/spec/topic_spec.rb +143 -0
  93. data/spec/wwf79_spec.rb +342 -0
  94. data/spec/wwf80_spec.rb +339 -0
  95. metadata +156 -87
  96. data/lib/www/impostor/phpbb2.rb +0 -258
  97. data/lib/www/impostor/phpbb3.rb +0 -236
  98. data/lib/www/impostor/wwf79.rb +0 -254
  99. data/lib/www/impostor/wwf80.rb +0 -264
  100. data/lib/www/impostor.rb +0 -269
  101. data/test/test_github.rb +0 -12
  102. data/test/test_www_impostor.rb +0 -165
  103. data/test/test_www_impostor_phpbb2.rb +0 -536
  104. data/test/test_www_impostor_phpbb3.rb +0 -483
  105. data/test/test_www_impostor_wwf79.rb +0 -535
  106. data/test/test_www_impostor_wwf80.rb +0 -535
  107. data/vendor/plugins/impostor/lib/autotest/discover.rb +0 -3
  108. data/vendor/plugins/impostor/lib/autotest/impostor.rb +0 -49
@@ -0,0 +1,346 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe "a phpbb2 impostor" do
4
+
5
+ describe "authentication routines" do
6
+
7
+ before do
8
+ @auth = phpbb2_auth
9
+
10
+ @login_uri = URI.parse("http://example.com/forum/login.php")
11
+
12
+ @login_page = load_fixture_page(
13
+ "phpbb2-login.html",
14
+ @auth.config.login_page, 200, @auth.config.agent
15
+ )
16
+
17
+ @logged_in_page = load_fixture_page(
18
+ "phpbb2-logged-in.html",
19
+ @auth.config.app_root, 200, @auth.config.agent
20
+ )
21
+
22
+ @not_logged_in_page = load_fixture_page(
23
+ "phpbb2-not-logged-in.html",
24
+ @auth.config.app_root, 200, @auth.config.agent
25
+ )
26
+
27
+ @junk_page = load_fixture_page(
28
+ "junk.html", @auth.config.login_page, 200, @auth.config.agent
29
+ )
30
+ end
31
+
32
+ it "should logout only if not logged in" do
33
+ @auth.should_receive(:authenticated?).once.and_return(false)
34
+ @auth.logout.should_not be_true
35
+ end
36
+
37
+ it "should logout" do
38
+ @auth.config.should_receive(:save_topics).once
39
+ @auth.config.should_receive(:save_cookie_jar).once
40
+ @auth.instance_variable_set("@authenticated", true)
41
+
42
+ @auth.logout.should be_true
43
+ @auth.authenticated?.should_not be_true
44
+ end
45
+
46
+ it "should be logged_in? when phpbb2 displays the user name" do
47
+ lambda {
48
+ @auth.logged_in?(@logged_in_page).should be_true
49
+ }.should_not raise_error
50
+ end
51
+
52
+ it "should not be logged_in? when phpbb2 does not display the user name" do
53
+ lambda {
54
+ @auth.logged_in?(@not_logged_in_page).should_not be_true
55
+ }.should_not raise_error
56
+ end
57
+
58
+ it "should return a page from fetch_login_page" do
59
+ @auth.config.agent.should_receive(:get).with(@login_uri)
60
+
61
+ lambda {
62
+ @auth.fetch_login_page
63
+ }.should_not raise_error
64
+ end
65
+
66
+ it "should handle an error in fetch_login_page" do
67
+ @auth.config.agent.should_receive(:get).with(@login_uri).and_raise(StandardError)
68
+
69
+ lambda {
70
+ @auth.fetch_login_page
71
+ }.should raise_error( Impostor::LoginError )
72
+ end
73
+
74
+ it "should return a login form from get_login_form" do
75
+ lambda {
76
+ @auth.get_login_form(@login_page).action.should match(/login\.php/)
77
+ }.should_not raise_error
78
+ end
79
+
80
+ it "should raise login error when get_login_form receives a bad page" do
81
+ lambda {
82
+ @auth.get_login_form(@junk_page)
83
+ }.should raise_error( Impostor::LoginError )
84
+ end
85
+
86
+ it "should setup login form in set_username_and_password" do
87
+ form = mock "login form"
88
+ form.should_receive(:[]=).with("username", "tester")
89
+ form.should_receive(:[]=).with("password", "password")
90
+ form.should_receive(:[]=).with("autologin", "on")
91
+ form.should_receive(:[]=).with("login", "Log in")
92
+ lambda {
93
+ @auth.set_username_and_password(form).should == form
94
+ }.should_not raise_error
95
+ end
96
+
97
+ it "should return a logged in page when posting the login" do
98
+ form = @auth.get_login_form(@login_page)
99
+ @auth.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@logged_in_page)
100
+ lambda {
101
+ @auth.post_login(form).should == @logged_in_page
102
+ }.should_not raise_error
103
+ end
104
+
105
+ it "should raise a login error when posting has an underlying exception" do
106
+ form = @auth.get_login_form(@login_page)
107
+ @auth.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_raise(StandardError)
108
+ lambda {
109
+ @auth.post_login(form)
110
+ }.should raise_error( Impostor::LoginError )
111
+ end
112
+
113
+ it "should login" do
114
+ @auth.config.agent.should_receive(:get).with(@login_uri).and_return(@login_page)
115
+ @auth.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@logged_in_page)
116
+
117
+ lambda {
118
+ @auth.login.should be_true
119
+ }.should_not raise_error
120
+ end
121
+ end
122
+
123
+ describe "posting routines" do
124
+
125
+ before do
126
+ @post = phpbb2_post
127
+
128
+ @reply_uri = URI.parse("http://example.com/forum/posting.php?mode=reply&t=2")
129
+
130
+ @reply_response_page = load_fixture_page(
131
+ "phpbb2-get-new_topic-form-good-response.html",
132
+ @reply_uri, 200, @post.config.agent
133
+ )
134
+
135
+ @good_post_page = load_fixture_page(
136
+ "phpbb2-post-reply-good-response.html",
137
+ @post.auth.config.app_root, 200, @post.config.agent
138
+ )
139
+
140
+ @junk_page = load_fixture_page(
141
+ "junk.html", @post.config.login_page, 200, @post.config.agent
142
+ )
143
+ end
144
+
145
+ it "should post a message in the topic of a forum" do
146
+ @post.auth.should_receive(:login_with_raises)
147
+ @post.config.agent.should_receive(:get).with(@reply_uri).and_return(@reply_response_page)
148
+ @post.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@good_post_page)
149
+
150
+ #lambda {
151
+ @post.post(formum=1, topic=2, message="Hello World").should == {
152
+ :forum => 1,
153
+ :topic => 2,
154
+ :post => 17,
155
+ :message => "Hello World",
156
+ :result => true
157
+ }
158
+ #}.should_not raise_error
159
+ end
160
+
161
+ it "should get a reply uri from get_reply_uri(forum, topic)" do
162
+ lambda {
163
+ @post.get_reply_uri(1,2).should == @reply_uri
164
+ }.should_not raise_error
165
+ end
166
+
167
+ it "should get_reply_page(uri)" do
168
+ @post.config.agent.should_receive(:get).with(@reply_uri).and_return(@reply_response_page)
169
+ lambda {
170
+ @post.get_reply_page(@reply_uri).should == @reply_response_page
171
+ }.should_not raise_error
172
+ end
173
+
174
+ it "should return reply from with get_post_form(page)" do
175
+ lambda {
176
+ @post.get_post_form(@reply_response_page).name.should == 'post'
177
+ }.should_not raise_error
178
+ end
179
+
180
+ it "should raise error when page to get_post_form(page) receives a bad page" do
181
+ lambda {
182
+ @post.get_post_form(@junk_page)
183
+ }.should raise_error( Impostor::PostError )
184
+ end
185
+
186
+ it "should set_message(form, message)" do
187
+ form = @post.get_post_form(@reply_response_page)
188
+ form.should_receive(:message=, "Hello World")
189
+ lambda {
190
+ @post.set_message(form, "Hello World")
191
+ }.should_not raise_error
192
+ end
193
+
194
+ it "should return response page from post_message(form)" do
195
+ form = mock "post form"
196
+ form.should_receive(:submit).and_return @good_post_page
197
+ lambda {
198
+ @post.post_message(form).should == @good_post_page
199
+ }.should_not raise_error
200
+ end
201
+
202
+ it "should raise post error when post_form fails" do
203
+ form = mock "post form"
204
+ form.should_receive(:submit).and_raise( Impostor::PostError )
205
+ lambda {
206
+ @post.post_message(form)
207
+ }.should raise_error( Impostor::PostError )
208
+ end
209
+
210
+ it "should not raise post error on valid reply get_post_from_result(page)" do
211
+ lambda {
212
+ @post.get_post_from_result(@good_post_page).should be_true
213
+ }.should_not raise_error
214
+ end
215
+
216
+ it "should raise post error on invalid reply get_post_from_result(page)" do
217
+ lambda {
218
+ @post.get_post_from_result(@junk_page)
219
+ }.should raise_error( Impostor::PostError )
220
+ end
221
+
222
+ end
223
+
224
+ describe "topic routines" do
225
+
226
+ before do
227
+ @topic = phpbb2_topic
228
+
229
+ @new_topic_uri = URI.parse("http://example.com/forum/posting.php?mode=newtopic&f=1")
230
+
231
+ @new_topic_page = load_fixture_page(
232
+ "phpbb2-get-new_topic-form-good-response.html",
233
+ @new_topic_uri, 200, @topic.config.agent
234
+ )
235
+
236
+ @new_topic_good_result_uri = URI.parse("http://example.com/forum/viewtopic.php?f=1&t=2&p=325")
237
+
238
+ @new_topic_good_result = load_fixture_page(
239
+ "phpbb2-post-new_topic-good-response.html",
240
+ @new_topic_good_result_uri, 200, @topic.config.agent
241
+ )
242
+
243
+ @viewtopic_from_new_topic_good_result = load_fixture_page(
244
+ "phpbb2-get-viewtopic-for-new-topic-good-response.html",
245
+ @new_topic_good_result_uri, 200, @topic.config.agent
246
+ )
247
+ end
248
+
249
+ it "should return new topic uri when get_new_topic_uri called" do
250
+ lambda {
251
+ @topic.get_new_topic_uri(1, "OMG!", "Hello World").should == @new_topic_uri
252
+ }.should_not raise_error
253
+ end
254
+
255
+ it "should return new topic page when get_new_topic_page called" do
256
+ @topic.config.agent.should_receive(:get).with(@new_topic_uri).and_return(@new_topic_page)
257
+
258
+ lambda {
259
+ @topic.get_new_topic_page(@new_topic_uri).should == @new_topic_page
260
+ }.should_not raise_error
261
+ end
262
+
263
+ it "should return new topic form when get_new_topic_form called" do
264
+ lambda {
265
+ @topic.get_new_topic_form(@new_topic_page).name.should == 'post'
266
+ }.should_not raise_error
267
+ end
268
+
269
+ it "should raise topic error when get_new_topic_form has error" do
270
+ @new_topic_page.should_receive(:form).with("post").and_return nil
271
+ lambda {
272
+ @topic.get_new_topic_form(@new_topic_page)
273
+ }.should raise_error( Impostor::TopicError )
274
+ end
275
+
276
+ it "should set subject and message on a form when set_subject_and_message called" do
277
+ form = mock "phpbb2 topic form"
278
+ form.should_receive(:subject=).with("OMG!")
279
+ form.should_receive(:message=).with("Hello World")
280
+ form.should_receive(:[]=).with("post", "Submit")
281
+ lambda {
282
+ @topic.set_subject_and_message(form, "OMG!", "Hello World")
283
+ }.should_not raise_error
284
+ end
285
+
286
+ it "should post new topic with form when post_new_topic called" do
287
+ @topic.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@new_topic_good_result)
288
+ new_topic_form = @topic.get_new_topic_form(@new_topic_page)
289
+ lambda {
290
+ @topic.post_new_topic(new_topic_form)
291
+ }.should_not raise_error
292
+ end
293
+
294
+ it "should raise topic error when posting_new_topic has an error" do
295
+ form = mock "topic form"
296
+ form.should_receive(:submit).and_raise(StandardError)
297
+ lambda {
298
+ @topic.post_new_topic(form)
299
+ }.should raise_error( Impostor::TopicError )
300
+ end
301
+
302
+ it "should not raise topic error on valid reply validate_new_topic_result(page)" do
303
+ @topic.config.agent.should_receive(:get).with(
304
+ {:url=>"http://localhost/phpBB2/viewtopic.php?p=60#60", :referer=>instance_of(Mechanize::Page) }
305
+ ).and_return(@viewtopic_from_new_topic_good_result)
306
+ lambda {
307
+ @topic.validate_new_topic_result(@new_topic_good_result).should == @viewtopic_from_new_topic_good_result
308
+ }.should_not raise_error
309
+ end
310
+
311
+ it "should return the created topic id from get_topic_from_result" do
312
+ lambda {
313
+ @topic.get_topic_from_result(@viewtopic_from_new_topic_good_result).should == 2
314
+ }.should_not raise_error
315
+ end
316
+
317
+ it "should create new topic" do
318
+ form = mock "topic form", :submit => @new_topic_good_result
319
+ form.should_receive(:subject=).with("OMG!")
320
+ form.should_receive(:message=).with("Hello World")
321
+ form.should_receive(:[]=).with("post", "Submit")
322
+
323
+ @topic.auth.should_receive(:login_with_raises)
324
+ @topic.config.agent.should_receive(:get).with(@new_topic_uri).and_return(@new_topic_page)
325
+ @topic.should_receive(:get_new_topic_form).with(@new_topic_page).and_return(form)
326
+
327
+ @topic.config.should_receive(:add_subject).with(1, 2, "OMG!")
328
+
329
+ @topic.config.agent.should_receive(:get).with(
330
+ {:url=>"http://localhost/phpBB2/viewtopic.php?p=60#60", :referer=>instance_of(Mechanize::Page) }
331
+ ).and_return(@viewtopic_from_new_topic_good_result)
332
+
333
+ lambda {
334
+ @topic.new_topic(formum=1, subject="OMG!", message="Hello World").should == {
335
+ :forum => 1,
336
+ :topic => 2,
337
+ :subject => "OMG!",
338
+ :message => "Hello World",
339
+ :result => true
340
+ }
341
+ }.should_not raise_error
342
+ end
343
+ end
344
+
345
+ end
346
+
@@ -0,0 +1,332 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe "a phpbb3 impostor" do
4
+
5
+ describe "authentication routines" do
6
+
7
+ before do
8
+ @auth = phpbb3_auth
9
+
10
+ @login_uri = URI.parse("http://example.com/forum/ucp.php?mode=login")
11
+
12
+ @reply_uri = URI.parse("http://example.com/forum/posting.php?mode=reply&f=1&t=2")
13
+
14
+ @login_page = load_fixture_page(
15
+ "phpbb3-login.html",
16
+ @auth.config.login_page, 200, @auth.config.agent
17
+ )
18
+
19
+ @logged_in_page = load_fixture_page(
20
+ "phpbb3-logged-in.html",
21
+ @auth.config.app_root, 200, @auth.config.agent
22
+ )
23
+
24
+ @not_logged_in_page = load_fixture_page(
25
+ "phpbb3-not-logged-in.html",
26
+ @auth.config.app_root, 200, @auth.config.agent
27
+ )
28
+
29
+ @junk_page = load_fixture_page(
30
+ "junk.html", @reply_uri, 200, @auth.config.agent
31
+ )
32
+ end
33
+
34
+ it "should logout only if not logged in" do
35
+ @auth.should_receive(:authenticated?).once.and_return(false)
36
+ @auth.logout.should_not be_true
37
+ end
38
+
39
+ it "should logout" do
40
+ @auth.config.should_receive(:save_topics).once
41
+ @auth.config.should_receive(:save_cookie_jar).once
42
+ @auth.instance_variable_set("@authenticated", true)
43
+
44
+ @auth.logout.should be_true
45
+ @auth.authenticated?.should_not be_true
46
+ end
47
+
48
+ it "should be logged_in? when phpbb3 displays the user name" do
49
+ lambda {
50
+ @auth.logged_in?(@logged_in_page).should be_true
51
+ }.should_not raise_error
52
+ end
53
+
54
+ it "should not be logged_in? when phpbb3 does not display the user name" do
55
+ lambda {
56
+ @auth.logged_in?(@not_logged_in_page).should_not be_true
57
+ }.should_not raise_error
58
+ end
59
+
60
+ it "should return a page from fetch_login_page" do
61
+ @auth.config.agent.should_receive(:get).with(@login_uri)
62
+
63
+ lambda {
64
+ @auth.fetch_login_page
65
+ }.should_not raise_error
66
+ end
67
+
68
+ it "should handle an error in fetch_login_page" do
69
+ @auth.config.agent.should_receive(:get).with(@login_uri).and_raise(StandardError)
70
+
71
+ lambda {
72
+ @auth.fetch_login_page
73
+ }.should raise_error( Impostor::LoginError )
74
+ end
75
+
76
+ it "should return a login form from get_login_form" do
77
+ lambda {
78
+ @auth.get_login_form(@login_page).action.should match(/\/ucp\.php\?mode=login/)
79
+ }.should_not raise_error
80
+ end
81
+
82
+ it "should raise login error when get_login_form receives a bad page" do
83
+ lambda {
84
+ @auth.get_login_form(@junk_page)
85
+ }.should raise_error( Impostor::LoginError )
86
+ end
87
+
88
+ it "should setup login form in set_username_and_password" do
89
+ form = mock "login form"
90
+ form.should_receive(:[]=).with("username", "tester")
91
+ form.should_receive(:[]=).with("password", "password")
92
+ form.should_receive(:[]=).with("login", "Login")
93
+ form.should_receive(:[]=).with("autologin", "on")
94
+ lambda {
95
+ @auth.set_username_and_password(form).should == form
96
+ }.should_not raise_error
97
+ end
98
+
99
+ it "should return a logged in page when posting the login" do
100
+ form = @auth.get_login_form(@login_page)
101
+ @auth.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@logged_in_page)
102
+ lambda {
103
+ @auth.post_login(form).should == @logged_in_page
104
+ }.should_not raise_error
105
+ end
106
+
107
+ it "should raise a login error when posting has an underlying exception" do
108
+ form = @auth.get_login_form(@login_page)
109
+ @auth.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_raise(StandardError)
110
+ lambda {
111
+ @auth.post_login(form)
112
+ }.should raise_error( Impostor::LoginError )
113
+ end
114
+
115
+ it "should login" do
116
+ @auth.config.agent.should_receive(:get).with(@login_uri).and_return(@login_page)
117
+ @auth.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@logged_in_page)
118
+
119
+ lambda {
120
+ @auth.login.should be_true
121
+ }.should_not raise_error
122
+ end
123
+ end
124
+
125
+ describe "posting routines" do
126
+
127
+ before do
128
+ @post = phpbb3_post
129
+
130
+ @reply_uri = URI.parse("http://example.com/forum/posting.php?mode=reply&f=1&t=2")
131
+
132
+ @response_uri = URI.parse("http://example.com/forum/viewtopic.php?f=1&t=2&p=355#p355")
133
+
134
+ @reply_page = load_fixture_page(
135
+ "phpbb3-get-reply-form-good-response.html",
136
+ @reply_uri, 200, @post.config.agent
137
+ )
138
+
139
+ @good_response_page = load_fixture_page(
140
+ "phpbb3-post-reply-good-response.html",
141
+ @response_uri, 200, @post.config.agent
142
+ )
143
+
144
+ @junk_page = load_fixture_page(
145
+ "junk.html", @reply_uri, 200, @post.config.agent
146
+ )
147
+ end
148
+
149
+ it "should post a message in the topic of a forum" do
150
+ @post.auth.should_receive(:login_with_raises)
151
+ @post.config.agent.should_receive(:get).with(@reply_uri).and_return(@reply_page)
152
+ @post.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@good_response_page)
153
+
154
+ lambda {
155
+ @post.post(formum=1, topic=2, message="Hello World").should == {
156
+ :forum => 1,
157
+ :topic => 2,
158
+ :post => 355,
159
+ :message => "Hello World",
160
+ :result => true
161
+ }
162
+ }.should_not raise_error
163
+ end
164
+
165
+ it "should get a reply uri from get_reply_uri(forum, topic)" do
166
+ lambda {
167
+ @post.get_reply_uri(1,2).should == @reply_uri
168
+ }.should_not raise_error
169
+ end
170
+
171
+ it "should get_reply_page(uri)" do
172
+ @post.config.agent.should_receive(:get).with(@reply_uri).and_return(@reply_page)
173
+ lambda {
174
+ @post.get_reply_page(@reply_uri).should == @reply_page
175
+ }.should_not raise_error
176
+ end
177
+
178
+ it "should return reply from with get_post_form(page)" do
179
+ lambda {
180
+ @post.get_post_form(@reply_page).name.should == 'postform'
181
+ }.should_not raise_error
182
+ end
183
+
184
+ it "should raise error when page to get_post_form(page) receives a bad page" do
185
+ lambda {
186
+ @post.get_post_form(@junk_page)
187
+ }.should raise_error( Impostor::PostError )
188
+ end
189
+
190
+ it "should set_message(form, message)" do
191
+ form = @post.get_post_form(@reply_page)
192
+ form.should_receive(:message=, "Hello World")
193
+ lambda {
194
+ @post.set_message(form, "Hello World")
195
+ }.should_not raise_error
196
+ end
197
+
198
+ it "should return response page from post_message(form)" do
199
+ form = mock "post form"
200
+ form.should_receive(:submit).and_return @reply_page
201
+ lambda {
202
+ @post.post_message(form).should == @reply_page
203
+ }.should_not raise_error
204
+ end
205
+
206
+ it "should raise post error when post_form fails" do
207
+ form = mock "post form"
208
+ form.should_receive(:submit).and_raise( Impostor::PostError )
209
+ lambda {
210
+ @post.post_message(form)
211
+ }.should raise_error( Impostor::PostError )
212
+ end
213
+
214
+ it "should not raise post error on valid reply get_post_from_result(page)" do
215
+ lambda {
216
+ @post.get_post_from_result(@good_response_page).should == 355
217
+ }.should_not raise_error
218
+ end
219
+
220
+ it "should raise post error on invalid reply get_post_from_result(page)" do
221
+ lambda {
222
+ @post.get_post_from_result(@junk_page)
223
+ }.should raise_error( Impostor::PostError )
224
+ end
225
+
226
+ end
227
+
228
+ describe "topic routines" do
229
+
230
+ before do
231
+ @topic = phpbb3_topic
232
+
233
+ @new_topic_uri = URI.parse("http://example.com/forum/posting.php?mode=post&f=1")
234
+
235
+ @new_topic_page = load_fixture_page(
236
+ "phpbb3-get-new-topic-form-good-response.html",
237
+ @new_topic_uri, 200, @topic.config.agent
238
+ )
239
+
240
+ @new_topic_good_result_uri = URI.parse("http://example.com/forum/viewtopic.php?f=1&t=2&p=325")
241
+
242
+ @new_topic_good_result = load_fixture_page(
243
+ "phpbb3-post-new_topic-good-response.html",
244
+ @new_topic_good_result_uri, 200, @topic.config.agent
245
+ )
246
+ end
247
+
248
+ it "should return new topic uri when get_new_topic_uri called" do
249
+ lambda {
250
+ @topic.get_new_topic_uri(1, "OMG!", "Hello World").should == @new_topic_uri
251
+ }.should_not raise_error
252
+ end
253
+
254
+ it "should return new topic page when get_new_topic_page called" do
255
+ @topic.config.agent.should_receive(:get).with(@new_topic_uri).and_return(@new_topic_page)
256
+
257
+ lambda {
258
+ @topic.get_new_topic_page(@new_topic_uri).should == @new_topic_page
259
+ }.should_not raise_error
260
+ end
261
+
262
+ it "should return new topic form when get_new_topic_form called" do
263
+ lambda {
264
+ @topic.get_new_topic_form(@new_topic_page).name.should == 'postform'
265
+ }.should_not raise_error
266
+ end
267
+
268
+ it "should raise topic error when get_new_topic_form has error" do
269
+ @new_topic_page.should_receive(:forms).and_return []
270
+ lambda {
271
+ @topic.get_new_topic_form(@new_topic_page)
272
+ }.should raise_error( Impostor::TopicError )
273
+ end
274
+
275
+ it "should set subject and message on a form when set_subject_and_message called" do
276
+ form = mock "phpbb3 topic form"
277
+ form.should_receive(:subject=).with("OMG!")
278
+ form.should_receive(:message=).with("Hello World")
279
+ form.should_receive(:[]=).with("post", "Submit")
280
+ lambda {
281
+ @topic.set_subject_and_message(form, "OMG!", "Hello World")
282
+ }.should_not raise_error
283
+ end
284
+
285
+ it "should post new topic with form when post_new_topic called" do
286
+ @topic.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@new_topic_good_result)
287
+ new_topic_form = @topic.get_new_topic_form(@new_topic_page)
288
+ lambda {
289
+ @topic.post_new_topic(new_topic_form)
290
+ }.should_not raise_error
291
+ end
292
+
293
+ it "should raise topic error when posting_new_topic has an error" do
294
+ form = mock "a phpbb3 form"
295
+ form.should_receive(:submit).and_raise(StandardError)
296
+ lambda {
297
+ @topic.post_new_topic(form)
298
+ }.should raise_error( Impostor::TopicError )
299
+ end
300
+
301
+ it "should not raise topic error on valid reply validate_new_topic_result(page)" do
302
+ lambda {
303
+ @topic.validate_new_topic_result(@new_topic_good_result).should == @new_topic_good_result
304
+ }.should_not raise_error
305
+ end
306
+
307
+ it "should return the created topic id from get_topic_from_result" do
308
+ lambda {
309
+ @topic.get_topic_from_result(@new_topic_good_result).should == 205
310
+ }.should_not raise_error
311
+ end
312
+
313
+ it "should create new topic" do
314
+ @topic.auth.should_receive(:login_with_raises)
315
+ @topic.config.agent.should_receive(:get).with(@new_topic_uri).and_return(@new_topic_page)
316
+ @topic.config.agent.should_receive(:submit).with(instance_of(Mechanize::Form), nil, {}).and_return(@new_topic_good_result)
317
+ @topic.config.should_receive(:add_subject).with(1, 205, "OMG!")
318
+
319
+ lambda {
320
+ @topic.new_topic(formum=1, subject="OMG!", message="Hello World").should == {
321
+ :forum => 1,
322
+ :topic => 205,
323
+ :subject => "OMG!",
324
+ :message => "Hello World",
325
+ :result => true
326
+ }
327
+ }.should_not raise_error
328
+ end
329
+ end
330
+
331
+ end
332
+