forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create assert_response_schema test helper
It is a common pattern to use JSON Schema to validate a API response[1], [2] and [3]. This patch creates the `assert_response_schema` test helper that helps people do this kind of validation easily on the controller tests. [1]: https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher [2]: https://github.com/sharethrough/json-schema-rspec [3]: rails-api#1011 (comment)
- Loading branch information
1 parent
fce856f
commit b8a9f97
Showing
12 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# How to test | ||
|
||
## Test helpers | ||
|
||
AMS provides a `assert_response_schema` method to be used on your controller tests to | ||
assert the response against a [JSON Schema](http://json-schema.org/). Let's take | ||
a look in a example. | ||
|
||
```ruby | ||
class PostsController < ApplicationController | ||
def show | ||
@post = Post.find(params[:id]) | ||
|
||
render json: @post | ||
end | ||
end | ||
``` | ||
|
||
To test the `posts#show` response of this controller we need to create a file in | ||
`test/support/schemas/posts/show.json` the helper uses a convention to the name | ||
of the file. | ||
|
||
This file is a JSON Schema representation of our response. | ||
|
||
```json | ||
{ | ||
"properties": { | ||
"title" : { "type" : "string" }, | ||
"content" : { "type" : "string" } | ||
} | ||
} | ||
``` | ||
|
||
With all in place we can go to our test and use the helper. | ||
|
||
```ruby | ||
class PostsControllerTest < ActionController::TestCase | ||
test "should render right response" do | ||
get :index | ||
assert_response_schema | ||
end | ||
end | ||
``` | ||
|
||
### Load a custom schema | ||
|
||
If we need to use other schema, for example when we have a namespaced API that | ||
shows the same response, we can pass the path of the schema. | ||
|
||
```ruby | ||
module V1 | ||
class PostsController < ApplicationController | ||
def show | ||
@post = Post.find(params[:id]) | ||
|
||
render json: @post | ||
end | ||
end | ||
end | ||
``` | ||
|
||
```ruby | ||
class V1::PostsControllerTest < ActionController::TestCase | ||
test "should render right response" do | ||
get :index | ||
assert_response_schema('posts/show.json') | ||
end | ||
end | ||
``` | ||
### Change the schema path | ||
|
||
By default all schemas are created at `test/support/schemas` if we are using | ||
RSpec for example we can change this to `spec/support/schemas` defining the | ||
default schema path in a initializer. | ||
|
||
```ruby | ||
ActiveModel::Serializer.config.schema_path = `spec/support/schemas` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'json-schema' | ||
|
||
module ActiveModel | ||
class Serializer | ||
module Assertions | ||
def assert_response_schema(schema_path = nil) | ||
controller_path = response.request.filtered_parameters[:controller] | ||
action = response.request.filtered_parameters[:action] | ||
schema_directory = ActiveModel::Serializer.config.schema_path | ||
schema_path ||= "#{controller_path}/#{action}.json" | ||
schema_full_path = "#{schema_directory}/#{schema_path}" | ||
JSON::Validator.validate!(schema_full_path, response.body, strict: true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class AssertionsTest < ActionController::TestCase | ||
class MyController < ActionController::Base | ||
def index | ||
render json: Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1') | ||
end | ||
|
||
def show | ||
index | ||
end | ||
end | ||
|
||
tests MyController | ||
|
||
def test_that_assert_with_a_valid_schema | ||
get :index | ||
assert_response_schema | ||
end | ||
|
||
def test_that_raises_a_json_schema_with_a_invalid_schema | ||
get :show | ||
assert_raises JSON::Schema::ValidationError do | ||
assert_response_schema | ||
end | ||
end | ||
|
||
def test_that_assert_with_a_custom_schema | ||
get :show | ||
assert_response_schema('custom/show.json') | ||
end | ||
|
||
def test_that_assert_with_a_custom_schema_directory | ||
original_schema_path = ActiveModel::Serializer.config.schema_path | ||
ActiveModel::Serializer.config.schema_path = 'test/support/custom_schemas' | ||
|
||
get :index | ||
assert_response_schema | ||
|
||
ActiveModel::Serializer.config.schema_path = original_schema_path | ||
end | ||
end | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
test/support/custom_schemas/active_model/serializer/assertions_test/my/index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"properties": { | ||
"name" : { "type" : "string" }, | ||
"description" : { "type" : "string" } | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
test/support/schemas/active_model/serializer/assertions_test/my/index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"properties": { | ||
"name" : { "type" : "string" }, | ||
"description" : { "type" : "string" } | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/support/schemas/active_model/serializer/assertions_test/my/show.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"properties": { | ||
"name" : { "type" : "string" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"properties": { | ||
"name" : { "type" : "string" }, | ||
"description" : { "type" : "string" } | ||
} | ||
} |