Project

General

Profile

Feature #37674 » 0001-adds-tests-for-UserQuery-model-37674.patch

Jens Krämer, 2022-09-21 06:06

View differences:

test/unit/user_query_test.rb
1
# frozen_string_literal: true
2
#
3
# Redmine - project management software
4
# Copyright (C) 2006-2022  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18

  
19
require File.expand_path('../../test_helper', __FILE__)
20

  
21
class UserQueryTest < ActiveSupport::TestCase
22
  fixtures :users, :groups_users, :email_addresses,
23
           :custom_fields, :custom_values, :auth_sources
24

  
25
  def test_available_columns_should_include_user_custom_fields
26
    query = UserQuery.new
27
    assert_include :cf_4, query.available_columns.map(&:name)
28
  end
29

  
30
  def test_filter_values_should_be_arrays
31
    q = UserQuery.new
32

  
33
    q.available_filters.each do |name, filter|
34
      values = filter.values
35
      assert (values.nil? || values.is_a?(Array)),
36
        "#values for #{name} filter returned a #{values.class.name}"
37
    end
38
  end
39

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

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

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

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

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

  
66
  def test_login_filter
67
    [
68
      ['~', 'jsmith', [2]],
69
      ['^', 'jsm', [2]],
70
      ['$', 'ith', [2]],
71
    ].each do |op, string, result|
72
      q = UserQuery.new name: '_'
73
      q.add_filter('login', op, [string])
74
      users = find_users_with_query q
75
      assert_equal result, users.map(&:id), "#{op} #{string} should have found #{result}"
76
    end
77
  end
78

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

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

  
93
  def test_mail_filter
94
    [
95
      ['~', 'somenet', [1, 2, 3, 4]],
96
      ['!~', 'somenet', [7, 8, 9]],
97
      ['^', 'dlop', [3]],
98
      ['$', 'bar', [7, 8, 9]],
99
      ['=', 'bar', []],
100
      ['=', 'someone@foo.bar', [7]],
101
      ['*', '', [1, 2, 3, 4, 7, 8, 9]],
102
      ['!*', '', []],
103
    ].each do |op, string, result|
104
      q = UserQuery.new name: '_'
105
      q.add_filter('mail', op, [string])
106
      users = find_users_with_query q
107
      assert_equal result, users.map(&:id), "#{op} #{string} should have found #{result}"
108
    end
109
  end
110

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

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

  
126
  def test_group_filter_any
127
    q = UserQuery.new name: '_'
128
    q.add_filter('is_member_of_group', '*', [''])
129
    users = find_users_with_query q
130
    assert_equal [8], users.map(&:id)
131
  end
132

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

  
141
  def test_auth_source_filter
142
    user = User.find(1)
143
    user.update_column :auth_source_id, 1
144

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

  
151
  def test_auth_source_filter_any
152
    user = User.find(1)
153
    user.update_column :auth_source_id, 1
154

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

  
161
  def test_auth_source_filter_none
162
    user = User.find(1)
163
    user.update_column :auth_source_id, 1
164

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

  
172
  def find_users_with_query(query)
173
    User.where(query.statement).to_a
174
  end
175
end
176

  
(10-10/11)