event_create_service_spec.rb 5.4 KB
Newer Older
1 2
require 'spec_helper'

3
describe EventCreateService do
4
  let(:service) { described_class.new }
5 6

  describe 'Issues' do
7
    describe '#open_issue' do
8 9
      let(:issue) { create(:issue) }

10
      it { expect(service.open_issue(issue, issue.author)).to be_truthy }
11

Valery Sizov's avatar
Valery Sizov committed
12
      it "creates new event" do
13 14 15 16
        expect { service.open_issue(issue, issue.author) }.to change { Event.count }
      end
    end

17
    describe '#close_issue' do
18 19
      let(:issue) { create(:issue) }

20
      it { expect(service.close_issue(issue, issue.author)).to be_truthy }
21

Valery Sizov's avatar
Valery Sizov committed
22
      it "creates new event" do
23 24 25 26
        expect { service.close_issue(issue, issue.author) }.to change { Event.count }
      end
    end

27
    describe '#reopen_issue' do
28 29
      let(:issue) { create(:issue) }

30
      it { expect(service.reopen_issue(issue, issue.author)).to be_truthy }
31

Valery Sizov's avatar
Valery Sizov committed
32
      it "creates new event" do
33 34 35 36 37 38
        expect { service.reopen_issue(issue, issue.author) }.to change { Event.count }
      end
    end
  end

  describe 'Merge Requests' do
39
    describe '#open_mr' do
40 41
      let(:merge_request) { create(:merge_request) }

42
      it { expect(service.open_mr(merge_request, merge_request.author)).to be_truthy }
43

44
      it "creates new event" do
45 46 47 48
        expect { service.open_mr(merge_request, merge_request.author) }.to change { Event.count }
      end
    end

49
    describe '#close_mr' do
50 51
      let(:merge_request) { create(:merge_request) }

52
      it { expect(service.close_mr(merge_request, merge_request.author)).to be_truthy }
53

54
      it "creates new event" do
55 56 57 58
        expect { service.close_mr(merge_request, merge_request.author) }.to change { Event.count }
      end
    end

59
    describe '#merge_mr' do
60 61
      let(:merge_request) { create(:merge_request) }

62
      it { expect(service.merge_mr(merge_request, merge_request.author)).to be_truthy }
63

64
      it "creates new event" do
65 66 67 68
        expect { service.merge_mr(merge_request, merge_request.author) }.to change { Event.count }
      end
    end

69
    describe '#reopen_mr' do
70 71
      let(:merge_request) { create(:merge_request) }

72
      it { expect(service.reopen_mr(merge_request, merge_request.author)).to be_truthy }
73

74
      it "creates new event" do
75 76 77 78 79 80 81 82
        expect { service.reopen_mr(merge_request, merge_request.author) }.to change { Event.count }
      end
    end
  end

  describe 'Milestone' do
    let(:user) { create :user }

83
    describe '#open_milestone' do
84 85
      let(:milestone) { create(:milestone) }

86
      it { expect(service.open_milestone(milestone, user)).to be_truthy }
87

88
      it "creates new event" do
89 90 91 92
        expect { service.open_milestone(milestone, user) }.to change { Event.count }
      end
    end

93
    describe '#close_mr' do
94 95
      let(:milestone) { create(:milestone) }

96
      it { expect(service.close_milestone(milestone, user)).to be_truthy }
97

98
      it "creates new event" do
99 100 101
        expect { service.close_milestone(milestone, user) }.to change { Event.count }
      end
    end
102

103
    describe '#destroy_mr' do
104 105 106 107
      let(:milestone) { create(:milestone) }

      it { expect(service.destroy_milestone(milestone, user)).to be_truthy }

108
      it "creates new event" do
109 110 111
        expect { service.destroy_milestone(milestone, user) }.to change { Event.count }
      end
    end
112
  end
113

114
  describe '#push', :clean_gitlab_redis_shared_state do
115
    let(:project) { create(:project) }
116 117
    let(:user) { create(:user) }

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    let(:push_data) do
      {
        commits: [
          {
            id: '1cf19a015df3523caf0a1f9d40c98a267d6a2fc2',
            message: 'This is a commit'
          }
        ],
        before: '0000000000000000000000000000000000000000',
        after: '1cf19a015df3523caf0a1f9d40c98a267d6a2fc2',
        total_commits_count: 1,
        ref: 'refs/heads/my-branch'
      }
    end

133
    it 'creates a new event' do
134 135 136 137 138 139 140 141 142
      expect { service.push(project, user, push_data) }.to change { Event.count }
    end

    it 'creates the push event payload' do
      expect(PushEventPayloadService).to receive(:new)
        .with(an_instance_of(PushEvent), push_data)
        .and_call_original

      service.push(project, user, push_data)
143 144 145
    end

    it 'updates user last activity' do
146
      expect { service.push(project, user, push_data) }
147
        .to change { user.last_activity_on }.to(Date.today)
148 149
    end

150 151 152 153 154 155 156 157
    it 'caches the last push event for the user' do
      expect_any_instance_of(Users::LastPushEventService)
        .to receive(:cache_last_push_event)
        .with(an_instance_of(PushEvent))

      service.push(project, user, push_data)
    end

158 159 160 161 162 163 164 165 166 167 168 169 170 171
    it 'does not create any event data when an error is raised' do
      payload_service = double(:service)

      allow(payload_service).to receive(:execute)
        .and_raise(RuntimeError)

      allow(PushEventPayloadService).to receive(:new)
        .and_return(payload_service)

      expect { service.push(project, user, push_data) }
        .to raise_error(RuntimeError)

      expect(Event.count).to eq(0)
      expect(PushEventPayload.count).to eq(0)
172 173 174
    end
  end

175 176
  describe 'Project' do
    let(:user) { create :user }
177
    let(:project) { create(:project) }
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

    describe '#join_project' do
      subject { service.join_project(project, user) }

      it { is_expected.to be_truthy }
      it { expect { subject }.to change { Event.count }.from(0).to(1) }
    end

    describe '#expired_leave_project' do
      subject { service.expired_leave_project(project, user) }

      it { is_expected.to be_truthy }
      it { expect { subject }.to change { Event.count }.from(0).to(1) }
    end
  end
193
end