commits_controller_spec.rb 3.36 KB
Newer Older
1 2
require 'spec_helper'

3
describe Projects::CommitsController do
4
  let(:project) { create(:project, :repository) }
5
  let(:user) { create(:user) }
6 7

  before do
8
    project.add_maintainer(user)
9 10
  end

11 12 13 14 15 16 17 18 19 20 21
  context 'signed in' do
    before do
      sign_in(user)
    end

    describe "GET commits_root" do
      context "no ref is provided" do
        it 'should redirect to the default branch of the project' do
          get(:commits_root,
              namespace_id: project.namespace,
              project_id: project)
22

23 24
          expect(response).to redirect_to project_commits_path(project)
        end
25 26 27
      end
    end

28 29
    describe "GET show" do
      render_views
30

31 32 33 34 35 36 37
      context 'with file path' do
        before do
          get(:show,
              namespace_id: project.namespace,
              project_id: project,
              id: id)
        end
38

39 40
        context "valid branch, valid file" do
          let(:id) { 'master/README.md' }
41

42 43
          it { is_expected.to respond_with(:success) }
        end
44

45 46
        context "valid branch, invalid file" do
          let(:id) { 'master/invalid-path.rb' }
47

48 49
          it { is_expected.to respond_with(:not_found) }
        end
50

51 52
        context "invalid branch, valid file" do
          let(:id) { 'invalid-branch/README.md' }
53

54 55
          it { is_expected.to respond_with(:not_found) }
        end
56 57
      end

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      context "when the ref name ends in .atom" do
        context "when the ref does not exist with the suffix" do
          before do
            get(:show,
                namespace_id: project.namespace,
                project_id: project,
                id: "master.atom")
          end

          it "renders as atom" do
            expect(response).to be_success
            expect(response.content_type).to eq('application/atom+xml')
          end

          it 'renders summary with type=html' do
            expect(response.body).to include('<summary type="html">')
          end
75
        end
76

77 78 79
        context "when the ref exists with the suffix" do
          before do
            commit = project.repository.commit('master')
80

81 82 83 84 85 86 87 88 89 90 91 92 93
            allow_any_instance_of(Repository).to receive(:commit).and_call_original
            allow_any_instance_of(Repository).to receive(:commit).with('master.atom').and_return(commit)

            get(:show,
                namespace_id: project.namespace,
                project_id: project,
                id: "master.atom")
          end

          it "renders as HTML" do
            expect(response).to be_success
            expect(response.content_type).to eq('text/html')
          end
94
        end
95
      end
96 97
    end
  end
98

99 100 101
  context 'token authentication' do
    context 'public project' do
      it_behaves_like 'authenticates sessionless user', :show, :atom, public: true do
102
        before do
103
          public_project = create(:project, :repository, :public)
104

105
          default_params.merge!(namespace_id: public_project.namespace, project_id: public_project, id: "master.atom")
106
        end
107 108 109 110 111 112 113 114
      end
    end

    context 'private project' do
      it_behaves_like 'authenticates sessionless user', :show, :atom, public: false do
        before do
          private_project = create(:project, :repository, :private)
          private_project.add_maintainer(user)
115

116
          default_params.merge!(namespace_id: private_project.namespace, project_id: private_project, id: "master.atom")
117
        end
118 119 120 121
      end
    end
  end
end