Handle removed author when rendering comments
[gitorious:mainline.git] / test / unit / presenters / comment_presenter_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2013 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18
19 require "test_helper"
20
21 class CommentPresenterTest < ActiveSupport::TestCase
22   context "#context" do
23     should "render context" do
24       comment = Comment.new(:context => "the foo")
25       presenter = CommentPresenter.new(comment, view_context)
26
27       assert_include presenter.context, "<code>the foo</code>"
28     end
29
30     should "not render context when blank" do
31       comment = Comment.new
32       presenter = CommentPresenter.new(comment, view_context)
33
34       assert_blank presenter.context
35     end
36   end
37
38   context "#state_changed" do
39     should "render nothing when state_changed_to is nil" do
40       comment = Comment.new
41       presenter = CommentPresenter.new(comment, view_context)
42
43       assert_blank presenter.state_changed
44     end
45
46     should "render only state changed to when state_changed_from is nil" do
47       comment = Comment.new(:state_change => ['Open'])
48       presenter = CommentPresenter.new(comment, view_context)
49
50       assert_include presenter.state_changed, '<span class="label label-success">Open</span>'
51     end
52
53     should "render both state changed form and to" do
54       comment = Comment.new(:state_change => ['Open', 'Closed'])
55       presenter = CommentPresenter.new(comment, view_context)
56
57       assert_include presenter.state_changed, '<span class="label label-success">Open</span>'
58       assert_include presenter.state_changed, '<span class="label label-inverse">Closed</span>'
59     end
60   end
61
62   context "#markdown" do
63     should "render comment's markdown" do
64       comment = Comment.new(:body => "*foo*")
65       presenter = CommentPresenter.new(comment, view_context)
66
67       assert_include presenter.markdown, '<em>foo</em>'
68     end
69   end
70
71   context "#avatar" do
72     should "render authors avatar" do
73       author = users(:johan)
74       author.avatar_file_name = "foo.png"
75
76       comment = Comment.new(:user => author)
77       presenter = CommentPresenter.new(comment, view_context)
78
79       assert_include presenter.avatar, 'johan/thumb/foo.png'
80     end
81
82     should "render default avatar for removed author" do
83       comment = Comment.new
84       presenter = CommentPresenter.new(comment, view_context)
85
86       assert_include presenter.avatar, Gitorious::View::AvatarHelper::DEFAULT_AVATAR_FILE
87     end
88   end
89
90   context "#author_link" do
91     should "render link to authors profile" do
92       author = users(:johan)
93       comment = Comment.new(:user => author)
94       presenter = CommentPresenter.new(comment, view_context)
95
96       assert_include presenter.author_link, 'johan'
97       assert_include presenter.author_link, view_context.user_path(author)
98     end
99
100     should "render a placeholder when author was removed" do
101       comment = Comment.new
102       presenter = CommentPresenter.new(comment, view_context)
103
104       assert_include presenter.author_link, 'Removed Author'
105     end
106   end
107
108   context "#label" do
109     should "render commit link if applies" do
110       comment = comments(:first_merge_request_version_comment)
111       comment.created_at = Time.parse("2013-10-17 10:12")
112       presenter = CommentPresenter.new(comment, view_context)
113
114       assert_include presenter.label, "#ffac0-a"
115       assert_include presenter.label, "Oct 17 2013, 10:12."
116     end
117
118     should "render only date if not attached to any commit" do
119       comment = Comment.new
120       comment.created_at = Time.parse("2013-10-17 10:12")
121       presenter = CommentPresenter.new(comment, view_context)
122
123       assert_equal "Oct 17 2013, 10:12.", presenter.label.strip
124     end
125   end
126
127   context "#edit_link" do
128     def view_context_with_current_user(user)
129       v = view_context
130       v.stubs(:current_user => user)
131       v
132     end
133
134     should "render edit link to the author" do
135       comment = comments(:first_merge_request_version_comment)
136
137       v = view_context_with_current_user(comment.user)
138       v.stubs(:edit_comment_path).with(comment).returns("/path/to/comment")
139
140       presenter = CommentPresenter.new(comment, v)
141
142       assert_include presenter.edit_link, "Edit comment"
143       assert_include presenter.edit_link, "/path/to/comment"
144     end
145
146     should "not render edit link for other users" do
147       comment = comments(:first_merge_request_version_comment)
148       v = view_context_with_current_user(users(:moe))
149       presenter = CommentPresenter.new(comment, v)
150
151       assert_blank presenter.edit_link
152     end
153   end
154 end