From dfeef6c22849c04ffd225a0356fd11fb8e4907f6 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 3 Apr 2013 15:55:08 +0300 Subject: [PATCH] Fixed API file raw functionality, Fixed tree controller tests. Added BlobController specs --- app/views/tree/_tree.html.haml | 39 +++++++++++------------- lib/api/projects.rb | 12 +++++--- spec/controllers/blob_controller_spec.rb | 37 ++++++++++++++++++++++ spec/controllers/tree_controller_spec.rb | 6 ++-- spec/models/commit_spec.rb | 4 +-- 5 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 spec/controllers/blob_controller_spec.rb diff --git a/app/views/tree/_tree.html.haml b/app/views/tree/_tree.html.haml index 24c005fc7fe..f77b8983726 100644 --- a/app/views/tree/_tree.html.haml +++ b/app/views/tree/_tree.html.haml @@ -12,30 +12,27 @@ = link_to title, '#' %div#tree-content-holder.tree-content-holder - - if tree.is_blob? - = render "tree/blob", blob: tree - - else - %table#tree-slider{class: "table_#{@hex_path} tree-table" } - %thead - %tr - %th Name - %th Last Update - %th Last Commit - %th= link_to "history", project_commits_path(@project, @id), class: "btn btn-tiny pull-right" + %table#tree-slider{class: "table_#{@hex_path} tree-table" } + %thead + %tr + %th Name + %th Last Update + %th Last Commit + %th= link_to "history", project_commits_path(@project, @id), class: "btn btn-tiny pull-right" - - if tree.up_dir? - %tr.tree-item - %td.tree-item-file-name - = image_tag "file_empty.png", size: '16x16' - = link_to "..", project_tree_path(@project, up_dir_path(tree)) - %td - %td - %td + - if tree.up_dir? + %tr.tree-item + %td.tree-item-file-name + = image_tag "file_empty.png", size: '16x16' + = link_to "..", project_tree_path(@project, up_dir_path(tree)) + %td + %td + %td - = render_tree(tree) + = render_tree(tree) - - if tree.readme - = render "tree/readme", readme: tree.readme + - if tree.readme + = render "tree/readme", readme: tree.readme %div.tree_progress diff --git a/lib/api/projects.rb b/lib/api/projects.rb index ce94c34bd09..75157e55730 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -493,14 +493,16 @@ module Gitlab ref = params[:sha] - commit = user_project.repository.commit ref + repo = user_project.repository + + commit = repo.commit(ref) not_found! "Commit" unless commit - tree = Tree.new commit.tree, ref, params[:filepath] - not_found! "File" unless tree.try(:tree) + blob = Gitlab::Git::Blob.new(repo, commit.id, ref, params[:filepath]) + not_found! "File" unless blob.exists? - content_type tree.mime_type - present tree.data + content_type blob.mime_type + present blob.data end # Get a specific project's keys diff --git a/spec/controllers/blob_controller_spec.rb b/spec/controllers/blob_controller_spec.rb new file mode 100644 index 00000000000..fe113459470 --- /dev/null +++ b/spec/controllers/blob_controller_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe BlobController do + let(:project) { create(:project_with_code) } + let(:user) { create(:user) } + + before do + sign_in(user) + + project.team << [user, :master] + + project.stub(:branches).and_return(['master', 'foo/bar/baz']) + project.stub(:tags).and_return(['v1.0.0', 'v2.0.0']) + controller.instance_variable_set(:@project, project) + end + + describe "GET show" do + render_views + + before { get :show, project_id: project.code, id: id } + + context "valid branch, valid file" do + let(:id) { 'master/README.md' } + it { should respond_with(:success) } + end + + context "valid branch, invalid file" do + let(:id) { 'master/invalid-path.rb' } + it { should respond_with(:not_found) } + end + + context "invalid branch, valid file" do + let(:id) { 'invalid-branch/README.md' } + it { should respond_with(:not_found) } + end + end +end diff --git a/spec/controllers/tree_controller_spec.rb b/spec/controllers/tree_controller_spec.rb index 8232f1472e1..f9fe4fe2010 100644 --- a/spec/controllers/tree_controller_spec.rb +++ b/spec/controllers/tree_controller_spec.rb @@ -26,17 +26,17 @@ describe TreeController do end context "valid branch, valid path" do - let(:id) { 'master/README.md' } + let(:id) { 'master/app/' } it { should respond_with(:success) } end context "valid branch, invalid path" do - let(:id) { 'master/invalid-path.rb' } + let(:id) { 'master/invalid-path/' } it { should respond_with(:not_found) } end context "invalid branch, valid path" do - let(:id) { 'invalid-branch/README.md' } + let(:id) { 'invalid-branch/app/' } it { should respond_with(:not_found) } end end diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index 6cf777bec04..ad99d8a390b 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -38,10 +38,10 @@ describe Commit do it { should respond_to(:message) } it { should respond_to(:authored_date) } it { should respond_to(:committed_date) } + it { should respond_to(:committer_email) } + it { should respond_to(:author_email) } it { should respond_to(:parents) } it { should respond_to(:date) } - it { should respond_to(:committer) } - it { should respond_to(:author) } it { should respond_to(:diffs) } it { should respond_to(:tree) } it { should respond_to(:id) }