foreman-tasks 0.16.1 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75231de648b74702c80ca64886f1d6c19caf3a7920d547807f892e130194cedb
4
- data.tar.gz: bf5f7331818981dbf841c3a696e0e768bec3328b9e349df13532d1757bbe86a9
3
+ metadata.gz: 753e1fdc22bc3c7c13f3bb3f22d4fb87598209363fabbd774c700e47fba723fa
4
+ data.tar.gz: a18ed71a7cd485770be04d40b0e30ac101dd83ee61c2b79623428b0df96d7cc8
5
5
  SHA512:
6
- metadata.gz: 8699acec8437f71f4fb52fb89a79dc96857cb08cd09375c7d33d9d4e72d67de8e95ee53352b14f1f80f1a62b880943df996d6fca16a666022ca030dcfdb4cd7d
7
- data.tar.gz: fc3e36ab57cadf68fa033a4224890c2fdc71a76e18d782b0758cac2bef7748a2cc5e3f30b3f7a37641cb2b9f3ff8d1c79f3b2d2e90cfb0e2050381f04e2a7794
6
+ metadata.gz: ba29345ff133bc71f39519dea6dff37d8a35331c158677faecaf50a949ff2655e713bec5ac2f7c35e1e9cb81171fbda8802abef81d7cc8fab4927f92572d19af
7
+ data.tar.gz: 6c9589f8d31aec28475186d7f64cc15232ef597f7ca07f84dc928c2f8706202e03da9d7cd9ac75b07380cd99e8638bb07e4b56214e8c15c4b851d067845df83e
@@ -16,7 +16,8 @@ module ForemanTasks
16
16
  end
17
17
 
18
18
  def summary
19
- render json: Task::Summarizer.new(resource_base, params[:recent_timeframe].to_i).summary
19
+ scope = resource_base.search_for(current_taxonomy_search).select(:id)
20
+ render json: Task::Summarizer.new(Task.where(:id => scope), params[:recent_timeframe].to_i).summary
20
21
  end
21
22
 
22
23
  def sub_tasks
@@ -49,10 +49,12 @@ module ForemanTasks
49
49
  scoped_search :on => :user_id,
50
50
  :complete_value => true,
51
51
  :rename => 'user.id',
52
- :validator => ->(value) { ScopedSearch::Validators::INTEGER.call(value) || value == 'current_user' },
53
- :aliases => ['owner.id'], :ext_method => :search_by_owner, :only_explicit => true
54
- scoped_search :relation => :user, :on => :login, :rename => 'user.login', :complete_value => true, :aliases => ['owner.login', 'user'], :ext_method => :search_by_owner, :only_explicit => true
55
- scoped_search :relation => :user, :on => :firstname, :rename => 'user.firstname', :complete_value => true, :aliases => ['owner.firstname'], :ext_method => :search_by_owner, :only_explicit => true
52
+ :validator => ->(value) { ScopedSearch::Validators::INTEGER.call(value) },
53
+ :value_translation => ->(value) { value == 'current_user' ? User.current.id : value },
54
+ :special_values => %w[current_user],
55
+ :aliases => ['owner.id'], :only_explicit => true
56
+ scoped_search :relation => :user, :on => :login, :complete_value => true, :aliases => ['owner.login', 'user'], :only_explicit => true
57
+ scoped_search :relation => :user, :on => :firstname, :rename => 'user.firstname', :complete_value => true, :aliases => ['owner.firstname'], :only_explicit => true
56
58
  scoped_search :relation => :task_groups, :on => :id, :complete_value => true, :rename => 'task_group.id', :validator => ScopedSearch::Validators::INTEGER
57
59
 
58
60
  scope :active, -> { where('foreman_tasks_tasks.state != ?', :stopped) }
@@ -22,31 +22,6 @@ module ForemanTasks
22
22
  sql = "foreman_tasks_locks_taxonomy#{uniq_suffix}.resource_id #{operator} ? OR foreman_tasks_locks_taxonomy#{uniq_suffix}.resource_id IS NULL"
23
23
  { :conditions => sanitize_sql_for_conditions([sql, value]), :joins => joins }
24
24
  end
25
-
26
- def search_by_owner(key, operator, value)
27
- return { :conditions => '0 = 1' } if value == 'current_user' && User.current.nil?
28
-
29
- key = 'owners.login' if key == 'user'
30
- # using uniq suffix to avoid colisions when searching by two different owners via ScopedSearch
31
- uniq_suffix = SecureRandom.hex(3)
32
- key_name = connection.quote_column_name(key.sub(/^.*\./, ''))
33
- value.sub!('*', '%%')
34
- condition = if key.blank?
35
- sanitize_sql_for_conditions(["users_#{uniq_suffix}.login #{operator} ? or users_#{uniq_suffix}.firstname #{operator} ? ", value, value])
36
- elsif key =~ /\.id\Z/
37
- value = User.current.id if value == 'current_user'
38
- sanitize_sql_for_conditions(["foreman_tasks_tasks.user_id #{operator} ?", value])
39
- else
40
- placeholder, value = operator == 'IN' ? ['(?)', value.split(',').map(&:strip)] : ['?', value]
41
- sanitize_sql_for_conditions(["users_#{uniq_suffix}.#{key_name} #{operator} #{placeholder}", value])
42
- end
43
- { :conditions => condition, :joins => joins_for_user_search(key, uniq_suffix) }
44
- end
45
-
46
- def joins_for_user_search(key, uniq_suffix)
47
- return '' if key =~ /\.id\Z/
48
- "INNER JOIN users AS users_#{uniq_suffix} ON users_#{uniq_suffix}.id = foreman_tasks_tasks.user_id"
49
- end
50
25
  end
51
26
  end
52
27
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanTasks
2
- VERSION = '0.16.1'.freeze
2
+ VERSION = '0.16.2'.freeze
3
3
  end
@@ -41,6 +41,41 @@ module ForemanTasks
41
41
  response = JSON.parse(@response.body)
42
42
  assert_equal 0, response['stopped']['total']
43
43
  end
44
+
45
+ describe 'taxonomy scoping' do
46
+ before do
47
+ @organizations = [0, 0].map { FactoryBot.create(:organization) }
48
+ @locations = [0, 0].map { FactoryBot.create(:location) }
49
+ @tasks = [0, 0].map { FactoryBot.create(:some_task) }
50
+ @tasks.zip(@organizations, @locations).each do |task, org, loc|
51
+ Lock.link!(org, task.id)
52
+ Lock.link!(loc, task.id)
53
+ end
54
+ end
55
+
56
+ it 'does not limit if unset' do
57
+ get(:summary, params: { recent_timeframe: 24 }, session: set_session_user)
58
+ assert_response :success
59
+ response = JSON.parse(@response.body)
60
+ assert_equal 2, response['stopped']['total']
61
+ end
62
+
63
+ it 'finds only tasks with matching taxonomies' do
64
+ get(:summary, params: { recent_timeframe: 24 },
65
+ session: set_session_user.merge(:organization_id => @organizations.first, :location_id => @locations.first))
66
+ assert_response :success
67
+ response = JSON.parse(@response.body)
68
+ assert_equal 1, response['stopped']['total']
69
+ end
70
+
71
+ it 'find no tasks when taxonomy combination contains no tasks' do
72
+ get(:summary, params: { recent_timeframe: 24 },
73
+ session: set_session_user.merge(:organization_id => @organizations.first, :location_id => @locations.last))
74
+ assert_response :success
75
+ response = JSON.parse(@response.body)
76
+ assert_equal 0, response['stopped']['total']
77
+ end
78
+ end
44
79
  end
45
80
 
46
81
  it 'supports csv export' do
@@ -30,20 +30,66 @@ class TasksTest < ActiveSupport::TestCase
30
30
  assert_equal [@task_one], ForemanTasks::Task.search_for("user = #{@user_one.login}")
31
31
  end
32
32
 
33
+ test 'cannot search by arbitrary key' do
34
+ proc { ForemanTasks::Task.search_for('user.my_key ~ 5') }.must_raise(ScopedSearch::QueryNotSupported)
35
+ proc { ForemanTasks::Task.search_for('user. = 5') }.must_raise(ScopedSearch::QueryNotSupported)
36
+ end
37
+
38
+ test 'can search the tasks by negated user' do
39
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user != #{@user_two.login}")
40
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user <> #{@user_two.login}")
41
+ SecureRandom.stubs(:hex).returns('abc')
42
+ assert_equal ForemanTasks::Task.search_for("user != #{@user_two.login}").to_sql,
43
+ ForemanTasks::Task.search_for("user <> #{@user_two.login}").to_sql
44
+ end
45
+
33
46
  test 'can search the tasks by user\'s id' do
34
47
  assert_equal [@task_one], ForemanTasks::Task.search_for("user.id = #{@user_one.id}")
35
48
  assert_equal [@task_one], ForemanTasks::Task.search_for("owner.id = #{@user_one.id}")
49
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user.id != #{@user_two.id}")
50
+ assert_equal [@task_one], ForemanTasks::Task.search_for("owner.id != #{@user_two.id}")
51
+ end
52
+
53
+ test 'can search by array of user ids' do
54
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user.id ^ (#{@user_one.id})")
55
+ assert_equal [@task_one], ForemanTasks::Task.search_for("owner.id ^ (#{@user_one.id})")
56
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user.id !^ (#{@user_two.id})")
57
+ assert_equal [@task_one], ForemanTasks::Task.search_for("owner.id !^ (#{@user_two.id})")
58
+ end
59
+
60
+ test 'cannot glob on user\'s id' do
61
+ proc { ForemanTasks::Task.search_for("user.id ~ something") }.must_raise(ScopedSearch::QueryNotSupported)
62
+ proc { ForemanTasks::Task.search_for("user.id ~ 5") }.must_raise(ScopedSearch::QueryNotSupported)
36
63
  end
37
64
 
38
65
  test 'can search the tasks by user with wildcards' do
39
- glob = '*' + @user_one.login[1..-1] # search for '*ser1' if login is 'user1'
40
- assert_equal [@task_one], ForemanTasks::Task.search_for("user ~ #{glob}")
66
+ part = @user_one.login[1..-1] # search for '*ser1' if login is 'user1'
67
+ # The following two should be equivalent
68
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user ~ #{part}")
69
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user ~ *#{part}*")
70
+ SecureRandom.stubs(:hex).returns('abc')
71
+ assert_equal ForemanTasks::Task.search_for("user ~ #{part}").to_sql,
72
+ ForemanTasks::Task.search_for("user ~ *#{part}*").to_sql
73
+ end
74
+
75
+ test 'can search the tasks by user with negated wildcards' do
76
+ part = @user_two.login[1..-1] # search for '*ser1' if login is 'user1'
77
+ # The following two should be equivalent
78
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user !~ #{part}")
79
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user !~ *#{part}*")
80
+ SecureRandom.stubs(:hex).returns('abc')
81
+ assert_equal ForemanTasks::Task.search_for("user !~ #{part}").to_sql,
82
+ ForemanTasks::Task.search_for("user !~ *#{part}*").to_sql
41
83
  end
42
84
 
43
85
  test 'can search the tasks by array' do
44
86
  assert_equal [@task_one], ForemanTasks::Task.search_for("user ^ (this_user, #{@user_one.login}, that_user)")
45
87
  end
46
88
 
89
+ test 'can search the tasks by negated array' do
90
+ assert_equal [@task_one], ForemanTasks::Task.search_for("user !^ (this_user, #{@user_two.login}, that_user)")
91
+ end
92
+
47
93
  test 'properly returns username' do
48
94
  assert_equal @task_one.username, @user_one.login
49
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman-tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.16.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Nečas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-05 00:00:00.000000000 Z
11
+ date: 2019-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: foreman-tasks-core