summaryrefslogtreecommitdiff
path: root/rt/t/web/rest-search-user.t
blob: 84a9673773d49962e7d33854bde4a250c69d1507 (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
use strict;
use warnings;
use RT::Test tests => undef;

my $root = RT::Test->load_or_create_user( Name => 'root', );
my $user_foo = RT::Test->load_or_create_user(
    Name     => 'foo',
    Password => 'password',
);
my $user_bar = RT::Test->load_or_create_user( Name => 'bar' );
my $user_baz = RT::Test->load_or_create_user( Name => 'baz' );
$user_baz->SetDisabled(1);

my ( $baseurl, $m ) = RT::Test->started_ok;

ok( $m->login, 'logged in' );

search_users_ok(
    { query => 'id = ' . $user_foo->id },
    [ $user_foo->id . ': foo' ],
    'search by id'
);

search_users_ok(
    {
        query  => 'Name = ' . $user_foo->Name,
        format => 's',
        fields => 'id,name'
    },
    [ "id\tName", $user_foo->id . "\tfoo" ],
    'search by name with customized fields'
);


search_users_ok(
    { query => 'foo = 3' },
    ['Invalid field specification: foo'],
    'invalid field'
);

search_users_ok(
    { query => 'id foo 3' },
    ['Invalid operator specification: foo'],
    'invalid op'
);

search_users_ok(
    { query => 'password = foo' },
    ['Invalid field specification: password'],
    "can't search password"
);

search_users_ok(
    { query => '', orderby => 'id' },
    [ $root->id . ': root', $user_foo->id . ': foo', $user_bar->id . ': bar', ],
    'order by id'
);

search_users_ok(
    { query => '', orderby => 'name' },
    [ $user_bar->id . ': bar', $user_foo->id . ': foo', $root->id . ': root' ],
    'order by name'
);

search_users_ok(
    { query => '', orderby => '+name' },
    [ $user_bar->id . ': bar', $user_foo->id . ': foo', $root->id . ': root' ],
    'order by +name'
);

search_users_ok(
    { query => '', orderby => '-name' },
    [ $root->id . ': root', $user_foo->id . ': foo', $user_bar->id . ': bar' ],
    'order by -name'
);

search_users_ok(
    { query => 'Disabled = 0', orderby => 'id' },
    [ $root->id . ': root', $user_foo->id . ': foo', $user_bar->id . ': bar', ],
    'enabled users'
);

search_users_ok(
    { query => 'Disabled = 1', orderby => 'id' },
    [ $user_baz->id . ': baz', ],
    'disabled users'
);

ok( $m->login( 'foo', 'password', logout => 1 ), 'logged in as foo' );
search_users_ok(
    { query => 'id = ' . $user_foo->id },
    [ 'Permission denied' ],
    "can't search without permission"
);

sub search_users_ok {
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    my $query    = shift;
    my $expected = shift;
    my $name     = shift || 'search users';

    my $uri = URI->new("$baseurl/REST/1.0/search/user");
    $uri->query_form(%$query);
    $m->get_ok($uri);

    my @lines = split /\n/, $m->content;
    shift @lines;    # header
    shift @lines;    # empty line

    is_deeply( \@lines, $expected, $name );

}

undef $m;
done_testing();