summaryrefslogtreecommitdiff
path: root/spec/finders/concerns/finder_methods_spec.rb
blob: 09ec8110129e49d5598bb3d80c95a5fd89a64b09 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe FinderMethods do
  let(:finder_class) do
    Class.new do
      include FinderMethods

      def initialize(user)
        @current_user = user
      end

      def execute
        Project.where.not(name: 'foo').order(id: :desc)
      end

      private

      attr_reader :current_user
    end
  end

  let_it_be(:user) { create(:user) }
  let_it_be(:authorized_project) { create(:project) }
  let_it_be(:unmatched_project) { create(:project, name: 'foo') }
  let_it_be(:unauthorized_project) { create(:project) }

  subject(:finder) { finder_class.new(user) }

  before_all do
    authorized_project.add_developer(user)
    unmatched_project.add_developer(user)
  end

  # rubocop:disable Rails/FindById
  describe '#find_by!' do
    it 'returns the project if the user has access' do
      expect(finder.find_by!(id: authorized_project.id)).to eq(authorized_project)
    end

    it 'raises not found when the project is not found by id' do
      expect { finder.find_by!(id: non_existing_record_id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'raises not found when the project is not found by filter' do
      expect { finder.find_by!(id: unmatched_project.id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'raises not found the user does not have access' do
      expect { finder.find_by!(id: unauthorized_project.id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'ignores ordering' do
      # Memoise the finder result so we can add message expectations to it
      relation = finder.execute
      allow(finder).to receive(:execute).and_return(relation)

      expect(relation).to receive(:reorder).with(nil).and_call_original

      finder.find_by!(id: authorized_project.id)
    end
  end
  # rubocop:enable Rails/FindById

  describe '#find' do
    it 'returns the project if the user has access' do
      expect(finder.find(authorized_project.id)).to eq(authorized_project)
    end

    it 'raises not found when the project is not found by id' do
      expect { finder.find(non_existing_record_id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'raises not found when the project is not found by filter' do
      expect { finder.find(unmatched_project.id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'raises not found the user does not have access' do
      expect { finder.find(unauthorized_project.id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'ignores ordering' do
      # Memoise the finder result so we can add message expectations to it
      relation = finder.execute
      allow(finder).to receive(:execute).and_return(relation)

      expect(relation).to receive(:reorder).with(nil).and_call_original

      finder.find(authorized_project.id)
    end
  end

  describe '#find_by' do
    it 'returns the project if the user has access' do
      expect(finder.find_by(id: authorized_project.id)).to eq(authorized_project)
    end

    it 'returns nil when the project is not found by id' do
      expect(finder.find_by(id: non_existing_record_id)).to be_nil
    end

    it 'returns nil when the project is not found by filter' do
      expect(finder.find_by(id: unmatched_project.id)).to be_nil
    end

    it 'returns nil when the user does not have access' do
      expect(finder.find_by(id: unauthorized_project.id)).to be_nil
    end

    it 'ignores ordering' do
      # Memoise the finder result so we can add message expectations to it
      relation = finder.execute
      allow(finder).to receive(:execute).and_return(relation)

      expect(relation).to receive(:reorder).with(nil).and_call_original

      finder.find_by(id: authorized_project.id)
    end
  end
end