Skip to content

Commit

Permalink
removed bug of empty ThirdPartyApp array when no TPA's exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Oct 30, 2023
1 parent f07eca3 commit 2f37f8b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
50 changes: 28 additions & 22 deletions app/api/chemotion/third_party_app_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@
module Chemotion
# Publish-Subscription MessageAPI
class ThirdPartyAppAPI < Grape::API
cache_options = { store: Rails.cache }

helpers do
def decode_token(token)
payload = JWT.decode(token, Rails.application.secrets.secret_key_base) unless token.nil?
error!('401 Unauthorized', 401) if payload&.length&.zero?
def extract_values(payload)
att_id = payload[0]['attID']&.to_i
user_id = payload[0]['userID']&.to_i
name_third_party_app = payload[0]['nameThirdPartyApp']&.to_s
[att_id, user_id, name_third_party_app]
end

def decode_token(token)
payload = JWT.decode(token, Rails.application.secrets.secret_key_base) unless token.nil?
error!('401 Unauthorized', 401) if payload&.length&.zero?
extract_values(payload)
end

def verify_token(token)
payload = decode_token(token)
@attachment = Attachment.find_by(id: payload[0])
@user = User.find_by(id: payload[1])
error!('401 Unauthorized', 401) if @attachment.nil? || @user.nil?
end

def perform_download(token_cached, attachment)
if token_cached.counter <= 3
attachment.read_file
else
error!('Too many requests with this token', 403)
end
end

def update_cache(cache_key, token_cached)
error!('Invalid token', 403) if token_cached.nil?
token_cached.counter = token_cached.counter + 1
Rails.cache.write(cache_key, token_cached)
end

def download_third_party_app(token)
content_type 'application/octet-stream'
verify_token(token)
Expand All @@ -32,27 +48,15 @@ def download_third_party_app(token)
env['api.format'] = :binary
cache_key = "token/#{payload[0]}/#{payload[1]}/#{payload[2]}"
token_cached = Rails.cache.read(cache_key)
if token_cached.nil?
error!('Invalid token', 403)
end
token_cached.counter = token_cached.counter + 1
Rails.cache.write(cache_key, token_cached)
if token_cached.counter <= 3
@attachment.read_file
else
error!('To many requests with this token', 403)
end
update_cache(cache_key, token_cached)
perform_download(token_cached, @attachment)
end

def upload_third_party_app(token, file_name, file, file_type)
payload = decode_token(token)
cache_key = "token/#{payload[0]}/#{payload[1]}/#{payload[2]}"
token_cached = Rails.cache.read(cache_key)
if token_cached.nil?
error!('Invalid token', 403)
end
token_cached.counter = token_cached.counter + 1
Rails.cache.write(cache_key, token_cached)
update_cache(cache_key, token_cached)
if token_cached.counter > 30
error!('To many request with this token', 403)
else
Expand Down Expand Up @@ -120,8 +124,10 @@ def cache_key_for_encoded_token(payload)
end
post '/name_unique' do
declared(params, include_missing: false)
result = ThirdPartyApp.all_names.exclude?(params[:name])
if result
result = ThirdPartyApp.all_names
if result.nil?
{ message: 'Name is unique' }
elsif ThirdPartyApp.all_names.exclude?(params[:name])
{ message: 'Name is unique' }
else
{ message: 'Name is not unique' }
Expand Down
2 changes: 0 additions & 2 deletions app/packs/src/apps/admin/ThirdPartyApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ export default class ThirdPartyApp extends React.Component {
reject();
}

console.log("test1")

ThirdPartyAppFetcher.isNameUnique(name)
.then((result) => {
const message = JSON.parse(result).message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default class ResearchPlanDetailsAttachments extends Component {
ThirdPartyAppFetcher.fetchThirdPartyAppNames()
.then((result) => {
this.setState({
thirdPartyAppNames: result
thirdPartyAppNames: result === null || result.length === 0 ? [] : result
})
});
}
Expand Down
36 changes: 18 additions & 18 deletions spec/api/chemotion/third_party_app_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
let!(:admin1) { create(:admin) }

before do
allow_any_instance_of(WardenAuthentication).to receive(:current_user).and_return(admin1)
allow_any_instance_of(WardenAuthentication).to receive(:current_user).and_return(admin1) # rubocop:disable RSpec/AnyInstance
end

describe 'List all third party apps API', type: :request do
describe 'List all third party apps API' do
describe 'GET /third_party_apps/all' do
before do
ThirdPartyApp.create(IPAddress: 'http://test.com', name: 'Test1')
Expand Down Expand Up @@ -37,12 +37,12 @@
end
end

describe 'new_third_party_app API', type: :request do
describe 'new_third_party_app API' do
describe 'POST /new_third_party_app' do
let(:params) do
{
IPAddress: 'http://127.0.0.1',
name: 'Example App'
name: 'Example App',
}
end

Expand All @@ -59,7 +59,7 @@
end
end

describe 'update_third_party_app API', type: :request do
describe 'update_third_party_app API' do
let(:tpa_id) do
ThirdPartyApp.create(IPAddress: 'http://test.com', name: 'Test1')
tpas = ThirdPartyApp.all
Expand Down Expand Up @@ -112,7 +112,7 @@
end
end

describe 'delete_third_party_app API', type: :request do
describe 'delete_third_party_app API' do
let(:tpa_id) do
ThirdPartyApp.create(IPAddress: 'http://test.com', name: 'Test1')
tpas = ThirdPartyApp.all
Expand All @@ -134,7 +134,7 @@
end
end

describe 'get_by_id a third party app', type: :request do
describe 'get_by_id a third party app' do
before do
ThirdPartyApp.create(IPAddress: 'http://test1.com', name: 'Test1')
ThirdPartyApp.create(IPAddress: 'http://test2.com', name: 'Test2')
Expand Down Expand Up @@ -176,7 +176,7 @@
end
end

describe 'get names of all third party apps', type: :request do
describe 'get names of all third party apps' do
before do
ThirdPartyApp.create(IPAddress: 'http://test1.com', name: 'Test1')
ThirdPartyApp.create(IPAddress: 'http://test2.com', name: 'Test2')
Expand All @@ -192,7 +192,7 @@
end
end

describe 'get ip address of a third party app by name', type: :request do
describe 'get ip address of a third party app by name' do
before do
ThirdPartyApp.create(IPAddress: 'http://test1.com', name: 'Test1')
ThirdPartyApp.create(IPAddress: 'http://test2.com', name: 'Test2')
Expand All @@ -213,7 +213,7 @@
end
end

describe 'get a token for an attachment', type: :request do
describe 'get a token for an attachment' do
let(:user_id) do
users = User.all
users[0].id
Expand All @@ -223,7 +223,7 @@
{
attID: 1,
userID: user_id,
nameThirdPartyApp: 'fakeName'
nameThirdPartyApp: 'fakeName',
}
end

Expand All @@ -238,7 +238,7 @@
end
end

describe 'get a file from the ELN', type: :request do
describe 'get a file from the ELN' do
let(:user) { create(:person) }
let!(:attachment) do
create(
Expand All @@ -265,7 +265,7 @@
token = JWT.encode(payload, secret, 'HS256')
token_class = CachedTokenThirdPartyApp.new(token, 0, 'fakeDownload')
Rails.cache.write(cache_key, token_class, expires_in: 48.hours)
params = {token: token}
params = { token: token }
file = File.open('spec/fixtures/upload.csv')
file_content = file.read
file.close
Expand All @@ -276,17 +276,17 @@

it 'download a file with an invalid token (not in cache)' do
payload_invalid = { attID: params_token[:attID], userID: params_token[:userID],
nameThirdPartyApp: "Invalid" }
nameThirdPartyApp: 'Invalid' }
secret_invalid = Rails.application.secrets.secret_key_base
token_invalid = JWT.encode(payload_invalid, secret_invalid, 'HS256')
params_invalid = {token: token_invalid}
params_invalid = { token: token_invalid }
get '/api/v1/public_third_party_app/download', params: params_invalid
res_invalid = response.body
expect(res_invalid).to eq("{\"error\":\"Invalid token\"}")
expect(res_invalid).to eq('{"error":"Invalid token"}')
end
end

describe 'upload a file to the ELN', type: :request do
describe 'upload a file to the ELN' do
let(:user) { create(:person) }
let!(:attachment) do
create(
Expand Down Expand Up @@ -315,7 +315,7 @@
Rails.cache.write(cache_key, token_class, expires_in: 48.hours)
file_path = 'spec/fixtures/upload.csv'
file = Rack::Test::UploadedFile.new(file_path, 'spec/fixtures/upload2.csv')
params = {token: token, attachmentName: 'NewName', file: file, fileType: '.csv'}
params = { token: token, attachmentName: 'NewName', file: file, fileType: '.csv' }
post '/api/v1/public_third_party_app/upload', params: params
expect(response.body).to include('File uploaded successfully')
end
Expand Down

0 comments on commit 2f37f8b

Please sign in to comment.