Merge pull request #3511 from artoscinote/ma_SCI_5968

Added access token to protocols.io requests [SCI-5968]
This commit is contained in:
Miha Mencin 2021-08-30 09:26:51 +02:00 committed by GitHub
commit 7608151282
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 26 deletions

3
.gitignore vendored
View file

@ -35,6 +35,9 @@ ehthumbs.db
# Ignore temporary files
public/system/*
# Ignore BioEddie installion in public folder
public/bio_eddie/
# Ignore ActiveStorage Disc service storage directory
storage/

View file

@ -40,7 +40,7 @@ module ProtocolImporters
@errors[:invalid_params][:page_id] = 'Page needs to be positive' if @query_params[:page_id]&.to_i&.negative?
# try if endpints exists
@errors[:invalid_params][:source_endpoint] = 'Wrong source endpoint' unless endpoint_name&.is_a?(String)
@errors[:invalid_params][:source_endpoint] = 'Wrong source endpoint' unless endpoint_name.is_a?(String)
succeed?
end

View file

@ -12,12 +12,9 @@ module ProtocolImporters
default_timeout CONSTANTS[:default_timeout]
logger Rails.logger, CONSTANTS[:debug_level]
def initialize(token = nil)
# Currently we support public tokens only (no token needed for public data)
@auth = { token: token }
def initialize
# Set default headers
self.class.headers('Authorization': "Bearer #{@auth[:token]}") if @auth[:token].present?
self.class.headers('Authorization' => "Bearer #{ENV.fetch('PROTOCOLS_IO_ACCESS_TOKEN')}")
end
# Query params available are:

View file

@ -4,7 +4,6 @@ require 'rails_helper'
describe ProtocolImporters::ProtocolsIo::V3::ApiClient do
CONSTANTS = Constants::PROTOCOLS_IO_V3_API
TOKEN = 'test_token'
describe '#protocol_list' do
context 'when search key is not given' do
@ -114,17 +113,6 @@ describe ProtocolImporters::ProtocolsIo::V3::ApiClient do
subject.protocol_list(query)
expect(WebMock).to have_requested(:get, URL).with(query: query)
end
it 'should send authorization token if provided on initialization' do
headers = { 'Authorization': "Bearer #{TOKEN}" }
stub_protocols.with(headers: headers, query: default_query_params_with_key)
.to_return(status: 200,
body: JSON.generate(status_code: 0),
headers: { 'Content-Type': 'application/json' })
ProtocolImporters::ProtocolsIo::V3::ApiClient.new(TOKEN).protocol_list(key_query)
expect(WebMock).to have_requested(:get, URL).with(headers: headers, query: default_query_params_with_key)
end
end
end
@ -152,14 +140,6 @@ describe ProtocolImporters::ProtocolsIo::V3::ApiClient do
expect { subject.single_protocol(PROTOCOL_ID) }.to raise_error(ProtocolImporters::ProtocolsIo::V3::NetworkError)
end
it 'should send authorization token if provided on initialization' do
headers = { 'Authorization': "Bearer #{TOKEN}" }
stub_single_protocol.with(headers: headers)
ProtocolImporters::ProtocolsIo::V3::ApiClient.new(TOKEN).single_protocol(PROTOCOL_ID)
expect(WebMock).to have_requested(:get, SINGLE_PROTOCOL_URL).with(headers: headers)
end
end
describe '#protocol_html_preview' do