BigW Consortium Gitlab

process_commit_worker_spec.rb 4.84 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe ProcessCommitWorker do
  let(:worker) { described_class.new }
  let(:user) { create(:user) }
6
  let(:project) { create(:project, :public, :repository) }
7 8 9 10 11 12 13
  let(:issue) { create(:issue, project: project, author: user) }
  let(:commit) { project.commit }

  describe '#perform' do
    it 'does not process the commit when the project does not exist' do
      expect(worker).not_to receive(:close_issues)

14
      worker.perform(-1, user.id, commit.to_hash)
15 16 17 18 19
    end

    it 'does not process the commit when the user does not exist' do
      expect(worker).not_to receive(:close_issues)

20
      worker.perform(project.id, -1, commit.to_hash)
21 22
    end

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    context 'when commit is a merge request merge commit' do
      let(:merge_request) do
        create(:merge_request,
               description: "Closes #{issue.to_reference}",
               source_branch: 'feature-merged',
               target_branch: 'master',
               source_project: project)
      end

      let(:commit) do
        project.repository.create_branch('feature-merged', 'feature')

        sha = project.repository.merge(user,
                                       merge_request.diff_head_sha,
                                       merge_request,
                                       "Closes #{issue.to_reference}")
        project.repository.commit(sha)
      end

Micaël Bergeron committed
42
      it 'it does not close any issues from the commit message' do
43 44 45 46 47 48
        expect(worker).not_to receive(:close_issues)

        worker.perform(project.id, user.id, commit.to_hash)
      end
    end

49 50 51
    it 'processes the commit message' do
      expect(worker).to receive(:process_commit_message).and_call_original

52
      worker.perform(project.id, user.id, commit.to_hash)
53 54 55 56 57
    end

    it 'updates the issue metrics' do
      expect(worker).to receive(:update_issue_metrics).and_call_original

58
      worker.perform(project.id, user.id, commit.to_hash)
59
    end
60 61

    context 'when commit already exists in upstream project' do
62
      let(:forked) { create(:project, :public, :repository) }
63 64 65 66 67 68 69 70 71

      it 'does not process commit message' do
        create(:forked_project_link, forked_to_project: forked, forked_from_project: project)

        expect(worker).not_to receive(:process_commit_message)

        worker.perform(forked.id, user.id, forked.commit.to_hash)
      end
    end
72 73 74 75 76
  end

  describe '#process_commit_message' do
    context 'when pushing to the default branch' do
      it 'closes issues that should be closed per the commit message' do
Micaël Bergeron committed
77
        allow(commit).to receive(:safe_message).and_return("Closes #{issue.to_reference}")
78

Micaël Bergeron committed
79
        expect(worker).to receive(:close_issues).with(project, user, user, commit, [issue])
80 81 82 83 84 85 86

        worker.process_commit_message(project, commit, user, user, true)
      end
    end

    context 'when pushing to a non-default branch' do
      it 'does not close any issues' do
Micaël Bergeron committed
87
        allow(commit).to receive(:safe_message).and_return("Closes #{issue.to_reference}")
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

        expect(worker).not_to receive(:close_issues)

        worker.process_commit_message(project, commit, user, user, false)
      end
    end

    it 'creates cross references' do
      expect(commit).to receive(:create_cross_references!)

      worker.process_commit_message(project, commit, user, user)
    end
  end

  describe '#close_issues' do
    context 'when the user can update the issues' do
      it 'closes the issues' do
        worker.close_issues(project, user, user, commit, [issue])

        issue.reload

        expect(issue.closed?).to eq(true)
      end
    end

    context 'when the user can not update the issues' do
      it 'does not close the issues' do
        other_user = create(:user)

        worker.close_issues(project, other_user, other_user, commit, [issue])

        issue.reload

        expect(issue.closed?).to eq(false)
      end
    end
  end

  describe '#update_issue_metrics' do
    it 'updates any existing issue metrics' do
Micaël Bergeron committed
128
      allow(commit).to receive(:safe_message).and_return("Closes #{issue.to_reference}")
129 130 131 132 133 134 135

      worker.update_issue_metrics(commit, user)

      metric = Issue::Metrics.first

      expect(metric.first_mentioned_in_commit_at).to eq(commit.committed_date)
    end
136 137

    it "doesn't execute any queries with false conditions" do
Micaël Bergeron committed
138
      allow(commit).to receive(:safe_message).and_return("Lorem Ipsum")
139

Micaël Bergeron committed
140 141
      expect { worker.update_issue_metrics(commit, user) }
        .not_to make_queries_matching(/WHERE (?:1=0|0=1)/)
142
    end
143
  end
144 145 146 147 148 149 150 151 152

  describe '#build_commit' do
    it 'returns a Commit' do
      commit = worker.build_commit(project, id: '123')

      expect(commit).to be_an_instance_of(Commit)
    end

    it 'parses date strings into Time instances' do
Micaël Bergeron committed
153 154 155
      commit = worker.build_commit(project,
                                   id: '123',
                                   authored_date: Time.now.to_s)
156 157 158 159

      expect(commit.authored_date).to be_an_instance_of(Time)
    end
  end
160
end