1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

Full test coverage of the OpenAPI

Also: fix related bugs
This commit is contained in:
Sylvain 2021-06-28 09:50:37 +02:00
parent 783f28762f
commit 66ba8e565c
17 changed files with 260 additions and 6 deletions

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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

Binary file not shown.

View File

7
test/fixtures/open_api_clients.yml vendored Normal file
View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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