forked from xaviershay/enki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_spec.rb
181 lines (148 loc) · 4.99 KB
/
comment_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require File.dirname(__FILE__) + '/../spec_helper'
describe Comment do
def valid_comment_attributes(extra = {})
{
:author => 'Don Alias',
:body => 'This is a comment',
:post => Post.new
}.merge(extra)
end
def set_comment_attributes(comment, extra = {})
valid_comment_attributes(extra).each_pair do |key, value|
comment.send("#{key}=", value)
end
end
before(:each) do
@comment = Comment.new
end
it "is invalid with no post" do
set_comment_attributes(@comment, :post => nil)
@comment.should_not be_valid
@comment.errors.should_not be_empty
end
it "is invalid with no body" do
set_comment_attributes(@comment, :body => '')
@comment.should_not be_valid
@comment.errors.should_not be_empty
end
it "is invalid with no author" do
set_comment_attributes(@comment, :author => '')
@comment.should_not be_valid
@comment.errors.should_not be_empty
end
it "is valid with a full set of valid attributes" do
set_comment_attributes(@comment)
@comment.should be_valid
end
it "requires OpenID authentication when the author's name contains a period" do
@comment.author = "Don Alias"
@comment.requires_openid_authentication?.should == false
@comment.author = "enkiblog.com"
@comment.requires_openid_authentication?.should == true
end
it "asks post to update it's comment counter after save" do
set_comment_attributes(@comment)
@comment.blank_openid_fields
@comment.post.update_attributes(:title => 'My Post', :body => "body")
@comment.post.save
@comment.save
@comment.post.approved_comments.count.should == 1
end
it "asks post to update it's comment counter after destroy" do
set_comment_attributes(@comment)
@comment.blank_openid_fields
@comment.post.update_attributes(:title => 'My Post', :body => "body")
@comment.post.save
@comment.save
@comment.destroy
@comment.post.approved_comments.count.should == 0
end
it "applies a Lesstile filter to body and store it in body_html before save" do
set_comment_attributes(@comment)
@comment.blank_openid_fields
@comment.post.update_attributes(:title => 'My Post', :body => "body")
@comment.post.save
@comment.save
@comment.body_html.should_not be_nil
end
it "responds to trusted_user? for defensio integration" do
@comment.respond_to?(:trusted_user?).should == true
end
it "responds to user_logged_in? for defensio integration" do
@comment.respond_to?(:user_logged_in?).should == true
end
it "delegates post_tile to post" do
@comment.post = mock_model(Post)
@comment.post.should_receive(:title).and_return("hello")
@comment.post_title.should == "hello"
end
# TODO: acts_as_defensio_comment tests
# TODO: OpenID error model
end
describe Comment, '#blank_openid_fields_if_unused' do
before(:each) do
@comment = Comment.new
@comment.blank_openid_fields
end
it('blanks out author_url') { @comment.author_url.should == '' }
it('blanks out author_email') { @comment.author_email.should == '' }
end
describe Comment, '.find_recent' do
it 'finds the most recent comments that were posted before now' do
now = Time.now
Time.stub!(:now).and_return(now)
Comment.should_receive(:find).with(:all, {
:order => 'created_at DESC',
:limit => Comment::DEFAULT_LIMIT
}).and_return(comments = [mock_model(Comment)])
Comment.find_recent.should == comments
end
it 'allows and override of the default limit' do
Comment.should_receive(:find).with(:all, hash_including(:limit => 999))
Comment.find_recent(:limit => 999)
end
end
describe Comment, '.build_for_preview' do
before(:each) do
@comment = Comment.build_for_preview(:author => 'Don Alias', :body => 'A Comment')
end
it 'returns a new comment' do
@comment.should be_new_record
end
it 'sets created_at' do
@comment.created_at.should_not be_nil
end
it 'applies filter to body' do
@comment.body_html.should == 'A Comment'
end
end
describe Comment, '.build_for_preview with OpenID author' do
before(:each) do
@comment = Comment.build_for_preview(:author => 'http://enkiblog.com', :body => 'A Comment')
end
it 'returns a new comment' do
@comment.should be_new_record
end
it 'sets created_at' do
@comment.created_at.should_not be_nil
end
it 'applies filter to body' do
@comment.body_html.should == 'A Comment'
end
it 'sets author_url to OpenID identity' do
@comment.author_url.should == 'http://enkiblog.com'
end
it 'sets author to "Your OpenID Name"' do
@comment.author.should == "Your OpenID Name"
end
end
describe Comment, '#requires_openid_authentication?' do
describe 'with an author containing a .' do
subject { Comment.new(:author => 'example.com').requires_openid_authentication? }
it { should be }
end
describe 'with a normal author' do
subject { Comment.new(:author => 'Don Alias').requires_openid_authentication? }
it { should_not be }
end
end