forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-shortcuts.t
214 lines (173 loc) · 10.9 KB
/
api-shortcuts.t
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
use Test::More tests => 82;
use Cwd;
use URI::Escape;
use MolochTest;
use JSON;
use Test::Differences;
use Data::Dumper;
use strict;
my $token = getTokenCookie();
my $otherToken = getTokenCookie('user2');
esPost("/tests_lookups/_delete_by_query?conflicts=proceed&refresh", '{ "query": { "match_all": {} } }');
esPost("/tests2_lookups/_delete_by_query?conflicts=proceed&refresh", '{ "query": { "match_all": {} } }');
multiPost("/regressionTests/flushCache");
# empty shortcuts
my $shortcuts = viewerGet("/lookups");
is(@{$shortcuts->{data}}, 0, "Empty shortcuts");
is($shortcuts->{recordsTotal}, 0, "Empty shortcuts with records total");
is($shortcuts->{recordsFiltered}, 0, "Empty shortcuts with records filtered");
# create shortcut required fields
my $json = viewerPostToken("/lookups", '{}', $token);
is($json->{text}, "Missing shortcut name", "shortcut name required");
$json = viewerPostToken("/lookups", '{"name":"test_shortcut"}', $token);
is($json->{text}, "Missing shortcut type", "shortcut type required");
$json = viewerPostToken("/api/shortcut", '{"name":"test_shortcut","type":"string"}', $token);
is($json->{text}, "Missing shortcut value", "shortcut value required");
# create shortcut requires token
$json = viewerPost("/lookups", '{"name":"test_shortcut","type":"string","value":"udp"}');
is($json->{text}, "Missing token", "create shortcut requires token");
# create shortcut
$json = viewerPostToken("/api/shortcut", '{"name":"test_shortcut~!@#$%^&*()+={}[]:;<>?,./","type":"string","value":"udp"}', $token);
ok($json->{success}, "create shortcut success");
ok(exists $json->{shortcut}->{id}, "returns shorcut with id");
my $shortcut1Id = $json->{shortcut}->{id}; # save id for cleanup later
# remove special chars from shortcut name
is($json->{shortcut}->{name}, "test_shortcut", "returns shortcut");
# shortcut names must be unique
$json = viewerPostToken("/lookups", '{"name":"test_shortcut","type":"string","value":"udp"}', $token);
ok(!$json->{success}, "unique shortcut names");
# update shortcut requires token
$json = viewerPut("/lookups/$shortcut1Id", "{}");
is($json->{text}, "Missing token", "update shortcut requires token");
# update shortcut required fields
$json = viewerPutToken("/lookups/$shortcut1Id", '{}', $token);
is($json->{text}, "Missing shortcut name", "shortcut name required");
$json = viewerPutToken("/lookups/$shortcut1Id", '{"name":"test_shortcut"}', $token);
is($json->{text}, "Missing shortcut type", "shortcut type required");
$json = viewerPutToken("/api/shortcut/$shortcut1Id", '{"name":"test_shortcut","type":"string"}', $token);
is($json->{text}, "Missing shortcut value", "shortcut value required");
# update shortcut
$json = viewerPutToken("/api/shortcut/$shortcut1Id", '{"name":"test_shortcut_updated","type":"ip","value":"10.0.0.1"}', $token);
is($json->{shortcut}->{name}, "test_shortcut_updated", "shortcut name updated");
is($json->{shortcut}->{value}, "10.0.0.1", "shortcut value updated");
# can update all fields of a shortcut
$json = viewerPutToken("/api/shortcut/$shortcut1Id", '{"name":"test_shortcut_updated","type":"string","value":"test","description":"test description"}', $token);
is($json->{shortcut}->{string}->[0], "test", "shortcut type updated");
is($json->{shortcut}->{value}, "test", "shortcut value updated");
is($json->{shortcut}->{description}, "test description", "shortcut description updated");
# turn it back to ip type for following tests to search for ip shortcuts
$json = viewerPutToken("/api/shortcut/$shortcut1Id", '{"name":"test_shortcut_updated","type":"ip","value":"10.0.0.1","description":"test description"}', $token);
# verify shortcut works
esGet("/_refresh");
countTest(1, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst=\$test_shortcut_updated"));
countTest(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst!=\$test_shortcut_updated"));
countTest(0, "molochRegressionUser=user2&date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst=\$test_shortcut_updated"));
countTest(0, "molochRegressionUser=user2&date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst!=\$test_shortcut_updated"));
countTest(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip!=\$test_shortcut_updated"));
# create another ip shortcut
$json = viewerPostToken("/api/shortcut", '{"name":"ip_shortcut","type":"ip","value":"10.0.0.3"}', $token);
my $ipShortcutId = $json->{shortcut}->{id};
# search by list of shortcuts
countTest(1, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst!=[\$test_shortcut_updated,\$ip_shortcut]"));
countTest(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst==[\$test_shortcut_updated,\$ip_shortcut]"));
countTest(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip==[\$test_shortcut_updated,\$ip_shortcut]"));
countTest(1, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip!=[\$test_shortcut_updated,\$ip_shortcut]"));
# same tests with multi
countTestMulti(1, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst=\$test_shortcut_updated"));
countTestMulti(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst!=\$test_shortcut_updated"));
countTestMulti(0, "molochRegressionUser=user2&date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst=\$test_shortcut_updated"));
countTestMulti(0, "molochRegressionUser=user2&date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst!=\$test_shortcut_updated"));
countTestMulti(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip!=\$test_shortcut_updated"));
countTestMulti(1, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst!=[\$test_shortcut_updated,\$ip_shortcut]"));
countTestMulti(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip.dst==[\$test_shortcut_updated,\$ip_shortcut]"));
countTestMulti(2, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip==[\$test_shortcut_updated,\$ip_shortcut]"));
countTestMulti(1, "date=-1&expression=" . uri_escape("file=*/pcap/bt-udp.pcap&&ip!=[\$test_shortcut_updated,\$ip_shortcut]"));
$json = viewerDeleteToken("/api/shortcut/$ipShortcutId", $token); # cleanup
# create shortcut by another user
$json = viewerPostToken("/api/shortcut?molochRegressionUser=user2", '{"name":"other_test_shortcut","type":"string","value":"udp"}', $otherToken);
ok($json->{success}, "create shortcut success");
my $shortcut2Id = $json->{shortcut}->{id}; # save id for cleanup later
# get shortcuts should have 1
$shortcuts = viewerGet("/api/shortcuts?molochRegressionUser=user2");
is(@{$shortcuts->{data}}, 1, "1 shortcut for this user");
# multi get shortcuts all should have 1
$shortcuts = multiGet("/api/shortcuts?molochRegressionUser=user2");
is(@{$shortcuts->{data}}, 1, "1 shortcut for this user");
# create shared shortcut by another user
$json = viewerPostToken("/lookups?molochRegressionUser=user2", '{"shared":true,"name":"other_test_shortcut_2","type":"string","value":"udp"}', $otherToken);
ok($json->{success}, "create shortcut success");
ok($json->{shortcut}->{shared}, "create shared shortcut");
my $shortcut3Id = $json->{shortcut}->{id}; # save id for cleanup later
# shared shortcut exists for user
$shortcuts = viewerGet("/lookups?molochRegressionUser=user2");
is(@{$shortcuts->{data}}, 2, "2 shortcuts for this user");
# can't update shortcut and duplicate name
$json = viewerPutToken("/api/shortcut/$shortcut3Id", '{"name":"test_shortcut_updated","type":"ip","value":"10.0.0.1"}', $token);
ok(!$json->{success}, "unique shortcut names");
# unshared shortcut can't be seen by nonadmin users
$shortcuts = viewerGet('/api/shortcuts?molochRegressionUser=user3');
is(@{$shortcuts->{data}}, 1, "nonadmin user can only see shared shortcuts");
# get only shortcuts of a specific type
$shortcuts = viewerGet("/lookups?fieldType=string");
is(@{$shortcuts->{data}}, 2, "should be 2 shortcuts of type string");
is($shortcuts->{data}->[0]->{type}, 'string', 'shortcut should be of type string');
$shortcuts = viewerGet("/api/shortcuts?fieldType=ip");
is(@{$shortcuts->{data}}, 1, "should be 1 shortcuts of type ip");
is($shortcuts->{data}->[0]->{type}, 'ip', 'shortcut should be of type ip');
# get shortcuts map
$shortcuts = viewerGet("/lookups?map=true");
ok(exists $shortcuts->{$shortcut1Id} && exists $shortcuts->{$shortcut3Id}, "Request lookup map");
# get shortcuts formatted for typeahead
$shortcuts = viewerGet("/api/shortcuts?fieldFormat=true&map=true");
ok(exists $shortcuts->{$shortcut1Id}->{exp}, "Shortcut has exp");
ok(exists $shortcuts->{$shortcut1Id}->{help}, "Shortcut has help");
ok(exists $shortcuts->{$shortcut1Id}->{dbField}, "Shortcut has dbField");
# the local (test2) cluster should sync with the remote (test) cluster
sleep(1);
esGet("/_refresh");
viewerGet2("/api/syncshortcuts");
sleep(1);
esGet("/_refresh");
sleep(2);
my $testsCluster = esGet("/tests_lookups/_search?sort=name")->{hits}->{hits};
my $tests2Cluster = esGet("/tests2_lookups/_search?sort=name")->{hits}->{hits};
for (my $i=0; $i < scalar(@{$testsCluster}); $i++) { # indexes are different
delete $testsCluster->[$i]->{_index};
delete $tests2Cluster->[$i]->{_index};
}
eq_or_diff($testsCluster, $tests2Cluster, "cluster sync failed", { context => 2 });
# delete shortcute requires token
$json = viewerDelete("/lookups/$shortcut1Id");
is($json->{text}, "Missing token", "delete shortcut requires token");
# can't delete another user's shortcuts
$json = viewerDeleteToken("/lookups/$shortcut1Id?molochRegressionUser=user2", $otherToken);
ok(!$json->{success}, "can't delete another user's shortcut");
is($json->{text}, "Permission denied");
# can't delete shortcut that doesn't exist
$json = viewerDeleteToken("/api/shortcut/fakeshortcutid", $token);
ok(!$json->{success}, "can't delete a nonexisting shortcut");
is($json->{text}, "Fetching shortcut to delete failed");
# delete shortcut (plus bonus cleanup)
$json = viewerDeleteToken("/api/shortcut/$shortcut1Id", $token);
ok($json->{success}, "delete shortcut success");
# cleanup
$json = viewerDeleteToken("/lookups/$shortcut2Id", $token);
$json = viewerDeleteToken("/lookups/$shortcut3Id", $token);
# make sure cleanup worked
$shortcuts = viewerGet("/lookups");
is(@{$shortcuts->{data}}, 0, "Empty shortcuts after cleanup");
$shortcuts = viewerGet("/api/shortcuts?molochRegressionUser=user2");
is(@{$shortcuts->{data}}, 0, "Empty shortcuts for user2 after cleanup");
# the local (test2) cluster should sync with the remote (test) cluster
viewerGet2("/api/syncshortcuts");
esGet("/_refresh");
sleep(2);
$testsCluster = esGet("/tests_lookups/_search?sort=name")->{hits}->{hits};
$tests2Cluster = esGet("/tests2_lookups/_search?sort=name")->{hits}->{hits};
for (my $i=0; $i < scalar(@{$testsCluster}); $i++) { # indexes are different
delete $testsCluster->[$i]->{_index};
delete $tests2Cluster->[$i]->{_index};
}
eq_or_diff($testsCluster, $tests2Cluster, "cluster sync failed", { context => 2 });
# remove shared user that gets added when creating shared shortcuts
viewerPostToken("/user/delete", "userId=_moloch_shared", $token);