|
136 | 136 | }
|
137 | 137 | }
|
138 | 138 |
|
| 139 | + use_user_context_action_definition = { |
| 140 | + name: 'use_user_context', |
| 141 | + fields: [foo], |
| 142 | + hooks: { |
| 143 | + :load => -> (context) { |
| 144 | + foo = context[:fields].find{|field| field[:field] == 'foo'} |
| 145 | + foo[:value] = context[:user]['first_name'] |
| 146 | + context[:fields] |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
139 | 151 | action = ForestLiana::Model::Action.new(action_definition)
|
140 | 152 | fail_action = ForestLiana::Model::Action.new(fail_action_definition)
|
141 | 153 | cheat_action = ForestLiana::Model::Action.new(cheat_action_definition)
|
142 | 154 | enums_action = ForestLiana::Model::Action.new(enums_action_definition)
|
143 | 155 | multiple_enums_action = ForestLiana::Model::Action.new(multiple_enums_action_definition)
|
| 156 | + use_user_context_action = ForestLiana::Model::Action.new(use_user_context_action_definition) |
144 | 157 | island = ForestLiana.apimap.find {|collection| collection.name.to_s == ForestLiana.name_for(Island)}
|
145 |
| - island.actions = [action, fail_action, cheat_action, enums_action, multiple_enums_action] |
| 158 | + island.actions = [action, fail_action, cheat_action, enums_action, multiple_enums_action, use_user_context_action] |
146 | 159 |
|
147 | 160 | describe 'call /load' do
|
148 | 161 | params = {
|
|
174 | 187 | expect(response.status).to eq(500)
|
175 | 188 | expect(JSON.parse(response.body)).to eq({'error' => 'Error in smart action load hook: hook must return an array of fields'})
|
176 | 189 | end
|
| 190 | + |
| 191 | + it 'should return the first_name of the user who call the action' do |
| 192 | + post '/forest/actions/use_user_context/hooks/load', params: JSON.dump(params), headers: headers |
| 193 | + expect(response.status).to eq(200) |
| 194 | + expect(JSON.parse(response.body)).to eq({'fields' => [foo.merge({:value => 'Michael'}).transform_keys { |key| key.to_s.camelize(:lower) }.stringify_keys]}) |
| 195 | + end |
177 | 196 | end
|
178 | 197 |
|
179 | 198 | describe 'call /change' do
|
|
0 commit comments