forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-notifiers.t
104 lines (88 loc) · 5.53 KB
/
api-notifiers.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
use Test::More tests => 33;
use Cwd;
use URI::Escape;
use MolochTest;
use JSON;
use Test::Differences;
use Data::Dumper;
use strict;
my $token = getTokenCookie();
my $notAdminToken = getTokenCookie('notadmin');
# notifier types
my $notifierTypes = viewerGetToken("/notifierTypes", $token);
ok(exists $notifierTypes->{email}, "email notifier exists");
ok(exists $notifierTypes->{slack}, "slack notifier exists");
ok(exists $notifierTypes->{twilio}, "twilio notifier exists");
# empty notifiers
my $notifiers = viewerGetToken("/notifiers", $token);
ok(!keys %{$notifiers}, "Empty notifiers");
# create notifier required items
my $json = viewerPostToken("/notifiers", '{}', $token);
is($json->{text}, "Missing notifier", "notifier object required");
$json = viewerPostToken("/notifiers", '{"notifier":{}}', $token);
is($json->{text}, "Missing a unique notifier name", "notifier name required");
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test1"}}', $token);
is($json->{text}, "Missing notifier type", "notifier type required");
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test1","type":"slack"}}', $token);
is($json->{text}, "Missing notifier fields", "notifier fields required");
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test1","type":"slack","fields":"badfields"}}', $token);
is($json->{text}, "Notifier fields must be an array", "notifier fields must be an array");
# create notifier requires token and admin access
$json = viewerPost("/notifiers", '{}');
is($json->{text}, "Missing token", "create notifier requires token");
$json = viewerPostToken("/notifiers?molochRegressionUser=notadmin", '{}', $notAdminToken);
is($json->{text}, "Need admin privelages to create a notifier", "create notifier requires admin");
# create notifier needs valid notifier type
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test1","type":"unknown","fields":[]}}', $token);
is($json->{text}, "Unknown notifier type", "invalid notifier type");
# create notifier
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test1","type":"slack","fields":[{"slackWebhookUrl":{"value":"test1url"}}]}}', $token);
ok($json->{success}, "notifier create success");
# create notifier requires unique notifier name
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test1","type":"slack","fields":[{"slackWebhookUrl":{"value":"test1url"}}]}}', $token);
is($json->{text}, "Notifier already exists", "notifier must have a unique name");
# create notifier sanitizes notifier name
$json = viewerPostToken("/notifiers", '{"notifier":{"name":"test2`~!@#$%^&*+[]{}(),.<>?","type":"slack","fields":[{"slackWebhookUrl":{"value":"test1url"}}]}}', $token);
is($json->{name}, "test2", "notifier name sanitization");
# update notifier requires admin access
$json = viewerPutToken("/notifiers/test1?molochRegressionUser=notadmin", '{}', $notAdminToken);
is($json->{text}, "Need admin privelages to update a notifier", "update notifier requires admin");
# update notifier needs valid name
$json = viewerPutToken("/notifiers/badname", '{}', $token);
is($json->{text}, "Cannot find notifer to udpate", "update notifier needs valid name");
# update notifier required fields
$json = viewerPutToken("/notifiers/test1", '{}', $token);
is($json->{text}, "Missing notifier", "notifier object required");
$json = viewerPutToken("/notifiers/test1", '{"notifier":{}}', $token);
is($json->{text}, "Missing a unique notifier name", "notifier name required");
$json = viewerPutToken("/notifiers/test1", '{"notifier":{"name":"test1a"}}', $token);
is($json->{text}, "Missing notifier type", "notifier type required");
$json = viewerPutToken("/notifiers/test1", '{"notifier":{"name":"test1a","type":"slack"}}', $token);
is($json->{text}, "Missing notifier fields", "notifier fields required");
$json = viewerPutToken("/notifiers/test1", '{"notifier":{"name":"test1a","type":"slack","fields":"badfields"}}', $token);
is($json->{text}, "Notifier fields must be an array", "notifier fields must be an array");
# update notifier needs valid notifier type
$json = viewerPutToken("/notifiers/test1", '{"notifier":{"name":"test1a","type":"unknown","fields":[]}}', $token);
is($json->{text}, "Unknown notifier type", "invalid notifier type");
# update notifier
$json = viewerPutToken("/notifiers/test1", '{"notifier":{"name":"test1a","type":"slack","fields":[{"slackWebhookUrl":{"value":"test1aurl"}}]}}', $token);
ok($json->{success}, "notifier update success");
is($json->{name}, "test1a", "notifier name update");
$notifiers = viewerGetToken("/notifiers", $token);
ok(exists $notifiers->{test1a}, "notifier update");
is($notifiers->{test1a}->{fields}[0]->{slackWebhookUrl}->{value}, "test1aurl", "notifier field value update");
# non admin no fields
$notifiers = viewerGetToken("/notifiers?molochRegressionUser=notadmin", $notAdminToken);
ok(exists $notifiers->{test1a}, "notifier update");
ok(!exists $notifiers->{test1a}->{fields}, "fields shouldn't exist for non admin");
# cleanup
$json = viewerDeleteToken("/notifiers/test1a", $token);
ok($json->{success}, "notifier delete success");
$json = viewerDeleteToken("/notifiers/test2", $token);
ok($json->{success}, "notifier delete success");
esGet("/_refresh");
$notifiers = viewerGetToken("/notifiers", $token);
ok(!exists $notifiers->{test1a}, "removed test1a notifier");
ok(!exists $notifiers->{test2}, "removed test2 notifier");
# remove shared user that gets added when creating notifiers
viewerPostToken("/user/delete", "userId=_moloch_shared", $token);