acts_as_textcaptcha 2.2.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,227 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class Widget < ActiveRecord::Base
4
- # uses textcaptcha.yml file for configuration
5
- acts_as_textcaptcha
6
- end
7
-
8
- class Comment < ActiveRecord::Base
9
- # inline options with api_key only
10
- acts_as_textcaptcha({'api_key' => '8u5ixtdnq9csc84cok0owswgo'})
11
- end
12
-
13
- class Review < ActiveRecord::Base
14
- # inline options with all possible options
15
- acts_as_textcaptcha({'api_key' => '8u5ixtdnq9csc84cok0owswgo',
16
- 'bcrypt_salt' => '$2a$10$j0bmycH.SVfD1b5mpEGPpe',
17
- 'bcrypt_cost' => '3',
18
- 'questions' => [{'question' => '1+1', 'answers' => '2,two'},
19
- {'question' => 'The green hat is what color?', 'answers' => 'green'},
20
- {'question' => 'Which is bigger: 67, 14 or 6', 'answers' => '67,sixtyseven,sixty seven,sixty-seven'}]})
21
- end
22
-
23
- class Note < ActiveRecord::Base
24
- # inline options with user defined questions only (no textcaptcha service)
25
- acts_as_textcaptcha('questions' => [{'question' => '1+1', 'answers' => '2,two'}])
26
- end
27
-
28
- class Contact
29
- # non active record object
30
- include ActiveModel::Validations
31
- include ActiveModel::Conversion
32
- extend ActsAsTextcaptcha::Textcaptcha
33
- acts_as_textcaptcha(:questions => [{:question => '1+1', :answers => '2,two'}])
34
- end
35
-
36
-
37
- describe 'ActsAsTextcaptcha' do
38
-
39
- before(:each) do
40
- @comment = Comment.new
41
- @review = Review.new
42
- @note = Note.new
43
- @contact = Contact.new
44
- end
45
-
46
- describe 'validations' do
47
-
48
- before(:each) do
49
- @note.generate_spam_question
50
- @note.validate_textcaptcha.should be_false
51
- @note.should_not be_valid
52
- end
53
-
54
- it "should validate non ActiveRecord object" do
55
- @contact.generate_spam_question
56
- @contact.spam_answer = 'wrong'
57
- @contact.validate_textcaptcha.should be_false
58
- @contact.should_not be_valid
59
-
60
- @contact.spam_answer = 'two'
61
- @contact.validate_textcaptcha.should be_true
62
- @contact.should be_valid
63
- end
64
-
65
- it 'should validate spam answer with possible answers' do
66
- @note.spam_answer = '2'
67
- @note.validate_textcaptcha.should be_true
68
- @note.should be_valid
69
-
70
- @note.spam_answer = 'two'
71
- @note.validate_textcaptcha.should be_true
72
- @note.should be_valid
73
-
74
- @note.spam_answer = 'wrong'
75
- @note.validate_textcaptcha.should be_false
76
- @note.errors[:spam_answer].should eql(['is incorrect, try another question instead'])
77
- @note.should_not be_valid
78
- end
79
-
80
- it 'should strip whitespace and downcase spam answer' do
81
- @note.spam_answer = ' tWo '
82
- @note.validate_textcaptcha.should be_true
83
- @note.should be_valid
84
-
85
- @note.spam_answer = ' 2 '
86
- @note.validate_textcaptcha.should be_true
87
- @note.should be_valid
88
- end
89
-
90
- it 'should always validate if not a new record' do
91
- @note.spam_answer = '2'
92
- @note.save!
93
- @note.generate_spam_question
94
-
95
- @note.new_record?.should be_false
96
- @note.validate_textcaptcha.should be_true
97
- @note.should be_valid
98
- end
99
- end
100
-
101
- describe 'encryption' do
102
-
103
- it 'should encrypt answers' do
104
- @review.encrypt_answers(['123', '456']).should eql(['$2a$10$j0bmycH.SVfD1b5mpEGPperNj9IlIHoieNk38UDQFdtREOmRFKgou',
105
- '$2a$10$j0bmycH.SVfD1b5mpEGPpeqf88jqdV6gIgeJLQNjUnufIn8dys1fW'])
106
- end
107
-
108
- it 'should encrypt a single answer' do
109
- @review.encrypt_answer('123').should eql('$2a$10$j0bmycH.SVfD1b5mpEGPperNj9IlIHoieNk38UDQFdtREOmRFKgou')
110
- end
111
-
112
- it 'should not encrypt if no bycrpt-salt set' do
113
- @comment.encrypt_answer('123').should eql('123')
114
- @comment.encrypt_answers(['123', '456']).should eql(['123', '456'])
115
- end
116
- end
117
-
118
- describe 'flags' do
119
-
120
- it 'should always be valid if skip_spam_check? is true' do
121
- @comment.generate_spam_question
122
- @comment.validate_textcaptcha.should be_false
123
- @comment.should_not be_valid
124
-
125
- @comment.stub!(:perform_spam_check?).and_return(false)
126
- @comment.validate_textcaptcha.should be_true
127
- @comment.should be_valid
128
- end
129
-
130
- it 'should always fail validation if allowed? is false' do
131
- @comment.validate_textcaptcha.should be_true
132
- @comment.stub!(:allowed?).and_return(false)
133
-
134
- @comment.validate_textcaptcha.should be_false
135
- @comment.errors[:base].should eql(['Sorry, adding a Comment is currently disabled'])
136
- @comment.should_not be_valid
137
- end
138
- end
139
-
140
- describe 'with inline options hash' do
141
-
142
- it 'should be configurable from inline options with keys as symbols' do
143
- @comment.textcaptcha_config.should eql({:api_key => '8u5ixtdnq9csc84cok0owswgo'})
144
- @review.textcaptcha_config.should eql({:bcrypt_cost =>'3', :questions => [{'question' => '1+1', 'answers' => '2,two'},
145
- {'question' => 'The green hat is what color?', 'answers' => 'green'},
146
- {'question' => 'Which is bigger: 67, 14 or 6', 'answers' => '67,sixtyseven,sixty seven,sixty-seven'}],
147
- :bcrypt_salt => '$2a$10$j0bmycH.SVfD1b5mpEGPpe',
148
- :api_key => '8u5ixtdnq9csc84cok0owswgo'})
149
- @note.textcaptcha_config.should eql({:questions => [{:question => '1+1', :answers => '2,two'}]})
150
- @contact.textcaptcha_config.should eql({:questions => [{:question => '1+1', :answers => '2,two'}]})
151
- end
152
-
153
- it 'should generate spam question from textcaptcha service' do
154
- @comment.generate_spam_question
155
- @comment.spam_question.should_not be_nil
156
- @comment.possible_answers.should_not be_nil
157
- @comment.possible_answers.should be_an(Array)
158
-
159
- @comment.validate_textcaptcha.should be_false
160
- @comment.should_not be_valid
161
- end
162
-
163
- describe 'and textcaptcha unavailable' do
164
-
165
- before(:each) do
166
- Net::HTTP.stub(:get).and_raise(SocketError)
167
- end
168
-
169
- it 'should fall back to random user defined question when set' do
170
- @review.generate_spam_question
171
- @review.spam_question.should_not be_nil
172
- @review.possible_answers.should_not be_nil
173
- @review.possible_answers.should be_an(Array)
174
-
175
- @review.validate_textcaptcha.should be_false
176
- @review.should_not be_valid
177
- end
178
-
179
- it 'should not generate any spam question/answer if no user defined questions set' do
180
- @comment.generate_spam_question
181
- @comment.spam_question.should be_nil
182
- @comment.possible_answers.should be_nil
183
- @comment.validate_textcaptcha.should be_true
184
- @comment.should be_valid
185
- end
186
- end
187
- end
188
-
189
- describe 'with textcaptcha yaml config file' do
190
-
191
- before(:each) do
192
- @widget = Widget.new
193
- end
194
-
195
- it 'should be configurable from config/textcaptcha.yml file' do
196
- @widget.textcaptcha_config[:api_key].should eql('8u5ixtdnq9csc84cok0owswgo')
197
- @widget.textcaptcha_config[:bcrypt_salt].should eql('$2a$10$j0bmycH.SVfD1b5mpEGPpe')
198
- @widget.textcaptcha_config[:bcrypt_cost].should eql(10)
199
- @widget.textcaptcha_config[:questions].length.should eql(10)
200
- end
201
-
202
- it 'should generate spam question' do
203
- @widget.generate_spam_question
204
- @widget.spam_question.should_not be_nil
205
- @widget.possible_answers.should_not be_nil
206
- @widget.possible_answers.should be_an(Array)
207
- @widget.validate_textcaptcha.should be_false
208
- @widget.should_not be_valid
209
- end
210
-
211
- describe 'and textcaptcha unavailable' do
212
-
213
- before(:each) do
214
- Net::HTTP.stub(:get).and_raise(SocketError)
215
- end
216
-
217
- it 'should fall back to a random user defined question' do
218
- @widget.generate_spam_question
219
- @widget.spam_question.should_not be_nil
220
- @widget.possible_answers.should_not be_nil
221
- @widget.possible_answers.should be_an(Array)
222
- @widget.validate_textcaptcha.should be_false
223
- @widget.should_not be_valid
224
- end
225
- end
226
- end
227
- end
data/spec/database.yml DELETED
@@ -1,21 +0,0 @@
1
- sqlite:
2
- :adapter: sqlite
3
- :database: acts_as_textcaptcha.sqlite.db
4
-
5
- sqlite3:
6
- :adapter: sqlite3
7
- :database: acts_as_textcaptcha.sqlite3.db
8
-
9
- postgresql:
10
- :adapter: postgresql
11
- :username: postgres
12
- :password: postgres
13
- :database: acts_as_textcaptcha_test
14
- :min_messages: ERROR
15
-
16
- mysql:
17
- :adapter: mysql
18
- :host: localhost
19
- :username: root
20
- :password:
21
- :database: acts_as_textcaptcha_test
data/spec/spec.opts DELETED
@@ -1,2 +0,0 @@
1
- -cfs
2
- --loadby mtime
data/spec/spec_helper.rb DELETED
@@ -1,8 +0,0 @@
1
- require 'bundler'
2
- require 'active_record'
3
- require 'rails'
4
- require 'acts_as_textcaptcha'
5
-
6
- # setup db
7
- ActiveRecord::Base.establish_connection(YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))[ENV['DB'] || 'sqlite3'])
8
- load(File.dirname(__FILE__) + "/schema.rb")