lopata 0.1.37 → 0.1.39
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.
- checksums.yaml +4 -4
- data/lib/lopata/role.rb +213 -109
- data/lib/lopata/scenario_builder.rb +526 -519
- data/lib/lopata/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6b391941e9c4d4e42dca98434fc3ccae1275ec3681cc38a3c3aded8fbabff909
|
|
4
|
+
data.tar.gz: 7c8a4d068f8ffdf583413f0fa156441c20d4cab95643a430c02d8b28c7c0fdea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: efbd16b2aea6b5debe630e5739a730cda91de1311682d01527aabc56c5b755055dc9aaae4a003ad317a7b5739450db117423c491be628f077b6d94af4f21476f
|
|
7
|
+
data.tar.gz: 2a7db79eda5d90471d111c54093fab7470055bcedd54aa75b58e5b101af5007671a3f3315f92f4d0baf48086ed1800f7790e9db07d5219867abe915355fb7c44
|
data/lib/lopata/role.rb
CHANGED
|
@@ -1,110 +1,214 @@
|
|
|
1
|
-
module Lopata
|
|
2
|
-
# Adds ability to run scenarios by given user roles
|
|
3
|
-
#
|
|
4
|
-
# @example Usage
|
|
5
|
-
# require 'lopata/role'
|
|
6
|
-
# Lopata.configure do |c|
|
|
7
|
-
# c.role_descriptions = {
|
|
8
|
-
# user: 'User',
|
|
9
|
-
# admin: 'Admin'
|
|
10
|
-
# }
|
|
11
|
-
# c.default_role = :user
|
|
12
|
-
# c.before_scenaro 'setup user'
|
|
13
|
-
# end
|
|
14
|
-
# Lopata.shared_step 'setup user' do
|
|
15
|
-
# setup { @user = User.create(role: current_role) if current_role }
|
|
16
|
-
# cleanup :user
|
|
17
|
-
# end
|
|
18
|
-
# Lopata.define 'login' do
|
|
19
|
-
# # will generate 2 scenarios, one for :user and one for :admin
|
|
20
|
-
# as :user, :admin
|
|
21
|
-
# action 'login'
|
|
22
|
-
# # verify the user is logged in
|
|
23
|
-
# end
|
|
24
|
-
#
|
|
25
|
-
# @see Lopata::Configuration#role_descriptions
|
|
26
|
-
# @see Lopata::Configuration#default_role
|
|
27
|
-
module Role
|
|
28
|
-
# To be included in Lopata::Scenario
|
|
29
|
-
module Methods
|
|
30
|
-
# @return [Symbol, nil] user role for current scenario or nil, if scenario is running without user.
|
|
31
|
-
def current_role
|
|
32
|
-
metadata[:as]
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# To be included in Lopata::ScenarioBuilder
|
|
37
|
-
module DSL
|
|
38
|
-
# Enumerate the roles the scenario to be runned under.
|
|
39
|
-
# If not invoked the default role only will be used.
|
|
40
|
-
#
|
|
41
|
-
# The scenario should be set to use the role via before_scenario step using #current_role param.
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
@roles
|
|
65
|
-
@use_all_roles =
|
|
66
|
-
@role_options = nil
|
|
67
|
-
end
|
|
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
|
-
end
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
1
|
+
module Lopata
|
|
2
|
+
# Adds ability to run scenarios by given user roles
|
|
3
|
+
#
|
|
4
|
+
# @example Usage
|
|
5
|
+
# require 'lopata/role'
|
|
6
|
+
# Lopata.configure do |c|
|
|
7
|
+
# c.role_descriptions = {
|
|
8
|
+
# user: 'User',
|
|
9
|
+
# admin: 'Admin'
|
|
10
|
+
# }
|
|
11
|
+
# c.default_role = :user
|
|
12
|
+
# c.before_scenaro 'setup user'
|
|
13
|
+
# end
|
|
14
|
+
# Lopata.shared_step 'setup user' do
|
|
15
|
+
# setup { @user = User.create(role: current_role) if current_role }
|
|
16
|
+
# cleanup :user
|
|
17
|
+
# end
|
|
18
|
+
# Lopata.define 'login' do
|
|
19
|
+
# # will generate 2 scenarios, one for :user and one for :admin
|
|
20
|
+
# as :user, :admin
|
|
21
|
+
# action 'login'
|
|
22
|
+
# # verify the user is logged in
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# @see Lopata::Configuration#role_descriptions
|
|
26
|
+
# @see Lopata::Configuration#default_role
|
|
27
|
+
module Role
|
|
28
|
+
# To be included in Lopata::Scenario
|
|
29
|
+
module Methods
|
|
30
|
+
# @return [Symbol, nil] user role for current scenario or nil, if scenario is running without user.
|
|
31
|
+
def current_role
|
|
32
|
+
metadata[:as]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# To be included in Lopata::ScenarioBuilder
|
|
37
|
+
module DSL
|
|
38
|
+
# Enumerate the roles the scenario to be runned under.
|
|
39
|
+
# If not invoked the default role only will be used.
|
|
40
|
+
#
|
|
41
|
+
# The scenario should be set to use the role via before_scenario step using #current_role param.
|
|
42
|
+
#
|
|
43
|
+
# When block without arguments is given, it defines a role group: the variants (diagonals and options)
|
|
44
|
+
# declared inside the block are applied to the given roles only. Several role groups may be declared,
|
|
45
|
+
# the scenarios are generated for each group separately.
|
|
46
|
+
#
|
|
47
|
+
# @example Role groups
|
|
48
|
+
# Lopata.define 'scenario' do
|
|
49
|
+
# as :user, :admin do
|
|
50
|
+
# diagonal :pc, 'one' => 1, 'two' => 2
|
|
51
|
+
# end
|
|
52
|
+
# as_first :guest do
|
|
53
|
+
# diagonal :pc, 'three' => 3
|
|
54
|
+
# end
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# @param args [Array<Symbol>] list of roles the scenario to be runned with.
|
|
58
|
+
# @param block [Block] the block to calculate role from scenario metadata (block with one argument),
|
|
59
|
+
# or the role group definition (block without arguments).
|
|
60
|
+
def as(*args, &block)
|
|
61
|
+
return add_role_group(args, true, &block) if block && block.arity == 0
|
|
62
|
+
raise_if_role_groups_defined
|
|
63
|
+
@roles = args.flatten
|
|
64
|
+
@roles << Lopata::ScenarioBuilder::CalculatedValue.new(&block) if block_given?
|
|
65
|
+
@use_all_roles = true
|
|
66
|
+
@role_options = nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Enumerate the roles the scenario can be run.
|
|
70
|
+
#
|
|
71
|
+
# The scenario should be set to use the role via before_scenario step using #current_role param.
|
|
72
|
+
#
|
|
73
|
+
# Only first role will be used if scenario has no options or diagonales. Some first roles will be used if
|
|
74
|
+
# options or diagonales are declared, in order of appearence.
|
|
75
|
+
#
|
|
76
|
+
# Use this to describe possible roles, but not needed to run scenario with all of them in order to save time
|
|
77
|
+
# of running.
|
|
78
|
+
#
|
|
79
|
+
# When block is given, it defines a role group. See #as for details.
|
|
80
|
+
#
|
|
81
|
+
# @param args [Array<Symbol>] list of roles the scenario to be runned with.
|
|
82
|
+
def as_first(*args, &block)
|
|
83
|
+
return add_role_group(args, false, &block) if block
|
|
84
|
+
raise_if_role_groups_defined
|
|
85
|
+
@roles = args.flatten
|
|
86
|
+
@use_all_roles = false
|
|
87
|
+
@role_options = nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @private
|
|
91
|
+
def role_options
|
|
92
|
+
@role_options ||= build_role_options
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Marks scenario to be runned without user.
|
|
96
|
+
#
|
|
97
|
+
# @example
|
|
98
|
+
# Lopata.define 'scenario withou user' do
|
|
99
|
+
# without_user
|
|
100
|
+
# it 'does not define the user' do
|
|
101
|
+
# expect(current_role).to be_nil
|
|
102
|
+
# end
|
|
103
|
+
# end
|
|
104
|
+
def without_user
|
|
105
|
+
@without_user = true
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# @private
|
|
109
|
+
def build_role_options
|
|
110
|
+
return [] unless roles
|
|
111
|
+
role_variants = roles.map { |r| [Lopata.configuration.role_descriptions[r], r] }
|
|
112
|
+
[Lopata::ScenarioBuilder::Diagonal.new(:as, role_variants, @use_all_roles)]
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# @private
|
|
116
|
+
def roles
|
|
117
|
+
return false if @without_user
|
|
118
|
+
@roles ||= [Lopata.configuration.default_role].compact
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# @private
|
|
122
|
+
def diagonals
|
|
123
|
+
return super + role_options unless @current_role_group
|
|
124
|
+
super + @current_role_group.diagonals + @current_role_group.role_options
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# @private
|
|
128
|
+
def options
|
|
129
|
+
return super unless @current_role_group
|
|
130
|
+
super + @current_role_group.options
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# @private
|
|
134
|
+
def option_combinations
|
|
135
|
+
return super() unless @role_groups
|
|
136
|
+
share_role_group_metadata_keys
|
|
137
|
+
@role_groups.flat_map do |group|
|
|
138
|
+
begin
|
|
139
|
+
@current_role_group = group
|
|
140
|
+
(options + diagonals).each(&:reset)
|
|
141
|
+
super()
|
|
142
|
+
ensure
|
|
143
|
+
@current_role_group = nil
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
# Variants of the same metadata key may be declared in several role groups. Each group knows only the keys
|
|
151
|
+
# of its own variants, so the keys used in other groups are not defined (NoMethodError on access).
|
|
152
|
+
# Share the keys across the groups to get nil for them, as for the variants of a single option.
|
|
153
|
+
def share_role_group_metadata_keys
|
|
154
|
+
all = @role_groups.flat_map { |g| g.options + g.diagonals }
|
|
155
|
+
all.group_by(&:key).each_value do |same_key|
|
|
156
|
+
keys = same_key.flat_map(&:available_metadata_keys).uniq
|
|
157
|
+
same_key.each { |o| o.available_metadata_keys = keys }
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def add_role_group(roles, use_all_roles, &block)
|
|
162
|
+
if @roles || @without_user
|
|
163
|
+
raise ArgumentError, 'Role groups (as/as_first with block) can not be combined with plain role declaration'
|
|
164
|
+
end
|
|
165
|
+
group = RoleGroup.new(roles.flatten, use_all_roles)
|
|
166
|
+
group.instance_exec(&block)
|
|
167
|
+
(@role_groups ||= []) << group
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def raise_if_role_groups_defined
|
|
171
|
+
return unless @role_groups
|
|
172
|
+
raise ArgumentError, 'Plain role declaration can not be combined with role groups (as/as_first with block)'
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Set of roles with own variants. Variants defined in the group are applied to the group roles only.
|
|
177
|
+
#
|
|
178
|
+
# @see DSL#as
|
|
179
|
+
class RoleGroup
|
|
180
|
+
# @private
|
|
181
|
+
attr_reader :roles, :options, :diagonals
|
|
182
|
+
|
|
183
|
+
# @private
|
|
184
|
+
def initialize(roles, use_all_roles)
|
|
185
|
+
@roles, @use_all_roles = roles, use_all_roles
|
|
186
|
+
@options, @diagonals = [], []
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Define option for the role group.
|
|
190
|
+
# @see Lopata::ScenarioBuilder#option
|
|
191
|
+
def option(metadata_key, variants)
|
|
192
|
+
@options << Lopata::ScenarioBuilder::Option.new(metadata_key, variants)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Define diagonal for the role group.
|
|
196
|
+
# @see Lopata::ScenarioBuilder#diagonal
|
|
197
|
+
def diagonal(metadata_key, variants)
|
|
198
|
+
@diagonals << Lopata::ScenarioBuilder::Diagonal.new(metadata_key, variants)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# @private
|
|
202
|
+
def role_options
|
|
203
|
+
@role_options ||= begin
|
|
204
|
+
role_variants = roles.map { |r| [Lopata.configuration.role_descriptions[r], r] }
|
|
205
|
+
[Lopata::ScenarioBuilder::Diagonal.new(:as, role_variants, @use_all_roles)]
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
Lopata::Scenario.include Lopata::Role::Methods
|
|
213
|
+
# Prepend the module to overload #diagonals method
|
|
110
214
|
Lopata::ScenarioBuilder.prepend Lopata::Role::DSL
|