Skip to content

Commit

Permalink
changing stubbing in token refresh function to use regex instead of m…
Browse files Browse the repository at this point in the history
…atching the exact query string
  • Loading branch information
davidcam-src committed Mar 13, 2024
1 parent e7e5a66 commit 0dd23d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
2 changes: 2 additions & 0 deletions app/services/tasks/dimensions_query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def query_dimensions(with_doi: true, page_size: 100)
cursor = 0
# Flag to track if retry has been attempted after token refresh
retry_attempted = false
wip_count = 0

loop do
begin
Expand Down Expand Up @@ -48,6 +49,7 @@ def query_dimensions(with_doi: true, page_size: 100)

break if cursor >= total_count
elsif response.code == 403
puts "Attempting to retrieve new token"
if !retry_attempted
# If the token has expired, retrieve a new token and try the query again
Rails.logger.warn('Received 403 Forbidden error. Retrying after token refresh.')
Expand Down
28 changes: 9 additions & 19 deletions spec/services/tasks/dimensions_query_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@
describe '#query_dimensions' do

with_doi_clause = lambda { |with_doi| with_doi ? 'where doi is not empty' : 'where doi is empty' }
query_template = <<~QUERY
search publications %{with_doi_clause} in raw_affiliations#{' '}
for """
"University of North Carolina, Chapel Hill" OR "UNC"
"""#{' '}
return publications[basics + extras]
limit %{page_size}
skip %{skip}
QUERY

it 'raises and logs an error if the query returns a status code that is not 403 or 200' do
allow(Rails.logger).to receive(:error)
Expand Down Expand Up @@ -102,27 +93,26 @@
File.read(File.expand_path('../../../fixtures/files/dimensions_pagination_query_response_1.json', __FILE__)),
File.read(File.expand_path('../../../fixtures/files/dimensions_pagination_query_response_2.json', __FILE__))
]
query_strings = [query_template % { with_doi_clause: with_doi_clause.call(true), page_size: 100, skip: 0 },
query_template % { with_doi_clause: with_doi_clause.call(true), page_size: 100, skip: 100 }]

stub = stub_request(:post, 'https://app.dimensions.ai/api/dsl')
# The first request will return a 403 error, the other requests will return a 200 response
# Repeating code to stub requests, encountered issues with chaining
stub_request(:post, 'https://app.dimensions.ai/api/dsl')
.with(
body: query_strings[0],
body: /skip 0/,
headers: { 'Content-Type' => 'application/json' }
)
.to_return(status: 200, body: dimensions_pagination_query_responses[0], headers: { 'Content-Type' => 'application/json' })
.times(1)

# The first request will return a 403 error, the second request will return a 200 response
stub = stub_request(:post, 'https://app.dimensions.ai/api/dsl')
stub_request(:post, 'https://app.dimensions.ai/api/dsl')
.with(
body: query_strings[1],
body: /skip 100/,
headers: { 'Content-Type' => 'application/json' }
)
.to_return({ status: 403, body: 'Unauthorized' },
{ status: 200, body: dimensions_pagination_query_responses[1], headers: { 'Content-Type' => 'application/json' }})
.times(2)

{ status: 200, body: dimensions_pagination_query_responses[1], headers: { 'Content-Type' => 'application/json' }})
.times(1)
publications = service.query_dimensions(with_doi: true)
expect(WebMock).to have_requested(:post, 'https://app.dimensions.ai/api/dsl').times(3)
expect(WebMock).to have_requested(:post, 'https://app.dimensions.ai/api/auth').times(2)
Expand Down

0 comments on commit 0dd23d1

Please sign in to comment.