To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 04 / 04353faa53c81964d7239a9938818ad1596cb331.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (7.24 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require File.expand_path('../../test_helper', __FILE__)
19
require 'messages_controller'
20

    
21
# Re-raise errors caught by the controller.
22
class MessagesController; def rescue_action(e) raise e end; end
23

    
24
class MessagesControllerTest < ActionController::TestCase
25
  fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
26

    
27
  def setup
28
    @controller = MessagesController.new
29
    @request    = ActionController::TestRequest.new
30
    @response   = ActionController::TestResponse.new
31
    User.current = nil
32
  end
33

    
34
  def test_show
35
    get :show, :board_id => 1, :id => 1
36
    assert_response :success
37
    assert_template 'show'
38
    assert_not_nil assigns(:board)
39
    assert_not_nil assigns(:project)
40
    assert_not_nil assigns(:topic)
41
  end
42
  
43
  def test_show_should_contain_reply_field_tags_for_quoting
44
    @request.session[:user_id] = 2
45
    get :show, :board_id => 1, :id => 1
46
    assert_response :success
47

    
48
    # tags required by MessagesController#quote
49
    assert_tag 'input', :attributes => {:id => 'message_subject'}
50
    assert_tag 'textarea', :attributes => {:id => 'message_content'}
51
    assert_tag 'div', :attributes => {:id => 'reply'}
52
  end
53

    
54
  def test_show_with_pagination
55
    message = Message.find(1)
56
    assert_difference 'Message.count', 30 do
57
      30.times do
58
        message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
59
      end
60
    end
61
    get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
62
    assert_response :success
63
    assert_template 'show'
64
    replies = assigns(:replies)
65
    assert_not_nil replies
66
    assert !replies.include?(message.children.first(:order => 'id'))
67
    assert replies.include?(message.children.last(:order => 'id'))
68
  end
69

    
70
  def test_show_with_reply_permission
71
    @request.session[:user_id] = 2
72
    get :show, :board_id => 1, :id => 1
73
    assert_response :success
74
    assert_template 'show'
75
    assert_tag :div, :attributes => { :id => 'reply' },
76
                     :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
77
  end
78

    
79
  def test_show_message_not_found
80
    get :show, :board_id => 1, :id => 99999
81
    assert_response 404
82
  end
83

    
84
  def test_show_message_from_invalid_board_should_respond_with_404
85
    get :show, :board_id => 999, :id => 1
86
    assert_response 404
87
  end
88

    
89
  def test_get_new
90
    @request.session[:user_id] = 2
91
    get :new, :board_id => 1
92
    assert_response :success
93
    assert_template 'new'
94
  end
95

    
96
  def test_post_new
97
    @request.session[:user_id] = 2
98
    ActionMailer::Base.deliveries.clear
99

    
100
    with_settings :notified_events => %w(message_posted) do
101
      post :new, :board_id => 1,
102
               :message => { :subject => 'Test created message',
103
                             :content => 'Message body'}
104
    end
105
    message = Message.find_by_subject('Test created message')
106
    assert_not_nil message
107
    assert_redirected_to "/boards/1/topics/#{message.to_param}"
108
    assert_equal 'Message body', message.content
109
    assert_equal 2, message.author_id
110
    assert_equal 1, message.board_id
111

    
112
    mail = ActionMailer::Base.deliveries.last
113
    assert_not_nil mail
114
    assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
115
    assert_mail_body_match 'Message body', mail
116
    # author
117
    assert mail.bcc.include?('jsmith@somenet.foo')
118
    # project member
119
    assert mail.bcc.include?('dlopper@somenet.foo')
120
  end
121

    
122
  def test_get_edit
123
    @request.session[:user_id] = 2
124
    get :edit, :board_id => 1, :id => 1
125
    assert_response :success
126
    assert_template 'edit'
127
  end
128

    
129
  def test_post_edit
130
    @request.session[:user_id] = 2
131
    post :edit, :board_id => 1, :id => 1,
132
                :message => { :subject => 'New subject',
133
                              :content => 'New body'}
134
    assert_redirected_to '/boards/1/topics/1'
135
    message = Message.find(1)
136
    assert_equal 'New subject', message.subject
137
    assert_equal 'New body', message.content
138
  end
139

    
140
  def test_post_edit_sticky_and_locked
141
    @request.session[:user_id] = 2
142
    post :edit, :board_id => 1, :id => 1,
143
                :message => { :subject => 'New subject',
144
                              :content => 'New body',
145
                              :locked => '1',
146
                              :sticky => '1'}
147
    assert_redirected_to '/boards/1/topics/1'
148
    message = Message.find(1)
149
    assert_equal true, message.sticky?
150
    assert_equal true, message.locked?
151
  end
152

    
153
  def test_post_edit_should_allow_to_change_board
154
    @request.session[:user_id] = 2
155
    post :edit, :board_id => 1, :id => 1,
156
                :message => { :subject => 'New subject',
157
                              :content => 'New body',
158
                              :board_id => 2}
159
    assert_redirected_to '/boards/2/topics/1'
160
    message = Message.find(1)
161
    assert_equal Board.find(2), message.board
162
  end
163

    
164
  def test_reply
165
    @request.session[:user_id] = 2
166
    post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
167
    reply = Message.find(:first, :order => 'id DESC')
168
    assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
169
    assert Message.find_by_subject('Test reply')
170
  end
171

    
172
  def test_destroy_topic
173
    @request.session[:user_id] = 2
174
    assert_difference 'Message.count', -3 do
175
      post :destroy, :board_id => 1, :id => 1
176
    end
177
    assert_redirected_to '/projects/ecookbook/boards/1'
178
    assert_nil Message.find_by_id(1)
179
  end
180

    
181
  def test_destroy_reply
182
    @request.session[:user_id] = 2
183
    assert_difference 'Message.count', -1 do
184
      post :destroy, :board_id => 1, :id => 2
185
    end
186
    assert_redirected_to '/boards/1/topics/1?r=2'
187
    assert_nil Message.find_by_id(2)
188
  end
189

    
190
  def test_quote
191
    @request.session[:user_id] = 2
192
    xhr :get, :quote, :board_id => 1, :id => 3
193
    assert_response :success
194
    assert_equal 'text/javascript', response.content_type
195
    assert_template 'quote'
196
    assert_include 'RE: First post', response.body
197
    assert_include '> An other reply', response.body
198
  end
199

    
200
  def test_preview_new
201
    @request.session[:user_id] = 2
202
    post :preview,
203
      :board_id => 1,
204
      :message => {:subject => "", :content => "Previewed text"}
205
    assert_response :success
206
    assert_template 'common/_preview'
207
  end
208

    
209
  def test_preview_edit
210
    @request.session[:user_id] = 2
211
    post :preview,
212
      :id => 4,
213
      :board_id => 1,
214
      :message => {:subject => "", :content => "Previewed text"}
215
    assert_response :success
216
    assert_template 'common/_preview'
217
  end
218
end