Skip to content

Commit 03b3b63

Browse files
authored
fix: ensure correct values are returned for user tags in context variables (#692)
1 parent 292a9f1 commit 03b3b63

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

app/services/forest_liana/utils/context_variables.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ def get_current_user_data(context_variable_key)
2929
end
3030

3131
if context_variable_key.start_with?(USER_VALUE_TAG_PREFIX)
32+
tag_key = context_variable_key[USER_VALUE_TAG_PREFIX.length..]
3233
user['tags'].each do |tag|
33-
return tag[context_variable_key[USER_VALUE_TAG_PREFIX.length..]] if tag.key?(context_variable_key[USER_VALUE_TAG_PREFIX.length..])
34+
return tag['value'] if tag['key'] == tag_key
3435
end
3536
end
3637

spec/services/forest_liana/utils/context_variables_injector_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Utils
1010
'lastName' => 'Doe',
1111
'fullName' => 'John Doe',
1212
'email' => '[email protected]',
13-
'tags' => [{ 'planet' => 'Death Star' }],
13+
'tags' => [{'key' => 'planet', 'value' => 'Death Star' }],
1414
'roleId' => 1,
1515
'permissionLevel' => 'admin'
1616
}
@@ -87,7 +87,7 @@ module Utils
8787
{ key: 'id', expected_value: user['id'] },
8888
{ key: 'permissionLevel', expected_value: user['permissionLevel'] },
8989
{ key: 'roleId', expected_value: user['roleId'] },
90-
{ key: 'tags.planet', expected_value: user['tags'][0]['planet'] },
90+
{ key: 'tags.planet', expected_value: user['tags'][0]['value'] },
9191
{ key: 'team.id', expected_value: team['id'] },
9292
{ key: 'team.name', expected_value: team['name'] }
9393
].each do |value|

spec/services/forest_liana/utils/context_variables_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,24 @@ module Utils
3535
it 'returns the corresponding value from the key provided of the user data' do
3636
context_variables = described_class.new(team, user, request_context_variables)
3737
expect(context_variables.get_value('currentUser.first_name')).to eq('John')
38-
expect(context_variables.get_value('currentUser.tags.key')).to eq('tag1')
38+
expect(context_variables.get_value('currentUser.tags.tag1')).to eq('value1')
3939
expect(context_variables.get_value('currentUser.team.id')).to eq(1)
4040
end
41+
42+
it 'returns nil when the user key does not exist' do
43+
context_variables = described_class.new(team, user, request_context_variables)
44+
expect(context_variables.get_value('currentUser.non_existent_key')).to eq(nil)
45+
end
46+
47+
it 'returns nil when the tag key does not exist' do
48+
context_variables = described_class.new(team, user, request_context_variables)
49+
expect(context_variables.get_value('currentUser.tags.non_existent_tag')).to eq(nil)
50+
end
51+
52+
it 'returns nil when the request context variable key does not exist' do
53+
context_variables = described_class.new(team, user, request_context_variables)
54+
expect(context_variables.get_value('non_existent_key')).to eq(nil)
55+
end
4156
end
4257
end
4358
end

0 commit comments

Comments
 (0)