subroutine 4.4.0 → 5.0.0.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,364 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- module Subroutine
6
- class AssociationTest < TestCase
7
-
8
- def doug
9
- @doug ||= ::User.new(id: 1, email_address: "doug@example.com")
10
- end
11
-
12
- def fred
13
- @fred ||= ::User.new(id: 2, email_address: "fred@example.com")
14
- end
15
-
16
- def murphy
17
- @murphy ||= ::StringIdUser.new(id: "ABACABADABACABA", email_address: "murphy@example.com")
18
- end
19
-
20
- def account
21
- @account ||= ::Account.new(id: 1)
22
- end
23
-
24
- def test_it_sets_accessors_on_init
25
- op = SimpleAssociationOp.new user: doug
26
- assert_equal "User", op.user_type
27
- assert_equal doug.id, op.user_id
28
- end
29
-
30
- def test_it_can_be_nil
31
- op = SimpleAssociationOp.new user: nil
32
- assert_nil op.user
33
- assert_nil op.user_id
34
-
35
- op = SimpleAssociationOp.new
36
- assert_nil op.user
37
- assert_nil op.user_id
38
- end
39
-
40
- def test_it_looks_up_an_association
41
- all_mock = mock
42
-
43
- ::User.expects(:all).returns(all_mock)
44
- all_mock.expects(:find_by!).with(id: 1).returns(doug)
45
-
46
- op = SimpleAssociationOp.new user_type: "User", user_id: doug.id
47
- assert_equal doug, op.user
48
- end
49
-
50
- def test_it_looks_up_an_association_with_string_ids
51
- all_mock = mock
52
-
53
- ::StringIdUser.expects(:all).returns(all_mock)
54
- all_mock.expects(:find_by!).with(id: "ABACABADABACABA").returns(murphy)
55
-
56
- op = ::SimpleAssociationWithStringIdOp.new(string_id_user_id: murphy.id)
57
- assert_equal murphy, op.string_id_user
58
- end
59
-
60
- def test_it_sanitizes_types
61
- all_mock = mock
62
-
63
- ::User.expects(:all).returns(all_mock)
64
- all_mock.expects(:find_by!).with(id: 1).returns(doug)
65
-
66
- op = SimpleAssociationOp.new user_id: doug.id
67
- assert_equal doug, op.user
68
- end
69
-
70
- def test_it_allows_an_association_to_be_looked_up_without_default_scoping
71
- all_mock = mock
72
- unscoped_mock = mock
73
-
74
- ::User.expects(:all).returns(all_mock)
75
- all_mock.expects(:unscoped).returns(unscoped_mock)
76
- unscoped_mock.expects(:find_by!).with(id: 1).returns(doug)
77
-
78
- op = UnscopedSimpleAssociationOp.new user_id: doug.id
79
- assert_equal doug, op.user
80
- end
81
-
82
- def test_it_allows_polymorphic_associations
83
- all_mock = mock
84
- ::User.expects(:all).never
85
- ::AdminUser.expects(:all).returns(all_mock)
86
- all_mock.expects(:find_by!).with(id: 1).returns(doug)
87
-
88
- op = PolymorphicAssociationOp.new(admin_type: "AdminUser", admin_id: doug.id)
89
- assert_equal doug, op.admin
90
- end
91
-
92
- def test_it_allows_the_class_to_be_set
93
- op = ::AssociationWithClassOp.new(admin: doug)
94
- assert_equal "AdminUser", op.admin_type
95
- end
96
-
97
- def test_it_allows_foreign_key_to_be_set
98
- all_mock = mock
99
- ::User.expects(:all).returns(all_mock)
100
- all_mock.expects(:find_by!).with(id: 10).returns(doug)
101
-
102
- op = ::AssociationWithForeignKeyOp.new(owner_id: 10)
103
- assert_equal doug, op.user
104
- assert_equal "owner_id", op.field_configurations[:user][:foreign_key]
105
- end
106
-
107
- def test_the_foreign_key_is_cast
108
- all_mock = mock
109
- ::User.expects(:all).returns(all_mock)
110
- all_mock.expects(:find_by!).with(id: 10).returns(doug)
111
-
112
- op = ::AssociationWithForeignKeyOp.new(owner_id: "10")
113
- assert_equal doug, op.user
114
- assert_equal 10, op.owner_id
115
- assert_equal "owner_id", op.field_configurations[:user][:foreign_key]
116
- end
117
-
118
- def test_it_allows_a_foreign_key_and_find_by_to_be_set
119
- all_mock = mock
120
- ::User.expects(:all).returns(all_mock)
121
- all_mock.expects(:find_by!).with(email_address: "foo@bar.com").returns(doug)
122
-
123
- op = ::AssociationWithFindByAndForeignKeyOp.new(email_address: "foo@bar.com")
124
- assert_equal doug, op.user
125
- assert_equal "foo@bar.com", op.email_address
126
- assert_equal "email_address", op.field_configurations[:user][:find_by]
127
- end
128
-
129
- def test_it_allows_a_find_by_to_be_set
130
- all_mock = mock
131
- ::User.expects(:all).returns(all_mock)
132
- all_mock.expects(:find_by!).with(email_address: doug.email_address).returns(doug)
133
-
134
- op = ::AssociationWithFindByKeyOp.new(user_id: doug.email_address)
135
- assert_equal doug, op.user
136
- assert_equal "email_address", op.field_configurations[:user][:find_by]
137
- end
138
-
139
- def test_it_allows_a_find_by_to_be_set_with_implicit_string
140
- all_mock = mock
141
- ::User.expects(:all).returns(all_mock)
142
- all_mock.expects(:find_by!).with(email_address: doug.email_address).returns(doug)
143
-
144
- op = ::AssociationWithImplicitStringFindByOp.new(user_id: doug.email_address)
145
- assert_equal doug, op.user
146
- assert_equal "email_address", op.field_configurations[:user][:find_by]
147
- end
148
-
149
- def test_values_are_correct_for_find_by_usage
150
- op = ::AssociationWithFindByKeyOp.new(user: doug)
151
- assert_equal doug, op.user
152
- assert_equal doug.email_address, op.user_id
153
- end
154
-
155
- def test_values_are_correct_for_foreign_key_usage
156
- op = ::AssociationWithForeignKeyOp.new(user: doug)
157
- assert_equal doug, op.user
158
- assert_equal doug.id, op.owner_id
159
- end
160
-
161
- def test_values_are_correct_for_both_foreign_key_and_find_by_usage
162
- op = ::AssociationWithFindByAndForeignKeyOp.new(user: doug)
163
- assert_equal doug, op.user
164
- assert_equal doug.email_address, op.email_address
165
- assert_equal false, op.respond_to?(:user_id)
166
- end
167
-
168
- def test_it_inherits_associations_via_fields_from
169
- all_mock = mock
170
-
171
- ::User.expects(:all).returns(all_mock)
172
- all_mock.expects(:find_by!).with(id: 1).returns(doug)
173
-
174
- op = ::InheritedSimpleAssociation.new(user_type: "User", user_id: doug.id)
175
- assert_equal doug, op.user
176
- assert_equal "User", op.user_type
177
- assert_equal doug.id, op.user_id
178
- end
179
-
180
- def test_it_inherits_associations_via_fields_from_and_preserves_options
181
- all_mock = mock
182
- unscoped_mock = mock
183
-
184
- ::User.expects(:all).returns(all_mock)
185
- all_mock.expects(:unscoped).returns(unscoped_mock)
186
- unscoped_mock.expects(:find_by!).with(id: 1).returns(doug)
187
-
188
- op = ::InheritedUnscopedAssociation.new(user_type: "User", user_id: doug.id)
189
- assert_equal doug, op.user
190
- assert_equal "User", op.user_type
191
- assert_equal doug.id, op.user_id
192
- end
193
-
194
- def test_it_inherits_polymorphic_associations_via_fields_from
195
- all_mock = mock
196
- ::User.expects(:all).never
197
- ::AdminUser.expects(:all).returns(all_mock)
198
- all_mock.expects(:find_by!).with(id: 1).returns(doug)
199
-
200
- op = ::InheritedPolymorphicAssociationOp.new(admin_type: "AdminUser", admin_id: doug.id)
201
- assert_equal doug, op.admin
202
- assert_equal "AdminUser", op.admin_type
203
- assert_equal doug.id, op.admin_id
204
- end
205
-
206
- def test_it_provides_answers_to_field_provided
207
- ::User.expects(:all).never
208
-
209
- op = SimpleAssociationOp.new user: doug
210
- assert_equal true, op.field_provided?(:user)
211
- assert_equal true, op.field_provided?(:user_id)
212
- assert_equal false, op.field_provided?(:user_type)
213
-
214
- op = SimpleAssociationOp.new user_id: doug.id
215
- assert_equal true, op.field_provided?(:user)
216
- assert_equal true, op.field_provided?(:user_id)
217
- assert_equal false, op.field_provided?(:user_type)
218
-
219
- op = SimpleAssociationOp.new
220
- assert_equal false, op.field_provided?(:user)
221
- assert_equal false, op.field_provided?(:user_id)
222
- assert_equal false, op.field_provided?(:user_type)
223
-
224
- op = PolymorphicAssociationOp.new admin: doug
225
- assert_equal true, op.field_provided?(:admin)
226
- assert_equal true, op.field_provided?(:admin_id)
227
- assert_equal true, op.field_provided?(:admin_type)
228
-
229
- op = PolymorphicAssociationOp.new admin_type: doug.class.name, admin_id: doug.id
230
- assert_equal true, op.field_provided?(:admin)
231
- assert_equal true, op.field_provided?(:admin_id)
232
- assert_equal true, op.field_provided?(:admin_type)
233
-
234
- op = PolymorphicAssociationOp.new admin_type: doug.class.name, admin_id: doug.id.to_s
235
- assert_equal true, op.field_provided?(:admin)
236
- assert_equal true, op.field_provided?(:admin_id)
237
- assert_equal true, op.field_provided?(:admin_type)
238
-
239
- op = PolymorphicAssociationOp.new admin_id: doug.id
240
- assert_equal false, op.field_provided?(:admin)
241
- assert_equal true, op.field_provided?(:admin_id)
242
- assert_equal false, op.field_provided?(:admin_type)
243
-
244
- op = PolymorphicAssociationOp.new
245
- assert_equal false, op.field_provided?(:admin)
246
- assert_equal false, op.field_provided?(:admin_id)
247
- assert_equal false, op.field_provided?(:admin_type)
248
- end
249
-
250
- def test_it_ensures_the_correct_type_of_resource_is_provded_to_an_association
251
- op = SimpleAssociationOp.new
252
- assert_raises ::Subroutine::AssociationFields::AssociationTypeMismatchError do
253
- op.user = account
254
- end
255
- end
256
-
257
- def test_params_does_not_contain_association_key_if_not_provided
258
- op = SimpleAssociationOp.new
259
- assert_equal [], op.params.keys
260
- end
261
-
262
- def test_getting_does_not_set_provided
263
- op = SimpleAssociationOp.new
264
- op.user
265
- assert_equal false, op.field_provided?(:user)
266
- assert_equal false, op.field_provided?(:user_id)
267
- assert_equal false, op.field_provided?(:user_type)
268
-
269
- op.user_id
270
- assert_equal false, op.field_provided?(:user)
271
- assert_equal false, op.field_provided?(:user_id)
272
- assert_equal false, op.field_provided?(:user_type)
273
-
274
- op.user_type
275
- assert_equal false, op.field_provided?(:user)
276
- assert_equal false, op.field_provided?(:user_id)
277
- assert_equal false, op.field_provided?(:user_type)
278
- end
279
-
280
- def test_association_class_names_can_be_declared_as_classes
281
- klass = Class.new(OpWithAssociation) do
282
- association :user, class_name: Account
283
- end
284
-
285
- account = ::Account.new(id: 1)
286
- op = klass.new(user: account)
287
- assert_equal account, op.user
288
- end
289
-
290
- def test_params_only_contain_the_id_and_type_of_associations
291
- user = ::User.new(id: 1)
292
- op = SimpleAssociationOp.new(user: user)
293
- op.user
294
- assert_equal({ "user_id" => 1 }, op.params)
295
-
296
- op = PolymorphicAssociationOp.new(admin: user)
297
- op.admin
298
- assert_equal({ "admin_id" => 1, "admin_type" => "User" }, op.params)
299
- end
300
-
301
- def test_params_id_type_as_integer_for_polymorphic_associations
302
- user = ::User.new(id: 1)
303
-
304
- op = PolymorphicAssociationOp.new(admin_id: user.id)
305
- op.admin
306
- assert_equal({ "admin_id" => 1 }, op.params)
307
-
308
- op = PolymorphicAssociationOp.new(admin_id: user.id.to_s)
309
- op.admin
310
- assert_equal({ "admin_id" => 1 }, op.params)
311
- end
312
-
313
- def test_params_can_be_accessed_with_associations_loaded
314
- user = User.new(id: 1)
315
- op = SimpleAssociationOp.new(user: user)
316
-
317
- assert_equal({ "user_id" => 1 }, op.params)
318
- assert_equal({ "user" => user }, op.params_with_associations)
319
-
320
- op = PolymorphicAssociationOp.new(admin: user)
321
- assert_equal({ "admin_id" => 1, "admin_type" => "User" }, op.params)
322
- assert_equal({ "admin" => user }, op.params_with_associations)
323
- end
324
-
325
- def test_groups_are_preserved_to_association_components
326
- user = ::User.new(id: 1)
327
- op = GroupedParamAssociationOp.new(user: user)
328
- assert_equal({ "user_id" => 1 }, op.params)
329
- assert_equal({ "user_id" => 1 }, op.info_params)
330
- assert_equal({}, op.without_info_params)
331
-
332
- op = GroupedPolymorphicParamAssociationOp.new(user: user)
333
- assert_equal({ "user_id" => 1, "user_type" => "User" }, op.params)
334
- assert_equal({ "user_id" => 1, "user_type" => "User" }, op.info_params)
335
- assert_equal({}, op.without_info_params)
336
- end
337
-
338
- def test_find_by_is_used_if_raise_on_miss_is_false
339
- all_mock = mock
340
-
341
- ::User.expects(:all).returns(all_mock)
342
- all_mock.expects(:find_by).with(id: 1).returns(nil)
343
-
344
- op = SafeAssociationOp.new user_type: "User", user_id: 1
345
- assert_nil op.user
346
- end
347
-
348
- def test_inheritable_options_are_inherited_by_child_fields
349
- Subroutine.stubs(:inheritable_field_options).returns(%i[foo bar])
350
- klass = Class.new(OpWithAssociation) do
351
- association :buster, foo: true, bar: false, baz: true
352
- association :mister, polymorphic: true, foo: true, bar: false, baz: true
353
- end
354
-
355
- %i[buster_id mister_id mister_type].each do |field_name|
356
- field = klass.field_configurations.fetch(field_name)
357
- assert_equal true, field[:foo]
358
- assert_equal false, field[:bar]
359
- assert_equal false, field.config.key?(:baz)
360
- end
361
- end
362
-
363
- end
364
- end
@@ -1,148 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- module Subroutine
6
- class AuthTest < TestCase
7
-
8
- def user
9
- @user ||= ::User.new(id: 4, email_address: "doug@example.com")
10
- end
11
-
12
- def test_it_throws_an_error_if_authorization_is_not_defined
13
- assert_raises ::Subroutine::Auth::AuthorizationNotDeclaredError do
14
- MissingAuthOp.new
15
- end
16
- end
17
-
18
- def test_it_throws_an_error_if_require_user_but_none_is_provided
19
- assert_raises ::Subroutine::Auth::NotAuthorizedError do
20
- RequireUserOp.submit!
21
- end
22
- end
23
-
24
- def test_it_does_not_throw_an_error_if_require_user_but_none_is_provided
25
- RequireUserOp.submit! user
26
- end
27
-
28
- def test_it_allows_an_id_to_be_passed
29
- ::User.expects(:find).with(user.id).returns(user)
30
- op = RequireUserOp.submit! user.id
31
- assert_equal op.current_user, user
32
- end
33
-
34
- def test_it_throws_an_error_if_require_no_user_but_one_is_present
35
- assert_raises ::Subroutine::Auth::NotAuthorizedError do
36
- RequireNoUserOp.submit! user
37
- end
38
- end
39
-
40
- def test_it_does_not_throw_an_error_if_require_no_user_and_none_is_provided
41
- RequireNoUserOp.submit!
42
- end
43
-
44
- def test_it_does_not_throw_an_error_if_no_user_requirements_and_one_is_provided
45
- NoUserRequirementsOp.submit! user
46
- end
47
-
48
- def test_it_does_not_throw_an_error_if_no_user_requirements_and_none_is_provided
49
- NoUserRequirementsOp.submit!
50
- end
51
-
52
- def test_it_runs_custom_authorizations
53
- CustomAuthorizeOp.submit! user
54
-
55
- assert_raises ::Subroutine::Auth::NotAuthorizedError do
56
- CustomAuthorizeOp.submit! User.new(email_address: "foo@bar.com")
57
- end
58
- end
59
-
60
- def test_authorization_checks_are_registered_on_the_class
61
- assert_equal false, MissingAuthOp.authorization_declared?
62
-
63
- assert_equal true, CustomAuthorizeOp.authorization_declared?
64
- assert_equal [:authorize_user_required, :authorize_user_is_correct], CustomAuthorizeOp.authorization_checks
65
-
66
- assert_equal true, NoUserRequirementsOp.authorization_declared?
67
- assert_equal [:authorize_user_not_required], NoUserRequirementsOp.authorization_checks
68
- end
69
-
70
- def test_the_current_user_can_be_defined_by_an_id
71
- user = CustomAuthorizeOp.new(1).current_user
72
- assert_equal 1, user.id
73
- assert_equal true, user.is_a?(::User)
74
- assert_equal false, user.is_a?(::AdminUser)
75
- end
76
-
77
- def test_the_user_class_can_be_overridden
78
- user = DifferentUserClassOp.new(1).current_user
79
- assert_equal 1, user.id
80
- assert_equal true, user.is_a?(::AdminUser)
81
- end
82
-
83
- def test_another_class_cant_be_used_as_the_user
84
- assert_raises "current_user must be one of the following types {AdminUser,Integer,NilClass} but was String" do
85
- DifferentUserClassOp.new("doug")
86
- end
87
- end
88
-
89
- def test_it_does_not_run_authorizations_if_explicitly_bypassed
90
- op = CustomAuthorizeOp.new User.new(email_address: "foo@bar.com")
91
- op.skip_auth_checks!
92
- op.submit!
93
- end
94
-
95
- def test_it_runs_policies_as_part_of_authorization
96
- assert_raises ::Subroutine::Auth::NotAuthorizedError do
97
- PolicyOp.submit! user
98
- end
99
-
100
- op = PolicyOp.new
101
- op.skip_auth_checks!
102
- op.submit!
103
- end
104
-
105
- def policy_invocations_are_registered_as_authorization_methods
106
- assert PolicyOp.authorization_checks.include?(:authorize_policy_user_can_access)
107
- end
108
-
109
- def test_it_runs_policies_with_conditionals
110
- # if: false
111
- op = IfConditionalPolicyOp.new(user, check_policy: false)
112
- assert op.submit!
113
- # unless: true
114
- op = UnlessConditionalPolicyOp.new(user, unless_check_policy: true)
115
- assert op.submit!
116
-
117
- # if: true
118
- op = IfConditionalPolicyOp.new(user, check_policy: true)
119
- assert_raises ::Subroutine::Auth::NotAuthorizedError do
120
- op.submit!
121
- end
122
-
123
- # unless: false
124
- op = UnlessConditionalPolicyOp.new(user, unless_check_policy: false)
125
- assert_raises ::Subroutine::Auth::NotAuthorizedError do
126
- op.submit!
127
- end
128
- end
129
-
130
- def test_current_user_is_not_called_by_constructor
131
- ::User.expects(:find).never
132
- RequireUserOp.new(user.id)
133
- end
134
-
135
- def test_actioncontroller_parameters_can_be_provided
136
- raw_params = { some_input: "foobarbaz" }.with_indifferent_access
137
- params = ::ActionController::Parameters.new(raw_params)
138
- op = RequireUserOp.new(user, params)
139
- op.submit!
140
-
141
- assert_equal "foobarbaz", op.some_input
142
-
143
- assert_equal raw_params, op.params
144
- assert_equal user, op.current_user
145
- end
146
-
147
- end
148
- end