Skip to content

Commit f3d4cc1

Browse files
authored
Merge pull request #2293 from broadinstitute/jb-search-export-private
Use correct auth token, properly sanitize descriptions in text export (SCP-6036)
2 parents ad95a1b + fb3063e commit f3d4cc1

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

app/controllers/api/v1/study_search_results_objects.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def study_text_export(study)
137137
'SCP',
138138
study.accession,
139139
study.name,
140-
study.description,
140+
Api::V1::StudySearchResultsObjects.strip_newlines(study.description),
141141
study.public,
142142
study.detached,
143143
study.cell_count,
@@ -156,7 +156,7 @@ def study_text_export(study)
156156
study[:hca_result] ? 'HCA' : 'TDR',
157157
study[:accession],
158158
study[:name],
159-
study[:description].gsub(/\n/, ''),
159+
Api::V1::StudySearchResultsObjects.strip_newlines(study[:description]),
160160
true,
161161
false,
162162
0,
@@ -182,6 +182,11 @@ def result_url_for(study)
182182
end
183183
end
184184

185+
# deal with very old or external descriptions which may have newlines in them
186+
def self.strip_newlines(text)
187+
text.to_s.gsub(/\n/, ' ').gsub(/\r/, '').strip
188+
end
189+
185190
# flatten facet matches into a text string for export
186191
def self.facet_results_as_text(facets)
187192
entries = []

app/javascript/lib/scp-api.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ export async function exportSearchResultsText(searchParams, mock=false) {
516516
const init = {
517517
method: 'GET',
518518
headers: {
519-
Authorization: `Bearer ${getOAuthToken()}`
519+
Authorization: `Bearer ${getAccessToken()}`
520520
}
521521
}
522522
const [searchResults, perfTimes] = await scpApi(path, init, mock, false, false)

test/js/lib/scp-api.test.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import CacheMock from 'browser-cache-mock';
44
import 'isomorphic-fetch';
55

6-
import scpApi, { fetchSearch, fetchFacetFilters, setupRenewalForReadOnlyToken, setUpRenewalForUserAccessToken } from 'lib/scp-api'
6+
import scpApi, { fetchSearch, exportSearchResultsText, fetchFacetFilters, setupRenewalForReadOnlyToken, setUpRenewalForUserAccessToken } from 'lib/scp-api'
77
import * as ServiceWorkerCache from 'lib/service-worker-cache'
88
import * as SCPContextProvider from '~/providers/SCPContextProvider'
99
import { getTokenExpiry } from '../upload-wizard/upload-wizard-test-utils'
@@ -39,7 +39,7 @@ describe('JavaScript client for SCP REST API', () => {
3939
})
4040

4141
// Note: tests that mock global.fetch must be cleared after every test
42-
afterEach(() => {
42+
beforeEach(() => {
4343
// Restores all mocks back to their original value
4444
jest.restoreAllMocks()
4545
jest.spyOn(global, 'setTimeout').mockReset()
@@ -220,4 +220,39 @@ describe('JavaScript client for SCP REST API', () => {
220220
})
221221
})
222222

223+
it('uses user access token for exportSearchResultsText', async () => {
224+
const mockSuccessResponse = "test response"
225+
const exportFilename = 'scp_search_results_2025-01-01.tsv'
226+
const mockFetchPromise = Promise.resolve({
227+
ok: true,
228+
headers: new Headers(
229+
{
230+
'Content-Disposition': `attachment; filename="${exportFilename}"`,
231+
'Content-Type': 'application/octet-stream'
232+
}
233+
),
234+
blob: () => {
235+
return mockSuccessResponse
236+
}
237+
})
238+
jest.spyOn(global, 'fetch').mockImplementation(() => mockFetchPromise)
239+
240+
window.SCP = {
241+
userAccessToken: 'ya11.b.foo_bar-baz'
242+
}
243+
244+
const params = {type: 'study', page: 1, terms: 'spatial', facets: {}}
245+
await exportSearchResultsText(params)
246+
247+
expect(global.fetch).toHaveBeenCalledWith(
248+
expect.anything(),
249+
expect.objectContaining({
250+
method: 'GET',
251+
headers: {
252+
Authorization: 'Bearer ya11.b.foo_bar-baz'
253+
}
254+
})
255+
)
256+
})
257+
223258
})

test/lib/study_search_results_objects_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ class StudySearchResultsObjectsTest < ActiveSupport::TestCase
2323
expected_names = ['B cell', 'bipolar neuron', 'retinal bipolar neuron', 'retinal cone cell', 'amacrine cell']
2424
assert_equal expected_names, merged_data[:cell_type].map { |filter| filter[:name]}
2525
end
26+
27+
test 'should remove any newlines from result descriptions' do
28+
description = "This is a study description.\nIt has newlines.\n\nAnd some more.\r\n\r\nAnd some Windows newlines."
29+
cleaned_description = Api::V1::StudySearchResultsObjects.strip_newlines(description)
30+
assert_equal 'This is a study description. It has newlines. And some more. And some Windows newlines.',
31+
cleaned_description
32+
end
2633
end

0 commit comments

Comments
 (0)