File: customfieldvalues.t

package info (click to toggle)
request-tracker5 5.0.3%2Bdfsg-3~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 77,648 kB
  • sloc: javascript: 187,930; perl: 79,061; sh: 1,302; makefile: 471; python: 37; php: 15
file content (232 lines) | stat: -rw-r--r-- 7,744 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use strict;
use warnings;
use lib 't/lib';
use RT::Test::REST2 tests => undef;

my $mech = RT::Test::REST2->mech;
my $auth = RT::Test::REST2->authorization_header;
my $rest_base_path = '/REST/2.0';
my $user = RT::Test::REST2->user;

my $select_cf = RT::CustomField->new(RT->SystemUser);
$select_cf->Create(Name => 'Select CF', Type => 'Select', MaxValues => 1);
$select_cf->AddValue(Name => 'First Value', SortOrder => 0);
$select_cf->AddValue(Name => 'Second Value', SortOrder => 1);
$select_cf->AddValue(Name => 'Third Value', SortOrder => 2);
my $select_cf_id = $select_cf->id;
my $select_cf_values = $select_cf->Values->ItemsArrayRef;

my $basedon_cf = RT::CustomField->new(RT->SystemUser);
$basedon_cf->Create(Name => 'SubSelect CF', Type => 'Select', MaxValues => 1, BasedOn => $select_cf->id);
$basedon_cf->AddValue(Name => 'With First Value', Category => $select_cf_values->[0]->Name, SortOder => 0);
$basedon_cf->AddValue(Name => 'With No Value', SortOder => 0);
my $basedon_cf_id = $basedon_cf->id;
my $basedon_cf_values = $basedon_cf->Values->ItemsArrayRef;

# Right test - retrieve all values without SeeCustomField
{
    my $res = $mech->get("$rest_base_path/customfield/$select_cf_id/values",
        'Authorization' => $auth,
    );
    is($res->code, 403);
}

$user->PrincipalObj->GrantRight(Right => 'SeeCustomField');

# Retrieve customfield's hypermedia link for customfieldvalues
{
    my $res = $mech->get("$rest_base_path/customfield/$select_cf_id",
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $content = $mech->json_response;
    my $links = $content->{_hyperlinks};
    my @cfvs_links = grep { $_->{ref} eq 'customfieldvalues' } @$links;
    is(scalar(@cfvs_links), 1);
    like($cfvs_links[0]->{_url}, qr{$rest_base_path/customfield/$select_cf_id/values$});
}

# No customfieldvalues hypermedia link for non-select customfield
{
    my $freeform_cf = RT::CustomField->new(RT->SystemUser);
    $freeform_cf->Create(Name => 'Freeform CF', Type => 'Freeform', MaxValues => 1, Queue => 'General');
    my $freeform_cf_id = $freeform_cf->id;

    my $res = $mech->get("$rest_base_path/customfield/$freeform_cf_id",
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $content = $mech->json_response;
    my $links = $content->{_hyperlinks};
    my @cfvs_links = grep { $_->{ref} eq 'customfieldvalues' } @$links;
    is(scalar(@cfvs_links), 0);
}

# Retrieve all values
{

    my $res = $mech->get("$rest_base_path/customfield/$select_cf_id/values",
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $content = $mech->json_response;
    is($content->{total}, 3);
    is($content->{count}, 3);
    my $items = $content->{items};
    is(scalar(@$items), 3);

    for (my $i=0; $i < scalar @$items; $i++) {
        my $cf_value_id = $select_cf_values->[$i]->id;
        is($items->[$i]->{type}, 'customfieldvalue');
        is($items->[$i]->{id}, $cf_value_id);
        is($items->[$i]->{name}, $select_cf_values->[$i]->Name);
        like($items->[$i]->{_url}, qr{$rest_base_path/customfield/$select_cf_id/value/$cf_value_id$});
    }
}

# Right test - udpate a value without AdminCustomFieldValues nor AdminCustomField
{
    my $payload = {
        Name => 'Third and Last Value',
    };
    my $res = $mech->put_json("$rest_base_path/customfield/$select_cf_id/value/" . $select_cf_values->[-1]->id,
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 403);

    $select_cf_values = $select_cf->Values->ItemsArrayRef;
    is($select_cf_values->[-1]->Name, 'Third Value');
}

# Right test - udpate a value without AdminCustomFieldValues but with AdminCustomField
{
    $user->PrincipalObj->GrantRight(Right => 'AdminCustomField');

    my $payload = {
        Name => 'Third and Last Value',
    };
    my $res = $mech->put_json("$rest_base_path/customfield/$select_cf_id/value/" . $select_cf_values->[-1]->id,
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 200);

    $select_cf_values = $select_cf->Values->ItemsArrayRef;
    is($select_cf_values->[-1]->Name, 'Third and Last Value');
}

# Right test - udpate a value without AdminCustomField but with AdminCustomFieldValues
{
    $user->PrincipalObj->RevokeRight(Right => 'AdminCustomField');
    $user->PrincipalObj->GrantRight(Right => 'AdminCustomFieldValues', Object => $select_cf);
    $user->PrincipalObj->GrantRight(Right => 'AdminCustomFieldValues', Object => $basedon_cf);

    my $payload = {
        Name => 'Third and Last but NOT Least Value',
    };
    my $res = $mech->put_json("$rest_base_path/customfield/$select_cf_id/value/" . $select_cf_values->[-1]->id,
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 200);

    $select_cf_values = $select_cf->Values->ItemsArrayRef;
    is($select_cf_values->[-1]->Name, 'Third and Last but NOT Least Value');
}

# Add a value
{
    my $payload = {
        Name      => 'Fourth Value',
        SortOrder => 3,
    };
    my $res = $mech->post_json("$rest_base_path/customfield/$select_cf_id/value",
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 201);

    $select_cf_values = $select_cf->Values->ItemsArrayRef;
    is(scalar(@$select_cf_values), 4);
    is($select_cf_values->[-1]->Name, 'Fourth Value');
}

# Retrieve a value
{
    my $cfv = $select_cf_values->[-2];
    my $cfv_id = $cfv->id;
    my $res = $mech->get("$rest_base_path/customfield/$select_cf_id/value/$cfv_id",
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $content = $mech->json_response;
    foreach my $field (qw/id Name Description SortOrder Category/) {
        is($content->{$field}, $cfv->$field);
    }

    ok(exists $content->{$_}, "got $_") for qw/Created Creator LastUpdated LastUpdatedBy/;

    is($content->{CustomField}->{id}, $select_cf_id);

    my $links = $content->{_hyperlinks};
    is(scalar @$links, 2);

    is($links->[0]{ref}, 'self');
    is($links->[0]{id}, $cfv_id);
    is($links->[0]{type}, 'customfieldvalue');
    like($links->[0]{_url}, qr{$rest_base_path/customfield/$select_cf_id/customfieldvalue/$cfv_id$});

    is($links->[1]{ref}, 'customfield');
    is($links->[1]{id}, $select_cf_id);
    is($links->[1]{type}, 'customfield');
    like($links->[1]{_url}, qr{$rest_base_path/customfield/$select_cf_id$});
}

# Retrieve all values filtered by category
{
    my $payload = [
        {
            field => 'Category',
            value => $select_cf_values->[0]->Name,
        }
    ];
    my $res = $mech->post_json("$rest_base_path/customfield/$basedon_cf_id/values",
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $content = $mech->json_response;
    is($content->{total}, 1);
    is($content->{count}, 1);
    my $items = $content->{items};
    is(scalar(@$items), 1);

    my @sub_cf_values = grep { ( $_->Category // '' ) eq $select_cf_values->[0]->Name } @$basedon_cf_values;
    for (my $i=0; $i < scalar @$items; $i++) {
        my $cf_value_id = $sub_cf_values[$i]->id;
        is($items->[$i]->{type}, 'customfieldvalue');
        is($items->[$i]->{id}, $cf_value_id);
        is($items->[$i]->{name}, $sub_cf_values[$i]->Name);
        like($items->[$i]->{_url}, qr{$rest_base_path/customfield/$basedon_cf_id/value/$cf_value_id$});
    }
}

# Delete a value
{
    my $res = $mech->delete("$rest_base_path/customfield/$select_cf_id/value/" . $select_cf_values->[-1]->id,
        'Authorization' => $auth,
    );
    is($res->code, 204);

    $select_cf_values = $select_cf->Values->ItemsArrayRef;
    is(scalar(@$select_cf_values), 3);
    is($select_cf_values->[-1]->Name, 'Third and Last but NOT Least Value');
}

done_testing;