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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# encoding: utf-8
#--
# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
require File.dirname(__FILE__) + '/../test_helper'
class MessagesControllerTest < ActionController::TestCase
def setup
@request.env["HTTPS"] = "on"
end
without_ssl_context do
context "SSL" do
setup do
login_as :moe
end
context "GET :index" do
setup { get :index }
should_redirect_to_ssl
end
context "GET :sent" do
setup { get :sent }
should_redirect_to_ssl
end
context "GET :read" do
setup { get :read }
should_redirect_to_ssl
end
context "GET :show" do
setup { get :show }
should_redirect_to_ssl
end
context "POST :create" do
setup { post :create }
should_redirect_to_ssl
end
context "POST :reply" do
setup { get :reply }
should_redirect_to_ssl
end
context "POST auto_complete_for_message_recipients" do
setup { post :auto_complete_for_message_recipients }
should_redirect_to_ssl
end
end
end
should_render_in_global_context
context 'On GET to index' do
setup do
login_as :moe
get :index
end
should_respond_with :success
should_assign_to :messages
should_render_template :index
end
context 'On GET to index with XML' do
setup do
login_as :moe
get :index, :format => 'xml'
end
should_respond_with :success
should_assign_to :messages
end
context 'On GET to sent' do
setup do
login_as :moe
get :sent
end
should_respond_with :success
should_assign_to :messages
should_render_template :sent
end
context 'On GET to show' do
setup do
@message = messages(:johans_message_to_moe)
login_as :moe
get :show, :id => @message.to_param
end
should_respond_with :success
should_assign_to :message
end
context "On GET to show (marking as read)" do
setup do
login_as :moe
@message = messages(:johans_message_to_moe)
@message.build_reply({
:body => "indeed", :sender => users(:moe), :recipient => users(:moe)
}).save!
@message.build_reply({
:body => "quite", :sender => users(:johan), :recipient => users(:moe)
}).save!
assert @message.messages_in_thread.size >= 2, "msg thread only have one msg"
end
should "mark all messages as read when viewing a thread" do
get :show, :id => @message.to_param
assert_response :success
assert_equal [true]*3, @message.reload.messages_in_thread.map(&:read?)
end
end
context 'On GET to show in XML' do
setup do
@message = messages(:johans_message_to_moe)
login_as :moe
get :show, :id => @message.to_param, :format => 'xml'
end
should_respond_with :success
should_assign_to :message
end
context 'Trying to peek at other peoples messages' do
setup do
login_as :mike
get :show, :id => @message.to_param
end
should_respond_with :not_found
end
context 'On PUT to read' do
setup do
login_as :moe
@message = messages(:johans_message_to_moe)
put :read, :id => @message.to_param, :format => 'js'
end
should_respond_with :success
should_assign_to :message#, @message)
end
context 'On POST to create' do
setup do
login_as :moe
post :create, :message => {:subject => "Hello", :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", :recipients => 'johan'}
end
should_respond_with :redirect
should_assign_to :messages
should_set_the_flash_to(/sent/i)
end
context 'On POST to create with several recipients' do
setup {login_as :moe}
should 'allow separating recipients with various separating tokens' do
[',',' ','.'].each do |token|
assert_incremented_by Message, :count, 2 do
post :create, :message => {:subject => 'Hello', :body => 'This is for several recipients', :recipients => %w(johan mike).join(token)}
end
end
end
end
context 'On POST to reply' do # POST /messages/2/reply
setup do
login_as :moe
@original_message = messages(:johans_message_to_moe)
post :reply, :id => @original_message.to_param, :message => {:body => "Yeah, great idea", :subject => "Well"}
end
should_assign_to :message
should_respond_with :redirect
should_set_the_flash_to(/sent/i)
should 'set the correct subject' do
result = assigns(:message)
assert_equal("Well", result.subject)
end
end
context 'On GET to new' do
setup do
login_as :johan
get :new
end
should_assign_to :message
should_respond_with :success
should_render_template :new
should "set the sender" do
assert_equal users(:johan), assigns(:message).sender
end
end
should "insert the username on GET new if the to querystring param is given" do
login_as :johan
get :new, :to => users(:mike).login
assert_select "#message_recipients[value=?]", users(:mike).login
end
context 'On GET to all' do
setup {
login_as :johan
get :all
}
should_assign_to :messages
should_respond_with :success
should_render_template :all
end
context 'On POST to auto_complete_for_message_recipients' do
setup do
login_as :johan
end
should 'not include current_user when looking up' do
post :auto_complete_for_message_recipients, :q => "joh", :format => "js"
assert_equal([], assigns(:users))
end
should 'assign an array of users when looking up' do
post :auto_complete_for_message_recipients, :q => "mik", :format => "js"
assert_equal([users(:mike)], assigns(:users))
end
end
context 'On PUT to bulk_update' do
setup do
@sender = Factory.create(:user)
@recipient = Factory.create(:user)
@messages = 10.times.collect{ |i|
Message.create(:sender => @sender, :recipient => @recipient, :subject => "Message #{i}", :body => "Hello world")
}
end
should 'mark the selected messages as read when supplying no action' do
@request.session[:user_id] = @recipient.id
put :bulk_update, :message_ids => @messages.collect(&:id)
assert_response :redirect
@messages.each do |msg|
assert msg.reload.read?
end
end
should 'archive the selected messages for the current user' do
@request.session[:user_id] = @recipient.id
put :bulk_update, :message_ids => @messages.collect(&:id), :requested_action => 'archive'
assert_response :redirect
@messages.each do |msg|
assert msg.reload.archived_by_recipient?
end
end
end
context 'Unauthenticated GET to index' do
setup {get :index}
should_respond_with :redirect
end
end
|