summaryrefslogtreecommitdiffstats
path: root/test/unit/user_query_test.rb
blob: e2d61a338e879fdbb8bb8c4f3c3e10eedb816145 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software

require_relative '../test_helper'

class UserQueryTest < ActiveSupport::TestCase
  fixtures :users, :groups_users, :email_addresses,
           :custom_fields, :custom_values, :auth_sources

  def test_available_columns_should_include_user_custom_fields
    query = UserQuery.new
    assert_include :cf_4, query.available_columns.map(&:name)
  end

  def test_filter_values_should_be_arrays
    q = UserQuery.new

    q.available_filters.each do |name, filter|
      values = filter.values
      assert (values.nil? || values.is_a?(Array)),
        "#values for #{name} filter returned a #{values.class.name}"
    end
  end

  def test_filter_by_admin
    q = UserQuery.new name: '_'
    q.filters = { 'admin' => { operator: '=', values: ['1'] }}
    users = find_users_with_query q
    assert_equal [true], users.map(&:admin?).uniq

    q.filters = { 'admin' => { operator: '!', values: ['1'] }}
    users = find_users_with_query q
    assert_equal [false], users.map(&:admin?).uniq

    q.filters = { 'admin' => { operator: '!', values: ['0'] }}
    users = find_users_with_query q
    assert_equal [true], users.map(&:admin?).uniq

    q.filters = { 'admin' => { operator: '!', values: ['1'] }}
    users = find_users_with_query q
    assert_equal [false], users.map(&:admin?).uniq
  end

  def test_filter_by_status
    q = UserQuery.new name: '_'
    q.filters = { 'status' => { operator: '=', values: [User::STATUS_LOCKED] }}
    users = find_users_with_query q
    assert_equal [5], users.map(&:id)
  end

  def test_login_filter
    [
      ['~', 'jsmith', [2]],
      ['^', 'jsm', [2]],
      ['$', 'ith', [2]],
    ].each do |op, string, result|
      q = UserQuery.new name: '_'
      q.add_filter('login', op, [string])
      users = find_users_with_query q
      assert_equal result, users.map(&:id), "#{op} #{string} should have found #{result}"
    end
  end

  def test_firstname_filter
    q = UserQuery.new name: '_'
    q.add_filter('firstname', '~', ['john'])
    users = find_users_with_query q
    assert_equal [2], users.map(&:id)
  end

  def test_lastname_filter
    q = UserQuery.new name: '_'
    q.add_filter('lastname', '~', ['smith'])
    users = find_users_with_query q
    assert_equal [2], users.map(&:id)
  end

  def test_mail_filter
    [
      ['~', 'somenet', [1, 2, 3, 4]],
      ['!~', 'somenet', [7, 8, 9]],
      ['^', 'dlop', [3]],
      ['$', 'bar', [7, 8, 9]],
      ['=', 'bar', []],
      ['=', 'someone@foo.bar', [7]],
      ['*', '', [1, 2, 3, 4, 7, 8, 9]],
      ['!*', '', []],
    ].each do |op, string, result|
      q = UserQuery.new name: '_'
      q.add_filter('mail', op, [string])
      users = find_users_with_query q
      assert_equal result, users.map(&:id).sort, "#{op} #{string} should have found #{result}"
    end
  end

  def test_name_or_email_or_login_filter
    [
      ['~', 'jsmith', [2]],
      ['^', 'jsm', [2]],
      ['$', 'ith', [2]],
      ['~', 'john', [2]],
      ['~', 'smith', [2]],
      ['~', 'somenet', [1, 2, 3, 4]],
      ['!~', 'somenet', [7, 8, 9]],
      ['^', 'dlop', [3]],
      ['$', 'bar', [7, 8, 9]],
      ['=', 'bar', []],
      ['=', 'someone@foo.bar', [7]],
      ['*', '', [1, 2, 3, 4, 7, 8, 9]],
      ['!*', '', []],
    ].each do |op, string, result|
      q = UserQuery.new name: '_'
      q.add_filter('name', op, [string])
      users = find_users_with_query q
      assert_equal result, users.map(&:id).sort, "#{op} #{string} should have found #{result}"
    end
  end

  def test_group_filter
    q = UserQuery.new name: '_'
    q.add_filter('is_member_of_group', '=', ['10', '99'])
    users = find_users_with_query q
    assert_equal [8], users.map(&:id)
  end

  def test_group_filter_not
    q = UserQuery.new name: '_'
    q.add_filter('is_member_of_group', '!', ['10'])
    users = find_users_with_query q
    assert users.any?
    assert_not users.map(&:id).include? 8
  end

  def test_group_filter_any
    q = UserQuery.new name: '_'
    q.add_filter('is_member_of_group', '*', [''])
    users = find_users_with_query q
    assert_equal [8], users.map(&:id)
  end

  def test_group_filter_none
    q = UserQuery.new name: '_'
    q.add_filter('is_member_of_group', '!*', [''])
    users = find_users_with_query q
    assert users.any?
    assert_not users.map(&:id).include? 8
  end

  def test_auth_source_filter
    user = User.find(1)
    user.update_column :auth_source_id, 1

    q = UserQuery.new name: '_'
    q.add_filter('auth_source_id', '=', ['1'])
    users = find_users_with_query q
    assert_equal [1], users.map(&:id)
  end

  def test_auth_source_filter_any
    user = User.find(1)
    user.update_column :auth_source_id, 1

    q = UserQuery.new name: '_'
    q.add_filter('auth_source_id', '*', [''])
    users = find_users_with_query q
    assert_equal [1], users.map(&:id)
  end

  def test_auth_source_filter_none
    user = User.find(1)
    user.update_column :auth_source_id, 1

    q = UserQuery.new name: '_'
    q.add_filter('auth_source_id', '!*', [''])
    users = find_users_with_query q
    assert users.any?
    assert_not users.map(&:id).include? 1
  end

  def test_auth_source_ordering
    auth = AuthSource.generate!(name: "Auth")

    user = User.find(1)
    user.update_column :auth_source_id, 1

    user2 = User.find(2)
    user2.update_column :auth_source_id, auth.id

    q = UserQuery.new name: '_'
    q.add_filter('auth_source_id', '*', [''])
    q.column_names = ['id', 'auth_source.name']
    q.sort_criteria = [['auth_source.name', 'asc']]

    users = q.results_scope

    assert_equal 2, users.size
    assert_equal [2, 1], users.pluck(:id)
  end

  def find_users_with_query(query)
    User.where(query.statement).to_a
  end
end