Debian Codegraph @

test/unit/console_tool/test_notification_rules.py

raw
1 ######################################################################
2 #
3 # File: test/unit/console_tool/test_notification_rules.py
4 #
5 # Copyright 2023 Backblaze Inc. All Rights Reserved.
6 #
7 # License https://www.backblaze.com/using_b2_code.html
8 #
9 ######################################################################
10 import json
11
12 import pytest
13
14
15 @pytest.fixture()
16 def bucket_notification_rule(b2_cli, bucket):
17 rule = {
18 "eventTypes": ["b2:ObjectCreated:*"],
19 "isEnabled": True,
20 "isSuspended": False,
21 "name": "test-rule",
22 "objectNamePrefix": "",
23 "suspensionReason": "",
24 "targetConfiguration": {
25 "targetType": "webhook",
26 "url": "https://example.com/webhook",
27 },
28 }
29 _, stdout, _ = b2_cli.run(
30 [
31 "notification-rules",
32 "create",
33 "--json",
34 f"b2://{bucket}",
35 "test-rule",
36 "--webhook-url",
37 "https://example.com/webhook",
38 "--event-type",
39 "b2:ObjectCreated:*",
40 ],
41 )
42 actual_rule = json.loads(stdout)
43 assert actual_rule == rule
44 return actual_rule
45
46
47 def test_notification_rules__list_all(b2_cli, bucket, bucket_notification_rule):
48 _, stdout, _ = b2_cli.run([
49 "notification-rules",
50 "list",
51 f"b2://{bucket}",
52 ])
53 assert (
54 stdout == f"""\
55 Notification rules for b2://{bucket}/ :
56 - name: test-rule
57 eventTypes:
58 - b2:ObjectCreated:*
59 isEnabled: true
60 isSuspended: false
61 objectNamePrefix: ''
62 suspensionReason: ''
63 targetConfiguration:
64 targetType: webhook
65 url: https://example.com/webhook
66 """
67 )
68
69
70 def test_notification_rules__list_all_json(b2_cli, bucket, bucket_notification_rule):
71 _, stdout, _ = b2_cli.run([
72 "notification-rules",
73 "list",
74 "--json",
75 f"b2://{bucket}",
76 ])
77 assert json.loads(stdout) == [bucket_notification_rule]
78
79
80 def test_notification_rules__update(b2_cli, bucket, bucket_notification_rule):
81 bucket_notification_rule["isEnabled"] = False
82 _, stdout, _ = b2_cli.run(
83 [
84 "notification-rules",
85 "update",
86 "--json",
87 f"b2://{bucket}",
88 bucket_notification_rule["name"],
89 "--disable",
90 "--custom-header",
91 "X-Custom-Header=value=1",
92 ],
93 )
94 bucket_notification_rule["targetConfiguration"]["customHeaders"] = {
95 "X-Custom-Header": "value=1"
96 }
97 assert json.loads(stdout) == bucket_notification_rule
98
99
100 def test_notification_rules__update__no_such_rule(b2_cli, bucket, bucket_notification_rule):
101 b2_cli.run(
102 [
103 "notification-rules",
104 "update",
105 f"b2://{bucket}",
106 f'{bucket_notification_rule["name"]}-unexisting',
107 "--disable",
108 ],
109 expected_stderr=(
110 "ERROR: rule with name 'test-rule-unexisting' does not exist on bucket "
111 "'my-bucket', available rules: ['test-rule']\n"
112 ),
113 expected_status=1,
114 )
115
116
117 def test_notification_rules__update__custom_header_malformed(
118 b2_cli, bucket, bucket_notification_rule
119 ):
120 bucket_notification_rule["isEnabled"] = False
121 _, stdout, _ = b2_cli.run(
122 [
123 "notification-rules",
124 "update",
125 "--json",
126 f"b2://{bucket}",
127 bucket_notification_rule["name"],
128 "--disable",
129 "--custom-header",
130 "X-Custom-Header: value",
131 ],
132 )
133 bucket_notification_rule["targetConfiguration"]["customHeaders"] = {
134 "X-Custom-Header: value": ""
135 }
136 assert json.loads(stdout) == bucket_notification_rule
137
138
139 def test_notification_rules__delete(b2_cli, bucket, bucket_notification_rule):
140 _, stdout, _ = b2_cli.run(
141 [
142 "notification-rules",
143 "delete",
144 f"b2://{bucket}",
145 bucket_notification_rule["name"],
146 ],
147 )
148 assert stdout == "Rule 'test-rule' has been deleted from b2://my-bucket/\n"
149
150
151 def test_notification_rules__delete_no_such_rule(b2_cli, bucket, bucket_notification_rule):
152 b2_cli.run(
153 [
154 "notification-rules",
155 "delete",
156 f"b2://{bucket}",
157 f'{bucket_notification_rule["name"]}-unexisting',
158 ],
159 expected_stderr=(
160 "ERROR: no such rule to delete: 'test-rule-unexisting', available rules: ['test-rule'];"
161 " No rules have been deleted.\n"
162 ),
163 expected_status=1,
164 )
165
166
167 @pytest.mark.parametrize(
168 "args,expected_stdout",
169 [
170 (["-q"], ""),
171 ([], "No notification rules for b2://my-bucket/\n"),
172 (["--json"], "[]\n"),
173 ],
174 )
175 def test_notification_rules__no_rules(b2_cli, bucket, args, expected_stdout):
176 b2_cli.run(
177 ["notification-rules", "list", f"b2://{bucket}", *args],
178 expected_stdout=expected_stdout,
179 )
180
181
182 def test_notification_rules__disable_enable(b2_cli, bucket, bucket_notification_rule):
183 _, stdout, _ = b2_cli.run(
184 [
185 "notification-rules",
186 "disable",
187 "--json",
188 f"b2://{bucket}",
189 bucket_notification_rule["name"],
190 ],
191 )
192 assert json.loads(stdout) == {**bucket_notification_rule, "isEnabled": False}
193
194 _, stdout, _ = b2_cli.run(
195 [
196 "notification-rules",
197 "enable",
198 "--json",
199 f"b2://{bucket}",
200 bucket_notification_rule["name"],
201 ],
202 )
203 assert json.loads(stdout) == {**bucket_notification_rule, "isEnabled": True}
204
205
206 @pytest.mark.parametrize(
207 "command",
208 ["disable", "enable"],
209 )
210 def test_notification_rules__disable_enable__no_such_rule(
211 b2_cli, bucket, bucket_notification_rule, command
212 ):
213 b2_cli.run(
214 [
215 "notification-rules",
216 command,
217 f"b2://{bucket}",
218 f'{bucket_notification_rule["name"]}-unexisting',
219 ],
220 expected_stderr=(
221 "ERROR: rule with name 'test-rule-unexisting' does not exist on bucket "
222 "'my-bucket', available rules: ['test-rule']\n"
223 ),
224 expected_status=1,
225 )
226
227
228 def test_notification_rules__sign_secret(b2_cli, bucket, bucket_notification_rule):
229 b2_cli.run(
230 [
231 "notification-rules",
232 "update",
233 "--json",
234 f"b2://{bucket}",
235 bucket_notification_rule["name"],
236 "--sign-secret",
237 "new-secret",
238 ],
239 expected_status=2,
240 )
241
242 _, stdout, _ = b2_cli.run(
243 [
244 "notification-rules",
245 "update",
246 "--json",
247 f"b2://{bucket}",
248 bucket_notification_rule["name"],
249 "--sign-secret",
250 "7" * 32,
251 ],
252 )
253 bucket_notification_rule["targetConfiguration"]["hmacSha256SigningSecret"] = "7" * 32
254 assert json.loads(stdout) == bucket_notification_rule
255
256 assert json.loads(b2_cli.run(["notification-rules", "list", "--json", f"b2://{bucket}"],)[1]
257 ) == [bucket_notification_rule]