Skip to content

Commit

Permalink
[#50543] added request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharonus committed Oct 18, 2023
1 parent 87036b9 commit 27fa563
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modules/storages/lib/api/v3/storages/storage_open_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class API::V3::Storages::StorageOpenAPI < API::OpenProjectAPI
get do
Storages::Peripherals::Registry
.resolve("queries.#{@storage.short_provider_type}.open_storage")
.call(storage: @storage, user: current_user, file_id: nil)
.call(storage: @storage, user: current_user)
.match(
on_success: ->(url) do
redirect url, body: "The requested resource can be viewed at #{url}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,61 @@
it_behaves_like 'not found'
end
end

describe 'GET /api/v3/project_storages/:id/open' do
let(:path) { api_v3_paths.project_storage_open(project_storage11.id) }
let(:location) { 'https://deathstar.storage.org/files' }
let(:location_project_folder) { 'https://deathstar.storage.org/files/data/project_destroy_alderan' }
let(:current_user) do
create(:user, member_with_permissions: { project1 => view_permissions })
end

before do
Storages::Peripherals::Registry.stub(
'queries.nextcloud.open_storage',
->(_) { ServiceResult.success(result: location) }
)
Storages::Peripherals::Registry.stub(
'queries.nextcloud.open_file_link',
->(_) { ServiceResult.success(result: location_project_folder) }
)
end

context 'as admin' do
let(:current_user) { create(:admin) }

it_behaves_like 'redirect response'
end

context 'if user belongs to a project related to project storage' do
it_behaves_like 'redirect response'

context 'if project storage has a configured project folder' do
let!(:project_storage12) do
create(:project_storage,
project: project1,
storage: storage2,
project_folder_id: '1337',
project_folder_mode: 'manual')
end
let(:path) { api_v3_paths.project_storage_open(project_storage12.id) }

it_behaves_like 'redirect response' do
let(:location) { location_project_folder }
end
end

context 'if user is missing permission view_file_links' do
let(:view_permissions) { [] }

it_behaves_like 'not found'
end
end

context 'if user is not member of the project' do
let(:path) { api_v3_paths.project_storage_open(project_storage21.id) }

it_behaves_like 'not found'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,40 @@
end
end

describe 'GET /api/v3/storages/:storage_id/open' do
let(:path) { api_v3_paths.storage_open(storage.id) }
let(:location) { 'https://deathstar.storage.org/files' }

before do
Storages::Peripherals::Registry.stub(
'queries.nextcloud.open_storage',
->(_) { ServiceResult.success(result: location) }
)
end

context 'as admin' do
let(:current_user) { create(:admin) }

it_behaves_like 'redirect response'
end

context 'if user belongs to a project using the given storage' do
it_behaves_like 'redirect response'

context 'if user is missing permission view_file_links' do
let(:permissions) { [] }

it_behaves_like 'not found'
end

context 'if no storage with that id exists' do
let(:path) { api_v3_paths.storage_open('1337') }

it_behaves_like 'not found'
end
end
end

describe 'POST /api/v3/storages/:storage_id/oauth_client_credentials' do
let(:path) { api_v3_paths.storage_oauth_client_credentials(storage.id) }
let(:client_id) { 'myl1ttlecl13ntidii' }
Expand Down
14 changes: 13 additions & 1 deletion spec/requests/api/v3/support/response_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
end
end

RSpec.shared_examples_for 'redirect response' do |code = 303|
let(:location) { '' }

it "has the status code #{code}" do
expect(last_response.status).to eq(code)
end

it 'redirects to expected location' do
expect(last_response.headers['Location']).to eq(location)
end
end

RSpec.shared_examples_for 'error response' do |code, id, provided_message = nil|
let(:expected_message) do
provided_message || message
Expand Down Expand Up @@ -113,7 +125,7 @@
it 'shows the given details' do
if details
expect(last_response.body).to be_json_eql(details.to_json)
.at_path('_embedded/details/parseError')
.at_path('_embedded/details/parseError')
end
end
end
Expand Down

0 comments on commit 27fa563

Please sign in to comment.