Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
tmp
test/standard_api/test_app/log
*.log
.DS_Store
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ GEM
benchmark-ips (2.8.4)
builder (3.2.4)
byebug (11.1.3)
concurrent-ruby (1.1.8)
concurrent-ruby (1.1.9)
crass (1.0.6)
docile (1.3.4)
erubi (1.10.0)
Expand All @@ -90,11 +90,11 @@ GEM
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faker (2.18.0)
i18n (>= 1.6, < 2)
faker (2.20.0)
i18n (>= 1.8.11, < 2)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.8.10)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
jbuilder (2.10.1)
activesupport (>= 5.0.0)
Expand All @@ -111,7 +111,7 @@ GEM
rake
mini_mime (1.1.0)
mini_portile2 (2.6.1)
minitest (5.14.2)
minitest (5.15.0)
mocha (1.12.0)
nio4r (2.5.7)
nokogiri (1.12.5)
Expand Down
1 change: 1 addition & 0 deletions lib/standard_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'standard_api/helpers'
require 'standard_api/route_helpers'
require 'standard_api/active_record/connection_adapters/postgresql/schema_statements'
require 'standard_api/active_record/persistence'
require 'standard_api/railtie'

module StandardAPI
Expand Down
22 changes: 22 additions & 0 deletions lib/standard_api/active_record/persistence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "active_record/persistence"

# If an association can't be saved, default to returning. By default, Rails does this
# for just the record, but not any of it's assocations.
module ActiveRecord::Persistence

def save(**options, &block)
create_or_update(**options, &block)
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
false
end

def update(attributes)
with_transaction_returning_status do
assign_attributes(attributes)
save
end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
false
end

end
1 change: 1 addition & 0 deletions lib/standard_api/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def nested_includes(model, attributes)
includes[key] = nested_includes(association.klass, value)
end
end

includes
end

Expand Down
18 changes: 18 additions & 0 deletions test/standard_api/controller/error_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'standard_api/test_helper'

class ControllerErrorTest < ActionDispatch::IntegrationTest

# = Including an invalid include

test "Controller#create with a invalid value" do
property = build(:property, name: nil)

post "/properties", params: { property: property.attributes }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"name" => ["can't be blank"]
}
end

end
135 changes: 135 additions & 0 deletions test/standard_api/controller/subresource_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
require 'standard_api/test_helper'

class ControllerSubresourceTest < ActionDispatch::IntegrationTest

# add_resource
test 'Controller#add_resource with has_many' do
property = create(:property, photos: [])
photo = create(:photo)

post "/properties/#{property.id}/photos/#{photo.id}"
assert_equal property.photos.reload.map(&:id), [photo.id]
assert_response :created

assert_raises ActiveRecord::RecordNotFound do
post "/properties/#{property.id}/photos/9999999"
end
end

test 'Controller#add_resource with has_and_belongs_to_many' do
photo1 = create(:photo)
photo2 = create(:photo)
property = create(:property, photos: [photo1])

post "/properties/#{property.id}/photos/#{photo2.id}"
assert_equal property.photos.reload.map(&:id), [photo1.id, photo2.id]
assert_response :created

assert_raises ActiveRecord::RecordNotFound do
post "/properties/#{property.id}/photos/9999999"
end
end

test 'Controller#add_resource with belongs_to' do
photo = create(:photo)
account = create(:account)

post "/photos/#{photo.id}/account/#{account.id}"
assert_equal photo.reload.account_id, account.id
assert_response :created
end

test 'Controller#add_resource with has_one' do
photo = create(:document)
property = create(:property)
post "/properties/#{property.id}/document/#{photo.id}"
assert_equal property.reload.document, photo
assert_response :created
end

test 'Controller#add_resource that is already there' do
photo = create(:photo)
property = create(:property, photos: [photo])
Expand All @@ -19,4 +64,94 @@ class ControllerSubresourceTest < ActionDispatch::IntegrationTest
end
end

# create_resource
test 'Controller#create_resource with has_many' do
property = create(:property, photos: [])
photo = build(:photo)

post "/properties/#{property.id}/photos", params: { photo: photo.attributes }, as: :json
assert_equal property.photos.reload.map(&:id), [JSON.parse(response.body)['id']]
assert_equal property.photos.count, 1
assert_response :created
end

test 'Controller#create_resource with has_and_belongs_to_many' do
photo1 = create(:photo)
photo2 = build(:photo)
property = create(:property, photos: [photo1])

post "/properties/#{property.id}/photos", params: { photo: photo2.attributes }, as: :json
assert_equal property.photos.reload.map(&:id), [photo1.id, JSON.parse(response.body)['id']]
assert_equal property.photos.count, 2
assert_response :created
end

test 'Controller#create_resource with belongs_to' do
photo = create(:photo)
account = build(:account)

post "/photos/#{photo.id}/account", params: { account: account.attributes }, as: :json
assert_equal photo.reload.account_id, JSON.parse(response.body)['id']
assert_response :created
end

test 'Controller#create_resource with has_one' do
account = build(:account)
property = create(:property)
post "/properties/#{property.id}/landlord", params: { landlord: account.attributes }, as: :json
assert_equal property.reload.landlord.id, JSON.parse(response.body)['id']
assert_response :created
end

# remove_resource
test 'Controller#remove_resource' do
photo = create(:photo)
property = create(:property, photos: [photo])
assert_equal property.photos.reload, [photo]
delete "/properties/#{property.id}/photos/#{photo.id}"
assert_equal property.photos.reload, []
assert_response :no_content

assert_raises ActiveRecord::RecordNotFound do
delete "/properties/#{property.id}/photos/9999999"
end
end

test 'Controller#remove_resource with has_one' do
photo = create(:document)
property = create(:property, document: photo)
assert_equal property.document, photo
delete "/properties/#{property.id}/document/#{photo.id}"
assert_nil property.reload.document
assert_response :no_content
end

test 'Controller#remove_resource with belongs_to' do
account = create(:account)
photo = create(:photo, account: account)

delete "/photos/#{photo.id}/account/#{account.id}"
assert_nil photo.reload.account_id
assert_response :no_content
end

test 'Controller#remove_resource with belongs_to unless not match' do
account1 = create(:account)
account2 = create(:account)
photo = create(:photo, account: account1)

delete "/photos/#{photo.id}/account/#{account2.id}"
assert_equal photo.reload.account_id, account1.id
assert_response :not_found
end

test 'Controller#remove_resource with belongs_to unless not match and is nil' do
account = create(:account)
photo = create(:photo)

delete "/photos/#{photo.id}/account/#{account.id}"
assert_nil photo.reload.account_id
assert_response :not_found
end

end
27 changes: 27 additions & 0 deletions test/standard_api/nested_attributes/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@ class BelongsToTest < ActionDispatch::IntegrationTest
photo.reload
assert_nil photo.account
end

# = Errors Test

test 'create record and create invalid nested record' do
@controller = PhotosController.new
post photos_path, params: { photo: { account: {property_id: 999}} }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"account.name": ["can't be blank"]
}
end

test 'update record and create invalid nested record' do
photo = create(:photo)

@controller = PhotosController.new
put photo_path(photo), params: {
photo: { account: {property_id: 999}},
include: :account,
}, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"account.name": ["can't be blank"]
}
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HasAndBelongsToManyTest < ActionDispatch::IntegrationTest
assert_equal 'Beach House', property.name
assert_equal ['image/jpeg'], property.photos.map(&:format)
end

test 'create record and update nested record' do
photo = create(:photo, format: 'image/png')

Expand Down Expand Up @@ -65,6 +65,29 @@ class HasAndBelongsToManyTest < ActionDispatch::IntegrationTest
assert_response :ok
assert_equal [], property.reload.photos
end

# = Errors Test

test 'create record and create invalid nested record' do
@controller = PhotosController.new
post photos_path, params: { photo: { format: 'image/jpeg', properties: [{size: 1000}] } }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"properties.name": ["can't be blank"]
}
end

test 'update record and create invalid nested record' do
photo = create(:photo)
@controller = PhotosController.new
put photo_path(photo), params: { photo: { format: 'image/jpeg', properties: [{size: 1000}] } }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"properties.name": ["can't be blank"]
}
end

end
end
23 changes: 23 additions & 0 deletions test/standard_api/nested_attributes/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ class HasManyTest < ActionDispatch::IntegrationTest
assert_equal account.id, attributes["accounts"][0]["id"]
assert_equal "B Co.", attributes["accounts"][0]["name"]
end

# = Errors Test

test 'create record and create invalid nested record' do
@controller = PropertiesController.new
post properties_path, params: { property: { name: 'Beach House', accounts: [{property_id: 182828}]} }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"accounts.name" => ["can't be blank"]
}
end

test 'update record and create invalid nested record' do
property = create(:property, name: 'Empire State Building')
@controller = PropertiesController.new
put property_path(property), params: { property: { accounts: [{property_id: 182828}]} }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"accounts.name" => ["can't be blank"]
}
end

end
end
25 changes: 25 additions & 0 deletions test/standard_api/nested_attributes/has_one_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ class HasOneTest < ActionDispatch::IntegrationTest
photo.reload
assert_nil photo.camera
end

# = Errors Test

test 'create record and create invalid nested record' do
@controller = PhotosController.new

post photos_path, params: { photo: { camera: {photo_id: 999}} }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"camera.make": ["can't be blank"]
}
end

test 'update record and create invalid nested record' do
photo = create(:photo)

@controller = PhotosController.new
put photo_path(photo), params: { photo: { camera: {photo_id: 999}} }, as: :json

assert_response :bad_request
assert_equal JSON.parse(response.body)["errors"], {
"camera.name": ["can't be blank"]
}
end

end
end
Loading