diff --git a/CHANGELOG.md b/CHANGELOG.md index 16f139073..204f226f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog Fab-manager - Ability to run `fablab:chain:all` non interactively +- Full test coverage of the OpenAPI +- Fix a bug: unable to get the Events without images from the OpenAPI +- Fix a bug: unable to get the Space reservations from the OpenAPI +- Fix a bug: unable to get invoices from the OpenAPI ## v5.0.7 2021 June 24 diff --git a/app/controllers/open_api/v1/invoices_controller.rb b/app/controllers/open_api/v1/invoices_controller.rb index 373e18910..f7254863d 100644 --- a/app/controllers/open_api/v1/invoices_controller.rb +++ b/app/controllers/open_api/v1/invoices_controller.rb @@ -8,6 +8,7 @@ class OpenAPI::V1::InvoicesController < OpenAPI::V1::BaseController def index @invoices = Invoice.order(created_at: :desc) + .includes(invoicing_profile: :user) .references(:invoicing_profiles) @invoices = @invoices.where('invoicing_profiles.user_id = ?', params[:user_id]) if params[:user_id].present? diff --git a/app/doc/open_api/v1/reservations_doc.rb b/app/doc/open_api/v1/reservations_doc.rb index f56458ec7..977e1c7e0 100644 --- a/app/doc/open_api/v1/reservations_doc.rb +++ b/app/doc/open_api/v1/reservations_doc.rb @@ -16,7 +16,7 @@ class OpenAPI::V1::ReservationsDoc < OpenAPI::V1::BaseDoc description 'Index of reservations made by users, with optional pagination. Order by *created_at* descendant.' param_group :pagination param :user_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various users.' - param :reservable_type, %w[Event Machine Training], optional: true, desc: 'Scope the request to a specific type of reservable.' + param :reservable_type, %w[Event Machine Space Training], optional: true, desc: 'Scope the request to a specific type of reservable.' param :reservable_id, [Integer, Array], optional: true, desc: 'Scope the request to one or various reservables.' example <<-RESERVATIONS diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 6db1212f3..37e5aa6cd 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -27,6 +27,7 @@ class Invoice < PaymentDocument def file dir = "invoices/#{invoicing_profile.id}" + dir = "test/fixtures/files/invoices/#{invoicing_profile.id}" if Rails.env.test? # create directories if they doesn't exists (invoice & invoicing_profile_id) FileUtils.mkdir_p dir diff --git a/app/views/open_api/v1/events/index.json.jbuilder b/app/views/open_api/v1/events/index.json.jbuilder index 57f083cd4..12f7ea1a2 100644 --- a/app/views/open_api/v1/events/index.json.jbuilder +++ b/app/views/open_api/v1/events/index.json.jbuilder @@ -3,10 +3,12 @@ json.events @events do |event| json.extract! event, :nb_total_places, :nb_free_places json.start_at event.availability.start_at json.end_at event.availability.end_at - json.event_image do - json.large_url root_url.chomp('/') + event.event_image.attachment.large.url - json.medium_url root_url.chomp('/') + event.event_image.attachment.medium.url - json.small_url root_url.chomp('/') + event.event_image.attachment.small.url + if event.event_image + json.event_image do + json.large_url root_url.chomp('/') + event.event_image.attachment.large.url + json.medium_url root_url.chomp('/') + event.event_image.attachment.medium.url + json.small_url root_url.chomp('/') + event.event_image.attachment.small.url + end end json.prices do json.normal do diff --git a/test/fixtures/files/invoices/7/FabManager_invoice-3_10062015.pdf b/test/fixtures/files/invoices/7/FabManager_invoice-3_10062015.pdf new file mode 100644 index 000000000..b142afa61 Binary files /dev/null and b/test/fixtures/files/invoices/7/FabManager_invoice-3_10062015.pdf differ diff --git a/test/fixtures/open_api_calls_count_tracings.yml b/test/fixtures/open_api_calls_count_tracings.yml new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/open_api_clients.yml b/test/fixtures/open_api_clients.yml new file mode 100644 index 000000000..012f973b4 --- /dev/null +++ b/test/fixtures/open_api_clients.yml @@ -0,0 +1,7 @@ +minitest: + id: 1 + name: minitest + calls_count: 0 + token: TcAbo97tro21vBkbesZTkg2L + created_at: '2021-06-28 06:43:39.237107' + updated_at: '2021-06-28 06:43:39.237107' diff --git a/test/integration/open_api/bookable_machines_test.rb b/test/integration/open_api/bookable_machines_test.rb new file mode 100644 index 000000000..373656659 --- /dev/null +++ b/test/integration/open_api/bookable_machines_test.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::BookableMachinesTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all bookable machines without user_id' do + get '/open_api/v1/bookable_machines', headers: open_api_headers(@token) + assert_response :internal_server_error + end + + test 'list all bookable machines' do + get '/open_api/v1/bookable_machines?user_id=3', headers: open_api_headers(@token) + assert_response :success + end +end diff --git a/test/integration/open_api/events_test.rb b/test/integration/open_api/events_test.rb new file mode 100644 index 000000000..d723449f2 --- /dev/null +++ b/test/integration/open_api/events_test.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::EventsTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all events' do + get '/open_api/v1/events', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all events with pagination' do + get '/open_api/v1/events?page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end + + test 'list events for a given IDs' do + get '/open_api/v1/events?id=[3,4]', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all upcoming events' do + get '/open_api/v1/events?upcoming=true', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all upcoming events with pagination' do + get '/open_api/v1/events?upcoming=true&page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end +end diff --git a/test/integration/open_api/invoices_test.rb b/test/integration/open_api/invoices_test.rb new file mode 100644 index 000000000..2455b03dd --- /dev/null +++ b/test/integration/open_api/invoices_test.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::InvoicesTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all invoices' do + get '/open_api/v1/invoices', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all invoices with pagination' do + get '/open_api/v1/invoices?page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all invoices for a user' do + get '/open_api/v1/invoices?user_id=3', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all invoices for a user with pagination' do + get '/open_api/v1/invoices?user_id=3&page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end + + test 'download an invoice' do + get '/open_api/v1/invoices/3/download', headers: open_api_headers(@token) + assert_response :success + assert_match /^inline; filename=/, response.headers['Content-Disposition'] + end +end diff --git a/test/integration/open_api/machines_test.rb b/test/integration/open_api/machines_test.rb new file mode 100644 index 000000000..9aa00d5ee --- /dev/null +++ b/test/integration/open_api/machines_test.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::MachinesTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all machines' do + get '/open_api/v1/machines', headers: open_api_headers(@token) + assert_response :success + end + + test 'create a machine' do + post '/open_api/v1/machines', + params: { + machine: { + name: 'IJFX 350 Laser', + description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore...', + spec: 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium...', + disabled: true + } + }.to_json, + headers: open_api_headers(@token) + assert_response :success + end + + test 'update a machine' do + patch '/open_api/v1/machines/3', + params: { + machine: { + disabled: true, + name: '[DISABLED] Shopbot' + } + }.to_json, + headers: open_api_headers(@token) + assert_response :success + end + + test 'get a machine' do + get '/open_api/v1/machines/3', headers: open_api_headers(@token) + assert_response :success + end + + test 'delete a machine' do + delete '/open_api/v1/machines/3', headers: open_api_headers(@token) + assert_response :success + end +end diff --git a/test/integration/open_api/reservations_test.rb b/test/integration/open_api/reservations_test.rb new file mode 100644 index 000000000..d1cec96f2 --- /dev/null +++ b/test/integration/open_api/reservations_test.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::ReservationsTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all reservations' do + get '/open_api/v1/reservations', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all reservations with pagination' do + get '/open_api/v1/reservations?page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all reservations for a user' do + get '/open_api/v1/reservations?user_id=3', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all reservations for a user with pagination' do + get '/open_api/v1/reservations?user_id=3&page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all machine reservations for a user' do + get '/open_api/v1/reservations?reservable_type=Machine&user_id=3', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all machine 4 reservations' do + get '/open_api/v1/reservations?reservable_type=Machine&reservable_id=4', headers: open_api_headers(@token) + assert_response :success + end +end diff --git a/test/integration/open_api/trainings_test.rb b/test/integration/open_api/trainings_test.rb new file mode 100644 index 000000000..deda96800 --- /dev/null +++ b/test/integration/open_api/trainings_test.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::TrainingsTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all trainings' do + get '/open_api/v1/trainings', headers: open_api_headers(@token) + assert_response :success + end + +end diff --git a/test/integration/open_api/users_test.rb b/test/integration/open_api/users_test.rb new file mode 100644 index 000000000..3c3c238cf --- /dev/null +++ b/test/integration/open_api/users_test.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'test_helper' + +module OpenApi; end + +class OpenApi::UsersTest < ActionDispatch::IntegrationTest + def setup + @token = OpenAPI::Client.find_by(name: 'minitest').token + end + + test 'list all users' do + get '/open_api/v1/users', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all users with pagination' do + get '/open_api/v1/users?page=1&per_page=5', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all users filtering by IDs' do + get '/open_api/v1/users?user_id=[3,4,5]', headers: open_api_headers(@token) + assert_response :success + end + + test 'list all users filtering by email' do + get '/open_api/v1/users?email=jean.dupond@gmail.com', headers: open_api_headers(@token) + assert_response :success + end +end diff --git a/test/controllers/payment_schedules_controller_test.rb b/test/integration/payment_schedules_test.rb similarity index 83% rename from test/controllers/payment_schedules_controller_test.rb rename to test/integration/payment_schedules_test.rb index 446e5a558..f662bec84 100644 --- a/test/controllers/payment_schedules_controller_test.rb +++ b/test/integration/payment_schedules_test.rb @@ -2,7 +2,7 @@ require 'test_helper' -class PaymentSchedulesControllerTest < ActionDispatch::IntegrationTest +class PaymentSchedulesTest < ActionDispatch::IntegrationTest def setup @user = User.friendly.find('pjproudhon') login_as(@user, scope: :user) diff --git a/test/test_helper.rb b/test/test_helper.rb index f5d45b1a7..39b482a24 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -36,6 +36,10 @@ class ActiveSupport::TestCase { 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s } end + def open_api_headers(token) + { 'Accept' => Mime[:json], 'Content-Type' => Mime[:json].to_s, 'Authorization' => "Token token=#{token}" } + end + def stripe_payment_method(error: nil) number = '4242424242424242' exp_month = 4