summaryrefslogtreecommitdiffstats
path: root/test/functional/commits_controller_test.rb
blob: fac82ea030f694d35c69d21a865d145f54c7cffb (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# 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 CommitsControllerTest < ActionController::TestCase

  context "showing a single commit" do
    setup do
      @project = projects(:johans)
      @repository = @project.repositories.mainlines.first
      @repository.update_attribute(:ready, true)
          
      Repository.any_instance.stubs(:full_repository_path).returns(grit_test_repo("dot_git"))
      @grit = Grit::Repo.new(grit_test_repo("dot_git"), :is_bare => true)
      Repository.any_instance.stubs(:git).returns(@grit)
      @sha = "3fa4e130fa18c92e3030d4accb5d3e0cadd40157"
    end
    
    should "get the correct project and repository" do
      get :show, {:project_id => @project.to_param, 
          :repository_id => @repository.to_param, :id => @sha}
      assert_equal @project, assigns(:project)
      assert_equal @repository, assigns(:repository)
    end
    
    should "gets the commit data" do
      get :show, {:project_id => @project.slug, 
          :repository_id => @repository.name, :id => @sha}
      assert_response :success
      assert_equal @repository.git, assigns(:git)
      assert_equal @repository.git.commit(@sha), assigns(:commit)
      assert_not_nil assigns(:diffs)
    end
  
    should "gets the comments for the commit" do
      get :show, {:project_id => @project.slug, 
          :repository_id => @repository.name, :id => @sha}
      assert_equal 0, assigns(:comment_count)
    end
  
    should "defaults to 'inline' diffmode" do
      get :show, {:project_id => @project.slug, 
          :repository_id => @repository.name, :id => @sha}
      assert_equal "inline", assigns(:diffmode)
    end
  
    should "sets sidebyside diffmode" do
      get :show, {:project_id => @project.slug, 
          :repository_id => @repository.name, :id => @sha, :diffmode => "sidebyside" }
      assert_equal "sidebyside", assigns(:diffmode)
    end
    
    should "get it in diff format" do
      get :show, :project_id => @project.slug, 
          :repository_id => @repository.name, :id => @sha, :format => "diff"
      assert_response :success
      assert_equal "text/plain", @response.content_type
      assert_equal @repository.git.commit(@sha).diffs.map{|d| d.diff}.join("\n"), @response.body
    end
    
    should "get it in patch format" do
      get :show, :project_id => @project.slug, 
          :repository_id => @repository.name, :id => @sha, :format => "patch"
      assert_response :success
      assert_equal "text/plain", @response.content_type
      assert_equal @repository.git.commit(@sha).to_patch, @response.body
    end
    
    should "redirect to the commit log with a msg if the SHA1 was not found" do
      @grit.expects(:commit).with("123").returns(nil)
      get :show, :project_id => @project.slug, 
          :repository_id => @repository.name, :id => "123"
      assert_response :redirect
      assert_match(/no such sha/i, flash[:error])
      assert_redirected_to project_repository_commits_path(@project, @repository)
    end
    
    should "not show diffs for the initial commit" do
      commit = @grit.commit(@sha)
      commit.stubs(:parents).returns([])
      @grit.expects(:commit).returns(commit)
      get :show, {:project_id => @project.to_param, 
          :repository_id => @repository.to_param, :id => @sha}
      assert_equal [], assigns(:diffs)
      assert_select "#content p", /This is the initial commit in this repository/
    end

    should "have a different last-modified if there's a comment" do
      Comment.create!({
          :user => users(:johan),
          :body => "foo",
          :sha1 => @sha,
          :target => @repository,
          :project => @repository.project,
      })
      get :show, :project_id => @project.slug,
          :repository_id => @repository.name, :id => @sha
      assert_response :success
      assert_not_equal "Fri, 18 Apr 2008 23:26:07 GMT", @response.headers["Last-Modified"]
    end
  end
  
  context "Routing" do
    setup do
      @project = projects(:johans)
      @repository = @project.repositories.first
      @repository.update_attribute(:ready, true)
      @sha = "3fa4e130fa18c92e3030d4accb5d3e0cadd40157"
    end
    
    should "route commits format" do
      assert_recognizes({
        :controller => "commits", 
        :action => "show", 
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
      }, {:path => "/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}", :method => :get})
      assert_generates("/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}", {
        :controller => "commits", 
        :action => "show", 
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
      })
    end
    
    should "route user-namespaced commits index, with dots in the username" do
      assert_recognizes({
        :controller => "commits", 
        :action => "show", 
        :user_id => "mc.hammer",
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
      }, {:path => "/~mc.hammer/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}", :method => :get})
      assert_generates("/~mc.hammer/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}", {
        :controller => "commits", 
        :action => "show", 
        :user_id => "mc.hammer",
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
      })
    end

    should "route diff format" do
      assert_recognizes({
        :controller => "commits", 
        :action => "show", 
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
        :format => "diff",
      }, {:path => "/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}.diff", :method => :get})
      assert_generates("/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}.diff", {
        :controller => "commits", 
        :action => "show", 
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
        :format => "diff"
      })
    end
    
    should "route patch format" do
      assert_recognizes({
        :controller => "commits", 
        :action => "show", 
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
        :format => "patch",
      }, {:path => "/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}.patch", :method => :get})
      assert_generates("/#{@project.to_param}/#{@repository.to_param}/commit/#{@sha}.patch", {
        :controller => "commits", 
        :action => "show", 
        :project_id => @project.to_param,
        :repository_id => @repository.to_param,
        :id => @sha,
        :format => "patch"
      })
    end
  end


  context "listing commits" do
    setup do
      @project = projects(:johans)
      @repository = @project.repositories.first
      @repository.update_attribute(:ready, true)
      Project.expects(:find_by_slug!).with(@project.slug) \
        .returns(@project)
      Repository.expects(:find_by_name_and_project_id!) \
          .with(@repository.name, @project.id).returns(@repository)
      
      Repository.any_instance.stubs(:full_repository_path).returns(grit_test_repo("dot_git"))
      @git = Grit::Repo.new(grit_test_repo("dot_git"), :is_bare => true)
      Repository.any_instance.stubs(:git).returns(@git)
    end

    context "#index" do
      should "GETs page 1 successfully" do
        get :index, {:project_id => @project.slug, 
          :repository_id => @repository.name, :page => nil, :branch => ["master"]}
        assert_response :success
        assert_equal @repository.git.commits("master", 30, 0), assigns(:commits)
      end

      should "GETs page 3 successfully" do
        get :index, {:project_id => @project.slug, 
          :repository_id => @repository.name, :page => nil, :branch => ["master"],
          :page => 3}
        assert_response :success
        assert_equal @repository.git.commits("master", 30, 60), assigns(:commits)
      end

      should "GETs the commits successfully" do
        get :index, {:project_id => @project.slug, 
          :repository_id => @repository.name, :page => nil, :branch => ["master"]}
        assert_response :success
        assert_equal "master", assigns(:root).title
        assert_equal @repository.git, assigns(:git)
        assert_equal @repository.git.commits("master", 30, 0), assigns(:commits)
      end
      
      should "GET the commits of a namedspaced branch successfully" do
        get :index, {:project_id => @project.slug, 
          :repository_id => @repository.name, :page => nil, :branch => ["test", "master"]}
        assert_response :success
        assert_equal "test/master", assigns(:ref)
        assert_equal "test/master", assigns(:root).title
        assert_equal @repository.git, assigns(:git)
        assert_equal @repository.git.commits("test/master", 30, 0), assigns(:commits)
      end
      
      should "deal gracefully if HEAD file refers to a non-existant ref" do
        @git.expects(:get_head).with("master").returns(nil)
        @git.expects(:commit).with("master").returns(nil)
        get :index, {:project_id => @project.slug, 
          :repository_id => @repository.name, :page => nil, :branch => ["master"]}
        assert_response :redirect
        assert_redirected_to project_repository_commits_in_ref_path(@project, 
                              @repository, @git.heads.first.name)
        assert_match(/not a valid ref/, flash[:error])
      end
      
      should "have a proper id in the atom feed" do
        #(repo, id, parents, tree, author, authored_date, committer, committed_date, message)
        commit = Grit::Commit.new(mock("repo"), "mycommitid", [], stub_everything("tree"), 
                      stub_everything("author"), Time.now, 
                      stub_everything("comitter"), Time.now, 
                      "my commit message".split(" "))
        @repository.git.expects(:commits).twice.returns([commit])
      
        get :feed, {:project_id => @project.slug, 
          :repository_id => @repository.name, :id => "master", :format => "atom"}
        assert @response.body.include?(%Q{<id>tag:test.host,2005:Grit::Commit/mycommitid</id>})
      end
      
      should "not explode when there's no commits" do
        @repository.git.expects(:commits).returns([])
        get :feed, {:project_id => @project.slug,
          :repository_id => @repository.name, :id => "master", :format => "atom"}
        assert_response :success
        assert_select "feed title", /#{@repository.gitdir}/
      end
      
      should "show branches with a # in them with great success" do
        git_repo = Grit::Repo.new(grit_test_repo("dot_git"), :is_bare => true)
        @repository.git.expects(:commit).with("ticket-#42") \
          .returns(git_repo.commit("master"))
        get :index, :project_id => @project.to_param, :repository_id => @repository.to_param,
          :branch => ["ticket-%2342"]
        assert_response :success
        assert_equal "ticket-#42", assigns(:ref)
      end
    end
  end
end