test/controllers/controller_test.rb in jsonapi-resources-0.1.0 vs test/controllers/controller_test.rb in jsonapi-resources-0.1.1
- old
+ new
@@ -1,8 +1,12 @@
require File.expand_path('../../test_helper', __FILE__)
require File.expand_path('../../fixtures/active_record', __FILE__)
+def set_content_type_header!
+ @request.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
+end
+
class PostsControllerTest < ActionController::TestCase
def test_index
get :index
assert_response :success
assert json_response['posts'].is_a?(Array)
@@ -260,10 +264,11 @@
assert_response :bad_request
assert_match /nil is not a valid field for posts./, json_response['errors'][0]['detail']
end
def test_create_simple
+ set_content_type_header!
post :create,
{
posts: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
@@ -279,10 +284,11 @@
assert_equal 'JR is Great', json_response['posts']['title']
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts']['body']
end
def test_create_link_to_missing_object
+ set_content_type_header!
post :create,
{
posts: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
@@ -296,10 +302,11 @@
# Todo: check if this validation is working
assert_match /author - can't be blank/, response.body
end
def test_create_extra_param
+ set_content_type_header!
post :create,
{
posts: {
asdfg: 'aaaa',
title: 'JR is Great',
@@ -313,10 +320,11 @@
assert_response :bad_request
assert_match /asdfg is not allowed/, response.body
end
def test_create_with_invalid_data
+ set_content_type_header!
post :create,
{
posts: {
title: 'JSONAPIResources is the greatest thing...',
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
@@ -336,10 +344,11 @@
assert_equal "is too long (maximum is 35 characters)", json_response['errors'][1]['detail']
assert_equal "title - is too long (maximum is 35 characters)", json_response['errors'][1]['title']
end
def test_create_multiple
+ set_content_type_header!
post :create,
{
posts: [
{
title: 'JR is Great',
@@ -365,10 +374,11 @@
assert_match /JR is Great/, response.body
assert_match /Ember is Great/, response.body
end
def test_create_multiple_wrong_case
+ set_content_type_header!
post :create,
{
posts: [
{
Title: 'JR is Great',
@@ -390,10 +400,11 @@
assert_response :bad_request
assert_match /Title/, json_response['errors'][0]['detail']
end
def test_create_simple_missing_posts
+ set_content_type_header!
post :create,
{
posts_spelled_wrong: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
@@ -406,10 +417,11 @@
assert_response :bad_request
assert_match /The required parameter, posts, is missing./, json_response['errors'][0]['detail']
end
def test_create_simple_unpermitted_attributes
+ set_content_type_header!
post :create,
{
posts: {
subject: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
@@ -422,10 +434,11 @@
assert_response :bad_request
assert_match /subject/, json_response['errors'][0]['detail']
end
def test_create_with_links
+ set_content_type_header!
post :create,
{
posts: {
title: 'JR is Great',
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
@@ -443,10 +456,11 @@
assert_equal 'JSONAPIResources is the greatest thing since unsliced bread.', json_response['posts']['body']
assert_equal ['3', '4'], json_response['posts']['links']['tags']
end
def test_create_with_links_include_and_fields
+ set_content_type_header!
post :create,
{
posts: {
title: 'JR is Great!',
body: 'JSONAPIResources is the greatest thing since unsliced bread!',
@@ -469,10 +483,11 @@
assert_not_nil json_response['linked']['people']
assert_nil json_response['linked']['tags']
end
def test_update_with_links
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: 3,
@@ -493,10 +508,11 @@
assert_equal 'AAAA', json_response['posts']['body']
assert matches_array?(['3', '4'], json_response['posts']['links']['tags'])
end
def test_update_remove_links
+ set_content_type_header!
put :update,
{
id: 3,
posts: {
title: 'A great new Post',
@@ -515,10 +531,11 @@
assert_equal 'AAAA', json_response['posts']['body']
assert matches_array?([], json_response['posts']['links']['tags'])
end
def test_update_relationship_has_one
+ set_content_type_header!
ruby = Section.find_by(name: 'ruby')
post_object = Post.find(3)
assert_not_equal ruby.id, post_object.section_id
put :update_association, {post_id: 3, association: 'section', sections: ruby.id}
@@ -527,19 +544,21 @@
post_object = Post.find(3)
assert_equal ruby.id, post_object.section_id
end
def test_update_relationship_has_one_singular_param
+ set_content_type_header!
ruby = Section.find_by(name: 'ruby')
post_object = Post.find(3)
put :update_association, {post_id: 3, association: 'section', section: ruby.id}
assert_response :bad_request
end
def test_update_relationship_has_one_singular_param_relation_nil
+ set_content_type_header!
ruby = Section.find_by(name: 'ruby')
post_object = Post.find(3)
post_object.section_id = nil
post_object.save!
@@ -549,10 +568,11 @@
post_object = Post.find(3)
assert_equal ruby.id, post_object.section_id
end
def test_create_relationship_has_one_singular_param_relation_nil
+ set_content_type_header!
ruby = Section.find_by(name: 'ruby')
post_object = Post.find(3)
post_object.section_id = nil
post_object.save!
@@ -562,10 +582,11 @@
post_object = Post.find(3)
assert_equal ruby.id, post_object.section_id
end
def test_create_relationship_has_one_singular_param_relation_not_nil
+ set_content_type_header!
ruby = Section.find_by(name: 'ruby')
js = Section.find_by(name: 'javascript')
post_object = Post.find(3)
post_object.section_id = js.id
post_object.save!
@@ -577,10 +598,11 @@
post_object = Post.find(3)
assert_equal js.id, post_object.section_id
end
def test_update_relationship_has_many_join_table_single
+ set_content_type_header!
put :update_association, {post_id: 3, association: 'tags', tags: []}
assert_response :no_content
post_object = Post.find(3)
assert_equal 0, post_object.tags.length
@@ -599,19 +621,21 @@
assert_equal 1, tags.length
assert matches_array? [5], tags
end
def test_update_relationship_has_many_join_table
+ set_content_type_header!
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
assert_response :no_content
post_object = Post.find(3)
assert_equal 2, post_object.tags.collect { |tag| tag.id }.length
assert matches_array? [2, 3], post_object.tags.collect { |tag| tag.id }
end
def test_create_relationship_has_many_join_table
+ set_content_type_header!
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
assert_response :no_content
post_object = Post.find(3)
assert_equal 2, post_object.tags.collect { |tag| tag.id }.length
@@ -624,17 +648,19 @@
assert_equal 3, post_object.tags.collect { |tag| tag.id }.length
assert matches_array? [2, 3, 5], post_object.tags.collect { |tag| tag.id }
end
def test_create_relationship_has_many_missing_tags
+ set_content_type_header!
post :create_association, {post_id: 3, association: 'tags'}
assert_response :bad_request
assert_match /The required parameter, tags, is missing./, response.body
end
def test_create_relationship_has_many_join_table_record_exists
+ set_content_type_header!
put :update_association, {post_id: 3, association: 'tags', tags: [2, 3]}
assert_response :no_content
post_object = Post.find(3)
assert_equal 2, post_object.tags.collect { |tag| tag.id }.length
@@ -645,24 +671,27 @@
assert_response :bad_request
assert_match /The relation to 2 already exists./, response.body
end
def test_update_relationship_has_one_mismatch_params
+ set_content_type_header!
post :create_association, {post_id: 3, association: 'section', authors: 1}
assert_response :bad_request
assert_match /The required parameter, sections, is missing./, response.body
end
def test_update_relationship_has_many_missing_tags
+ set_content_type_header!
put :update_association, {post_id: 3, association: 'tags'}
assert_response :bad_request
assert_match /The required parameter, tags, is missing./, response.body
end
def test_delete_relationship_has_one
+ set_content_type_header!
ruby = Section.find_by(name: 'ruby')
post :create_association, {post_id: 9, association: 'section', sections: ruby.id}
assert_response :no_content
@@ -673,10 +702,11 @@
post = Post.find(9)
assert_nil post.section
end
def test_delete_relationship_has_many
+ set_content_type_header!
put :update_association, {post_id: 9, association: 'tags', tags: [2, 3]}
assert_response :no_content
p = Post.find(9)
assert_equal [2, 3], p.tag_ids
@@ -686,10 +716,11 @@
assert_response :no_content
assert_equal [2], p.tag_ids
end
def test_delete_relationship_has_many_does_not_exist
+ set_content_type_header!
put :update_association, {post_id: 9, association: 'tags', tags: [2, 3]}
assert_response :no_content
p = Post.find(9)
assert_equal [2, 3], p.tag_ids
@@ -699,10 +730,11 @@
assert_response :not_found
assert_equal [2, 3], p.tag_ids
end
def test_update_mismatched_keys
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: 3,
@@ -719,10 +751,11 @@
assert_response :bad_request
assert_match /The URL does not support the key 2/, response.body
end
def test_update_extra_param
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: 3,
@@ -739,10 +772,11 @@
assert_response :bad_request
assert_match /asdfg is not allowed/, response.body
end
def test_update_extra_param_in_links
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: 3,
@@ -759,10 +793,11 @@
assert_response :bad_request
assert_match /asdfg is not allowed/, response.body
end
def test_update_missing_param
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: 3,
@@ -778,10 +813,11 @@
assert_response :bad_request
assert_match /The required parameter, posts, is missing./, response.body
end
def test_update_multiple
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: [3, 9],
@@ -818,10 +854,11 @@
assert_equal json_response['posts'][1]['body'], 'AAAA'
assert_equal json_response['posts'][1]['links']['tags'], ['3', '4']
end
def test_update_multiple_missing_keys
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: [3, 9],
@@ -845,10 +882,11 @@
assert_response :bad_request
assert_match /A key is required/, response.body
end
def test_update_mismatch_keys
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: [3, 9],
@@ -874,10 +912,11 @@
assert_response :bad_request
assert_match /The URL does not support the key 8/, response.body
end
def test_update_multiple_count_mismatch
+ set_content_type_header!
javascript = Section.find_by(name: 'javascript')
put :update,
{
id: [3, 9, 2],
@@ -903,10 +942,11 @@
assert_response :bad_request
assert_match /Count to key mismatch/, response.body
end
def test_update_unpermitted_attributes
+ set_content_type_header!
put :update,
{
id: 3,
posts: {
subject: 'A great new Post',
@@ -921,10 +961,11 @@
assert_match /author is not allowed./, response.body
assert_match /subject is not allowed./, response.body
end
def test_update_bad_attributes
+ set_content_type_header!
put :update,
{
id: 3,
posts: {
subject: 'A great new Post',
@@ -1084,10 +1125,11 @@
assert json_response['linked']['isoCurrencies'][0].has_key?('name')
refute json_response['linked']['isoCurrencies'][0].has_key?('countryName')
end
def test_create_expense_entries_underscored
+ set_content_type_header!
JSONAPI.configuration.json_key_format = :underscored_key
post :create,
{
expense_entries: {
@@ -1111,10 +1153,11 @@
delete :destroy, {id: json_response['expense_entries']['id']}
assert_response :no_content
end
def test_create_expense_entries_camelized_key
+ set_content_type_header!
JSONAPI.configuration.json_key_format = :camelized_key
post :create,
{
expenseEntries: {
@@ -1138,10 +1181,11 @@
delete :destroy, {id: json_response['expenseEntries']['id']}
assert_response :no_content
end
def test_create_expense_entries_dasherized_key
+ set_content_type_header!
JSONAPI.configuration.json_key_format = :dasherized_key
post :create,
{
'expense-entries' => {
@@ -1285,10 +1329,11 @@
end
end
class PeopleControllerTest < ActionController::TestCase
def test_create_validations
+ set_content_type_header!
post :create,
{
people: {
name: 'Steve Jobs',
email: 'sj@email.zzz',
@@ -1298,10 +1343,11 @@
assert_response :success
end
def test_create_validations_missing_attribute
+ set_content_type_header!
post :create,
{
people: {
email: 'sj@email.zzz'
}
@@ -1314,10 +1360,11 @@
assert_match /date_joined - can't be blank/, response.body
assert_match /name - can't be blank/, response.body
end
def test_update_validations_missing_attribute
+ set_content_type_header!
put :update,
{
id: 3,
people: {
name: ''
@@ -1402,10 +1449,11 @@
assert_equal '2', json_response['breeds'][1]['id']
assert_equal 'Sphinx', json_response['breeds'][1]['name']
end
def test_poro_create_simple
+ set_content_type_header!
post :create,
{
breeds: {
name: 'tabby'
}
@@ -1415,10 +1463,11 @@
assert json_response['breeds'].is_a?(Hash)
assert_equal 'Tabby', json_response['breeds']['name']
end
def test_poro_create_update
+ set_content_type_header!
post :create,
{
breeds: {
name: 'CALIC'
}
@@ -1498,9 +1547,10 @@
assert_response :success
assert_equal "Update This Later - Multiple", json_response['posts'][0]['title']
end
def test_create_simple_namespaced
+ set_content_type_header!
post :create,
{
posts: {
title: 'JR - now with Namespacing',
body: 'JSONAPIResources is the greatest thing since unsliced bread now that it has namespaced resources.',