impostor 0.0.1

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 (53) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +50 -0
  3. data/README.txt +65 -0
  4. data/Rakefile +40 -0
  5. data/lib/impostor.rb +6 -0
  6. data/lib/www/impostor/phpbb2.rb +260 -0
  7. data/lib/www/impostor/wwf79.rb +254 -0
  8. data/lib/www/impostor/wwf80.rb +264 -0
  9. data/lib/www/impostor.rb +269 -0
  10. data/test/fixtures/phpbb2-get-new_topic-form-good-response.html +725 -0
  11. data/test/fixtures/phpbb2-get-viewtopic-for-new-topic-good-response.html +364 -0
  12. data/test/fixtures/phpbb2-get-viewtopic-for-new-topic-malformed-response.html +364 -0
  13. data/test/fixtures/phpbb2-index.html +361 -0
  14. data/test/fixtures/phpbb2-logged-in.html +349 -0
  15. data/test/fixtures/phpbb2-login.html +306 -0
  16. data/test/fixtures/phpbb2-not-logged-in.html +349 -0
  17. data/test/fixtures/phpbb2-post-new_topic-good-response.html +290 -0
  18. data/test/fixtures/phpbb2-post-reply-good-response.html +290 -0
  19. data/test/fixtures/phpbb2-post-reply-throttled-response.html +290 -0
  20. data/test/fixtures/phpbb2-too-many-posts.html +290 -0
  21. data/test/fixtures/wwf79-forum_posts.html +422 -0
  22. data/test/fixtures/wwf79-general-new-topic-error.html +46 -0
  23. data/test/fixtures/wwf79-general-posting-error.html +46 -0
  24. data/test/fixtures/wwf79-good-post-forum_posts.html +462 -0
  25. data/test/fixtures/wwf79-index.html +137 -0
  26. data/test/fixtures/wwf79-logged-in.html +46 -0
  27. data/test/fixtures/wwf79-login.html +123 -0
  28. data/test/fixtures/wwf79-new-topic-forum_posts-response.html +305 -0
  29. data/test/fixtures/wwf79-new-topic-post_message_form.html +212 -0
  30. data/test/fixtures/wwf79-not-logged-in.html +128 -0
  31. data/test/fixtures/wwf79-too-many-posts.html +46 -0
  32. data/test/fixtures/wwf79-too-many-topics.html +46 -0
  33. data/test/fixtures/wwf80-general-posting-error.html +54 -0
  34. data/test/fixtures/wwf80-get-new_topic-form-good-response.html +217 -0
  35. data/test/fixtures/wwf80-get-viewtopic-for-new-topic-good-response.html +290 -0
  36. data/test/fixtures/wwf80-index.html +371 -0
  37. data/test/fixtures/wwf80-logged-in.html +52 -0
  38. data/test/fixtures/wwf80-login.html +125 -0
  39. data/test/fixtures/wwf80-new_reply_form.html +204 -0
  40. data/test/fixtures/wwf80-not-logged-in.html +136 -0
  41. data/test/fixtures/wwf80-post-new_topic-good-response.html +293 -0
  42. data/test/fixtures/wwf80-post-reply-good-response.html +331 -0
  43. data/test/fixtures/wwf80-too-many-posts.html +54 -0
  44. data/test/test_helper.rb +38 -0
  45. data/test/test_www_impostor.rb +165 -0
  46. data/test/test_www_impostor_phpbb2.rb +536 -0
  47. data/test/test_www_impostor_wwf79.rb +535 -0
  48. data/test/test_www_impostor_wwf80.rb +535 -0
  49. data/vendor/plugins/impostor/lib/autotest/discover.rb +3 -0
  50. data/vendor/plugins/impostor/lib/autotest/impostor.rb +49 -0
  51. data.tar.gz.sig +3 -0
  52. metadata +156 -0
  53. metadata.gz.sig +3 -0
@@ -0,0 +1,535 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "www", "impostor")
2
+ require File.join(File.dirname(__FILE__), "..", "lib", "www", "impostor", "wwf79")
3
+ require File.join(File.dirname(__FILE__), "test_helper")
4
+
5
+ require 'test/unit'
6
+ require 'rubygems'
7
+ require 'mocha'
8
+ require 'mechanize'
9
+
10
+ class TestWwwImpostorWwf79 < Test::Unit::TestCase
11
+ include TestHelper
12
+
13
+ def setup
14
+ @cookie_jar = File.join(Dir.tmpdir, 'www_impostor_wwf79_test.yml')
15
+ @app_root = 'http://localhost/wwf79/'
16
+ @im = WWW::Impostor.new(config())
17
+ end
18
+
19
+ def teardown
20
+ File.delete(@cookie_jar) if File.exist?(@cookie_jar)
21
+ end
22
+
23
+ def config(config={})
24
+ c = {:type => :wwf79,
25
+ :app_root => @app_root,
26
+ :login_page => 'login_user.asp',
27
+ :forum_posts_page => 'forum_posts.asp',
28
+ :post_message_page => 'post_message_form.asp',
29
+ :user_agent => 'Windows IE 7',
30
+ :username => 'tester',
31
+ :password => 'test',
32
+ :cookie_jar => @cookie_jar
33
+ }.merge(config)
34
+ c
35
+ end
36
+
37
+ def wwf79_good_submit_post_form
38
+ %q!<form action="post_message.asp?PN=" method="post" name="frmAddMessage">
39
+ <input name="message" value="" type="hidden">
40
+ <input name="Submit" type="submit">
41
+ </form>!
42
+ end
43
+
44
+ def wwf79_good_submit_new_topic_form
45
+ %q!<form action="post_message.asp?PN=" method="post" name="frmAddMessage">
46
+ <input name="subject" type="text">
47
+ <input name="message" value="" type="hidden">
48
+ <input name="Submit" type="submit">
49
+ </form>!
50
+ end
51
+
52
+ def test_initialize_with_cookie_jar
53
+ FileUtils.touch(@cookie_jar)
54
+
55
+ WWW::Mechanize::CookieJar.any_instance.expects(:load).once.with(@cookie_jar)
56
+ im = WWW::Impostor.new(config())
57
+ assert im
58
+ end
59
+
60
+ def test_initialize_without_cookie_jar
61
+ WWW::Mechanize::CookieJar.any_instance.expects(:load).never
62
+ im = WWW::Impostor.new(config())
63
+ assert im
64
+ end
65
+
66
+ def test_version
67
+ assert @im.version
68
+ end
69
+
70
+ def test_should_be_logged_in?
71
+ response = {'content-type' => 'text/html'}
72
+ body = load_page('wwf79-logged-in.html').join
73
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
74
+ assert_equal true, @im.send(:logged_in?, page)
75
+ end
76
+
77
+ def test_should_not_be_logged_in?
78
+ response = {'content-type' => 'text/html'}
79
+ body = load_page('wwf79-not-logged-in.html').join
80
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
81
+ assert_equal false, @im.send(:logged_in?, page)
82
+ end
83
+
84
+ def test_fetch_login_page
85
+ page = load_page('wwf79-login.html').join
86
+ WWW::Mechanize.any_instance.expects(:get).once.with(
87
+ URI.join(@app_root, config[:login_page])
88
+ ).returns(page)
89
+
90
+ assert_equal page, @im.send(:fetch_login_page)
91
+ end
92
+
93
+ def test_login_form_and_button_should_raise_login_error_when_form_is_missing
94
+ err = assert_raise(WWW::Impostor::LoginError) do
95
+ form, button = @im.send(:login_form_and_button, nil)
96
+ end
97
+ assert_equal "unknown login page format", err.original_exception.message
98
+ end
99
+
100
+ def test_login_form_and_button_should_return_a_form_and_button
101
+ response = {'content-type' => 'text/html'}
102
+ body = load_page('wwf79-login.html').join
103
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
104
+ form, button = @im.send(:login_form_and_button, page)
105
+ assert_equal true, form.is_a?(WWW::Mechanize::Form)
106
+ assert_equal true, button.is_a?(WWW::Mechanize::Form::Button)
107
+ end
108
+
109
+ def test_post_login_should_return_page
110
+ response = {'content-type' => 'text/html'}
111
+ body = load_page('wwf79-logged-in.html').join
112
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
113
+ form = mock()
114
+ button = mock()
115
+ WWW::Mechanize.any_instance.expects(:submit).once.with(form, button).returns(page)
116
+
117
+ assert_equal page, @im.send(:post_login, form, button)
118
+ end
119
+
120
+ def test_post_login_should_raise_login_error
121
+ errmsg = "from test #{Time.now.to_s}"
122
+ WWW::Mechanize.any_instance.expects(:submit).raises(StandardError, errmsg)
123
+ err = assert_raise(WWW::Impostor::LoginError) do
124
+ page = @im.send(:post_login, nil, nil)
125
+ end
126
+ assert_equal errmsg, err.original_exception.message
127
+ end
128
+
129
+ def test_bad_login_page_should_raise_exception
130
+ errmsg = "from test #{Time.now.to_s}"
131
+ WWW::Mechanize.any_instance.expects(:get).once.with(
132
+ URI.join(@app_root, config[:login_page])
133
+ ).raises(StandardError, errmsg)
134
+
135
+ err = assert_raise(WWW::Impostor::LoginError) do
136
+ @im.send(:fetch_login_page)
137
+ end
138
+ assert_equal errmsg, err.original_exception.message
139
+ end
140
+
141
+ def test_already_logged_in_should_not_post_login_information_again_instance_varialbe
142
+ @im.instance_variable_set(:@loggedin, true)
143
+ @im.expects(:fetch_login_page).never
144
+ assert_equal true, @im.login
145
+ end
146
+
147
+ def test_already_logged_in_should_not_post_login_information_again
148
+ @im.instance_variable_set(:@loggedin, false)
149
+ page = mock()
150
+ @im.stubs(:fetch_login_page).returns(page)
151
+ @im.expects(:logged_in?).once.with(page).returns(true)
152
+ @im.expects(:login_form_and_button).with(page).never
153
+ assert_equal true, @im.login
154
+ end
155
+
156
+ def test_login_should_login
157
+ @im.instance_variable_set(:@loggedin, false)
158
+ login_page = mock()
159
+ @im.stubs(:fetch_login_page).returns(login_page)
160
+ @im.expects(:logged_in?).once.with(login_page).returns(false)
161
+ form = mock()
162
+ button = mock()
163
+ @im.expects(:login_form_and_button).with(login_page).returns([form, button])
164
+ logged_in_page = mock()
165
+ @im.expects(:post_login).with(form, button).returns(logged_in_page)
166
+ @im.expects(:logged_in?).once.with(logged_in_page).returns(true)
167
+ @im.expects(:load_topics).once.returns(true)
168
+
169
+ assert_equal true, @im.login
170
+ end
171
+
172
+ def test_logout_does_nothing_if_logged_out
173
+ @im.instance_variable_set(:@loggedin, false)
174
+ @im.expects(:cookie_jar).never
175
+ @im.expects(:save_topics).never
176
+ assert_equal false, @im.logout
177
+ end
178
+
179
+ def test_logout
180
+ @im.instance_variable_set(:@loggedin, true)
181
+ cookie_jar = mock()
182
+ @im.expects(:cookie_jar).times(2).returns(cookie_jar)
183
+ WWW::Mechanize::CookieJar.any_instance.expects(:save_as).once.with(cookie_jar).returns(nil)
184
+ @im.expects(:save_topics).once
185
+ assert_equal true, @im.logout
186
+ assert_equal nil, @im.instance_variable_get(:@forum)
187
+ assert_equal nil, @im.instance_variable_get(:@topic)
188
+ assert_equal nil, @im.instance_variable_get(:@subject)
189
+ assert_equal nil, @im.instance_variable_get(:@message)
190
+ assert_equal false, @im.instance_variable_get(:@loggedin)
191
+ end
192
+
193
+ def test_forum_posts_page
194
+ c = config
195
+ assert_equal URI.join(@app_root, c[:forum_posts_page]), @im.forum_posts_page
196
+ end
197
+
198
+ def test_post_message_page
199
+ c = config
200
+ assert_equal URI.join(@app_root, c[:post_message_page]), @im.post_message_page
201
+ end
202
+
203
+ def test_post_without_forum_set_should_raise_exception
204
+ @im.instance_variable_set(:@forum, nil)
205
+ err = assert_raise(WWW::Impostor::PostError) do
206
+ @im.post
207
+ end
208
+ assert_equal "forum not set", err.original_exception.message
209
+ err = assert_raise(WWW::Impostor::PostError) do
210
+ @im.post(f=nil,t=nil,m=nil)
211
+ end
212
+ assert_equal "forum not set", err.original_exception.message
213
+ end
214
+
215
+ def test_post_without_topic_set_should_raise_exception
216
+ @im.instance_variable_set(:@forum, 1)
217
+ @im.instance_variable_set(:@topic, nil)
218
+ err = assert_raise(WWW::Impostor::PostError) do
219
+ @im.post
220
+ end
221
+ assert_equal "topic not set", err.original_exception.message
222
+ err = assert_raise(WWW::Impostor::PostError) do
223
+ @im.post(f=2,t=nil,m=nil)
224
+ end
225
+ assert_equal "topic not set", err.original_exception.message
226
+ end
227
+
228
+ def test_post_without_message_set_should_raise_exception
229
+ @im.instance_variable_set(:@forum, 1)
230
+ @im.instance_variable_set(:@topic, 1)
231
+ @im.instance_variable_set(:@message, nil)
232
+ err = assert_raise(WWW::Impostor::PostError) do
233
+ @im.post
234
+ end
235
+ assert_equal "message not set", err.original_exception.message
236
+ err = assert_raise(WWW::Impostor::PostError) do
237
+ @im.post(f=2,t=2,m=nil)
238
+ end
239
+ assert_equal "message not set", err.original_exception.message
240
+ end
241
+
242
+ def test_post_not_logged_in_should_raise_exception
243
+ @im.expects(:login).once.returns(false)
244
+ @im.instance_variable_set(:@loggedin, false)
245
+ err = assert_raise(WWW::Impostor::PostError) do
246
+ @im.post(2,2,'hello')
247
+ end
248
+ assert_equal "not logged in", err.original_exception.message
249
+ end
250
+
251
+ def test_bad_post_page_for_post_should_raise_exception
252
+ @im.instance_variable_set(:@loggedin, true)
253
+ topic = 2
254
+ forum_posts_page = @im.forum_posts_page
255
+ forum_posts_page.query = "TID=#{topic}&TPN=10000"
256
+ errmsg = "from test #{Time.now.to_s}"
257
+ WWW::Mechanize.any_instance.expects(:get).once.with(
258
+ forum_posts_page
259
+ ).raises(StandardError, errmsg)
260
+ err = assert_raise(WWW::Impostor::PostError) do
261
+ @im.post(7,topic,'hello')
262
+ end
263
+ assert_equal errmsg, err.original_exception.message
264
+ end
265
+
266
+ def test_bad_post_form_for_post_should_raise_exception
267
+ @im.instance_variable_set(:@loggedin, true)
268
+ response = {'content-type' => 'text/html'}
269
+ body = '<form action="post_message.asp?PN=" method="post" name="frmAddMessage"></form>'
270
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
271
+ topic = 2
272
+ forum_posts_page = @im.forum_posts_page
273
+ forum_posts_page.query = "TID=#{topic}&TPN=10000"
274
+ WWW::Mechanize.any_instance.expects(:get).once.with(forum_posts_page).returns(page)
275
+ err = assert_raise(WWW::Impostor::PostError) do
276
+ @im.post(1,topic,'hello')
277
+ end
278
+ assert_equal "post form not found", err.original_exception.message
279
+ end
280
+
281
+ def test_submitting_bad_post_form_for_post_should_raise_exception
282
+ @im.instance_variable_set(:@loggedin, true)
283
+ response = {'content-type' => 'text/html'}
284
+ body = wwf79_good_submit_post_form
285
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
286
+ topic = 2
287
+ forum_posts_page = @im.forum_posts_page
288
+ forum_posts_page.query = "TID=#{topic}&TPN=10000"
289
+ WWW::Mechanize.any_instance.expects(:get).once.with(forum_posts_page).returns(page)
290
+ errmsg = "from test #{Time.now.to_s}"
291
+ WWW::Mechanize.any_instance.expects(:submit).once.raises(StandardError, errmsg)
292
+ err = assert_raise(WWW::Impostor::PostError) do
293
+ @im.post(1,topic,'hello')
294
+ end
295
+ assert_equal errmsg, err.original_exception.message
296
+ end
297
+
298
+ def test_too_many_posts_for_post_should_raise_exception
299
+ @im.instance_variable_set(:@loggedin, true)
300
+ response = {'content-type' => 'text/html'}
301
+ body = wwf79_good_submit_post_form
302
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
303
+ topic = 2
304
+ forum_posts_page = @im.forum_posts_page
305
+ forum_posts_page.query = "TID=#{topic}&TPN=10000"
306
+ WWW::Mechanize.any_instance.expects(:get).once.with(forum_posts_page).returns(page)
307
+ body = load_page('wwf79-too-many-posts.html').join
308
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
309
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
310
+
311
+ err = assert_raise(WWW::Impostor::ThrottledError) do
312
+ @im.post(1,topic,'hello')
313
+ end
314
+ assert_equal "You have exceeded the number of posts permitted in the time span", err.original_exception.message
315
+ end
316
+
317
+ def test_getting_unknown_post_response_should_raise_exception
318
+ @im.instance_variable_set(:@loggedin, true)
319
+ response = {'content-type' => 'text/html'}
320
+ body = wwf79_good_submit_post_form
321
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
322
+ topic = 2
323
+ forum_posts_page = @im.forum_posts_page
324
+ forum_posts_page.query = "TID=#{topic}&TPN=10000"
325
+ WWW::Mechanize.any_instance.expects(:get).once.with(forum_posts_page).returns(page)
326
+ body = load_page('wwf79-general-posting-error.html').join
327
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
328
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
329
+
330
+ err = assert_raise(WWW::Impostor::PostError) do
331
+ @im.post(1,topic,'hello')
332
+ end
333
+ assert_equal "There was an error making the post", err.original_exception.message
334
+ end
335
+
336
+ def test_should_post
337
+ @im.instance_variable_set(:@loggedin, true)
338
+ response = {'content-type' => 'text/html'}
339
+ body = wwf79_good_submit_post_form
340
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
341
+ topic = 2
342
+ forum_posts_page = @im.forum_posts_page
343
+ forum_posts_page.query = "TID=#{topic}&TPN=10000"
344
+ WWW::Mechanize.any_instance.expects(:get).once.with(forum_posts_page).returns(page)
345
+ body = load_page('wwf79-good-post-forum_posts.html').join
346
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
347
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
348
+ subject = "test #{Time.now.to_s}"
349
+ @im.expects(:get_subject).once.returns(subject)
350
+
351
+ assert_equal true, @im.post(1,topic,'hello')
352
+ assert_equal 1, @im.instance_variable_get(:@forum)
353
+ assert_equal topic, @im.instance_variable_get(:@topic)
354
+ assert_equal subject, @im.instance_variable_get(:@subject)
355
+ assert_equal 'hello', @im.instance_variable_get(:@message)
356
+ end
357
+
358
+ def test_new_topic_without_forum_set_should_raise_exception
359
+ @im.instance_variable_set(:@forum, nil)
360
+ err = assert_raise(WWW::Impostor::PostError) do
361
+ @im.new_topic
362
+ end
363
+ assert_equal "forum not set", err.original_exception.message
364
+ err = assert_raise(WWW::Impostor::PostError) do
365
+ @im.new_topic(f=nil,s="hello world",m="hello world")
366
+ end
367
+ assert_equal "forum not set", err.original_exception.message
368
+ end
369
+
370
+ def test_new_topic_without_subject_set_should_raise_exception
371
+ @im.instance_variable_set(:@forum, 1)
372
+ @im.instance_variable_set(:@subject, nil)
373
+ err = assert_raise(WWW::Impostor::PostError) do
374
+ @im.new_topic
375
+ end
376
+ assert_equal "topic name not given", err.original_exception.message
377
+ err = assert_raise(WWW::Impostor::PostError) do
378
+ @im.new_topic(f=1,s=nil,m="hello world")
379
+ end
380
+ assert_equal "topic name not given", err.original_exception.message
381
+ end
382
+
383
+ def test_new_topic_without_message_set_should_raise_exception
384
+ @im.instance_variable_set(:@forum, 1)
385
+ @im.instance_variable_set(:@subject, 'test')
386
+ @im.instance_variable_set(:@message, nil)
387
+ err = assert_raise(WWW::Impostor::PostError) do
388
+ @im.new_topic
389
+ end
390
+ assert_equal "message not set", err.original_exception.message
391
+ err = assert_raise(WWW::Impostor::PostError) do
392
+ @im.new_topic(f=1,s="hello world",m=nil)
393
+ end
394
+ assert_equal "message not set", err.original_exception.message
395
+ end
396
+
397
+ def test_new_topic_not_logged_in_should_raise_exception
398
+ @im.expects(:login).once.returns(false)
399
+ @im.instance_variable_set(:@loggedin, false)
400
+
401
+ err = assert_raise(WWW::Impostor::PostError) do
402
+ @im.new_topic(f=2,s="hello world",m="hello ruby")
403
+ end
404
+ assert_equal "not logged in", err.original_exception.message
405
+ end
406
+
407
+ def test_getting_bad_post_page_for_new_topic_should_raise_exception
408
+ @im.instance_variable_set(:@loggedin, true)
409
+ forum = 2
410
+ post_message_page = @im.post_message_page
411
+ post_message_page.query = "FID=#{forum}"
412
+ errmsg = "from test #{Time.now.to_s}"
413
+ WWW::Mechanize.any_instance.expects(:get).once.with(
414
+ post_message_page
415
+ ).raises(StandardError, errmsg)
416
+
417
+ err = assert_raise(WWW::Impostor::PostError) do
418
+ @im.new_topic(f=forum,s="hello world",m="hello ruby")
419
+ end
420
+ assert_equal errmsg, err.original_exception.message
421
+ end
422
+
423
+ def test_getting_bad_post_form_for_new_topic_should_raise_exception
424
+ @im.instance_variable_set(:@loggedin, true)
425
+ response = {'content-type' => 'text/html'}
426
+ body = '<form action="post_message.asp?PN=" method="post" name="frmAddMessage"></form>'
427
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
428
+ forum = 2
429
+ post_message_page = @im.post_message_page
430
+ post_message_page.query = "FID=#{forum}"
431
+ WWW::Mechanize.any_instance.expects(:get).once.with(post_message_page).returns(page)
432
+ err = assert_raise(WWW::Impostor::PostError) do
433
+ @im.new_topic(f=forum,s="hello world",m="hello ruby")
434
+ end
435
+ assert_equal "post form not found", err.original_exception.message
436
+ end
437
+
438
+ def test_submitting_bad_post_for_new_topic_form_should_raise_exception
439
+ @im.instance_variable_set(:@loggedin, true)
440
+ response = {'content-type' => 'text/html'}
441
+ body = wwf79_good_submit_new_topic_form
442
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
443
+ forum = 2
444
+ post_message_page = @im.post_message_page
445
+ post_message_page.query = "FID=#{forum}"
446
+ WWW::Mechanize.any_instance.expects(:get).once.with(post_message_page).returns(page)
447
+ errmsg = "from test #{Time.now.to_s}"
448
+ WWW::Mechanize.any_instance.expects(:submit).once.raises(StandardError, errmsg)
449
+ err = assert_raise(WWW::Impostor::PostError) do
450
+ @im.new_topic(f=forum,s="hello world",m="hello ruby")
451
+ end
452
+ assert_equal errmsg, err.original_exception.message
453
+ end
454
+
455
+ def test_too_many_posts_for_new_topic_should_raise_exception
456
+ @im.instance_variable_set(:@loggedin, true)
457
+ response = {'content-type' => 'text/html'}
458
+ body = wwf79_good_submit_new_topic_form
459
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
460
+ forum = 2
461
+ post_message_page = @im.post_message_page
462
+ post_message_page.query = "FID=#{forum}"
463
+ WWW::Mechanize.any_instance.expects(:get).once.with(post_message_page).returns(page)
464
+ body = load_page('wwf79-too-many-topics.html').join
465
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
466
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
467
+
468
+ err = assert_raise(WWW::Impostor::ThrottledError) do
469
+ @im.new_topic(f=forum,s="hello world",m="hello ruby")
470
+ end
471
+ assert_equal "You have exceeded the number of posts permitted in the time span", err.original_exception.message
472
+ end
473
+
474
+ def test_getting_unknown_new_topic_response_should_raise_exception
475
+ @im.instance_variable_set(:@loggedin, true)
476
+ response = {'content-type' => 'text/html'}
477
+ body = wwf79_good_submit_new_topic_form
478
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
479
+ forum = 2
480
+ post_message_page = @im.post_message_page
481
+ post_message_page.query = "FID=#{forum}"
482
+ WWW::Mechanize.any_instance.expects(:get).once.with(post_message_page).returns(page)
483
+ body = load_page('wwf79-general-new-topic-error.html').join
484
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
485
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
486
+
487
+ err = assert_raise(WWW::Impostor::PostError) do
488
+ @im.new_topic(f=forum,s="hello world",m="hello ruby")
489
+ end
490
+ assert_equal "There was an error creating the topic", err.original_exception.message
491
+ end
492
+
493
+ def test_malformed_form_with_topicid_for_new_topic_should_raise_exception
494
+ @im.instance_variable_set(:@loggedin, true)
495
+ response = {'content-type' => 'text/html'}
496
+ body = wwf79_good_submit_new_topic_form
497
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
498
+ forum = 2
499
+ post_message_page = @im.post_message_page
500
+ post_message_page.query = "FID=#{forum}"
501
+ WWW::Mechanize.any_instance.expects(:get).once.with(post_message_page).returns(page)
502
+
503
+ body = 'junk'
504
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
505
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
506
+
507
+ err = assert_raise(WWW::Impostor::PostError) do
508
+ @im.new_topic(f=forum,s="hello world",m="hello ruby")
509
+ end
510
+ assert_equal "unexpected new topic ID", err.original_exception.message
511
+ end
512
+
513
+ def test_new_topic_should_work
514
+ @im.instance_variable_set(:@loggedin, true)
515
+ response = {'content-type' => 'text/html'}
516
+ body = wwf79_good_submit_new_topic_form
517
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
518
+ forum = 2
519
+ subject = "hello world"
520
+ message = "hello ruby"
521
+ post_message_page = @im.post_message_page
522
+ post_message_page.query = "FID=#{forum}"
523
+ WWW::Mechanize.any_instance.expects(:get).once.with(post_message_page).returns(page)
524
+ body = load_page('wwf79-new-topic-forum_posts-response.html').join
525
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
526
+ WWW::Mechanize.any_instance.expects(:submit).once.returns(page)
527
+
528
+ @im.expects(:add_subject).once.with(forum,357,subject)
529
+ assert_equal true, @im.new_topic(f=forum,s=subject,m=message)
530
+ assert_equal forum, @im.instance_variable_get(:@forum)
531
+ assert_equal 357, @im.instance_variable_get(:@topic)
532
+ assert_equal subject, @im.instance_variable_get(:@subject)
533
+ assert_equal message, @im.instance_variable_get(:@message)
534
+ end
535
+ end