Commit e9c06d39 authored by Sean Arnold's avatar Sean Arnold

Use class method instead of scope

- Refactor specs a bit too
- Rename finder to be singular
parent ff92f865
# frozen_string_literal: true
class SentryIssuesFinder
def initialize(project, current_user = nil)
@project = project
@current_user = current_user
end
def find_by_identifier(identifier)
return unless authorized?
sentry_issue = SentryIssue.for_identifier(identifier).first
return unless sentry_issue && sentry_issue.issue.project == @project
sentry_issue
end
private
def authorized?
Ability.allowed?(@current_user, :read_sentry_issue, @project)
end
end
# frozen_string_literal: true
require 'spec_helper'
describe SentryIssuesFinder do
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
let(:issue) { create(:issue, project: project) }
let(:sentry_issue) { create(:sentry_issue, issue: issue) }
let(:finder) { described_class.new(project, user) }
describe '#find_by_identifier' do
let(:identifier) { sentry_issue.sentry_issue_identifier }
subject { finder.find_by_identifier(identifier) }
context 'when the user is not part of the project' do
it 'returns no sentry issues' do
is_expected.to eq(nil)
end
end
context 'when the user is a project developer' do
before do
project.add_developer(user)
end
it 'returns the matching sentry issue' do
expect(subject).to eq(sentry_issue)
end
context 'when identifier is incorrect is false' do
let(:identifier) { 1234 }
it 'does not return a sentry issue' do
expect(subject).to eq(nil)
end
end
context 'when accessing another projects identifier' do
let(:second_project) { create(:project) }
let(:second_issue) { create(:issue, project: second_project) }
let(:second_sentry_issue) { create(:sentry_issue, issue: second_issue) }
let(:identifier) { second_sentry_issue.sentry_issue_identifier }
it 'does not return a sentry issue' do
expect(subject).to eq(nil)
end
end
end
end
end
......@@ -39,18 +39,14 @@ describe SentryIssue do
end
describe 'scopes' do
describe '#for_identifier' do
let(:sentry_issue) { create(:sentry_issue) }
describe 'for_identifier' do
let!(:sentry_issue) { create(:sentry_issue) }
let(:identifier) { sentry_issue.sentry_issue_identifier }
before do
# create second sentry_issue
create(:sentry_issue)
end
let!(:second_sentry_issue) { create(:sentry_issue) }
subject { described_class.for_identifier(identifier) }
it { is_expected.to contain_exactly(sentry_issue) }
it { is_expected.to eq(sentry_issue) }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment