-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmiddleware_test.rb
187 lines (163 loc) · 7.13 KB
/
middleware_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# frozen_string_literal: true
require_relative 'test_helper'
module Dynflow
module MiddlewareTest
describe 'Middleware' do
let(:world) { WorldFactory.create_world }
let(:log) { Support::MiddlewareExample::LogMiddleware.log }
before do
Support::MiddlewareExample::LogMiddleware.reset_log
end
it "wraps the action method calls" do
delay = world.delay(Support::MiddlewareExample::LoggingAction, { :start_at => Time.now.utc - 60 }, {})
plan = world.persistence.load_delayed_plan delay.execution_plan_id
plan.plan
plan.execute.future.wait
_(log).must_equal %w[LogMiddleware::before_delay
delay
LogMiddleware::after_delay
LogMiddleware::before_plan_phase
LogMiddleware::before_plan
plan
LogMiddleware::after_plan
LogMiddleware::after_plan_phase
LogMiddleware::before_run
run
LogMiddleware::after_run
LogMiddleware::before_finalize_phase
LogMiddleware::before_finalize
finalize
LogMiddleware::after_finalize
LogMiddleware::after_finalize_phase]
end
it "inherits the middleware" do
world.trigger(Support::MiddlewareExample::SubAction, {}).finished.wait
_(log).must_equal %w[LogRunMiddleware::before_run
AnotherLogRunMiddleware::before_run
run
AnotherLogRunMiddleware::after_run
LogRunMiddleware::after_run]
end
describe "world.middleware" do
let(:world_with_middleware) do
WorldFactory.create_world.tap do |world|
world.middleware.use(Support::MiddlewareExample::AnotherLogRunMiddleware)
end
end
it "puts the middleware to the beginning of the stack" do
world_with_middleware.trigger(Support::MiddlewareExample::Action, {}).finished.wait
_(log).must_equal %w[AnotherLogRunMiddleware::before_run
LogRunMiddleware::before_run
run
LogRunMiddleware::after_run
AnotherLogRunMiddleware::after_run]
end
end
describe "rules" do
describe "before" do
specify do
world.trigger(Support::MiddlewareExample::SubActionBeforeRule, {}).finished.wait
_(log).must_equal %w[AnotherLogRunMiddleware::before_run
LogRunMiddleware::before_run
run
LogRunMiddleware::after_run
AnotherLogRunMiddleware::after_run]
end
end
describe "after" do
let(:world_with_middleware) do
WorldFactory.create_world.tap do |world|
world.middleware.use(Support::MiddlewareExample::AnotherLogRunMiddleware,
after: Support::MiddlewareExample::LogRunMiddleware)
end
end
specify do
world_with_middleware.trigger(Support::MiddlewareExample::Action, {}).finished.wait
_(log).must_equal %w[LogRunMiddleware::before_run
AnotherLogRunMiddleware::before_run
run
AnotherLogRunMiddleware::after_run
LogRunMiddleware::after_run]
end
end
describe "replace" do
specify do
world.trigger(Support::MiddlewareExample::SubActionReplaceRule, {}).finished.wait
_(log).must_equal %w[AnotherLogRunMiddleware::before_run
run
AnotherLogRunMiddleware::after_run]
end
end
describe "remove" do
specify do
world.trigger(Support::MiddlewareExample::SubActionDoNotUseRule, {}).finished.wait
_(log).must_equal %w[run]
end
end
end
it "allows access the running action" do
world = WorldFactory.create_world
world.middleware.use(Support::MiddlewareExample::ObservingMiddleware,
replace: Support::MiddlewareExample::LogRunMiddleware)
world.trigger(Support::MiddlewareExample::Action, message: 'hello').finished.wait
_(log).must_equal %w[input#message:hello
run
output#message:finished]
end
it "allows modification of the running action when delaying execution" do
world = WorldFactory.create_world
world.middleware.use(Support::MiddlewareExample::AnotherObservingMiddleware,
replace: Support::MiddlewareExample::LogRunMiddleware)
delay = world.delay(Support::MiddlewareExample::Action, { :start_at => Time.now - 60 })
plan = world.persistence.load_delayed_plan delay.execution_plan_id
plan.plan
plan.execute.future.wait
_(log).must_equal ["delay#set-input:#{world.id}",
"plan#input:#{world.id}",
"input#message:#{world.id}",
'run',
'output#message:finished']
end
describe 'Presnet middleware' do
let(:world_with_middleware) do
WorldFactory.create_world.tap do |world|
world.middleware.use(Support::MiddlewareExample::FilterSensitiveData)
end
end
let :execution_plan do
result = world.trigger(Support::CodeWorkflowExample::IncomingIssue, issue_data)
_(result).must_be :planned?
result.finished.value
end
let :execution_plan_2 do
result = world.trigger(Support::MiddlewareExample::SecretAction)
_(result).must_be :planned?
result.finished.value
end
let :filtered_execution_plan do
world_with_middleware.persistence.load_execution_plan(execution_plan.id)
end
let :issue_data do
{ 'author' => 'Harry Potter', 'text' => 'Lord Voldemort is comming' }
end
let :presenter do
filtered_execution_plan.root_plan_step.action filtered_execution_plan
end
let :presenter_2 do
execution_plan_2.root_plan_step.action execution_plan_2
end
let :presenter_without_middleware do
execution_plan.root_plan_step.action execution_plan
end
it 'filters the data ===' do
_(presenter.input['text']).must_equal('You-Know-Who is comming')
_(presenter_2.output['spell']).must_equal('***')
end
it "doesn't affect stored data" do
_(presenter.input['text']).must_equal('You-Know-Who is comming')
_(presenter_without_middleware.input['text']).must_equal('Lord Voldemort is comming')
end
end
end
end
end