Skip to content

Commit be2912f

Browse files
committed
Add check_include tests
1 parent 3330e77 commit be2912f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/jsonapi/request_parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def check_include(resource_klass, include_parts)
341341
else
342342
fail JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type), include_parts.first)
343343
end
344+
true
344345
end
345346

346347
def parse_include_directives(resource_klass, raw_include)

test/unit/jsonapi_request/jsonapi_request_test.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,72 @@ def test_parse_blank_includes
4747
assert_empty include_directives.model_includes
4848
end
4949

50+
def test_check_include_allowed
51+
assert JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "isoCurrency".partition('.'))
52+
end
53+
54+
def test_check_nested_include_allowed
55+
assert JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "employee.expenseEntries".partition('.'))
56+
end
57+
58+
def test_check_include_relationship_does_not_exist
59+
assert_raises JSONAPI::Exceptions::InvalidInclude do
60+
assert JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "foo".partition('.'))
61+
end
62+
end
63+
64+
def test_check_nested_include_relationship_does_not_exist_wrong_format
65+
assert_raises JSONAPI::Exceptions::InvalidInclude do
66+
assert JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "employee.expense-entries".partition('.'))
67+
end
68+
end
69+
70+
def test_check_include_has_one_not_allowed_default
71+
assert JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "isoCurrency".partition('.'))
72+
JSONAPI.configuration.default_allow_include_to_one = false
73+
74+
assert_raises JSONAPI::Exceptions::InvalidInclude do
75+
JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "isoCurrency".partition('.'))
76+
end
77+
ensure
78+
JSONAPI.configuration.default_allow_include_to_one = true
79+
end
80+
81+
def test_check_include_has_one_not_allowed_resource
82+
assert JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "isoCurrency".partition('.'))
83+
ExpenseEntryResource._relationship(:iso_currency).allow_include = false
84+
85+
assert_raises JSONAPI::Exceptions::InvalidInclude do
86+
JSONAPI::RequestParser.new.check_include(ExpenseEntryResource, "isoCurrency".partition('.'))
87+
end
88+
ensure
89+
ExpenseEntryResource._relationship(:iso_currency).allow_include = nil
90+
end
91+
92+
def test_check_include_has_many_not_allowed_default
93+
JSONAPI.configuration.default_allow_include_to_many = true
94+
95+
assert JSONAPI::RequestParser.new.check_include(EmployeeResource, "expenseEntries".partition('.'))
96+
JSONAPI.configuration.default_allow_include_to_many = false
97+
98+
assert_raises JSONAPI::Exceptions::InvalidInclude do
99+
JSONAPI::RequestParser.new.check_include(EmployeeResource, "expenseEntries".partition('.'))
100+
end
101+
ensure
102+
JSONAPI.configuration.default_allow_include_to_many = true
103+
end
104+
105+
def test_check_include_has_many_not_allowed_relationship
106+
assert JSONAPI::RequestParser.new.check_include(EmployeeResource, "expenseEntries".partition('.'))
107+
EmployeeResource._relationship(:expense_entries).allow_include = false
108+
109+
assert_raises JSONAPI::Exceptions::InvalidInclude do
110+
JSONAPI::RequestParser.new.check_include(EmployeeResource, "expenseEntries".partition('.'))
111+
end
112+
ensure
113+
EmployeeResource._relationship(:expense_entries).allow_include = nil
114+
end
115+
50116
def test_parse_dasherized_with_dasherized_include
51117
params = ActionController::Parameters.new(
52118
{

0 commit comments

Comments
 (0)