Skip to content

Commit

Permalink
Begin implementing tests for Api::V2::CoursesController.
Browse files Browse the repository at this point in the history
  • Loading branch information
moveson authored and legoabram committed Mar 9, 2018
1 parent 53dad9f commit eb43e92
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .jsonapicfg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
namespace: api
2 changes: 1 addition & 1 deletion app/controllers/api/v2/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/jsonapi_suite_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class JsonapiSuiteController < ApplicationController

private

def editable?
def editable_filter?
params.dig(:filter, :editable)&.to_boolean
end

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 3 additions & 5 deletions spec/api/v2/courses/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions spec/api/v2/courses/destroy_spec.rb
Original file line number Diff line number Diff line change
@@ -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' => {})
Expand Down
2 changes: 1 addition & 1 deletion spec/api/v2/courses/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:params) { {} }

subject(:make_request) do
jsonapi_get "/api/v1/courses",
jsonapi_get "/api/v2/courses",
params: params
end

Expand Down
2 changes: 1 addition & 1 deletion spec/api/v2/courses/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/api/v2/courses/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/payloads/course.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/support/jsonapi_suite.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eb43e92

Please sign in to comment.