Skip to content

Commit

Permalink
Making negative_captcha hashes predictable for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
epetre committed Sep 11, 2012
1 parent a3efc21 commit 7798e35
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/negative_captcha.rb
Expand Up @@ -10,6 +10,11 @@ class NegativeCaptcha
:timestamp,
:error

TEST_MODE = false
def self.test_mode=(value)
const_set("TEST_MODE", value)
end

def initialize(opts)
self.secret = opts[:secret] ||
Digest::MD5.hexdigest("this_is_a_secret_key")
Expand All @@ -30,7 +35,7 @@ def initialize(opts)
MESSAGE

self.fields = opts[:fields].inject({}) do |hash, field_name|
hash[field_name] = Digest::MD5.hexdigest(
hash[field_name] = TEST_MODE ? "test-#{field_name}" : Digest::MD5.hexdigest(
[field_name, spinner, secret].join('-')
)

Expand Down
2 changes: 1 addition & 1 deletion negative_captcha.gemspec
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = "negative_captcha"
s.version = "0.3"
s.version = "0.3.0"
s.authors = ["Erik Peterson"]
s.email = ["erik@subwindow.com"]
s.homepage = "http://github.com/subwindow/negative-captcha"
Expand Down
25 changes: 25 additions & 0 deletions test/testable_negative_captcha_test.rb
@@ -0,0 +1,25 @@
require 'rubygems'
require 'active_support'
require 'action_view'
require 'test/unit'
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/negative-captcha'))

class TestableNegativeCaptchaTest < Test::Unit::TestCase
def test_can_be_predictable_for_tests
NegativeCaptcha.test_mode = true
fields = [:name, :comment]

captcha = NegativeCaptcha.new(:fields => fields)
assert captcha.fields.is_a?(Hash)
assert_equal captcha.fields.keys.sort{|a,b|a.to_s<=>b.to_s}, fields.sort{|a,b|a.to_s<=>b.to_s}

filled_form = NegativeCaptcha.new(
:fields => fields,
:timestamp => captcha.timestamp,
:params => {:timestamp => captcha.timestamp, :spinner => captcha.spinner}.merge(captcha.fields.inject({}){|hash, name, encrypted_name| hash[encrypted_name] = name; hash})
)

assert_equal "", filled_form.error
assert filled_form.valid?
end
end

0 comments on commit 7798e35

Please sign in to comment.