diff --git a/app/models/publication.rb b/app/models/publication.rb index 15d8c1ff4..45a143e6c 100644 --- a/app/models/publication.rb +++ b/app/models/publication.rb @@ -584,7 +584,7 @@ def self.filter_by_activity_insight_id(query, activity_insight_id) .where(publication_imports: { source: 'Activity Insight', source_identifier: activity_insight_id - }) + }).uniq end def self.filter_by_doi(query, doi) @@ -599,7 +599,7 @@ def self.filter_by_doi(query, doi) doi = url_prefix + doi end - query.where(doi: doi) + query.where(doi: doi).uniq end private diff --git a/spec/requests/api/v1/publications_spec.rb b/spec/requests/api/v1/publications_spec.rb index 48539080c..310725c20 100644 --- a/spec/requests/api/v1/publications_spec.rb +++ b/spec/requests/api/v1/publications_spec.rb @@ -28,8 +28,8 @@ def query_pubs let!(:inaccessible_pub) { create(:publication, visible: true) } let(:params) { '' } let!(:token) { create :api_token, token: 'token123', total_requests: 0, last_used_at: nil } - let(:org) { create :organization } - let(:user) { create :user } + let!(:org) { create :organization } + let!(:user) { create :user } before do publications.each { |p| create :authorship, publication: p, user: user } @@ -59,21 +59,42 @@ def query_pubs describe 'params:' do describe 'activity_insight_id' do - let(:ai_pub) { create(:publication, visible: true, imports: [pub_import]) } - let(:pub_import) { create(:publication_import, source: 'Activity Insight', source_identifier: '123') } + let!(:ai_pub) { create(:publication, visible: true, imports: [pub_import]) } + let!(:pub_import) { create(:publication_import, source: 'Activity Insight', source_identifier: '123') } before do create :authorship, user: user, publication: ai_pub - query_pubs + unless RSpec.current_example.metadata[:skip_before] + query_pubs + end end context 'with a valid Activity Insight ID' do let(:params) { '?activity_insight_id=123' } - it 'returns a publication matching the specified Activity Insight ID' do - expect(json_response[:data].size).to eq(1) - expect(json_response[:data].first[:attributes][:activity_insight_ids].size).to eq(1) - expect(json_response[:data].first[:attributes][:activity_insight_ids].first).to eq('123') + context 'when one record is returned from the query' do + it 'returns a publication matching the specified Activity Insight ID' do + expect(json_response[:data].size).to eq(1) + expect(json_response[:data].first[:attributes][:activity_insight_ids].size).to eq(1) + expect(json_response[:data].first[:attributes][:activity_insight_ids].first).to eq('123') + end + end + + context 'when the user of the found record is in multiple orgs (query returns multiple of same record)' do + let!(:org2) { create :organization } + + before do + create :organization_api_permission, organization: org2, api_token: token + user.organizations << org2 + user.save + query_pubs + end + + it 'returns a unique list of publications matching the specified Activity Insight ID', skip_before: true do + expect(json_response[:data].size).to eq(1) + expect(json_response[:data].first[:attributes][:activity_insight_ids].size).to eq(1) + expect(json_response[:data].first[:attributes][:activity_insight_ids].first).to eq('123') + end end end @@ -91,7 +112,9 @@ def query_pubs before do create :authorship, user: user, publication: doi_pub - query_pubs + unless RSpec.current_example.metadata[:skip_before] + query_pubs + end end context 'with a full DOI URL' do @@ -101,6 +124,22 @@ def query_pubs expect(json_response[:data].size).to eq(1) expect(json_response[:data].first[:attributes][:doi]).to eq('https://doi.org/10.26207/46a7-9981') end + + context 'when the user of the found record is in multiple orgs (query returns multiple of same record)' do + let!(:org2) { create :organization } + + before do + create :organization_api_permission, organization: org2, api_token: token + user.organizations << org2 + user.save + query_pubs + end + + it 'returns a unique list of publications matching the specified DOI', skip_before: true do + expect(json_response[:data].size).to eq(1) + expect(json_response[:data].first[:attributes][:doi]).to eq('https://doi.org/10.26207/46a7-9981') + end + end end context 'with a DOI starting with the doi: prefix' do