commit_controller_spec.rb 2.8 KB
Newer Older
1 2
require 'spec_helper'

3
describe Projects::CommitController do
D
Dmitriy Zaporozhets 已提交
4
  let(:project) { create(:project) }
5
  let(:user)    { create(:user) }
D
Dmitriy Zaporozhets 已提交
6
  let(:commit)  { project.repository.commit("master") }
7 8 9

  before do
    sign_in(user)
D
Dmitriy Zaporozhets 已提交
10
    project.team << [user, :master]
11 12 13 14 15
  end

  describe "#show" do
    shared_examples "export as" do |format|
      it "should generally work" do
V
Vinnie Okada 已提交
16 17
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
18 19 20 21 22

        expect(response).to be_success
      end

      it "should generate it" do
23
        expect_any_instance_of(Commit).to receive(:"to_#{format}")
24

V
Vinnie Okada 已提交
25 26
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
27 28 29
      end

      it "should render it" do
V
Vinnie Okada 已提交
30 31
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
32 33 34 35 36

        expect(response.body).to eq(commit.send(:"to_#{format}"))
      end

      it "should not escape Html" do
J
Jeroen van Baarsen 已提交
37 38
        allow_any_instance_of(Commit).to receive(:"to_#{format}").
          and_return('HTML entities &<>" ')
39

V
Vinnie Okada 已提交
40 41
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
42 43 44 45 46 47 48 49 50 51 52 53 54

        expect(response.body).to_not include('&amp;')
        expect(response.body).to_not include('&gt;')
        expect(response.body).to_not include('&lt;')
        expect(response.body).to_not include('&quot;')
      end
    end

    describe "as diff" do
      include_examples "export as", :diff
      let(:format) { :diff }

      it "should really only be a git diff" do
V
Vinnie Okada 已提交
55 56
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
57 58 59 60 61 62 63 64 65 66

        expect(response.body).to start_with("diff --git")
      end
    end

    describe "as patch" do
      include_examples "export as", :patch
      let(:format) { :patch }

      it "should really be a git email patch" do
V
Vinnie Okada 已提交
67 68
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
69 70 71 72 73

        expect(response.body).to start_with("From #{commit.id}")
      end

      it "should contain a git diff" do
V
Vinnie Okada 已提交
74 75
        get(:show, namespace_id: project.namespace.to_param,
            project_id: project.to_param, id: commit.id, format: format)
76 77 78 79 80

        expect(response.body).to match(/^diff --git/)
      end
    end
  end
81 82 83

  describe "#branches" do
    it "contains branch and tags information" do
V
Vinnie Okada 已提交
84 85
      get(:branches, namespace_id: project.namespace.to_param,
          project_id: project.to_param, id: commit.id)
86 87 88 89 90

      expect(assigns(:branches)).to include("master", "feature_conflict")
      expect(assigns(:tags)).to include("v1.1.0")
    end
  end
91
end