redirect to HEAD of no commit was found from the sha for blob+raw blob
[gitorious:yousource.git] / spec / controllers / browse_controller_spec.rb
1 require File.dirname(__FILE__) + '/../spec_helper'
2
3 describe BrowseController do
4   before(:each) do
5     @project = projects(:johans)
6     @repository = @project.repositories.first
7     @repository.stub!(:full_repository_path).and_return(repo_path)
8     
9     Project.should_receive(:find_by_slug!).with(@project.slug) \
10       .and_return(@project)
11     @project.repositories.should_receive(:find_by_name!) \
12       .with(@repository.name).and_return(@repository)
13     @repository.stub!(:has_commits?).and_return(true)
14     
15     @git = mock("Grit mock", :null_object => true)
16     @repository.stub!(:git).and_return(@git)
17     @head = mock("master branch")
18     @head.stub!(:name).and_return("master")
19     @repository.stub!(:head_candidate).and_return(@head)
20   end
21   
22   describe "#index" do
23     def do_get
24       get :index, :project_id => @project.slug, :repository_id => @repository.name
25     end
26     
27     it "GETs successfully" do
28       do_get
29       flash[:notice].should == nil
30       response.should be_success
31     end
32     
33     it "fetches the specified log entries" do
34       @git.should_receive(:commits).with("master", BrowseController::LOGS_PER_PAGE) \
35         .and_return(commits=mock("logentries"))
36       do_get
37       assigns[:commits].should == commits
38     end    
39   end
40   
41   describe "#tree" do
42     it "GETs successfully" do
43       tree_mock = mock("tree")
44       tree_mock.stub!(:id).and_return("123")
45       @commit_mock = mock("commit")
46       @commit_mock.stub!(:tree).and_return(tree_mock)
47       @git.should_receive(:commit).with("a"*40).and_return(@commit_mock)
48       @git.should_receive(:tree).with(tree_mock.id, ["foo/bar/"]).and_return(tree_mock)
49       get :tree, :project_id => @project.slug, 
50         :repository_id => @repository.name, :sha => "a"*40, :path => ["foo", "bar"]
51         
52       response.should be_success
53       assigns[:git].should == @git
54       assigns[:tree].should == tree_mock
55     end
56     
57     it "redirects to HEAD if provided sha was not found (backwards compat)" do
58       @git.should_receive(:commit).with("a"*40).and_return(nil)
59       get :tree, :project_id => @project.slug, 
60         :repository_id => @repository.name, :sha => "a"*40, :path => ["foo"]
61       
62       response.should redirect_to(project_repository_tree_path(@project, @repository, "HEAD", ["foo"]))
63     end
64   end
65   
66   describe "#commit" do    
67     before(:each) do
68       @commit_mock = mock("commit")
69       @diff_mock = mock("diff mock")
70       @commit_mock.should_receive(:diffs).and_return(@diff_mock)
71       @git.should_receive(:commit).with("a"*40).and_return(@commit_mock)
72     end
73     
74     def do_get(opts={})
75       get :commit, {:project_id => @project.slug, 
76           :repository_id => @repository.name, :sha => "a"*40}.merge(opts)
77     end
78     
79     it "gets the commit data" do
80       do_get
81       response.should be_success
82       assigns[:git].should == @git
83       assigns[:commit].should == @commit_mock
84       assigns[:diffs].should == @diff_mock
85     end
86     
87     it "gets the comments for the commit" do
88       do_get
89       assigns[:comment_count].should == 0
90     end
91     
92     it "defaults to 'inline' diffmode" do
93       do_get
94       assigns[:diffmode].should == "inline"
95     end
96     
97     it "sets sidebyside diffmode" do
98       do_get(:diffmode => "sidebyside")
99       assigns[:diffmode].should == "sidebyside"
100     end
101   end
102   
103   describe "#diff" do
104     it "diffs the sha's provided" do
105       diff_mock = mock("diff")
106       @git.should_receive(:diff).with("a"*40, "b"*40).and_return(diff_mock)
107       
108       get :diff, {:project_id => @project.slug, 
109           :repository_id => @repository.name, :sha => "a"*40, 
110           :other_sha => "b"*40}
111       
112       response.should be_success
113       assigns[:git].should == @git
114       assigns[:diff].should == diff_mock
115     end
116   end
117   
118   describe "#blob" do
119     it "gets the blob data for the sha provided" do
120       blob_mock = mock("blob")
121       blob_mock.stub!(:contents).and_return([blob_mock]) #meh
122       commit_stub = mock("commit")
123       commit_stub.stub!(:id).and_return("a"*40)
124       commit_stub.stub!(:tree).and_return(commit_stub)
125       @git.should_receive(:commit).and_return(commit_stub)
126       @git.should_receive(:tree).and_return(blob_mock)
127       
128       get :blob, {:project_id => @project.slug, 
129           :repository_id => @repository.name, :sha => "a"*40, :path => []}
130       
131       response.should be_success
132       assigns[:git].should == @git
133       assigns[:blob].should == blob_mock
134     end 
135     
136     it "redirects to HEAD if provided sha was not found (backwards compat)" do
137       @git.should_receive(:commit).with("a"*40).and_return(nil)
138       get :blob, {:project_id => @project.slug, 
139           :repository_id => @repository.name, :sha => "a"*40, :path => ["foo.rb"]}
140       
141       response.should redirect_to(project_repository_blob_path(@project, @repository, "HEAD", ["foo.rb"]))
142     end   
143   end
144   
145   describe "#raw" do
146     it "gets the blob data from the sha and renders it as text/plain" do
147       blob_mock = mock("blob")
148       blob_mock.stub!(:contents).and_return([blob_mock]) #meh
149       blob_mock.should_receive(:data).and_return("blabla")
150       commit_stub = mock("commit")
151       commit_stub.stub!(:id).and_return("a"*40)
152       commit_stub.stub!(:tree).and_return(commit_stub)
153       @git.should_receive(:commit).and_return(commit_stub)
154       @git.should_receive(:tree).and_return(blob_mock)
155       
156       get :raw, {:project_id => @project.slug, 
157           :repository_id => @repository.name, :sha => "a"*40, :path => []}
158       
159       response.should be_success
160       assigns[:git].should == @git
161       assigns[:blob].should == blob_mock
162       response.body.should == "blabla"
163       response.content_type.should == "text/plain"
164     end
165     
166     it "redirects to HEAD if provided sha was not found (backwards compat)" do
167       @git.should_receive(:commit).with("a"*40).and_return(nil)
168       get :raw, {:project_id => @project.slug, 
169           :repository_id => @repository.name, :sha => "a"*40, :path => ["foo.rb"]}
170       
171       response.should redirect_to(project_repository_raw_blob_path(@project, @repository, "HEAD", ["foo.rb"]))
172     end
173   end
174   
175   describe "#log" do
176     def do_get(opts = {})
177       get :log, {:project_id => @project.slug, 
178         :repository_id => @repository.name, :page => nil}.merge(opts)
179     end
180     
181     it "GETs page 1 successfully" do
182       @git.should_receive(:commits).with("master", 30, 0).and_return(mock("logentries"))
183       do_get
184     end
185     
186     it "GETs page 3 successfully" do
187       @git.should_receive(:commits).with("master", 30, 60).and_return(mock("logentries"))
188       do_get(:page => 3)
189     end
190     
191     it "GETs the commits successfully" do
192       commits = mock("logentries")
193       @git.should_receive(:commits).with("master", 30, 0).and_return(commits)
194       do_get
195       response.should be_success
196       assigns[:git].should == @git
197       assigns[:commits].should == commits
198     end
199   end
200
201 end