subroutine 0.10.0.beta → 0.10.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  module Subroutine
6
6
  class AuthTest < TestCase
7
7
 
8
8
  def user
9
- @user ||= ::User.new(email_address: 'doug@example.com')
9
+ @user ||= ::User.new(id: 4, email_address: "doug@example.com")
10
10
  end
11
11
 
12
12
  def test_it_throws_an_error_if_authorization_is_not_defined
@@ -25,6 +25,12 @@ module Subroutine
25
25
  RequireUserOp.submit! user
26
26
  end
27
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
+
28
34
  def test_it_throws_an_error_if_require_no_user_but_one_is_present
29
35
  assert_raises ::Subroutine::Auth::NotAuthorizedError do
30
36
  RequireNoUserOp.submit! user
@@ -47,7 +53,7 @@ module Subroutine
47
53
  CustomAuthorizeOp.submit! user
48
54
 
49
55
  assert_raises ::Subroutine::Auth::NotAuthorizedError do
50
- CustomAuthorizeOp.submit! User.new(email_address: 'foo@bar.com')
56
+ CustomAuthorizeOp.submit! User.new(email_address: "foo@bar.com")
51
57
  end
52
58
  end
53
59
 
@@ -71,7 +77,7 @@ module Subroutine
71
77
  end
72
78
 
73
79
  def test_it_does_not_run_authorizations_if_explicitly_bypassed
74
- op = CustomAuthorizeOp.new User.new(email_address: 'foo@bar.com')
80
+ op = CustomAuthorizeOp.new User.new(email_address: "foo@bar.com")
75
81
  op.skip_auth_checks!
76
82
  op.submit!
77
83
  end
@@ -106,5 +112,6 @@ module Subroutine
106
112
  op.submit!
107
113
  end
108
114
  end
115
+
109
116
  end
110
117
  end
@@ -1,24 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'test_helper'
3
+ require "test_helper"
4
4
 
5
5
  module Subroutine
6
6
  class OpTest < TestCase
7
+
7
8
  def test_simple_fields_definition
8
9
  op = ::SignupOp.new
9
- assert_equal [:email, :password], op._fields.keys.sort
10
+ assert_equal %i[email password], op.field_configurations.keys.sort
10
11
  end
11
12
 
12
13
  def test_inherited_fields
13
14
  op = ::AdminSignupOp.new
14
- assert_equal [:email, :password, :privileges], op._fields.keys.sort
15
+ assert_equal %i[email password privileges], op.field_configurations.keys.sort
15
16
  end
16
17
 
17
18
  def test_class_attribute_usage
18
19
  assert ::AdminSignupOp < ::SignupOp
19
20
 
20
- sid = ::SignupOp._fields.object_id
21
- bid = ::AdminSignupOp._fields.object_id
21
+ sid = ::SignupOp.field_configurations.object_id
22
+ bid = ::AdminSignupOp.field_configurations.object_id
22
23
 
23
24
  refute_equal sid, bid
24
25
  end
@@ -26,8 +27,8 @@ module Subroutine
26
27
  def test_inputs_from_inherited_fields_without_inheriting_from_the_class
27
28
  refute ::BusinessSignupOp < ::SignupOp
28
29
 
29
- user_fields = ::SignupOp._fields.keys
30
- biz_fields = ::BusinessSignupOp._fields.keys
30
+ user_fields = ::SignupOp.field_configurations.keys
31
+ biz_fields = ::BusinessSignupOp.field_configurations.keys
31
32
 
32
33
  user_fields.each do |field|
33
34
  assert_includes biz_fields, field
@@ -36,28 +37,28 @@ module Subroutine
36
37
 
37
38
  def test_inputs_from_ignores_except_fields
38
39
  op = ::ExceptFooBarOp.new
39
- refute op._fields.key?(:foo)
40
- refute op._fields.key?(:bar)
41
- assert_equal [:baz], op._fields.keys.sort
40
+ refute op.field_configurations.key?(:foo)
41
+ refute op.field_configurations.key?(:bar)
42
+ assert_equal [:baz], op.field_configurations.keys.sort
42
43
  end
43
44
 
44
45
  def test_inputs_from_only_fields
45
46
  op = ::OnlyFooBarOp.new
46
- assert op._fields.key?(:foo)
47
- assert op._fields.key?(:bar)
48
- refute_equal [:baz], op._fields.keys.sort
47
+ assert op.field_configurations.key?(:foo)
48
+ assert op.field_configurations.key?(:bar)
49
+ refute_equal [:baz], op.field_configurations.keys.sort
49
50
  end
50
51
 
51
52
  def test_defaults_declaration_options
52
53
  op = ::DefaultsOp.new
53
- assert_equal 'foo', op.foo
54
- assert_equal 'bar', op.bar
54
+ assert_equal "foo", op.foo
55
+ assert_equal "bar", op.bar
55
56
  assert_equal false, op.baz
56
57
  end
57
58
 
58
59
  def test_inherited_defaults_override_correctly
59
60
  op = ::InheritedDefaultsOp.new
60
- assert_equal 'barstool', op.bar
61
+ assert_equal "barstool", op.bar
61
62
  end
62
63
 
63
64
  def test_accessors_are_created
@@ -85,10 +86,10 @@ module Subroutine
85
86
 
86
87
  assert_nil op.email
87
88
  assert_nil op.password
88
- assert_equal 'min', op.privileges
89
+ assert_equal "min", op.privileges
89
90
 
90
- op.privileges = 'max'
91
- assert_equal 'max', op.privileges
91
+ op.privileges = "max"
92
+ assert_equal "max", op.privileges
92
93
  end
93
94
 
94
95
  def test_validations_are_evaluated_before_perform_is_invoked
@@ -102,18 +103,18 @@ module Subroutine
102
103
  end
103
104
 
104
105
  def test_validation_errors_can_be_inherited_and_transformed
105
- op = ::AdminSignupOp.new(email: 'foo@bar.com', password: 'password123')
106
+ op = ::AdminSignupOp.new(email: "foo@bar.com", password: "password123")
106
107
 
107
108
  refute op.submit
108
109
 
109
110
  assert op.perform_called
110
111
  refute op.perform_finished
111
112
 
112
- assert_equal ['has gotta be @admin.com'], op.errors[:email]
113
+ assert_equal ["has gotta be @admin.com"], op.errors[:email]
113
114
  end
114
115
 
115
116
  def test_when_valid_perform_completes_it_returns_control
116
- op = ::SignupOp.new(email: 'foo@bar.com', password: 'password123')
117
+ op = ::SignupOp.new(email: "foo@bar.com", password: "password123")
117
118
  op.submit!
118
119
 
119
120
  assert op.perform_called
@@ -121,11 +122,11 @@ module Subroutine
121
122
 
122
123
  u = op.created_user
123
124
 
124
- assert_equal 'foo@bar.com', u.email_address
125
+ assert_equal "foo@bar.com", u.email_address
125
126
  end
126
127
 
127
128
  def test_it_raises_an_error_when_used_with_a_bang_and_performing_or_validation_fails
128
- op = ::SignupOp.new(email: 'foo@bar.com')
129
+ op = ::SignupOp.new(email: "foo@bar.com")
129
130
 
130
131
  err = assert_raises ::Subroutine::Failure do
131
132
  op.submit!
@@ -134,6 +135,16 @@ module Subroutine
134
135
  assert_equal "Password can't be blank", err.message
135
136
  end
136
137
 
138
+ def test_uses_failure_class_to_raise_error
139
+ op = ::CustomFailureClassOp.new
140
+
141
+ err = assert_raises ::CustomFailureClassOp::Failure do
142
+ op.submit!
143
+ end
144
+
145
+ assert_equal "Will never work", err.message
146
+ end
147
+
137
148
  def test_the_result_of_perform_doesnt_matter
138
149
  op = ::FalsePerformOp.new
139
150
  assert op.submit!
@@ -147,82 +158,82 @@ module Subroutine
147
158
  SignupOp.submit!
148
159
  end
149
160
 
150
- op = SignupOp.submit! email: 'foo@bar.com', password: 'password123'
151
- assert_equal 'foo@bar.com', op.created_user.email_address
161
+ op = SignupOp.submit! email: "foo@bar.com", password: "password123"
162
+ assert_equal "foo@bar.com", op.created_user.email_address
152
163
  end
153
164
 
154
165
  def test_it_sets_the_params_and_defaults_immediately
155
- op = ::AdminSignupOp.new(email: 'foo')
166
+ op = ::AdminSignupOp.new(email: "foo")
156
167
  assert_equal({
157
- 'email' => 'foo',
158
- 'privileges' => 'min',
159
- }, op.params)
168
+ "email" => "foo",
169
+ "privileges" => "min",
170
+ }, op.params)
160
171
 
161
172
  assert_equal({
162
- 'privileges' => 'min'
163
- }, op.defaults)
173
+ "privileges" => "min",
174
+ }, op.defaults)
164
175
  end
165
176
 
166
177
  def test_it_allows_defaults_to_be_overridden
167
- op = ::AdminSignupOp.new(email: 'foo', privileges: nil)
178
+ op = ::AdminSignupOp.new(email: "foo", privileges: nil)
168
179
 
169
180
  assert_equal({
170
- 'email' => 'foo',
171
- 'privileges' => nil
172
- }, op.params)
181
+ "email" => "foo",
182
+ "privileges" => nil,
183
+ }, op.params)
173
184
 
174
- assert_equal({"privileges" => "min"}, op.defaults)
185
+ assert_equal({ "privileges" => "min" }, op.defaults)
175
186
  end
176
187
 
177
188
  def test_it_overriding_default_does_not_alter_default
178
- op = ::AdminSignupOp.new(email: 'foo')
179
- op.privileges << 'bangbang'
189
+ op = ::AdminSignupOp.new(email: "foo")
190
+ op.privileges << "bangbang"
180
191
 
181
- op = ::AdminSignupOp.new(email: 'foo', privileges: nil)
192
+ op = ::AdminSignupOp.new(email: "foo", privileges: nil)
182
193
 
183
194
  assert_equal({
184
- 'email' => 'foo',
185
- 'privileges' => nil
186
- }, op.params)
195
+ "email" => "foo",
196
+ "privileges" => nil,
197
+ }, op.params)
187
198
 
188
199
  assert_equal({
189
- 'privileges' => 'min'
190
- }, op.defaults)
200
+ "privileges" => "min",
201
+ }, op.defaults)
191
202
  end
192
203
 
193
204
  def test_it_overrides_defaults_with_nils
194
- op = ::AdminSignupOp.new(email: 'foo', privileges: nil)
205
+ op = ::AdminSignupOp.new(email: "foo", privileges: nil)
195
206
  assert_equal({
196
- 'privileges' => nil,
197
- 'email' => 'foo'
198
- }, op.params)
207
+ "privileges" => nil,
208
+ "email" => "foo",
209
+ }, op.params)
199
210
  end
200
211
 
201
212
  def test_it_casts_params_on_the_way_in
202
- op = ::TypeCastOp.new(integer_input: '25')
203
- assert_equal(25, op.params['integer_input'])
213
+ op = ::TypeCastOp.new(integer_input: "25")
214
+ assert_equal(25, op.params["integer_input"])
204
215
 
205
- op.decimal_input = '25.3'
206
- assert_equal(BigDecimal('25.3'), op.params['decimal_input'])
216
+ op.decimal_input = "25.3"
217
+ assert_equal(BigDecimal("25.3"), op.params["decimal_input"])
207
218
  end
208
219
 
209
220
  def test_it_allow_retrival_of_outputs
210
- op = ::SignupOp.submit!(email: 'foo@bar.com', password: 'password123')
221
+ op = ::SignupOp.submit!(email: "foo@bar.com", password: "password123")
211
222
  u = op.created_user
212
223
 
213
- assert_equal 'foo@bar.com', u.email_address
224
+ assert_equal "foo@bar.com", u.email_address
214
225
  end
215
226
 
216
227
  def test_it_raises_an_error_if_an_output_is_not_defined_but_is_set
217
228
  op = ::MissingOutputOp.new
218
- assert_raises ::Subroutine::UnknownOutputError do
229
+ assert_raises ::Subroutine::Outputs::UnknownOutputError do
219
230
  op.submit
220
231
  end
221
232
  end
222
233
 
223
234
  def test_it_raises_an_error_if_not_all_outputs_were_set
224
235
  op = ::MissingOutputSetOp.new
225
- assert_raises ::Subroutine::OutputNotSetError do
236
+ assert_raises ::Subroutine::Outputs::OutputNotSetError do
226
237
  op.submit
227
238
  end
228
239
  end
@@ -246,7 +257,7 @@ module Subroutine
246
257
  msg =~ %r{test/support/ops\.rb:[\d]+.+foo}
247
258
  end
248
259
 
249
- refute_nil found, 'Expected backtrace to include original caller of foo'
260
+ refute_nil found, "Expected backtrace to include original caller of foo"
250
261
  end
251
262
  end
252
263
 
@@ -278,5 +289,6 @@ module Subroutine
278
289
  end
279
290
  end
280
291
  end
292
+
281
293
  end
282
294
  end
@@ -11,11 +11,14 @@ module Subroutine
11
11
  include Subroutine::Fields
12
12
 
13
13
  string :foo, default: "foo"
14
- integer :bar, default: -> { 3 }
15
- date :baz
16
-
14
+ string :qux, default: "qux"
17
15
  string :protekted, mass_assignable: false
18
16
 
17
+ integer :bar, default: -> { 3 }, group: :sekret
18
+ string :protekted_group_input, group: :sekret
19
+
20
+ date :baz, group: :the_bazzes
21
+
19
22
  def initialize(options = {})
20
23
  setup_fields(options)
21
24
  end
@@ -23,11 +26,13 @@ module Subroutine
23
26
  end
24
27
 
25
28
  def test_fields_are_configured
26
- assert_equal 4, Whatever._fields.size
27
- assert_equal :string, Whatever._fields[:foo][:type]
28
- assert_equal :integer, Whatever._fields[:bar][:type]
29
- assert_equal :date, Whatever._fields[:baz][:type]
30
- assert_equal :string, Whatever._fields[:protekted][:type]
29
+ assert_equal 6, Whatever.field_configurations.size
30
+ assert_equal :string, Whatever.field_configurations[:foo][:type]
31
+ assert_equal :string, Whatever.field_configurations[:qux][:type]
32
+ assert_equal :integer, Whatever.field_configurations[:bar][:type]
33
+ assert_equal :date, Whatever.field_configurations[:baz][:type]
34
+ assert_equal :string, Whatever.field_configurations[:protekted][:type]
35
+ assert_equal :string, Whatever.field_configurations[:protekted_group_input][:type]
31
36
  end
32
37
 
33
38
  def test_field_defaults_are_handled
@@ -50,7 +55,7 @@ module Subroutine
50
55
  instance = DefaultsOp.new
51
56
  assert_equal false, instance.field_provided?(:foo)
52
57
 
53
- instance = DefaultsOp.new(foo: 'foo')
58
+ instance = DefaultsOp.new(foo: "foo")
54
59
  assert_equal true, instance.field_provided?(:foo)
55
60
  end
56
61
 
@@ -70,12 +75,18 @@ module Subroutine
70
75
 
71
76
  def test_params_include_defaults
72
77
  instance = Whatever.new(foo: "abc")
73
- assert_equal({ "foo" => "abc", "bar" => 3 }, instance.params)
74
- assert_equal({ "foo" => "foo", "bar" => 3 }, instance.defaults)
78
+ assert_equal({ "foo" => "foo", "bar" => 3, "qux" => "qux" }, instance.defaults)
79
+ assert_equal({ "foo" => "abc", "qux" => "qux" }, instance.params)
80
+ assert_equal({ "foo" => "abc", "bar" => 3, "qux" => "qux" }, instance.all_params)
81
+ end
82
+
83
+ def test_named_params_include_defaults
84
+ instance = Whatever.new(foo: "abc")
85
+ assert_equal({ "bar" => 3 }, instance.sekret_params)
75
86
  end
76
87
 
77
88
  def test_fields_can_opt_out_of_mass_assignment
78
- assert_raises "`protekted` is not mass assignable" do
89
+ assert_raises Subroutine::Fields::MassAssignmentError do
79
90
  Whatever.new(foo: "abc", protekted: "foo")
80
91
  end
81
92
  end
@@ -88,5 +99,31 @@ module Subroutine
88
99
  assert_equal true, instance.field_provided?(:protekted)
89
100
  end
90
101
 
102
+ def test_get_field
103
+ instance = Whatever.new
104
+
105
+ assert_equal "foo", instance.get_field(:foo)
106
+ end
107
+
108
+ def test_set_field
109
+ instance = Whatever.new
110
+ instance.set_field(:foo, "bar")
111
+
112
+ assert_equal "bar", instance.foo
113
+ end
114
+
115
+ def test_group_fields_are_accessible_at_the_class
116
+ results = Whatever.fields_in_group(:sekret)
117
+ assert_equal true, results.key?(:protekted_group_input)
118
+ assert_equal true, results.key?(:bar)
119
+ assert_equal false, results.key?(:protekted)
120
+ end
121
+
122
+ def test_groups_fields_are_accessible
123
+ op = Whatever.new(foo: "bar", protekted_group_input: "pgi", bar: 8)
124
+ assert_equal({ protekted_group_input: "pgi", bar: 8 }.with_indifferent_access, op.sekret_params)
125
+ assert_equal({ foo: "bar", qux: "qux" }.with_indifferent_access, op.params)
126
+ end
127
+
91
128
  end
92
129
  end