Skip to content

Commit

Permalink
Merge pull request #55 from tiagopog/update-jsonapi-resources-to-0-8-3
Browse files Browse the repository at this point in the history
Update jsonapi-resources to v0.8.3
  • Loading branch information
tiagopog authored Mar 13, 2017
2 parents 0a11793 + 104ba66 commit cb0021f
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 175 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ Support:
For Rails 4 add this to your application's Gemfile:

```ruby
gem 'jsonapi-utils', '~> 0.4.8'
gem 'jsonapi-utils', '~> 0.4.9'
```

For Rails 5:

```ruby
gem 'jsonapi-utils', '~> 0.5.1'
gem 'jsonapi-utils', '~> 0.5.2'
```

And then execute:
Expand Down
2 changes: 1 addition & 1 deletion jsonapi-utils.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'jsonapi-resources', '~> 0.8.0'
spec.add_runtime_dependency 'jsonapi-resources', '0.8.3'

spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonapi/utils/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module JSONAPI
module Utils
VERSION = '0.5.1'.freeze
VERSION = '0.5.2'.freeze
end
end
92 changes: 47 additions & 45 deletions spec/controllers/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
describe PostsController, type: :controller do
include_context 'JSON API headers'

before(:all) { FactoryGirl.create_list(:post, 3) }
before(:all) do
@post = FactoryGirl.create_list(:post, 3).first
end

before(:each) do
JSONAPI.configuration.json_key_format = :underscored_key
end

let(:fields) { (PostResource.fields - %i(id author category)).map(&:to_s) }
let(:relationships) { %w(author category) }
let(:resource) { Post.first }
let(:parent_id) { resource.user_id }
let(:category_id) { resource.category_id }
let(:relationships) { PostResource._relationships.keys.map(&:to_s) }
let(:fields) { PostResource.fields.reject { |e| e == :id }.map(&:to_s) - relationships }
let(:blog_post) { @post }
let(:parent_id) { blog_post.user_id }
let(:category_id) { blog_post.category_id }

let(:attributes) do
{ title: 'Lorem ipsum', body: 'Lorem ipsum dolor sit amet.', content_type: 'article' }
Expand All @@ -38,7 +40,7 @@
end

describe 'GET #index' do
subject { get :index, params }
subject { get :index, params: params }

let(:params) { { user_id: parent_id } }

Expand All @@ -53,7 +55,7 @@
end

context 'with Hash' do
subject { get :index_with_hash, params }
subject { get :index_with_hash, params: params }

it 'renders a collection of users' do
expect(subject).to have_http_status :ok
Expand Down Expand Up @@ -148,19 +150,19 @@

describe 'GET #show' do
context 'with ActiveRecord' do
subject { get :show, id: resource.id }
subject { get :show, params: { id: blog_post.id } }

it 'renders a single post' do
expect(subject).to have_http_status :ok
expect(subject).to have_primary_data('posts')
expect(subject).to have_data_attributes(fields)
expect(subject).to have_relationships(relationships)
expect(data.dig('attributes', 'title')).to eq("Title for Post #{resource.id}")
expect(data.dig('attributes', 'title')).to eq("Title for Post #{blog_post.id}")
end
end

context 'with Hash' do
subject { get :show_with_hash, id: resource.id }
subject { get :show_with_hash, params: { id: blog_post.id } }

it 'renders a single post' do
expect(subject).to have_http_status :ok
Expand All @@ -173,7 +175,7 @@

context 'when resource was not found' do
context 'with conventional id' do
subject { get :show, id: 999 }
subject { get :show, params: { id: 999 } }

it 'renders a 404 response' do
expect(subject).to have_http_status :not_found
Expand All @@ -184,7 +186,7 @@
end

context 'with uuid' do
subject { get :show, id: uuid }
subject { get :show, params: { id: uuid } }

let(:uuid) { SecureRandom.uuid }

Expand All @@ -197,7 +199,7 @@
end

context 'with slug' do
subject { get :show, id: slug }
subject { get :show, params: { id: slug } }

let(:slug) { 'some-awesome-slug' }

Expand All @@ -212,7 +214,7 @@
end

describe 'POST #create' do
subject { post :create, params.merge(body) }
subject { post :create, params: params.merge(body) }

let (:params) { { user_id: parent_id } }

Expand All @@ -225,7 +227,7 @@
end

context 'when validation fails on an attribute' do
subject { post :create, params.merge(invalid_body) }
subject { post :create, params: params.merge(invalid_body) }

let(:invalid_body) do
body.tap { |b| b[:data][:attributes][:title] = nil }
Expand All @@ -234,15 +236,15 @@
it 'renders a 422 response' do
expect { subject }.to change(Post, :count).by(0)
expect(response).to have_http_status :unprocessable_entity
expect(errors[0]['id']).to eq('title')
expect(errors[0]['title']).to eq('Title can\'t be blank')
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']['pointer']).to eq('/data/attributes/title')
expect(errors.dig(0, 'id')).to eq('title')
expect(errors.dig(0, 'title')).to eq('Title can\'t be blank')
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/title')
end
end

context 'when validation fails on a relationship' do
subject { post :create, params.merge(invalid_body) }
subject { post :create, params: params.merge(invalid_body) }

let(:invalid_body) do
body.tap { |b| b[:data][:relationships][:author] = nil }
Expand All @@ -252,15 +254,15 @@
expect { subject }.to change(Post, :count).by(0)
expect(subject).to have_http_status :unprocessable_entity

expect(errors[0]['id']).to eq('author')
expect(errors[0]['title']).to eq('Author can\'t be blank')
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']['pointer']).to eq('/data/relationships/author')
expect(errors.dig(0, 'id')).to eq('author')
expect(errors.dig(0, 'title')).to eq('Author can\'t be blank')
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/relationships/author')
end
end

context 'when validation fails on a foreign key' do
subject { post :create, params.merge(invalid_body) }
subject { post :create, params: params.merge(invalid_body) }

let(:invalid_body) do
body.tap { |b| b[:data][:relationships][:category] = nil }
Expand All @@ -270,15 +272,15 @@
expect { subject }.to change(Post, :count).by(0)
expect(subject).to have_http_status :unprocessable_entity

expect(errors[0]['id']).to eq('category')
expect(errors[0]['title']).to eq('Category can\'t be blank')
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']['pointer']).to eq('/data/relationships/category')
expect(errors.dig(0, 'id')).to eq('category')
expect(errors.dig(0, 'title')).to eq('Category can\'t be blank')
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/relationships/category')
end
end

context 'when validation fails on a private attribute' do
subject { post :create, params.merge(invalid_body) }
subject { post :create, params: params.merge(invalid_body) }

let(:invalid_body) do
body.tap { |b| b[:data][:attributes][:title] = 'Fail Hidden' }
Expand All @@ -288,15 +290,15 @@
expect { subject }.to change(Post, :count).by(0)
expect(subject).to have_http_status :unprocessable_entity

expect(errors[0]['id']).to eq('hidden_field')
expect(errors[0]['title']).to eq('Hidden field error was tripped')
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']).to be_nil
expect(errors.dig(0, 'id')).to eq('hidden_field')
expect(errors.dig(0, 'title')).to eq('Hidden field error was tripped')
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to be_nil
end
end

context 'when validation fails with a formatted attribute key' do
subject { post :create, params.merge(invalid_body) }
subject { post :create, params: params.merge(invalid_body) }

let(:invalid_body) do
body.tap { |b| b[:data][:attributes][:title] = 'Fail Hidden' }
Expand All @@ -315,17 +317,17 @@
expect { subject }.to change(Post, :count).by(0)
expect(subject).to have_http_status :unprocessable_entity

expect(errors[0]['id']).to eq('content-type')
expect(errors[0]['title']).to eq('Content type can\'t be blank')
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']['pointer']).to eq('/data/attributes/content-type')
expect(errors.dig(0, 'id')).to eq('content-type')
expect(errors.dig(0, 'title')).to eq('Content type can\'t be blank')
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/content-type')
end
end
end

describe 'PATCH #update' do
shared_context 'update request' do |action:|
subject { patch action, params.merge(body) }
subject { patch action, params: params.merge(body) }

let(:params) { { id: 1 } }
let(:body) { { data: { id: 1, type: 'posts', attributes: { title: 'Foo' } } } }
Expand All @@ -343,10 +345,10 @@
expect { subject }.to change(Post, :count).by(0)
expect(response).to have_http_status :unprocessable_entity

expect(errors[0]['id']).to eq('base')
expect(errors[0]['title']).to eq('This is an error on the base')
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']['pointer']).to eq('/data')
expect(errors.dig(0, 'id')).to eq('base')
expect(errors.dig(0, 'title')).to eq('This is an error on the base')
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to eq('/data')
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/controllers/profile_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
describe ProfileController, type: :controller do
include_context 'JSON API headers'

let(:fields) { (ProfileResource.fields - %i(id)).map(&:to_s) }
let(:resource) { Profile }
let(:attributes) { { location: 'Springfield, USA' } }
let(:relationships) { ProfileResource._relationships.keys.map(&:to_s) }
let(:fields) { ProfileResource.fields.reject { |e| e == :id }.map(&:to_s) - relationships }
let(:attributes) { { nickname: 'Foobar', location: 'Springfield, USA' } }

let(:body) do
{
Expand All @@ -30,10 +30,10 @@
it 'renders a 422 response' do
patch :update, params: body
expect(response).to have_http_status :unprocessable_entity
expect(errors[0]['id']).to eq('location')
expect(errors[0]['title']).to eq("Location can't be blank")
expect(errors[0]['code']).to eq('100')
expect(errors[0]['source']['pointer']).to eq('/data/attributes/location')
expect(errors.dig(0, 'id')).to eq('nickname')
expect(errors.dig(0, 'title')).to eq("Nickname can't be blank")
expect(errors.dig(0, 'code')).to eq('100')
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/nickname')
end
end
end
Loading

0 comments on commit cb0021f

Please sign in to comment.