Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 427
- Log:
Version 1.0.1 of the Hub application, which adds reCaptcha
verification to the sign-up form. Implemented after spambots
attacked the ROOL site and signed up hundreds of bogus users.
- Author:
- rool
- Date:
- Tue Aug 30 11:48:22 +0100 2011
- Size:
- 4370 Bytes
1 | # coding: utf-8 |
2 | |
3 | require 'test/unit' |
4 | require 'rubygems' |
5 | require 'active_support/core_ext/string' |
6 | require 'mocha' |
7 | require 'i18n' |
8 | require 'net/http' |
9 | require File.dirname(File.expand_path(__FILE__)) + '/../lib/recaptcha' |
10 | |
11 | class RecaptchaVerifyTest < Test::Unit::TestCase |
12 | def setup |
13 | Recaptcha.configuration.private_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' |
14 | @controller = TestController.new |
15 | @controller.request = stub(:remote_ip => "1.1.1.1") |
16 | @controller.params = {:recaptcha_challenge_field => "challenge", :recaptcha_response_field => "response"} |
17 | |
18 | @expected_post_data = {} |
19 | @expected_post_data["privatekey"] = Recaptcha.configuration.private_key |
20 | @expected_post_data["remoteip"] = @controller.request.remote_ip |
21 | @expected_post_data["challenge"] = "challenge" |
22 | @expected_post_data["response"] = "response" |
23 | |
24 | @expected_uri = URI.parse(Recaptcha.configuration.verify_url) |
25 | end |
26 | |
27 | def test_should_raise_exception_without_private_key |
28 | assert_raise Recaptcha::RecaptchaError do |
29 | Recaptcha.configuration.private_key = nil |
30 | @controller.verify_recaptcha |
31 | end |
32 | end |
33 | |
34 | def test_should_return_false_when_key_is_invalid |
35 | expect_http_post(response_with_body("false\ninvalid-site-private-key")) |
36 | |
37 | assert !@controller.verify_recaptcha |
38 | assert_equal "invalid-site-private-key", @controller.flash[:recaptcha_error] |
39 | end |
40 | |
41 | def test_returns_true_on_success |
42 | @controller.flash[:recaptcha_error] = "previous error that should be cleared" |
43 | expect_http_post(response_with_body("true\n")) |
44 | |
45 | assert @controller.verify_recaptcha |
46 | assert_nil @controller.flash[:recaptcha_error] |
47 | end |
48 | |
49 | def test_errors_should_be_added_to_model |
50 | expect_http_post(response_with_body("false\nbad-news")) |
51 | |
52 | errors = mock |
53 | errors.expects(:add).with(:base, "Word verification response is incorrect, please try again.") |
54 | model = mock(:errors => errors) |
55 | |
56 | assert !@controller.verify_recaptcha(:model => model) |
57 | assert_equal "bad-news", @controller.flash[:recaptcha_error] |
58 | end |
59 | |
60 | def test_returns_true_on_success_with_optional_key |
61 | @controller.flash[:recaptcha_error] = "previous error that should be cleared" |
62 | # reset private key |
63 | @expected_post_data["privatekey"] = 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX' |
64 | expect_http_post(response_with_body("true\n")) |
65 | |
66 | assert @controller.verify_recaptcha(:private_key => 'ADIFFERENTPRIVATEKEYXXXXXXXXXXXXXX') |
67 | assert_nil @controller.flash[:recaptcha_error] |
68 | end |
69 | |
70 | def test_timeout |
71 | expect_http_post(Timeout::Error, :exception => true) |
72 | assert !@controller.verify_recaptcha() |
73 | assert_equal "recaptcha-not-reachable", @controller.flash[:recaptcha_error] |
74 | end |
75 | |
76 | def test_message_should_use_i18n |
77 | I18n.locale = :de |
78 | verification_failed_translated = "Sicherheitscode konnte nicht verifiziert werden." |
79 | verification_failed_default = "Word verification response is incorrect, please try again." |
80 | recaptcha_unreachable_translated = "Netzwerkfehler, bitte versuchen Sie es später erneut." |
81 | recaptcha_unreachable_default = "Oops, we failed to validate your word verification response. Please try again." |
82 | I18n.expects(:translate).with(:'recaptcha.errors.verification_failed', :default => verification_failed_default).returns(verification_failed_translated) |
83 | I18n.expects(:translate).with(:'recaptcha.errors.recaptcha_unreachable', :default => recaptcha_unreachable_default).returns(recaptcha_unreachable_translated) |
84 | |
85 | errors = mock |
86 | errors.expects(:add).with(:base, verification_failed_translated) |
87 | errors.expects(:add).with(:base, recaptcha_unreachable_translated) |
88 | model = mock; model.stubs(:errors => errors) |
89 | |
90 | expect_http_post(response_with_body("false\nbad-news")) |
91 | @controller.verify_recaptcha(:model => model) |
92 | |
93 | expect_http_post(Timeout::Error, :exception => true) |
94 | @controller.verify_recaptcha(:model => model) |
95 | |
96 | end |
97 | |
98 | private |
99 | |
100 | class TestController |
101 | include Recaptcha::Verify |
102 | attr_accessor :request, :params, :flash |
103 | |
104 | def initialize |
105 | @flash = {} |
106 | end |
107 | end |
108 | |
109 | def expect_http_post(response, options = {}) |
110 | unless options[:exception] |
111 | Net::HTTP.expects(:post_form).with(@expected_uri, @expected_post_data).returns(response) |
112 | else |
113 | Net::HTTP.expects(:post_form).raises response |
114 | end |
115 | end |
116 | |
117 | def response_with_body(body) |
118 | stub(:body => body) |
119 | end |
120 | end |