debian-mirror-gitlab/spec/finders/concerns/finder_methods_spec.rb

122 lines
3.7 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe FinderMethods do
2018-03-27 19:54:05 +05:30
let(:finder_class) do
Class.new do
include FinderMethods
def initialize(user)
@current_user = user
end
def execute
2022-06-21 17:19:12 +05:30
Project.where.not(name: 'foo').order(id: :desc)
2018-03-27 19:54:05 +05:30
end
2020-11-24 15:15:51 +05:30
private
attr_reader :current_user
2018-03-27 19:54:05 +05:30
end
end
2022-06-21 17:19:12 +05:30
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) }
2018-03-27 19:54:05 +05:30
2022-06-21 17:19:12 +05:30
subject(:finder) { finder_class.new(user) }
before_all do
2018-03-27 19:54:05 +05:30
authorized_project.add_developer(user)
2022-06-21 17:19:12 +05:30
unmatched_project.add_developer(user)
2018-03-27 19:54:05 +05:30
end
2022-06-21 17:19:12 +05:30
# rubocop:disable Rails/FindById
2018-03-27 19:54:05 +05:30
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
2022-06-21 17:19:12 +05:30
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)
2018-03-27 19:54:05 +05:30
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
2019-07-07 11:18:12 +05:30
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
2018-03-27 19:54:05 +05:30
end
2022-06-21 17:19:12 +05:30
# rubocop:enable Rails/FindById
2018-03-27 19:54:05 +05:30
describe '#find' do
it 'returns the project if the user has access' do
expect(finder.find(authorized_project.id)).to eq(authorized_project)
end
2022-06-21 17:19:12 +05:30
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)
2018-03-27 19:54:05 +05:30
end
it 'raises not found the user does not have access' do
expect { finder.find(unauthorized_project.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
2022-06-21 17:19:12 +05:30
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
2018-03-27 19:54:05 +05:30
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
2022-06-21 17:19:12 +05:30
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
2018-03-27 19:54:05 +05:30
end
it 'returns nil when the user does not have access' do
expect(finder.find_by(id: unauthorized_project.id)).to be_nil
end
2019-07-07 11:18:12 +05:30
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
2018-03-27 19:54:05 +05:30
end
end