diff --git a/.jsonapicfg.yml b/.jsonapicfg.yml new file mode 100644 index 000000000..38cf70cf6 --- /dev/null +++ b/.jsonapicfg.yml @@ -0,0 +1,2 @@ +--- +namespace: api diff --git a/app/controllers/api/v2/courses_controller.rb b/app/controllers/api/v2/courses_controller.rb index ecdf8a4ba..e3a8abd4e 100644 --- a/app/controllers/api/v2/courses_controller.rb +++ b/app/controllers/api/v2/courses_controller.rb @@ -4,7 +4,7 @@ class Api::V2::CoursesController < JsonapiSuiteController def index authorize Course - scope = CoursePolicy::Scope.new(current_user, Course, editable_only: editable?).resolve + scope = CoursePolicy::Scope.new(current_user, Course, editable_only: editable_filter?).resolve render_jsonapi(scope) end diff --git a/app/controllers/jsonapi_suite_controller.rb b/app/controllers/jsonapi_suite_controller.rb index f4492ffbd..957c9c885 100644 --- a/app/controllers/jsonapi_suite_controller.rb +++ b/app/controllers/jsonapi_suite_controller.rb @@ -20,7 +20,7 @@ class JsonapiSuiteController < ApplicationController private - def editable? + def editable_filter? params.dig(:filter, :editable)&.to_boolean end diff --git a/config/routes.rb b/config/routes.rb index 397ca21ab..7de0a121b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,6 +124,8 @@ end namespace :api do + resources :docs, only: [:index], path: '/swagger' + namespace :v1 do resources :aid_stations, only: [:show, :create, :update, :destroy] resources :courses, only: [:index, :show, :create, :update, :destroy] diff --git a/spec/api/v2/courses/create_spec.rb b/spec/api/v2/courses/create_spec.rb index c5f1b0088..3f66f22e5 100644 --- a/spec/api/v2/courses/create_spec.rb +++ b/spec/api/v2/courses/create_spec.rb @@ -2,7 +2,7 @@ RSpec.describe "courses#create", type: :request do subject(:make_request) do - jsonapi_post "/api/v1/courses", payload + jsonapi_post "/api/v2/courses", payload end describe 'basic create' do @@ -11,16 +11,14 @@ data: { type: 'courses', attributes: { - # ... your attrs here + name: 'Test Course' } } } end it 'creates the resource' do - expect { - make_request - }.to change { Course.count }.by(1) + expect { make_request }.to change { Course.count }.by(1) course = Course.last assert_payload(:course, course, json_item) diff --git a/spec/api/v2/courses/destroy_spec.rb b/spec/api/v2/courses/destroy_spec.rb index 6939ca4b6..5fa17fdd6 100644 --- a/spec/api/v2/courses/destroy_spec.rb +++ b/spec/api/v2/courses/destroy_spec.rb @@ -1,17 +1,17 @@ require 'rails_helper' RSpec.describe "courses#destroy", type: :request do + before { sign_in users(:alice), scope: :admin } + subject(:make_request) do - jsonapi_delete "/api/v1/courses/#{course.id}" + jsonapi_delete "/api/v2/courses/#{course.id}" end describe 'basic destroy' do let!(:course) { create(:course) } it 'updates the resource' do - expect { - make_request - }.to change { Course.count }.by(-1) + expect { make_request }.to change { Course.count }.by(-1) expect(response.status).to eq(200) expect(json).to eq('meta' => {}) diff --git a/spec/api/v2/courses/index_spec.rb b/spec/api/v2/courses/index_spec.rb index 3e67e8314..e136261c8 100644 --- a/spec/api/v2/courses/index_spec.rb +++ b/spec/api/v2/courses/index_spec.rb @@ -4,7 +4,7 @@ let(:params) { {} } subject(:make_request) do - jsonapi_get "/api/v1/courses", + jsonapi_get "/api/v2/courses", params: params end diff --git a/spec/api/v2/courses/show_spec.rb b/spec/api/v2/courses/show_spec.rb index b4e85ef49..89932eabe 100644 --- a/spec/api/v2/courses/show_spec.rb +++ b/spec/api/v2/courses/show_spec.rb @@ -4,7 +4,7 @@ let(:params) { {} } subject(:make_request) do - jsonapi_get "/api/v1/courses/#{course.id}", + jsonapi_get "/api/v2/courses/#{course.id}", params: params end diff --git a/spec/api/v2/courses/update_spec.rb b/spec/api/v2/courses/update_spec.rb index 9e772367e..5490232cf 100644 --- a/spec/api/v2/courses/update_spec.rb +++ b/spec/api/v2/courses/update_spec.rb @@ -2,7 +2,7 @@ RSpec.describe "courses#update", type: :request do subject(:make_request) do - jsonapi_put "/api/v1/courses/#{course.id}", payload + jsonapi_put "/api/v2/courses/#{course.id}", payload end describe 'basic update' do diff --git a/spec/payloads/course.rb b/spec/payloads/course.rb new file mode 100644 index 000000000..a16494bce --- /dev/null +++ b/spec/payloads/course.rb @@ -0,0 +1,34 @@ +# Register a payload to validate against. +# Add expected attributes within this block, e.g.: +# +# key(:name) +# +# Optionally validate the type as well: +# +# key(:name, String) +# +# This will: +# +# * Compare record.name == json['name'] +# * Ensure no extra keys are in the json payload +# * Ensure no values are nil (unless allow_nil: true is passed) +# * Ensures json['name'] is a string +# +# If you have custom serialization logic and want to compare against +# something other than "record.name", pass a block: +# +# key(:name) { |record| record.name.upcase } +# +# Or, if this is a one-off for a particular spec, do that customization at +# runtime: +# +# assert_payload(:person, person_record, json_item) do +# key(:name) { 'Homer Simpson' } +# end +# +# For more information, see https://jsonapi-suite.github.io/jsonapi_spec_helpers/ +JsonapiSpecHelpers::Payload.register(:course) do + key(:name, String) + key(:description, String) + key(:next_start_time, DateTime) +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index dd8447169..b69901816 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -17,6 +17,7 @@ require 'active_support/core_ext/hash/keys' require 'active_support/inflector' require 'rack/test' +require 'jsonapi_spec_helpers' # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are diff --git a/spec/support/jsonapi_suite.rb b/spec/support/jsonapi_suite.rb new file mode 100644 index 000000000..c550b1b28 --- /dev/null +++ b/spec/support/jsonapi_suite.rb @@ -0,0 +1,5 @@ +RSpec.configure do |config| + config.include JsonapiSpecHelpers, type: :request + config.extend ControllerMacros, type: :request + config.include Devise::Test::ControllerHelpers, type: :request +end