Repository navigation
Add support for sending header values to Quepid #482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matt-bernhardt
wants to merge
2
commits into
main
Choose a base branch
from
use-660
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1888,7 +1888,7 @@ def source_filter_count(controller) | |
| 'Expected tuning parameters to affect results, but counts and title orders are identical.') | ||
| end | ||
|
|
||
| test 'results can be returned in JSON format when env is set and valid token is provided' do | ||
| test 'results can be returned in JSON format when env is set and valid token is provided via param' do | ||
| secret_value = 'sooper_sekret' | ||
| quepid_ua = 'Quepid/1.0 (Web Scraper)' | ||
| ClimateControl.modify FORMAT_TOKEN: secret_value do | ||
|
|
@@ -1903,6 +1903,21 @@ def source_filter_count(controller) | |
| end | ||
| end | ||
|
|
||
| test 'results can be returned in JSON format when env is set and valid token is provided via header' do | ||
| secret_value = 'sooper_sekret' | ||
| quepid_ua = 'Quepid/1.0 (Web Scraper)' | ||
| ClimateControl.modify FORMAT_TOKEN: secret_value do | ||
| mock_timdex_search_with_hits(10) | ||
| get "/results?q=test&format=json", headers: { 'HTTP_USER_AGENT' => quepid_ua, 'HTTP_FORMAT_TOKEN' => secret_value } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assert_response :success | ||
| assert_equal 'application/json; charset=utf-8', response.content_type | ||
| response_json = JSON.parse(response.body) | ||
| assert_equal %w[results hits errors], response_json.keys | ||
| assert_instance_of Array, response_json['results'] | ||
| assert_instance_of Integer, response_json['hits'] | ||
| end | ||
| end | ||
|
|
||
| test 'results in JSON format will report zero hit count in the face of missing data' do | ||
| SearchController.any_instance.stubs(:load_timdex_results).returns(nil) | ||
| secret_value = 'sooper_sekret' | ||
|
|
@@ -1927,7 +1942,7 @@ def source_filter_count(controller) | |
| end | ||
| end | ||
|
|
||
| test 'results can be returned in JSON format when env is set and valid token is provided even with bot challenge' do | ||
| test 'results returned in JSON format when valid token is provided via querystring even with bot challenge' do | ||
| secret_value = 'sooper_sekret' | ||
| quepid_ua = 'Quepid/1.0 (Web Scraper)' | ||
|
|
||
|
|
@@ -1943,6 +1958,22 @@ def source_filter_count(controller) | |
| end | ||
| end | ||
|
|
||
| test 'results returned in JSON format when valid token is provided via headers even with bot challenge' do | ||
| secret_value = 'sooper_sekret' | ||
| quepid_ua = 'Quepid/1.0 (Web Scraper)' | ||
|
|
||
| ClimateControl.modify(FORMAT_TOKEN: secret_value, FEATURE_BOT_DETECTION: 'true') do | ||
| mock_timdex_search_with_hits(10) | ||
| get "/results?q=test&format=json", headers: { 'HTTP_FORMAT_TOKEN' => secret_value, 'HTTP_USER_AGENT' => quepid_ua } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assert_response :success | ||
| assert_equal 'application/json; charset=utf-8', response.content_type | ||
| response_json = JSON.parse(response.body) | ||
| assert_equal %w[results hits errors], response_json.keys | ||
| assert_instance_of Array, response_json['results'] | ||
| assert_instance_of Integer, response_json['hits'] | ||
| end | ||
| end | ||
|
|
||
| test 'results can be returned in JSON format when env is set while load-more pagination is enabled' do | ||
| secret_value = 'sooper_sekret' | ||
| quepid_ua = 'Quepid/1.0 (Web Scraper)' | ||
|
|
@@ -1967,12 +1998,31 @@ def source_filter_count(controller) | |
| end | ||
| end | ||
|
|
||
| # We don't mock anything here because the error prevents any external lookups | ||
| test 'requests for JSON results with an empty token generate an unauthorized error' do | ||
| secret_value = 'sooper_sekret' | ||
| ClimateControl.modify FORMAT_TOKEN: secret_value do | ||
| get '/results?q=test&format=json', headers: { 'HTTP_FORMAT_TOKEN' => '' } | ||
|
|
||
| assert_response :unauthorized | ||
|
|
||
| get '/results?q=test&format=json&format_token=' | ||
|
|
||
| assert_response :unauthorized | ||
| end | ||
| end | ||
|
|
||
| # We don't mock anything here because the error prevents any external lookups | ||
| test 'requests for JSON results when env var is not set generate an unauthorized error' do | ||
| secret_value = 'irrelevant' | ||
| ClimateControl.modify FORMAT_TOKEN: '' do | ||
| # via param | ||
| get "/results?q=test&format=json&format_token=#{secret_value}" | ||
| assert_response :unauthorized | ||
|
|
||
| # via headers | ||
| get '/results?q=test&format=json', headers: { 'HTTP_FORMAT_TOKEN' => secret_value } | ||
| assert_response :unauthorized | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -1983,8 +2033,30 @@ def source_filter_count(controller) | |
| refute_equal secret_value, wrong_secret | ||
|
|
||
| ClimateControl.modify FORMAT_TOKEN: secret_value do | ||
| # via param | ||
| get "/results?q=test&format=json&format_token=#{wrong_secret}" | ||
| assert_response :unauthorized | ||
|
|
||
| # via headers | ||
| get '/results?q=test&format=json', headers: { 'HTTP_FORMAT_TOKEN' => wrong_secret } | ||
| assert_response :unauthorized | ||
| end | ||
| end | ||
|
|
||
| test 'requests for JSON results assign precedence to request header over query params if both present' do | ||
| secret_value = 'sooper_sekret' | ||
| wrong_secret = 'something_else' | ||
| refute_equal secret_value, wrong_secret | ||
|
|
||
| ClimateControl.modify FORMAT_TOKEN: secret_value do | ||
| # wrong header, correct query params - results in an unauthorized request | ||
| get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_FORMAT_TOKEN' => wrong_secret } | ||
| assert_response :unauthorized | ||
|
|
||
| # correct header, wrong query params - results in success. Mocked response because we just test for success. | ||
| mock_timdex_search_with_hits(10) | ||
| get "/results?q=test&format=json&format_token=#{wrong_secret}", headers: { 'HTTP_FORMAT_TOKEN' => secret_value } | ||
| assert_response :success | ||
| end | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the search controller in a different way, using
.presencewithin theformat_tokens_defined?method as well as within the validity check. This prevents the need for a guard clause oncandidate.blank?, as it stopsvalid_token?from being called in the first place. I've added a test on lines 2001-2013 to confirm the intended behavior (red-green refactor FTW, I think)