summaryrefslogtreecommitdiff
path: root/spec/models/sentry_issue_spec.rb
blob: 33654bf5e1a3d047c40bc1349c4af8b0ef8b260d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe SentryIssue do
  describe 'associations' do
    it { is_expected.to belong_to(:issue) }
  end

  describe 'validations' do
    let!(:sentry_issue) { create(:sentry_issue) }

    it { is_expected.to validate_presence_of(:issue) }
    it { is_expected.to validate_uniqueness_of(:issue) }
    it { is_expected.to validate_presence_of(:sentry_issue_identifier) }

    it 'allows duplicated sentry_issue_identifier' do
      duplicate_sentry_issue = build(:sentry_issue, sentry_issue_identifier: sentry_issue.sentry_issue_identifier)

      expect(duplicate_sentry_issue).to be_valid
    end

    it 'validates uniqueness of sentry_issue_identifier per project' do
      second_issue = create(:issue, project: sentry_issue.issue.project)
      duplicate_sentry_issue = build(:sentry_issue, issue: second_issue, sentry_issue_identifier: sentry_issue.sentry_issue_identifier)

      expect(duplicate_sentry_issue).to be_invalid
      expect(duplicate_sentry_issue.errors.full_messages.first).to include('is already associated')
    end
  end

  describe 'callbacks' do
    context 'after create commit do' do
      it 'updates Sentry with a reciprocal link on creation' do
        issue = create(:issue)

        expect(ErrorTrackingIssueLinkWorker).to receive(:perform_async).with(issue.id)

        create(:sentry_issue, issue: issue)
      end
    end
  end

  describe '.for_project_and_identifier' do
    it 'finds the most recent per project and sentry_issue_identifier' do
      sentry_issue = create(:sentry_issue)
      create(:sentry_issue)
      project = sentry_issue.issue.project
      sentry_issue_3 = build(:sentry_issue, issue: create(:issue, project: project), sentry_issue_identifier: sentry_issue.sentry_issue_identifier)
      sentry_issue_3.save(validate: false)

      result = described_class.for_project_and_identifier(project, sentry_issue.sentry_issue_identifier)

      expect(result).to eq(sentry_issue_3)
    end
  end
end