Debian Codegraph @

ansible_collections/community/hrobot/tests/unit/plugins/modules/test_storagebox_subaccount.py

raw
1 # Copyright (c) 2025 Victor LEFEBVRE <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import absolute_import, division, print_function
6
7 __metaclass__ = type
8
9
10 from ansible_collections.community.internal_test_tools.tests.unit.utils.fetch_url_module_framework import (
11 FetchUrlCall,
12 BaseTestModule,
13 )
14
15 from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import MagicMock
16
17 from ansible_collections.community.hrobot.plugins.module_utils.api import API_BASE_URL
18 from ansible_collections.community.hrobot.plugins.module_utils.robot import BASE_URL
19 from ansible_collections.community.hrobot.plugins.modules import (
20 storagebox_subaccount,
21 )
22
23 LEGACY_STORAGEBOX_SUBACCOUNTS = [
24 {
25 "subaccount": {
26 "username": "u2342-sub1",
27 "accountid": "u2342",
28 "server": "u12345-sub1.your-storagebox.de",
29 "homedirectory": "test",
30 "samba": True,
31 "ssh": True,
32 "external_reachability": True,
33 "webdav": False,
34 "readonly": False,
35 "createtime": "2017-05-24 00:00:00",
36 "comment": "Test account",
37 }
38 },
39 {
40 "subaccount": {
41 "username": "u2342-sub2",
42 "accountid": "u2342",
43 "server": "u12345-sub2.your-storagebox.de",
44 "homedirectory": "test2",
45 "samba": False,
46 "ssh": True,
47 "external_reachability": True,
48 "webdav": True,
49 "readonly": False,
50 "createtime": "2025-01-24 00:00:00",
51 "comment": "Test account 2",
52 }
53 },
54 ]
55
56
57 STORAGEBOX_SUBACCOUNTS = [
58 {
59 "id": 1,
60 "username": "u2342-sub1",
61 "server": "u12345-sub1.your-storagebox.de",
62 "storage_box": 2342,
63 "home_directory": "test",
64 "access_settings": {
65 "samba_enabled": True,
66 "ssh_enabled": True,
67 "webdav_enabled": False,
68 "reachable_externally": True,
69 "readonly": False,
70 },
71 "description": "Test account",
72 "created": "2017-05-24 00:00:00",
73 "labels": {},
74 },
75 {
76 "id": 2,
77 "username": "u2342-sub2",
78 "server": "u12345-sub2.your-storagebox.de",
79 "storage_box": 2342,
80 "home_directory": "test2",
81 "access_settings": {
82 "samba_enabled": False,
83 "ssh_enabled": True,
84 "webdav_enabled": True,
85 "reachable_externally": True,
86 "readonly": False,
87 },
88 "description": "Test account 2",
89 "created": "2025-01-24 00:00:00",
90 "labels": {},
91 },
92 ]
93
94
95 class TestHetznerStorageboxSubbacountLegacy(BaseTestModule):
96 MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox_subaccount.AnsibleModule'
97 MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.robot.fetch_url'
98
99 def test_storagebox_id_unknown(self, mocker):
100 result = self.run_module_failed(mocker, storagebox_subaccount, {
101 'hetzner_user': '',
102 'hetzner_password': '',
103 'storagebox_id': 23,
104 }, [
105 FetchUrlCall('GET', 404)
106 .result_json({
107 'error': {
108 'status': 404,
109 'code': 'STORAGEBOX_NOT_FOUND',
110 'message': 'Storage Box with ID 23 does not exist',
111 }
112 })
113 .expect_url(BASE_URL + '/storagebox/23/subaccount')
114 ])
115 assert result['msg'] == 'Storagebox with ID 23 does not exist'
116
117 def test_delete_unknown_subaccount_noop(self, mocker):
118 """Test deletion of a subaccount that doesn't exist (no-op)."""
119 result = self.run_module_success(mocker, storagebox_subaccount, {
120 'hetzner_user': '',
121 'hetzner_password': '',
122 'storagebox_id': 1234,
123 'state': 'absent',
124 'username': 'ghost_user',
125 }, [
126 FetchUrlCall('GET', 200)
127 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
128 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
129 ])
130
131 assert result['changed'] is False
132 assert result['created'] is False
133 assert result['deleted'] is False
134 assert result['updated'] is False
135 assert result['password_updated'] is False
136 assert result['subaccount'] is None
137
138 def test_delete_existing_subaccount(self, mocker):
139 """Test successful deletion of an existing subaccount."""
140 result = self.run_module_success(mocker, storagebox_subaccount, {
141 'hetzner_user': '',
142 'hetzner_password': '',
143 'storagebox_id': 1234,
144 'state': 'absent',
145 'username': 'u2342-sub2',
146 }, [
147 FetchUrlCall('GET', 200)
148 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
149 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
150
151 FetchUrlCall('DELETE', 200)
152 .result_json({})
153 .expect_url(BASE_URL + '/storagebox/1234/subaccount/u2342-sub2')
154 ])
155
156 assert result['changed'] is True
157 assert result['created'] is False
158 assert result['deleted'] is True
159 assert result['updated'] is False
160 assert result['password_updated'] is False
161 assert result['subaccount'] is None
162
163 def test_delete_existing_subaccount_idempotence_by_comment(self, mocker):
164 """Test successful deletion of an existing subaccount."""
165 result = self.run_module_success(mocker, storagebox_subaccount, {
166 'hetzner_user': '',
167 'hetzner_password': '',
168 'storagebox_id': 1234,
169 'state': 'absent',
170 'comment': 'Test account',
171 'idempotence': 'comment'
172 }, [
173 FetchUrlCall('GET', 200)
174 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
175 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
176
177 FetchUrlCall('DELETE', 200)
178 .result_json({})
179 .expect_url(BASE_URL + '/storagebox/1234/subaccount/u2342-sub1')
180 ])
181
182 assert result['changed'] is True
183 assert result['created'] is False
184 assert result['deleted'] is True
185 assert result['updated'] is False
186 assert result['password_updated'] is False
187 assert result['subaccount'] is None
188
189 def test_create_subaccount(self, mocker):
190 result = self.run_module_success(
191 mocker,
192 storagebox_subaccount,
193 {
194 'hetzner_user': '',
195 'hetzner_password': '',
196 'storagebox_id': 1234,
197 # subaccount
198 'homedirectory': '/data/newsub',
199 'samba': True,
200 'ssh': False,
201 'webdav': True,
202 'readonly': False,
203 'comment': 'My new subaccount'
204 },
205 [
206 FetchUrlCall('GET', 200)
207 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
208 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
209
210 FetchUrlCall('POST', 200)
211 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
212 .expect_form_value('homedirectory', '/data/newsub')
213 .expect_form_value('samba', 'true')
214 .expect_form_value('ssh', 'false')
215 .expect_form_value('webdav', 'true')
216 .expect_form_value('readonly', 'false')
217 .expect_form_value('comment', 'My new subaccount')
218 .result_json({
219 'subaccount': {
220 'username': 'generated_user',
221 'homedirectory': '/data/newsub',
222 'password': 'autogeneratedpass123'
223 }
224 }),
225 ]
226 )
227
228 assert result['changed'] is True
229 assert result['created'] is True
230 assert result['deleted'] is False
231 assert result['updated'] is False
232 assert result['password_updated'] is False
233 assert result['subaccount'] == {
234 'username': 'generated_user',
235 'homedirectory': '/data/newsub',
236 'password': 'autogeneratedpass123',
237 'samba': True,
238 'ssh': False,
239 'webdav': True,
240 'readonly': False,
241 'comment': 'My new subaccount',
242 }
243
244 # Ensures providing a username doesn't trigger accidental update (if username isn't known)
245 def test_create_subaccount_unknown_username(self, mocker):
246 result = self.run_module_success(
247 mocker,
248 storagebox_subaccount,
249 {
250 'hetzner_user': '',
251 'hetzner_password': '',
252 'storagebox_id': 1234,
253 # subaccount data
254 'homedirectory': '/data/newsub',
255 'samba': True,
256 'ssh': False,
257 'webdav': True,
258 'readonly': False,
259 'comment': 'My new subaccount',
260 'username': "I'll be ignored",
261 },
262 [
263 FetchUrlCall('GET', 200)
264 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
265 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
266
267 FetchUrlCall('POST', 200)
268 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
269 .expect_form_value('homedirectory', '/data/newsub')
270 .expect_form_value('samba', 'true')
271 .expect_form_value('ssh', 'false')
272 .expect_form_value('webdav', 'true')
273 .expect_form_value('readonly', 'false')
274 .expect_form_value('comment', 'My new subaccount')
275 .result_json({
276 'subaccount': {
277 'username': 'generated_user',
278 'homedirectory': '/data/newsub',
279 'password': 'autogeneratedpass123'
280 }
281 }),
282 ]
283 )
284
285 assert result['changed'] is True
286 assert result['created'] is True
287 assert result['deleted'] is False
288 assert result['updated'] is False
289 assert result['password_updated'] is False
290 assert result['subaccount'] == {
291 'username': 'generated_user',
292 'homedirectory': '/data/newsub',
293 'password': 'autogeneratedpass123',
294 'samba': True,
295 'ssh': False,
296 'webdav': True,
297 'readonly': False,
298 'comment': 'My new subaccount',
299 }
300
301 def test_create_subaccount_set_to_random(self, mocker):
302 result = self.run_module_success(
303 mocker,
304 storagebox_subaccount,
305 {
306 'hetzner_user': '',
307 'hetzner_password': '',
308 'storagebox_id': 1234,
309 'password_mode': 'set-to-random',
310 # subaccount
311 'homedirectory': '/data/newsub',
312 'samba': True,
313 'ssh': False,
314 'webdav': True,
315 'readonly': False,
316 'comment': 'My new subaccount',
317 'password': 'toto'
318 },
319 [
320 FetchUrlCall('GET', 200)
321 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
322 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
323
324 FetchUrlCall('POST', 200)
325 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
326 .expect_form_value('homedirectory', '/data/newsub')
327 .expect_form_value('samba', 'true')
328 .expect_form_value('ssh', 'false')
329 .expect_form_value('webdav', 'true')
330 .expect_form_value('readonly', 'false')
331 .expect_form_value('comment', 'My new subaccount')
332 .expect_form_value_absent('password')
333 .result_json({
334 'subaccount': {
335 'username': 'generated_user',
336 'homedirectory': '/data/newsub',
337 'password': 'autogeneratedpass123'
338 }
339 }),
340 ]
341 )
342
343 assert result['changed'] is True
344 assert result['created'] is True
345 assert result['deleted'] is False
346 assert result['updated'] is False
347 assert result['password_updated'] is False
348 assert result['subaccount'] == {
349 'username': 'generated_user',
350 'homedirectory': '/data/newsub',
351 'password': 'autogeneratedpass123',
352 'samba': True,
353 'ssh': False,
354 'webdav': True,
355 'readonly': False,
356 'comment': 'My new subaccount',
357 }
358
359 def test_create_subaccount_limit_exceeded(self, mocker):
360 result = self.run_module_failed(
361 mocker,
362 storagebox_subaccount,
363 {
364 'hetzner_user': '',
365 'hetzner_password': '',
366 'storagebox_id': 1234,
367 # subaccount
368 'homedirectory': '/data/newsub',
369 'samba': True,
370 'ssh': False,
371 'webdav': True,
372 'readonly': False,
373 'comment': 'My new subaccount'
374 },
375 [
376 FetchUrlCall('GET', 200)
377 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
378 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
379
380 FetchUrlCall('POST', 400)
381 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
382 .expect_form_value('homedirectory', '/data/newsub')
383 .expect_form_value('samba', 'true')
384 .expect_form_value('ssh', 'false')
385 .expect_form_value('webdav', 'true')
386 .expect_form_value('readonly', 'false')
387 .expect_form_value('comment', 'My new subaccount')
388 .result_json({
389 "error": {
390 "status": 400,
391 "code": "STORAGEBOX_SUBACCOUNT_LIMIT_EXCEEDED",
392 "message": "Too many requests"
393 }
394 })
395 ]
396 )
397 assert result['msg'] == "Subaccount limit exceeded"
398
399 def test_create_subaccount_invalid_password(self, mocker):
400 result = self.run_module_failed(
401 mocker,
402 storagebox_subaccount,
403 {
404 'hetzner_user': '',
405 'hetzner_password': '',
406 'storagebox_id': 1234,
407 # subaccount
408 'homedirectory': '/data/newsub',
409 'samba': True,
410 'ssh': False,
411 'webdav': True,
412 'readonly': False,
413 'comment': 'Invalid password attempt',
414 'password': '123'
415 },
416 [
417 FetchUrlCall('GET', 200)
418 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
419 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
420
421 FetchUrlCall('POST', 400)
422 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
423 .expect_form_value("password", "123")
424 .result_json({
425 "error": {
426 "status": 400,
427 "code": "STORAGEBOX_INVALID_PASSWORD",
428 "message": "Password does not meet security requirements"
429 }
430 })
431 ]
432 )
433
434 assert result['msg'] == "Invalid password (says Hetzner)"
435
436 def test_update_subaccount_noop(self, mocker):
437 result = self.run_module_success(
438 mocker,
439 storagebox_subaccount,
440 {
441 'hetzner_user': '',
442 'hetzner_password': '',
443 'storagebox_id': 1234,
444 # subaccount
445 "username": "u2342-sub1",
446 "homedirectory": "test",
447 "samba": True,
448 "ssh": True,
449 "external_reachability": True,
450 "webdav": False,
451 "readonly": False,
452 "comment": "Test account",
453 },
454 [
455 FetchUrlCall('GET', 200)
456 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
457 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
458 ]
459 )
460
461 assert result['changed'] is False
462 assert result['created'] is False
463 assert result['deleted'] is False
464 assert result['updated'] is False
465 assert result['password_updated'] is False
466 assert result['subaccount'] == {
467 'username': 'u2342-sub1',
468 'accountid': 'u2342',
469 'server': 'u12345-sub1.your-storagebox.de',
470 'homedirectory': 'test',
471 'samba': True,
472 'ssh': True,
473 'external_reachability': True,
474 'webdav': False,
475 'readonly': False,
476 'createtime': '2017-05-24 00:00:00',
477 'comment': 'Test account',
478 }
479
480 def test_update_subaccount_noop_idempotence_by_comment(self, mocker):
481 result = self.run_module_success(
482 mocker,
483 storagebox_subaccount,
484 {
485 'hetzner_user': '',
486 'hetzner_password': '',
487 'storagebox_id': 1234,
488 # subaccount
489 'idempotence': 'comment',
490 "homedirectory": "test",
491 "samba": True,
492 "ssh": True,
493 "external_reachability": True,
494 "webdav": False,
495 "readonly": False,
496 "comment": "Test account",
497 },
498 [
499 FetchUrlCall('GET', 200)
500 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
501 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
502 ]
503 )
504
505 assert result['changed'] is False
506 assert result['created'] is False
507 assert result['deleted'] is False
508 assert result['updated'] is False
509 assert result['password_updated'] is False
510 assert result['subaccount'] == {
511 'username': 'u2342-sub1',
512 'accountid': 'u2342',
513 'server': 'u12345-sub1.your-storagebox.de',
514 'homedirectory': 'test',
515 'samba': True,
516 'ssh': True,
517 'external_reachability': True,
518 'webdav': False,
519 'readonly': False,
520 'createtime': '2017-05-24 00:00:00',
521 'comment': 'Test account',
522 }
523
524 def test_update_subaccount(self, mocker):
525 input = {
526 'hetzner_user': '',
527 'hetzner_password': '',
528 'storagebox_id': 1234,
529 # subaccount
530 "username": "u2342-sub1",
531 "homedirectory": "test",
532 "samba": True,
533 "ssh": True,
534 "external_reachability": True,
535 "webdav": False,
536 "readonly": False,
537 "comment": "new comment",
538 }
539 result = self.run_module_success(
540 mocker,
541 storagebox_subaccount,
542 input,
543 [
544 FetchUrlCall('GET', 200)
545 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
546 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
547
548 FetchUrlCall('PUT', 200)
549 .expect_url(BASE_URL + '/storagebox/1234/subaccount/' + input['username'])
550 .expect_form_value("homedirectory", input["homedirectory"])
551 .expect_form_value("samba", str(input["samba"]).lower())
552 .expect_form_value("ssh", str(input["ssh"]).lower())
553 .expect_form_value("external_reachability", str(input["external_reachability"]).lower())
554 .expect_form_value("webdav", str(input["webdav"]).lower())
555 .expect_form_value("readonly", str(input["readonly"]).lower())
556 .expect_form_value("comment", input["comment"])
557 .result_json({})
558 ]
559 )
560
561 assert result['changed'] is True
562 assert result['created'] is False
563 assert result['deleted'] is False
564 assert result['updated'] is True
565 assert result['password_updated'] is False
566 assert result['subaccount'] == {
567 'username': 'u2342-sub1',
568 'accountid': 'u2342',
569 'server': 'u12345-sub1.your-storagebox.de',
570 'homedirectory': 'test',
571 'samba': True,
572 'ssh': True,
573 'external_reachability': True,
574 'webdav': False,
575 'readonly': False,
576 'createtime': '2017-05-24 00:00:00',
577 'comment': 'new comment',
578 }
579
580 def test_update_subaccount_idempotence_by_comment(self, mocker):
581 input = {
582 'hetzner_user': '',
583 'hetzner_password': '',
584 'storagebox_id': 1234,
585 # subaccount
586 'idempotence': 'comment',
587 "homedirectory": "/new/homedir",
588 "samba": True,
589 "ssh": True,
590 "external_reachability": True,
591 "webdav": False,
592 "readonly": False,
593 "comment": "Test account",
594 }
595 result = self.run_module_success(
596 mocker,
597 storagebox_subaccount,
598 input,
599 [
600 FetchUrlCall('GET', 200)
601 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
602 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
603
604 FetchUrlCall('PUT', 200)
605 .expect_url(BASE_URL + '/storagebox/1234/subaccount/' + LEGACY_STORAGEBOX_SUBACCOUNTS[0]['subaccount']['username'])
606 .expect_form_value("homedirectory", input["homedirectory"])
607 .expect_form_value("samba", str(input["samba"]).lower())
608 .expect_form_value("ssh", str(input["ssh"]).lower())
609 .expect_form_value("external_reachability", str(input["external_reachability"]).lower())
610 .expect_form_value("webdav", str(input["webdav"]).lower())
611 .expect_form_value("readonly", str(input["readonly"]).lower())
612 .expect_form_value("comment", input["comment"])
613 .result_json({})
614 ]
615 )
616
617 assert result['changed'] is True
618 assert result['created'] is False
619 assert result['deleted'] is False
620 assert result['updated'] is True
621 assert result['password_updated'] is False
622 assert result['subaccount'] == {
623 'username': 'u2342-sub1',
624 'accountid': 'u2342',
625 'server': 'u12345-sub1.your-storagebox.de',
626 'homedirectory': '/new/homedir',
627 'samba': True,
628 'ssh': True,
629 'external_reachability': True,
630 'webdav': False,
631 'readonly': False,
632 'createtime': '2017-05-24 00:00:00',
633 'comment': 'Test account',
634 }
635
636 def test_update_subaccount_set_to_random(self, mocker):
637 input = {
638 'hetzner_user': '',
639 'hetzner_password': '',
640 'storagebox_id': 1234,
641 # subaccount
642 "username": "u2342-sub1",
643 "homedirectory": "test",
644 "samba": True,
645 "ssh": True,
646 "external_reachability": True,
647 "webdav": False,
648 "readonly": False,
649 "comment": "new comment",
650 "password": "toto",
651 "password_mode": "set-to-random",
652 }
653 result = self.run_module_success(
654 mocker,
655 storagebox_subaccount,
656 input,
657 [
658 FetchUrlCall('GET', 200)
659 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
660 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
661
662 FetchUrlCall('POST', 200)
663 .expect_url(BASE_URL + '/storagebox/1234/subaccount/' + input['username'] + '/password')
664 .expect_form_value_absent("password")
665 .result_json({"password": "newRandomPassword"}),
666
667 FetchUrlCall('PUT', 200)
668 .expect_url(BASE_URL + '/storagebox/1234/subaccount/' + input['username'])
669 .expect_form_value("homedirectory", input["homedirectory"])
670 .expect_form_value("samba", str(input["samba"]).lower())
671 .expect_form_value("ssh", str(input["ssh"]).lower())
672 .expect_form_value("external_reachability", str(input["external_reachability"]).lower())
673 .expect_form_value("webdav", str(input["webdav"]).lower())
674 .expect_form_value("readonly", str(input["readonly"]).lower())
675 .expect_form_value("comment", input["comment"])
676 .result_json({})
677 ]
678 )
679
680 assert result['changed'] is True
681 assert result['created'] is False
682 assert result['deleted'] is False
683 assert result['updated'] is True
684 assert result['password_updated'] is True
685 assert result['subaccount'] == {
686 'username': 'u2342-sub1',
687 'accountid': 'u2342',
688 'server': 'u12345-sub1.your-storagebox.de',
689 'homedirectory': 'test',
690 'samba': True,
691 'ssh': True,
692 'external_reachability': True,
693 'webdav': False,
694 'readonly': False,
695 'createtime': '2017-05-24 00:00:00',
696 'comment': 'new comment',
697 'password': 'newRandomPassword',
698 }
699
700 def test_update_password_only(self, mocker):
701 result = self.run_module_success(
702 mocker,
703 storagebox_subaccount,
704 {
705 'hetzner_user': '',
706 'hetzner_password': '',
707 'storagebox_id': 1234,
708 # account
709 "password": "newsecurepassword",
710 "username": "u2342-sub1",
711 "homedirectory": "test",
712 "samba": True,
713 "ssh": True,
714 "external_reachability": True,
715 "webdav": False,
716 "readonly": False,
717 "comment": "Test account",
718 },
719 [
720 FetchUrlCall('GET', 200)
721 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
722 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
723
724 FetchUrlCall('POST', 200)
725 .expect_url(BASE_URL + '/storagebox/1234/subaccount/u2342-sub1/password')
726 .expect_form_value("password", "newsecurepassword")
727 .result_json({"password": "newsecurepassword"})
728 ]
729 )
730
731 assert result['changed'] is True
732 assert result['created'] is False
733 assert result['deleted'] is False
734 assert result['updated'] is False
735 assert result['password_updated'] is True
736 assert result['subaccount'] == {
737 'username': 'u2342-sub1',
738 'accountid': 'u2342',
739 'server': 'u12345-sub1.your-storagebox.de',
740 'homedirectory': 'test',
741 'samba': True,
742 'ssh': True,
743 'external_reachability': True,
744 'webdav': False,
745 'readonly': False,
746 'createtime': '2017-05-24 00:00:00',
747 'comment': 'Test account',
748 'password': 'newsecurepassword',
749 }
750
751 def test_update_subaccount_invalid_password(self, mocker):
752 result = self.run_module_failed(
753 mocker,
754 storagebox_subaccount,
755 {
756 'hetzner_user': '',
757 'hetzner_password': '',
758 'storagebox_id': 1234,
759 # subaccount
760 "password": "123",
761 "username": "u2342-sub1",
762 "homedirectory": "test",
763 "samba": True,
764 "ssh": True,
765 "external_reachability": True,
766 "webdav": False,
767 "readonly": False,
768 "comment": "Test account",
769 },
770 [
771 FetchUrlCall('GET', 200)
772 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
773 .expect_url(BASE_URL + '/storagebox/1234/subaccount'),
774
775 FetchUrlCall('POST', 400)
776 .expect_url(BASE_URL + '/storagebox/1234/subaccount/u2342-sub1/password')
777 .expect_form_value("password", "123")
778 .result_json({
779 "error": {
780 "status": 400,
781 "code": "STORAGEBOX_INVALID_PASSWORD",
782 "message": "Password does not meet security requirements"
783 }
784 })
785 ]
786 )
787
788 assert result['msg'] == "Invalid password (says Hetzner)"
789
790 def test_invalid_spec_create_no_homedir(self, mocker):
791 result = self.run_module_failed(
792 mocker,
793 storagebox_subaccount,
794 {
795 'hetzner_user': '',
796 'hetzner_password': '',
797 'storagebox_id': 1234,
798 'state': 'present',
799 },
800 [
801 FetchUrlCall('GET', 200)
802 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
803 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
804 ]
805 )
806
807 assert result['msg'] == "homedirectory is required when creating a new subaccount"
808
809 # Check mode tests
810 def test_update_password_only_CHECK_MODE(self, mocker):
811 result = self.run_module_success(
812 mocker,
813 storagebox_subaccount,
814 {
815 'hetzner_user': '',
816 'hetzner_password': '',
817 'storagebox_id': 1234,
818 '_ansible_check_mode': True,
819 # subaccount
820 "password": "newsecurepassword",
821 "username": "u2342-sub1",
822 "homedirectory": "test",
823 "samba": True,
824 "ssh": True,
825 "external_reachability": True,
826 "webdav": False,
827 "readonly": False,
828 "comment": "Test account",
829 },
830 [
831 FetchUrlCall('GET', 200)
832 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
833 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
834 ]
835 )
836
837 assert result['changed'] is True
838 assert result['created'] is False
839 assert result['deleted'] is False
840 assert result['updated'] is False
841 assert result['password_updated'] is True
842 assert result['subaccount'] == {
843 'username': 'u2342-sub1',
844 'accountid': 'u2342',
845 'server': 'u12345-sub1.your-storagebox.de',
846 'homedirectory': 'test',
847 'samba': True,
848 'ssh': True,
849 'external_reachability': True,
850 'webdav': False,
851 'readonly': False,
852 'createtime': '2017-05-24 00:00:00',
853 'comment': 'Test account',
854 'password': 'newsecurepassword',
855 }
856
857 def test_create_subaccount_CHECK_MODE(self, mocker):
858 result = self.run_module_success(
859 mocker,
860 storagebox_subaccount,
861 {
862 'hetzner_user': '',
863 'hetzner_password': '',
864 'storagebox_id': 1234,
865 # subaccount,
866 'homedirectory': '/data/newsub',
867 'samba': True,
868 'ssh': False,
869 'webdav': True,
870 'readonly': False,
871 'comment': 'My new subaccount',
872 '_ansible_check_mode': True,
873 },
874 [
875 FetchUrlCall('GET', 200)
876 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
877 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
878 ]
879 )
880
881 assert result['changed'] is True
882 assert result['created'] is True
883 assert result['deleted'] is False
884 assert result['updated'] is False
885 assert result['password_updated'] is False
886 assert result['subaccount'] == {
887 'homedirectory': '/data/newsub',
888 'samba': True,
889 'ssh': False,
890 'webdav': True,
891 'readonly': False,
892 'comment': 'My new subaccount',
893 }
894
895 def test_update_subaccount_CHECK_MODE(self, mocker):
896 result = self.run_module_success(
897 mocker,
898 storagebox_subaccount,
899 {
900 'hetzner_user': '',
901 'hetzner_password': '',
902 'storagebox_id': 1234,
903 '_ansible_check_mode': True,
904 # subaccount
905 "username": "u2342-sub1",
906 "homedirectory": "test",
907 "samba": True,
908 "ssh": True,
909 "external_reachability": True,
910 "webdav": False,
911 "readonly": False,
912 "comment": "new comment",
913
914 },
915 [
916 FetchUrlCall('GET', 200)
917 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
918 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
919 ]
920 )
921
922 assert result['changed'] is True
923 assert result['created'] is False
924 assert result['deleted'] is False
925 assert result['updated'] is True
926 assert result['password_updated'] is False
927 assert result['subaccount'] == {
928 'username': 'u2342-sub1',
929 'accountid': 'u2342',
930 'server': 'u12345-sub1.your-storagebox.de',
931 'homedirectory': 'test',
932 'samba': True,
933 'ssh': True,
934 'external_reachability': True,
935 'webdav': False,
936 'readonly': False,
937 'createtime': '2017-05-24 00:00:00',
938 'comment': 'new comment',
939 }
940
941 def test_delete_existing_subaccount_CHECK_MODE(self, mocker):
942 """Test successful deletion of an existing subaccount."""
943 result = self.run_module_success(mocker, storagebox_subaccount, {
944 'hetzner_user': '',
945 'hetzner_password': '',
946 'storagebox_id': 1234,
947 'state': 'absent',
948 'username': 'u2342-sub2',
949 '_ansible_check_mode': True,
950 }, [
951 FetchUrlCall('GET', 200)
952 .result_json(LEGACY_STORAGEBOX_SUBACCOUNTS)
953 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
954 ])
955
956 assert result['changed'] is True
957 assert result['created'] is False
958 assert result['deleted'] is True
959 assert result['updated'] is False
960 assert result['password_updated'] is False
961 assert result['subaccount'] is None
962
963 def test_broken_idempotence_same_comment_multiple_accounts(self, mocker):
964 result = self.run_module_failed(mocker, storagebox_subaccount, {
965 'hetzner_user': '',
966 'hetzner_password': '',
967 'storagebox_id': 1234,
968 'idempotence': 'comment',
969 'comment': LEGACY_STORAGEBOX_SUBACCOUNTS[0]['subaccount']['comment'],
970 }, [
971 FetchUrlCall('GET', 200)
972 .result_json([LEGACY_STORAGEBOX_SUBACCOUNTS[0], LEGACY_STORAGEBOX_SUBACCOUNTS[0]])
973 .expect_url(BASE_URL + '/storagebox/1234/subaccount')
974 ])
975
976 assert result['msg'] == "More than one subaccount matched the idempotence criteria."
977
978
979 class TestHetznerStorageboxSubbacount(BaseTestModule):
980 MOCK_ANSIBLE_MODULEUTILS_BASIC_ANSIBLEMODULE = 'ansible_collections.community.hrobot.plugins.modules.storagebox_subaccount.AnsibleModule'
981 MOCK_ANSIBLE_MODULEUTILS_URLS_FETCH_URL = 'ansible_collections.community.hrobot.plugins.module_utils.api.fetch_url'
982
983 def test_storagebox_id_unknown(self, mocker):
984 result = self.run_module_failed(mocker, storagebox_subaccount, {
985 'hetzner_token': '',
986 'storagebox_id': 23,
987 }, [
988 FetchUrlCall('GET', 404)
989 .result_json({
990 'error': {
991 'code': 'not_found',
992 'message': 'Storage Box not found',
993 'details': {},
994 }
995 })
996 .expect_url('{0}/v1/storage_boxes/23/subaccounts'.format(API_BASE_URL))
997 ])
998 assert result['msg'] == 'Storagebox with ID 23 does not exist'
999
1000 def test_delete_unknown_subaccount_noop(self, mocker):
1001 """Test deletion of a subaccount that doesn't exist (no-op)."""
1002 result = self.run_module_success(mocker, storagebox_subaccount, {
1003 'hetzner_token': '',
1004 'storagebox_id': 1234,
1005 'state': 'absent',
1006 'username': 'ghost_user',
1007 }, [
1008 FetchUrlCall('GET', 200)
1009 .result_json({
1010 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1011 })
1012 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=ghost_user'.format(API_BASE_URL))
1013 ])
1014
1015 assert result['changed'] is False
1016 assert result['created'] is False
1017 assert result['deleted'] is False
1018 assert result['updated'] is False
1019 assert result['password_updated'] is False
1020 assert result['subaccount'] is None
1021
1022 def test_delete_existing_subaccount(self, mocker):
1023 """Test successful deletion of an existing subaccount."""
1024 sleep_mock = MagicMock()
1025 mocker.patch('time.sleep', sleep_mock)
1026 result = self.run_module_success(mocker, storagebox_subaccount, {
1027 'hetzner_token': '',
1028 'storagebox_id': 1234,
1029 'state': 'absent',
1030 'username': 'u2342-sub2',
1031 }, [
1032 FetchUrlCall('GET', 200)
1033 .result_json({
1034 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1035 })
1036 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub2'.format(API_BASE_URL)),
1037
1038 FetchUrlCall('DELETE', 200)
1039 .result_json({
1040 "action": {
1041 "id": 13,
1042 "command": "delete_subaccount",
1043 "status": "running",
1044 "progress": 0,
1045 "started": "2016-01-30T23:50:00+00:00",
1046 "finished": None,
1047 "resources": [
1048 {
1049 "id": 23,
1050 "type": "storage_box",
1051 },
1052 {
1053 "id": 1,
1054 "type": "storage_box_subaccount"
1055 },
1056 ],
1057 "error": None,
1058 }
1059 })
1060 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/2'.format(API_BASE_URL)),
1061 FetchUrlCall('GET', 200)
1062 .result_json({
1063 "action": {
1064 "id": 13,
1065 "command": "delete_subaccount",
1066 "status": "success",
1067 "progress": 0,
1068 "started": "2016-01-30T23:50:00+00:00",
1069 "finished": "2016-01-30T23:50:00+00:00",
1070 "resources": [
1071 {
1072 "id": 1234,
1073 "type": "storage_box",
1074 },
1075 {
1076 "id": 1,
1077 "type": "storage_box_subaccount"
1078 },
1079 ],
1080 "error": None,
1081 }
1082 })
1083 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1084 ])
1085
1086 assert result['changed'] is True
1087 assert result['created'] is False
1088 assert result['deleted'] is True
1089 assert result['updated'] is False
1090 assert result['password_updated'] is False
1091 assert result['subaccount'] is None
1092
1093 def test_delete_existing_subaccount_idempotence_by_comment(self, mocker):
1094 """Test successful deletion of an existing subaccount."""
1095 sleep_mock = MagicMock()
1096 mocker.patch('time.sleep', sleep_mock)
1097 result = self.run_module_success(mocker, storagebox_subaccount, {
1098 'hetzner_token': '',
1099 'storagebox_id': 1234,
1100 'state': 'absent',
1101 'comment': 'Test account',
1102 'idempotence': 'comment'
1103 }, [
1104 FetchUrlCall('GET', 200)
1105 .result_json({
1106 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1107 })
1108 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1109
1110 FetchUrlCall('DELETE', 200)
1111 .result_json({
1112 "action": {
1113 "id": 13,
1114 "command": "delete_subaccount",
1115 "status": "running",
1116 "progress": 0,
1117 "started": "2016-01-30T23:50:00+00:00",
1118 "finished": None,
1119 "resources": [
1120 {
1121 "id": 1234,
1122 "type": "storage_box",
1123 },
1124 {
1125 "id": 1,
1126 "type": "storage_box_subaccount"
1127 },
1128 ],
1129 "error": None,
1130 }
1131 })
1132 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1'.format(API_BASE_URL)),
1133 FetchUrlCall('GET', 200)
1134 .result_json({
1135 "action": {
1136 "id": 13,
1137 "command": "delete_subaccount",
1138 "status": "success",
1139 "progress": 0,
1140 "started": "2016-01-30T23:50:00+00:00",
1141 "finished": "2016-01-30T23:50:00+00:00",
1142 "resources": [
1143 {
1144 "id": 23,
1145 "type": "storage_box",
1146 },
1147 {
1148 "id": 1,
1149 "type": "storage_box_subaccount"
1150 },
1151 ],
1152 "error": None,
1153 }
1154 })
1155 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1156 ])
1157
1158 assert result['changed'] is True
1159 assert result['created'] is False
1160 assert result['deleted'] is True
1161 assert result['updated'] is False
1162 assert result['password_updated'] is False
1163 assert result['subaccount'] is None
1164
1165 def test_delete_existing_subaccount_fail(self, mocker):
1166 """Test successful deletion of an existing subaccount."""
1167 sleep_mock = MagicMock()
1168 mocker.patch('time.sleep', sleep_mock)
1169 result = self.run_module_failed(mocker, storagebox_subaccount, {
1170 'hetzner_token': '',
1171 'storagebox_id': 1234,
1172 'state': 'absent',
1173 'username': 'u2342-sub2',
1174 }, [
1175 FetchUrlCall('GET', 200)
1176 .result_json({
1177 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1178 })
1179 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub2'.format(API_BASE_URL)),
1180
1181 FetchUrlCall('DELETE', 200)
1182 .result_json({
1183 "action": {
1184 "id": 13,
1185 "command": "delete_subaccount",
1186 "status": "running",
1187 "progress": 0,
1188 "started": "2016-01-30T23:50:00+00:00",
1189 "finished": None,
1190 "resources": [
1191 {
1192 "id": 23,
1193 "type": "storage_box",
1194 },
1195 {
1196 "id": 1,
1197 "type": "storage_box_subaccount"
1198 },
1199 ],
1200 "error": None,
1201 }
1202 })
1203 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/2'.format(API_BASE_URL)),
1204 FetchUrlCall('GET', 200)
1205 .result_json({
1206 "action": {
1207 "id": 13,
1208 "command": "delete_subaccount",
1209 "status": "error",
1210 "progress": 0,
1211 "started": "2016-01-30T23:50:00+00:00",
1212 "finished": "2016-01-30T23:50:00+00:00",
1213 "resources": [
1214 {
1215 "id": 1234,
1216 "type": "storage_box",
1217 },
1218 {
1219 "id": 1,
1220 "type": "storage_box_subaccount"
1221 },
1222 ],
1223 "error": {
1224 "code": "action_failed",
1225 "message": "Action failed",
1226 },
1227 }
1228 })
1229 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1230 ])
1231
1232 assert result['msg'] == "Error while deleting subaccount: [action_failed] Action failed"
1233
1234 def test_create_subaccount(self, mocker):
1235 sleep_mock = MagicMock()
1236 mocker.patch('time.sleep', sleep_mock)
1237 result = self.run_module_success(
1238 mocker,
1239 storagebox_subaccount,
1240 {
1241 'hetzner_token': '',
1242 'storagebox_id': 1234,
1243 # subaccount
1244 'homedirectory': '/data/newsub',
1245 'samba': True,
1246 'ssh': False,
1247 'webdav': True,
1248 'readonly': False,
1249 'comment': 'My new subaccount',
1250 'password': 'toto',
1251 },
1252 [
1253 FetchUrlCall('GET', 200)
1254 .result_json({
1255 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1256 })
1257 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1258
1259 FetchUrlCall('POST', 200)
1260 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
1261 .expect_json_value(['home_directory'], 'data/newsub')
1262 .expect_json_value(['access_settings', 'samba_enabled'], True)
1263 .expect_json_value(['access_settings', 'ssh_enabled'], False)
1264 .expect_json_value(['access_settings', 'webdav_enabled'], True)
1265 .expect_json_value(['access_settings', 'readonly'], False)
1266 .expect_json_value(['description'], 'My new subaccount')
1267 .result_json({
1268 "subaccount": {
1269 "id": 1,
1270 "storage_box": 1234,
1271 },
1272 "action": {
1273 "id": 13,
1274 "command": "create_subaccount",
1275 "status": "running",
1276 "progress": 0,
1277 "started": "2016-01-30T23:50:00+00:00",
1278 "finished": None,
1279 "resources": [
1280 {
1281 "id": 23,
1282 "type": "storage_box",
1283 },
1284 {
1285 "id": 1,
1286 "type": "storage_box_subaccount"
1287 },
1288 ],
1289 "error": None,
1290 },
1291 }),
1292 FetchUrlCall('GET', 200)
1293 .result_json({
1294 "action": {
1295 "id": 13,
1296 "command": "create_subaccount",
1297 "status": "success",
1298 "progress": 0,
1299 "started": "2016-01-30T23:50:00+00:00",
1300 "finished": "2016-01-30T23:50:00+00:00",
1301 "resources": [
1302 {
1303 "id": 1234,
1304 "type": "storage_box",
1305 },
1306 {
1307 "id": 1,
1308 "type": "storage_box_subaccount"
1309 },
1310 ],
1311 "error": None,
1312 }
1313 })
1314 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1315 FetchUrlCall('GET', 200)
1316 .result_json({
1317 "subaccount": STORAGEBOX_SUBACCOUNTS[0],
1318 })
1319 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1'.format(API_BASE_URL)),
1320 ]
1321 )
1322
1323 assert result['changed'] is True
1324 assert result['created'] is True
1325 assert result['deleted'] is False
1326 assert result['updated'] is False
1327 assert result['password_updated'] is False
1328 assert result['subaccount'] == {
1329 'access_settings': {
1330 'readonly': False,
1331 'reachable_externally': True,
1332 'samba_enabled': True,
1333 'ssh_enabled': False,
1334 'webdav_enabled': True,
1335 },
1336 'created': '2017-05-24 00:00:00',
1337 'createtime': '2017-05-24 00:00:00',
1338 'comment': 'My new subaccount',
1339 'description': 'My new subaccount',
1340 'external_reachability': True,
1341 'home_directory': '/data/newsub',
1342 'homedirectory': '/data/newsub',
1343 'id': 1,
1344 'labels': {},
1345 'password': 'toto',
1346 'readonly': False,
1347 'samba': True,
1348 'server': 'u12345-sub1.your-storagebox.de',
1349 'ssh': False,
1350 'storage_box': 2342,
1351 'username': 'u2342-sub1',
1352 'webdav': True,
1353 }
1354
1355 # Ensures providing a username doesn't trigger accidental update (if username isn't known)
1356 def test_create_subaccount_unknown_username(self, mocker):
1357 sleep_mock = MagicMock()
1358 mocker.patch('time.sleep', sleep_mock)
1359 result = self.run_module_success(
1360 mocker,
1361 storagebox_subaccount,
1362 {
1363 'hetzner_token': '',
1364 'storagebox_id': 1234,
1365 # subaccount data
1366 'homedirectory': '/data/newsub',
1367 'samba': True,
1368 'ssh': False,
1369 'webdav': True,
1370 'readonly': False,
1371 'comment': 'My new subaccount',
1372 'username': "I'll be ignored",
1373 'password': 'toto',
1374 },
1375 [
1376 FetchUrlCall('GET', 200)
1377 .result_json({
1378 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1379 })
1380 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=I%27ll+be+ignored'.format(API_BASE_URL)),
1381
1382 FetchUrlCall('POST', 200)
1383 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
1384 .expect_json_value(['home_directory'], 'data/newsub')
1385 .expect_json_value(['access_settings', 'samba_enabled'], True)
1386 .expect_json_value(['access_settings', 'ssh_enabled'], False)
1387 .expect_json_value(['access_settings', 'webdav_enabled'], True)
1388 .expect_json_value(['access_settings', 'readonly'], False)
1389 .expect_json_value(['description'], 'My new subaccount')
1390 .expect_json_value(['password'], 'toto')
1391 .expect_json_value_absent(['username'])
1392 .result_json({
1393 "subaccount": {
1394 "id": 2,
1395 "storage_box": 1234,
1396 },
1397 "action": {
1398 "id": 13,
1399 "command": "create_subaccount",
1400 "status": "running",
1401 "progress": 0,
1402 "started": "2016-01-30T23:50:00+00:00",
1403 "finished": None,
1404 "resources": [
1405 {
1406 "id": 23,
1407 "type": "storage_box",
1408 },
1409 {
1410 "id": 2,
1411 "type": "storage_box_subaccount"
1412 },
1413 ],
1414 "error": None,
1415 },
1416 }),
1417 FetchUrlCall('GET', 200)
1418 .result_json({
1419 "action": {
1420 "id": 13,
1421 "command": "create_subaccount",
1422 "status": "success",
1423 "progress": 0,
1424 "started": "2016-01-30T23:50:00+00:00",
1425 "finished": "2016-01-30T23:50:00+00:00",
1426 "resources": [
1427 {
1428 "id": 1234,
1429 "type": "storage_box",
1430 },
1431 {
1432 "id": 2,
1433 "type": "storage_box_subaccount"
1434 },
1435 ],
1436 "error": None,
1437 }
1438 })
1439 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1440 FetchUrlCall('GET', 200)
1441 .result_json({
1442 "subaccount": STORAGEBOX_SUBACCOUNTS[1],
1443 })
1444 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/2'.format(API_BASE_URL)),
1445 ]
1446 )
1447
1448 assert result['changed'] is True
1449 assert result['created'] is True
1450 assert result['deleted'] is False
1451 assert result['updated'] is False
1452 assert result['password_updated'] is False
1453 assert result['subaccount'] == {
1454 'access_settings': {
1455 'reachable_externally': True,
1456 'readonly': False,
1457 'samba_enabled': True,
1458 'ssh_enabled': False,
1459 'webdav_enabled': True,
1460 },
1461 'comment': 'My new subaccount',
1462 'created': '2025-01-24 00:00:00',
1463 'createtime': '2025-01-24 00:00:00',
1464 'description': 'My new subaccount',
1465 'external_reachability': True,
1466 'home_directory': '/data/newsub',
1467 'homedirectory': '/data/newsub',
1468 'id': 2,
1469 'labels': {},
1470 'password': 'toto',
1471 'readonly': False,
1472 'samba': True,
1473 'server': 'u12345-sub2.your-storagebox.de',
1474 'ssh': False,
1475 'storage_box': 2342,
1476 'username': 'u2342-sub2',
1477 'webdav': True,
1478 }
1479
1480 def test_create_subaccount_set_to_random(self, mocker):
1481 result = self.run_module_failed(mocker, storagebox_subaccount, {
1482 'hetzner_token': '',
1483 'storagebox_id': 1234,
1484 'password_mode': 'set-to-random',
1485 # subaccount
1486 'homedirectory': '/data/newsub',
1487 'samba': True,
1488 'ssh': False,
1489 'webdav': True,
1490 'readonly': False,
1491 'comment': 'My new subaccount',
1492 'password': 'toto'
1493 }, [])
1494
1495 assert result['msg'] == 'The new Hetzner API does not support password_mode=set-to-random'
1496
1497 def test_create_subaccount_no_password(self, mocker):
1498 result = self.run_module_failed(mocker, storagebox_subaccount, {
1499 'hetzner_token': '',
1500 'storagebox_id': 1234,
1501 # subaccount
1502 'homedirectory': '/data/newsub',
1503 'samba': True,
1504 'ssh': False,
1505 'webdav': True,
1506 'readonly': False,
1507 'comment': 'My new subaccount',
1508 }, [
1509 FetchUrlCall('GET', 200)
1510 .result_json({
1511 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1512 })
1513 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1514 ])
1515
1516 assert result['msg'] == 'password is required when creating a new subaccount'
1517
1518 def test_create_subaccount_limit_exceeded(self, mocker):
1519 result = self.run_module_failed(
1520 mocker,
1521 storagebox_subaccount,
1522 {
1523 'hetzner_token': '',
1524 'storagebox_id': 1234,
1525 # subaccount
1526 'homedirectory': '/data/newsub',
1527 'samba': True,
1528 'ssh': False,
1529 'webdav': True,
1530 'readonly': False,
1531 'comment': 'My new subaccount',
1532 'password': 'toto',
1533 },
1534 [
1535 FetchUrlCall('GET', 200)
1536 .result_json({
1537 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1538 })
1539 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1540
1541 FetchUrlCall('POST', 400)
1542 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
1543 .expect_json_value(['home_directory'], 'data/newsub')
1544 .expect_json_value(['access_settings', 'samba_enabled'], True)
1545 .expect_json_value(['access_settings', 'ssh_enabled'], False)
1546 .expect_json_value(['access_settings', 'webdav_enabled'], True)
1547 .expect_json_value(['access_settings', 'readonly'], False)
1548 .expect_json_value(['description'], 'My new subaccount')
1549 .expect_json_value(['password'], 'toto')
1550 .expect_json_value_absent(['username'])
1551 .result_json({
1552 "error": {
1553 "code": "resource_limit_exceeded",
1554 "message": "snapshot limit exceeded",
1555 "details": {
1556 "limits": [
1557 {
1558 "name": "subaccount_limit"
1559 },
1560 ],
1561 },
1562 },
1563 }),
1564 ]
1565 )
1566 assert result['msg'] in (
1567 "Request POST https://api.hetzner.com/v1/storage_boxes/1234/subaccounts failed:"
1568 " [resource_limit_exceeded] snapshot limit exceeded. Details: {'limits': [{'name': 'subaccount_limit'}]}",
1569 "Request POST https://api.hetzner.com/v1/storage_boxes/1234/subaccounts failed:"
1570 " [resource_limit_exceeded] snapshot limit exceeded. Details: {u'limits': [{u'name': u'subaccount_limit'}]}",
1571 )
1572
1573 def test_create_subaccount_invalid_password(self, mocker):
1574 sleep_mock = MagicMock()
1575 mocker.patch('time.sleep', sleep_mock)
1576 result = self.run_module_failed(
1577 mocker,
1578 storagebox_subaccount,
1579 {
1580 'hetzner_token': '',
1581 'storagebox_id': 1234,
1582 # subaccount
1583 'homedirectory': '/data/newsub',
1584 'samba': True,
1585 'ssh': False,
1586 'webdav': True,
1587 'readonly': False,
1588 'comment': 'Invalid password attempt',
1589 'password': '123',
1590 },
1591 [
1592 FetchUrlCall('GET', 200)
1593 .result_json({
1594 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1595 })
1596 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1597
1598 FetchUrlCall('POST', 200)
1599 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
1600 .expect_json_value(['home_directory'], 'data/newsub')
1601 .expect_json_value(['access_settings', 'samba_enabled'], True)
1602 .expect_json_value(['access_settings', 'ssh_enabled'], False)
1603 .expect_json_value(['access_settings', 'webdav_enabled'], True)
1604 .expect_json_value(['access_settings', 'readonly'], False)
1605 .expect_json_value(['description'], 'Invalid password attempt')
1606 .expect_json_value(['password'], '123')
1607 .expect_json_value_absent(['username'])
1608 .result_json({
1609 "subaccount": {
1610 "id": 42,
1611 "storage_box": 1234,
1612 },
1613 "action": {
1614 "id": 13,
1615 "command": "create_subaccount",
1616 "status": "running",
1617 "progress": 0,
1618 "started": "2016-01-30T23:50:00+00:00",
1619 "finished": None,
1620 "resources": [
1621 {
1622 "id": 23,
1623 "type": "storage_box",
1624 },
1625 {
1626 "id": 42,
1627 "type": "storage_box_subaccount"
1628 },
1629 ],
1630 "error": None,
1631 },
1632 }),
1633 FetchUrlCall('GET', 200)
1634 .result_json({
1635 "action": {
1636 "id": 13,
1637 "command": "create_subaccount",
1638 "status": "error",
1639 "progress": 0,
1640 "started": "2016-01-30T23:50:00+00:00",
1641 "finished": "2016-01-30T23:50:00+00:00",
1642 "resources": [
1643 {
1644 "id": 1234,
1645 "type": "storage_box",
1646 },
1647 {
1648 "id": 42,
1649 "type": "storage_box_subaccount"
1650 },
1651 ],
1652 "error": {
1653 "code": "action_failed",
1654 "message": "Action failed",
1655 },
1656 },
1657 })
1658 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1659 ]
1660 )
1661
1662 assert result['msg'] == "Error while creating subaccount: [action_failed] Action failed"
1663
1664 def test_update_subaccount_noop(self, mocker):
1665 result = self.run_module_success(
1666 mocker,
1667 storagebox_subaccount,
1668 {
1669 'hetzner_token': '',
1670 'storagebox_id': 1234,
1671 # subaccount
1672 "username": "u2342-sub1",
1673 "homedirectory": "test",
1674 "samba": True,
1675 "ssh": True,
1676 "external_reachability": True,
1677 "webdav": False,
1678 "readonly": False,
1679 "comment": "Test account",
1680 },
1681 [
1682 FetchUrlCall('GET', 200)
1683 .result_json({
1684 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1685 })
1686 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL)),
1687 ]
1688 )
1689
1690 assert result['changed'] is False
1691 assert result['created'] is False
1692 assert result['deleted'] is False
1693 assert result['updated'] is False
1694 assert result['password_updated'] is False
1695 assert result['subaccount'] == {
1696 'access_settings': {
1697 'reachable_externally': True,
1698 'readonly': False,
1699 'samba_enabled': True,
1700 'ssh_enabled': True,
1701 'webdav_enabled': False,
1702 },
1703 'username': 'u2342-sub1',
1704 'server': 'u12345-sub1.your-storagebox.de',
1705 'homedirectory': 'test',
1706 'home_directory': 'test',
1707 'samba': True,
1708 'ssh': True,
1709 'external_reachability': True,
1710 'webdav': False,
1711 'readonly': False,
1712 'created': '2017-05-24 00:00:00',
1713 'createtime': '2017-05-24 00:00:00',
1714 'comment': 'Test account',
1715 'description': 'Test account',
1716 'id': 1,
1717 'labels': {},
1718 'storage_box': 2342,
1719 }
1720
1721 def test_update_subaccount_noop_idempotence_by_comment(self, mocker):
1722 result = self.run_module_success(
1723 mocker,
1724 storagebox_subaccount,
1725 {
1726 'hetzner_token': '',
1727 'storagebox_id': 1234,
1728 # subaccount
1729 'idempotence': 'comment',
1730 "homedirectory": "test",
1731 "samba": True,
1732 "ssh": True,
1733 "external_reachability": True,
1734 "webdav": False,
1735 "readonly": False,
1736 "comment": "Test account",
1737 },
1738 [
1739 FetchUrlCall('GET', 200)
1740 .result_json({
1741 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1742 })
1743 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1744 ]
1745 )
1746
1747 assert result['changed'] is False
1748 assert result['created'] is False
1749 assert result['deleted'] is False
1750 assert result['updated'] is False
1751 assert result['password_updated'] is False
1752 assert result['subaccount'] == {
1753 'access_settings': {
1754 'reachable_externally': True,
1755 'readonly': False,
1756 'samba_enabled': True,
1757 'ssh_enabled': True,
1758 'webdav_enabled': False,
1759 },
1760 'username': 'u2342-sub1',
1761 'server': 'u12345-sub1.your-storagebox.de',
1762 'homedirectory': 'test',
1763 'home_directory': 'test',
1764 'samba': True,
1765 'ssh': True,
1766 'external_reachability': True,
1767 'webdav': False,
1768 'readonly': False,
1769 'created': '2017-05-24 00:00:00',
1770 'createtime': '2017-05-24 00:00:00',
1771 'comment': 'Test account',
1772 'description': 'Test account',
1773 'id': 1,
1774 'labels': {},
1775 'storage_box': 2342,
1776 }
1777
1778 def test_update_subaccount(self, mocker):
1779 sleep_mock = MagicMock()
1780 mocker.patch('time.sleep', sleep_mock)
1781 input = {
1782 'hetzner_token': '',
1783 'storagebox_id': 1234,
1784 # subaccount
1785 "username": "u2342-sub1",
1786 "homedirectory": "test",
1787 "samba": True,
1788 "ssh": True,
1789 "external_reachability": False,
1790 "webdav": True,
1791 "readonly": False,
1792 "comment": "new comment",
1793 }
1794 updated_1 = dict(STORAGEBOX_SUBACCOUNTS[0])
1795 updated_1['home_directory'] = '/test'
1796 result = self.run_module_success(
1797 mocker,
1798 storagebox_subaccount,
1799 input,
1800 [
1801 FetchUrlCall('GET', 200)
1802 .result_json({
1803 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1804 })
1805 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL)),
1806
1807 FetchUrlCall('PUT', 200)
1808 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1'.format(API_BASE_URL))
1809 .expect_json_value(["description"], 'new comment')
1810 .result_json({
1811 "subaccount": updated_1,
1812 }),
1813
1814 FetchUrlCall('POST', 200)
1815 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/update_access_settings'.format(API_BASE_URL))
1816 .expect_json_value_absent(["home_directory"])
1817 .expect_json_value_absent(["samba_enabled"])
1818 .expect_json_value_absent(["ssh_enabled"])
1819 .expect_json_value(["reachable_externally"], False)
1820 .expect_json_value(["webdav_enabled"], True)
1821 .expect_json_value_absent(["readonly"])
1822 .expect_json_value_absent(["description"])
1823 .result_json({
1824 "action": {
1825 "id": 13,
1826 "command": "update_access_settings",
1827 "status": "running",
1828 "progress": 0,
1829 "started": "2016-01-30T23:50:00+00:00",
1830 "finished": None,
1831 "resources": [
1832 {
1833 "id": 23,
1834 "type": "storage_box",
1835 },
1836 {
1837 "id": 42,
1838 "type": "storage_box_subaccount"
1839 },
1840 ],
1841 "error": None,
1842 },
1843 }),
1844 FetchUrlCall('GET', 200)
1845 .result_json({
1846 "action": {
1847 "id": 13,
1848 "command": "update_access_settings",
1849 "status": "success",
1850 "progress": 0,
1851 "started": "2016-01-30T23:50:00+00:00",
1852 "finished": "2016-01-30T23:50:00+00:00",
1853 "resources": [
1854 {
1855 "id": 1234,
1856 "type": "storage_box",
1857 },
1858 {
1859 "id": 42,
1860 "type": "storage_box_subaccount"
1861 },
1862 ],
1863 "error": None,
1864 },
1865 })
1866 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
1867 ]
1868 )
1869
1870 assert result['changed'] is True
1871 assert result['created'] is False
1872 assert result['deleted'] is False
1873 assert result['updated'] is True
1874 assert result['password_updated'] is False
1875 assert result['subaccount'] == {
1876 'access_settings': {
1877 'reachable_externally': False,
1878 'readonly': False,
1879 'samba_enabled': True,
1880 'ssh_enabled': True,
1881 'webdav_enabled': True,
1882 },
1883 'comment': 'new comment',
1884 'created': '2017-05-24 00:00:00',
1885 'createtime': '2017-05-24 00:00:00',
1886 'description': 'new comment',
1887 'external_reachability': False,
1888 'home_directory': 'test',
1889 'homedirectory': 'test',
1890 'id': 1,
1891 'labels': {},
1892 'readonly': False,
1893 'samba': True,
1894 'server': 'u12345-sub1.your-storagebox.de',
1895 'ssh': True,
1896 'storage_box': 2342,
1897 'username': 'u2342-sub1',
1898 'webdav': True,
1899 }
1900
1901 def test_update_subaccount_idempotence_by_comment(self, mocker):
1902 sleep_mock = MagicMock()
1903 mocker.patch('time.sleep', sleep_mock)
1904 input = {
1905 'hetzner_token': '',
1906 'storagebox_id': 1234,
1907 # subaccount
1908 'idempotence': 'comment',
1909 "homedirectory": "/new/homedir",
1910 "samba": True,
1911 "ssh": True,
1912 "external_reachability": True,
1913 "webdav": True,
1914 "readonly": False,
1915 "comment": "Test account",
1916 }
1917 result = self.run_module_success(
1918 mocker,
1919 storagebox_subaccount,
1920 input,
1921 [
1922 FetchUrlCall('GET', 200)
1923 .result_json({
1924 "subaccounts": STORAGEBOX_SUBACCOUNTS,
1925 })
1926 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
1927
1928 FetchUrlCall('POST', 200)
1929 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/change_home_directory'.format(API_BASE_URL))
1930 .expect_json_value(["home_directory"], 'new/homedir')
1931 .result_json({
1932 "action": {
1933 "id": 12,
1934 "command": "change_home_directory",
1935 "status": "running",
1936 "progress": 0,
1937 "started": "2016-01-30T23:50:00+00:00",
1938 "finished": None,
1939 "resources": [
1940 {
1941 "id": 1234,
1942 "type": "storage_box",
1943 },
1944 {
1945 "id": 42,
1946 "type": "storage_box_subaccount"
1947 },
1948 ],
1949 "error": None,
1950 },
1951 }),
1952 FetchUrlCall('GET', 200)
1953 .result_json({
1954 "action": {
1955 "id": 12,
1956 "command": "change_home_directory",
1957 "status": "success",
1958 "progress": 0,
1959 "started": "2016-01-30T23:50:00+00:00",
1960 "finished": "2016-01-30T23:50:00+00:00",
1961 "resources": [
1962 {
1963 "id": 23,
1964 "type": "storage_box",
1965 },
1966 {
1967 "id": 42,
1968 "type": "storage_box_subaccount"
1969 },
1970 ],
1971 "error": None,
1972 },
1973 })
1974 .expect_url('{0}/v1/storage_boxes/actions/12'.format(API_BASE_URL)),
1975
1976 FetchUrlCall('POST', 200)
1977 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/update_access_settings'.format(API_BASE_URL))
1978 .expect_json_value_absent(["home_directory"])
1979 .expect_json_value_absent(["samba_enabled"])
1980 .expect_json_value_absent(["ssh_enabled"])
1981 .expect_json_value_absent(["reachable_externally"])
1982 .expect_json_value(["webdav_enabled"], True)
1983 .expect_json_value_absent(["readonly"])
1984 .expect_json_value_absent(["comment"])
1985 .result_json({
1986 "action": {
1987 "id": 13,
1988 "command": "update_access_settings",
1989 "status": "running",
1990 "progress": 0,
1991 "started": "2016-01-30T23:50:00+00:00",
1992 "finished": None,
1993 "resources": [
1994 {
1995 "id": 1234,
1996 "type": "storage_box",
1997 },
1998 {
1999 "id": 42,
2000 "type": "storage_box_subaccount"
2001 },
2002 ],
2003 "error": None,
2004 },
2005 }),
2006 FetchUrlCall('GET', 200)
2007 .result_json({
2008 "action": {
2009 "id": 13,
2010 "command": "update_access_settings",
2011 "status": "success",
2012 "progress": 0,
2013 "started": "2016-01-30T23:50:00+00:00",
2014 "finished": "2016-01-30T23:50:00+00:00",
2015 "resources": [
2016 {
2017 "id": 23,
2018 "type": "storage_box",
2019 },
2020 {
2021 "id": 42,
2022 "type": "storage_box_subaccount"
2023 },
2024 ],
2025 "error": None,
2026 },
2027 })
2028 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
2029 ]
2030 )
2031
2032 assert result['changed'] is True
2033 assert result['created'] is False
2034 assert result['deleted'] is False
2035 assert result['updated'] is True
2036 assert result['password_updated'] is False
2037 assert result['subaccount'] == {
2038 'access_settings': {
2039 'reachable_externally': True,
2040 'readonly': False,
2041 'samba_enabled': True,
2042 'ssh_enabled': True,
2043 'webdav_enabled': True,
2044 },
2045 'comment': 'Test account',
2046 'created': '2017-05-24 00:00:00',
2047 'createtime': '2017-05-24 00:00:00',
2048 'description': 'Test account',
2049 'external_reachability': True,
2050 'home_directory': '/new/homedir',
2051 'homedirectory': '/new/homedir',
2052 'id': 1,
2053 'labels': {},
2054 'readonly': False,
2055 'samba': True,
2056 'server': 'u12345-sub1.your-storagebox.de',
2057 'ssh': True,
2058 'storage_box': 2342,
2059 'username': 'u2342-sub1',
2060 'webdav': True,
2061 }
2062
2063 def test_update_subaccount_idempotence_by_comment_fail(self, mocker):
2064 sleep_mock = MagicMock()
2065 mocker.patch('time.sleep', sleep_mock)
2066 input = {
2067 'hetzner_token': '',
2068 'storagebox_id': 1234,
2069 # subaccount
2070 'idempotence': 'comment',
2071 "homedirectory": "/new/homedir",
2072 "samba": True,
2073 "ssh": True,
2074 "external_reachability": True,
2075 "webdav": True,
2076 "readonly": False,
2077 "comment": "Test account",
2078 }
2079 result = self.run_module_failed(
2080 mocker,
2081 storagebox_subaccount,
2082 input,
2083 [
2084 FetchUrlCall('GET', 200)
2085 .result_json({
2086 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2087 })
2088 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
2089
2090 FetchUrlCall('POST', 200)
2091 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/change_home_directory'.format(API_BASE_URL))
2092 .expect_json_value(["home_directory"], 'new/homedir')
2093 .result_json({
2094 "action": {
2095 "id": 12,
2096 "command": "change_home_directory",
2097 "status": "running",
2098 "progress": 0,
2099 "started": "2016-01-30T23:50:00+00:00",
2100 "finished": None,
2101 "resources": [
2102 {
2103 "id": 1234,
2104 "type": "storage_box",
2105 },
2106 {
2107 "id": 42,
2108 "type": "storage_box_subaccount"
2109 },
2110 ],
2111 "error": None,
2112 },
2113 }),
2114 FetchUrlCall('GET', 200)
2115 .result_json({
2116 "action": {
2117 "id": 12,
2118 "command": "change_home_directory",
2119 "status": "success",
2120 "progress": 0,
2121 "started": "2016-01-30T23:50:00+00:00",
2122 "finished": "2016-01-30T23:50:00+00:00",
2123 "resources": [
2124 {
2125 "id": 23,
2126 "type": "storage_box",
2127 },
2128 {
2129 "id": 42,
2130 "type": "storage_box_subaccount"
2131 },
2132 ],
2133 "error": None,
2134 },
2135 })
2136 .expect_url('{0}/v1/storage_boxes/actions/12'.format(API_BASE_URL)),
2137
2138 FetchUrlCall('POST', 200)
2139 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/update_access_settings'.format(API_BASE_URL))
2140 .expect_json_value_absent(["home_directory"])
2141 .expect_json_value_absent(["samba_enabled"])
2142 .expect_json_value_absent(["ssh_enabled"])
2143 .expect_json_value_absent(["reachable_externally"])
2144 .expect_json_value(["webdav_enabled"], True)
2145 .expect_json_value_absent(["readonly"])
2146 .expect_json_value_absent(["comment"])
2147 .result_json({
2148 "action": {
2149 "id": 13,
2150 "command": "update_access_settings",
2151 "status": "running",
2152 "progress": 0,
2153 "started": "2016-01-30T23:50:00+00:00",
2154 "finished": None,
2155 "resources": [
2156 {
2157 "id": 1234,
2158 "type": "storage_box",
2159 },
2160 {
2161 "id": 42,
2162 "type": "storage_box_subaccount"
2163 },
2164 ],
2165 "error": None,
2166 },
2167 }),
2168 FetchUrlCall('GET', 200)
2169 .result_json({
2170 "action": {
2171 "id": 13,
2172 "command": "update_access_settings",
2173 "status": "error",
2174 "progress": 0,
2175 "started": "2016-01-30T23:50:00+00:00",
2176 "finished": "2016-01-30T23:50:00+00:00",
2177 "resources": [
2178 {
2179 "id": 23,
2180 "type": "storage_box",
2181 },
2182 {
2183 "id": 42,
2184 "type": "storage_box_subaccount"
2185 },
2186 ],
2187 "error": {
2188 "code": "action_failed",
2189 "message": "Action failed",
2190 },
2191 },
2192 })
2193 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
2194 ]
2195 )
2196
2197 assert result['msg'] == "Error while updating access settings: [action_failed] Action failed"
2198
2199 def test_update_subaccount_idempotence_by_comment_2_fail(self, mocker):
2200 sleep_mock = MagicMock()
2201 mocker.patch('time.sleep', sleep_mock)
2202 input = {
2203 'hetzner_token': '',
2204 'storagebox_id': 1234,
2205 # subaccount
2206 'idempotence': 'comment',
2207 "homedirectory": "/new/homedir",
2208 "samba": True,
2209 "ssh": True,
2210 "external_reachability": True,
2211 "webdav": True,
2212 "readonly": False,
2213 "comment": "Test account",
2214 }
2215 result = self.run_module_failed(
2216 mocker,
2217 storagebox_subaccount,
2218 input,
2219 [
2220 FetchUrlCall('GET', 200)
2221 .result_json({
2222 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2223 })
2224 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL)),
2225
2226 FetchUrlCall('POST', 200)
2227 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/change_home_directory'.format(API_BASE_URL))
2228 .expect_json_value(["home_directory"], 'new/homedir')
2229 .result_json({
2230 "action": {
2231 "id": 12,
2232 "command": "change_home_directory",
2233 "status": "running",
2234 "progress": 0,
2235 "started": "2016-01-30T23:50:00+00:00",
2236 "finished": None,
2237 "resources": [
2238 {
2239 "id": 1234,
2240 "type": "storage_box",
2241 },
2242 {
2243 "id": 42,
2244 "type": "storage_box_subaccount"
2245 },
2246 ],
2247 "error": None,
2248 },
2249 }),
2250 FetchUrlCall('GET', 200)
2251 .result_json({
2252 "action": {
2253 "id": 12,
2254 "command": "change_home_directory",
2255 "status": "error",
2256 "progress": 0,
2257 "started": "2016-01-30T23:50:00+00:00",
2258 "finished": "2016-01-30T23:50:00+00:00",
2259 "resources": [
2260 {
2261 "id": 23,
2262 "type": "storage_box",
2263 },
2264 {
2265 "id": 42,
2266 "type": "storage_box_subaccount"
2267 },
2268 ],
2269 "error": {
2270 "code": "action_failed",
2271 "message": "Action failed",
2272 },
2273 },
2274 })
2275 .expect_url('{0}/v1/storage_boxes/actions/12'.format(API_BASE_URL)),
2276 ]
2277 )
2278
2279 assert result['msg'] == "Error while updating home directory: [action_failed] Action failed"
2280
2281 def test_update_password_only(self, mocker):
2282 sleep_mock = MagicMock()
2283 mocker.patch('time.sleep', sleep_mock)
2284 result = self.run_module_success(
2285 mocker,
2286 storagebox_subaccount,
2287 {
2288 'hetzner_token': '',
2289 'storagebox_id': 1234,
2290 # account
2291 "password": "newsecurepassword",
2292 "username": "u2342-sub1",
2293 "homedirectory": "test",
2294 "samba": True,
2295 "ssh": True,
2296 "external_reachability": True,
2297 "webdav": False,
2298 "readonly": False,
2299 "comment": "Test account",
2300 },
2301 [
2302 FetchUrlCall('GET', 200)
2303 .result_json({
2304 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2305 })
2306 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL)),
2307
2308 FetchUrlCall('POST', 200)
2309 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password'.format(API_BASE_URL))
2310 .expect_json_value(["password"], "newsecurepassword")
2311 .result_json({
2312 "action": {
2313 "id": 13,
2314 "command": "reset_subaccount_password",
2315 "status": "running",
2316 "progress": 0,
2317 "started": "2016-01-30T23:50:00+00:00",
2318 "finished": None,
2319 "resources": [
2320 {
2321 "id": 1234,
2322 "type": "storage_box",
2323 },
2324 {
2325 "id": 1,
2326 "type": "storage_box_subaccount"
2327 },
2328 ],
2329 "error": None,
2330 }
2331 }),
2332 FetchUrlCall('GET', 200)
2333 .result_json({
2334 "action": {
2335 "id": 13,
2336 "command": "reset_subaccount_password",
2337 "status": "success",
2338 "progress": 0,
2339 "started": "2016-01-30T23:50:00+00:00",
2340 "finished": "2016-01-30T23:50:00+00:00",
2341 "resources": [
2342 {
2343 "id": 1234,
2344 "type": "storage_box",
2345 },
2346 {
2347 "id": 1,
2348 "type": "storage_box_subaccount"
2349 },
2350 ],
2351 "error": None,
2352 }
2353 })
2354 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
2355 ]
2356 )
2357
2358 assert result['changed'] is True
2359 assert result['created'] is False
2360 assert result['deleted'] is False
2361 assert result['updated'] is False
2362 assert result['password_updated'] is True
2363 assert result['subaccount'] == {
2364 'access_settings': {
2365 'reachable_externally': True,
2366 'readonly': False,
2367 'samba_enabled': True,
2368 'ssh_enabled': True,
2369 'webdav_enabled': False,
2370 },
2371 'username': 'u2342-sub1',
2372 'server': 'u12345-sub1.your-storagebox.de',
2373 'homedirectory': 'test',
2374 'home_directory': 'test',
2375 'samba': True,
2376 'ssh': True,
2377 'external_reachability': True,
2378 'webdav': False,
2379 'readonly': False,
2380 'created': '2017-05-24 00:00:00',
2381 'createtime': '2017-05-24 00:00:00',
2382 'comment': 'Test account',
2383 'description': 'Test account',
2384 'password': 'newsecurepassword',
2385 'id': 1,
2386 'labels': {},
2387 'storage_box': 2342,
2388 }
2389
2390 def test_update_password_only_fail(self, mocker):
2391 sleep_mock = MagicMock()
2392 mocker.patch('time.sleep', sleep_mock)
2393 result = self.run_module_failed(
2394 mocker,
2395 storagebox_subaccount,
2396 {
2397 'hetzner_token': '',
2398 'storagebox_id': 1234,
2399 # account
2400 "password": "newsecurepassword",
2401 "username": "u2342-sub1",
2402 "homedirectory": "test",
2403 "samba": True,
2404 "ssh": True,
2405 "external_reachability": True,
2406 "webdav": False,
2407 "readonly": False,
2408 "comment": "Test account",
2409 },
2410 [
2411 FetchUrlCall('GET', 200)
2412 .result_json({
2413 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2414 })
2415 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL)),
2416
2417 FetchUrlCall('POST', 200)
2418 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password'.format(API_BASE_URL))
2419 .expect_json_value(["password"], "newsecurepassword")
2420 .result_json({
2421 "action": {
2422 "id": 13,
2423 "command": "reset_subaccount_password",
2424 "status": "running",
2425 "progress": 0,
2426 "started": "2016-01-30T23:50:00+00:00",
2427 "finished": None,
2428 "resources": [
2429 {
2430 "id": 1234,
2431 "type": "storage_box",
2432 },
2433 {
2434 "id": 1,
2435 "type": "storage_box_subaccount"
2436 },
2437 ],
2438 "error": None,
2439 }
2440 }),
2441 FetchUrlCall('GET', 200)
2442 .result_json({
2443 "action": {
2444 "id": 13,
2445 "command": "reset_subaccount_password",
2446 "status": "error",
2447 "progress": 0,
2448 "started": "2016-01-30T23:50:00+00:00",
2449 "finished": "2016-01-30T23:50:00+00:00",
2450 "resources": [
2451 {
2452 "id": 1234,
2453 "type": "storage_box",
2454 },
2455 {
2456 "id": 1,
2457 "type": "storage_box_subaccount"
2458 },
2459 ],
2460 "error": {
2461 "code": "action_failed",
2462 "message": "Action failed",
2463 },
2464 },
2465 })
2466 .expect_url('{0}/v1/storage_boxes/actions/13'.format(API_BASE_URL)),
2467 ]
2468 )
2469
2470 assert result['msg'] == "Error while updating password: [action_failed] Action failed"
2471
2472 def test_update_subaccount_invalid_password(self, mocker):
2473 result = self.run_module_failed(
2474 mocker,
2475 storagebox_subaccount,
2476 {
2477 'hetzner_token': '',
2478 'storagebox_id': 1234,
2479 # subaccount
2480 "password": "123",
2481 "username": "u2342-sub1",
2482 "homedirectory": "test",
2483 "samba": True,
2484 "ssh": True,
2485 "external_reachability": True,
2486 "webdav": False,
2487 "readonly": False,
2488 "comment": "Test account",
2489 },
2490 [
2491 FetchUrlCall('GET', 200)
2492 .result_json({
2493 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2494 })
2495 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL)),
2496
2497 FetchUrlCall('POST', 400)
2498 .expect_url('{0}/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password'.format(API_BASE_URL))
2499 .expect_json_value(["password"], "123")
2500 .result_json({
2501 "error": {
2502 'code': 'invalid_input',
2503 'message': "invalid input in field 'password': is too weak",
2504 'details': {
2505 'fields': [
2506 {
2507 'name': 'password',
2508 'message': 'is too weak',
2509 },
2510 ],
2511 },
2512 }
2513 })
2514 ]
2515 )
2516
2517 assert result['msg'] in (
2518 "Request POST https://api.hetzner.com/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password"
2519 " failed: [invalid_input] invalid input in field 'password': is too weak."
2520 " Details: {'fields': [{'name': 'password', 'message': 'is too weak'}]}",
2521 # Python 3.5:
2522 "Request POST https://api.hetzner.com/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password"
2523 " failed: [invalid_input] invalid input in field 'password': is too weak."
2524 " Details: {'fields': [{'message': 'is too weak', 'name': 'password'}]}",
2525 # Python 2.7:
2526 "Request POST https://api.hetzner.com/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password"
2527 " failed: [invalid_input] invalid input in field 'password': is too weak."
2528 " Details: {u'fields': [{u'name': u'password', u'message': u'is too weak'}]}",
2529 "Request POST https://api.hetzner.com/v1/storage_boxes/1234/subaccounts/1/actions/reset_subaccount_password"
2530 " failed: [invalid_input] invalid input in field 'password': is too weak."
2531 " Details: {u'fields': [{u'message': u'is too weak', u'name': u'password'}]}",
2532 )
2533
2534 def test_invalid_spec_create_no_homedir(self, mocker):
2535 result = self.run_module_failed(
2536 mocker,
2537 storagebox_subaccount,
2538 {
2539 'hetzner_token': '',
2540 'storagebox_id': 1234,
2541 'state': 'present',
2542 },
2543 [
2544 FetchUrlCall('GET', 200)
2545 .result_json({
2546 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2547 })
2548 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
2549 ]
2550 )
2551
2552 assert result['msg'] == "homedirectory is required when creating a new subaccount"
2553
2554 # Check mode tests
2555 def test_update_password_only_CHECK_MODE(self, mocker):
2556 result = self.run_module_success(
2557 mocker,
2558 storagebox_subaccount,
2559 {
2560 'hetzner_token': '',
2561 'storagebox_id': 1234,
2562 '_ansible_check_mode': True,
2563 # subaccount
2564 "password": "newsecurepassword",
2565 "username": "u2342-sub1",
2566 "homedirectory": "test",
2567 "samba": True,
2568 "ssh": True,
2569 "external_reachability": True,
2570 "webdav": False,
2571 "readonly": False,
2572 "comment": "Test account",
2573 },
2574 [
2575 FetchUrlCall('GET', 200)
2576 .result_json({
2577 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2578 })
2579 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL))
2580 ]
2581 )
2582
2583 assert result['changed'] is True
2584 assert result['created'] is False
2585 assert result['deleted'] is False
2586 assert result['updated'] is False
2587 assert result['password_updated'] is True
2588 assert result['subaccount'] == {
2589 'access_settings': {
2590 'reachable_externally': True,
2591 'readonly': False,
2592 'samba_enabled': True,
2593 'ssh_enabled': True,
2594 'webdav_enabled': False,
2595 },
2596 'username': 'u2342-sub1',
2597 'server': 'u12345-sub1.your-storagebox.de',
2598 'homedirectory': 'test',
2599 'home_directory': 'test',
2600 'samba': True,
2601 'ssh': True,
2602 'external_reachability': True,
2603 'webdav': False,
2604 'readonly': False,
2605 'created': '2017-05-24 00:00:00',
2606 'createtime': '2017-05-24 00:00:00',
2607 'comment': 'Test account',
2608 'description': 'Test account',
2609 'id': 1,
2610 'labels': {},
2611 'storage_box': 2342,
2612 'password': 'newsecurepassword',
2613 }
2614
2615 def test_update_home_directory_only_CHECK_MODE(self, mocker):
2616 result = self.run_module_success(
2617 mocker,
2618 storagebox_subaccount,
2619 {
2620 'hetzner_token': '',
2621 'storagebox_id': 1234,
2622 '_ansible_check_mode': True,
2623 # subaccount
2624 "username": "u2342-sub1",
2625 "homedirectory": "test2",
2626 "samba": True,
2627 "ssh": True,
2628 "external_reachability": True,
2629 "webdav": False,
2630 "readonly": False,
2631 "comment": "Test account",
2632 },
2633 [
2634 FetchUrlCall('GET', 200)
2635 .result_json({
2636 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2637 })
2638 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL))
2639 ]
2640 )
2641
2642 assert result['changed'] is True
2643 assert result['created'] is False
2644 assert result['deleted'] is False
2645 assert result['updated'] is True
2646 assert result['password_updated'] is False
2647 assert result['subaccount'] == {
2648 'access_settings': {
2649 'reachable_externally': True,
2650 'readonly': False,
2651 'samba_enabled': True,
2652 'ssh_enabled': True,
2653 'webdav_enabled': False,
2654 },
2655 'username': 'u2342-sub1',
2656 'server': 'u12345-sub1.your-storagebox.de',
2657 'homedirectory': 'test2',
2658 'home_directory': 'test2',
2659 'samba': True,
2660 'ssh': True,
2661 'external_reachability': True,
2662 'webdav': False,
2663 'readonly': False,
2664 'created': '2017-05-24 00:00:00',
2665 'createtime': '2017-05-24 00:00:00',
2666 'comment': 'Test account',
2667 'description': 'Test account',
2668 'id': 1,
2669 'labels': {},
2670 'storage_box': 2342,
2671 }
2672
2673 def test_create_subaccount_CHECK_MODE(self, mocker):
2674 result = self.run_module_success(
2675 mocker,
2676 storagebox_subaccount,
2677 {
2678 'hetzner_token': '',
2679 'storagebox_id': 1234,
2680 # subaccount,
2681 'homedirectory': '/data/newsub',
2682 'samba': True,
2683 'ssh': False,
2684 'webdav': True,
2685 'readonly': False,
2686 'comment': 'My new subaccount',
2687 'password': 'toto',
2688 '_ansible_check_mode': True,
2689 },
2690 [
2691 FetchUrlCall('GET', 200)
2692 .result_json({
2693 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2694 })
2695 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
2696 ]
2697 )
2698
2699 assert result['changed'] is True
2700 assert result['created'] is True
2701 assert result['deleted'] is False
2702 assert result['updated'] is False
2703 assert result['password_updated'] is False
2704 assert result['subaccount'] == {
2705 'access_settings': {
2706 'readonly': False,
2707 'samba_enabled': True,
2708 'ssh_enabled': False,
2709 'webdav_enabled': True,
2710 },
2711 'description': 'My new subaccount',
2712 'home_directory': '/data/newsub',
2713 'homedirectory': '/data/newsub',
2714 'samba': True,
2715 'ssh': False,
2716 'webdav': True,
2717 'readonly': False,
2718 'comment': 'My new subaccount',
2719 'password': 'toto',
2720 }
2721
2722 def test_create_subaccount_CHECK_MODE_less_info(self, mocker):
2723 result = self.run_module_success(
2724 mocker,
2725 storagebox_subaccount,
2726 {
2727 'hetzner_token': '',
2728 'storagebox_id': 1234,
2729 # subaccount,
2730 'homedirectory': '/data/newsub',
2731 'comment': 'My new subaccount',
2732 'password': 'toto',
2733 '_ansible_check_mode': True,
2734 },
2735 [
2736 FetchUrlCall('GET', 200)
2737 .result_json({
2738 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2739 })
2740 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
2741 ]
2742 )
2743
2744 assert result['changed'] is True
2745 assert result['created'] is True
2746 assert result['deleted'] is False
2747 assert result['updated'] is False
2748 assert result['password_updated'] is False
2749 assert result['subaccount'] == {
2750 'description': 'My new subaccount',
2751 'home_directory': '/data/newsub',
2752 'homedirectory': '/data/newsub',
2753 'comment': 'My new subaccount',
2754 'password': 'toto',
2755 }
2756
2757 def test_update_subaccount_CHECK_MODE(self, mocker):
2758 result = self.run_module_success(
2759 mocker,
2760 storagebox_subaccount,
2761 {
2762 'hetzner_token': '',
2763 'storagebox_id': 1234,
2764 '_ansible_check_mode': True,
2765 # subaccount
2766 "username": "u2342-sub1",
2767 "homedirectory": "test",
2768 "samba": True,
2769 "ssh": True,
2770 "external_reachability": True,
2771 "webdav": False,
2772 "readonly": True,
2773 "comment": "new comment",
2774
2775 },
2776 [
2777 FetchUrlCall('GET', 200)
2778 .result_json({
2779 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2780 })
2781 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL))
2782 ]
2783 )
2784
2785 assert result['changed'] is True
2786 assert result['created'] is False
2787 assert result['deleted'] is False
2788 assert result['updated'] is True
2789 assert result['password_updated'] is False
2790 assert result['subaccount'] == {
2791 'access_settings': {
2792 'reachable_externally': True,
2793 'readonly': True,
2794 'samba_enabled': True,
2795 'ssh_enabled': True,
2796 'webdav_enabled': False,
2797 },
2798 'username': 'u2342-sub1',
2799 'server': 'u12345-sub1.your-storagebox.de',
2800 'homedirectory': 'test',
2801 'home_directory': 'test',
2802 'samba': True,
2803 'ssh': True,
2804 'external_reachability': True,
2805 'webdav': False,
2806 'readonly': True,
2807 'created': '2017-05-24 00:00:00',
2808 'createtime': '2017-05-24 00:00:00',
2809 'comment': 'new comment',
2810 'description': 'new comment',
2811 'id': 1,
2812 'labels': {},
2813 'storage_box': 2342,
2814 }
2815
2816 def test_update_subaccount_CHECK_MODE_less_info(self, mocker):
2817 result = self.run_module_success(
2818 mocker,
2819 storagebox_subaccount,
2820 {
2821 'hetzner_token': '',
2822 'storagebox_id': 1234,
2823 '_ansible_check_mode': True,
2824 # subaccount
2825 "username": "u2342-sub1",
2826 "comment": "new comment",
2827
2828 },
2829 [
2830 FetchUrlCall('GET', 200)
2831 .result_json({
2832 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2833 })
2834 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub1'.format(API_BASE_URL))
2835 ]
2836 )
2837
2838 assert result['changed'] is True
2839 assert result['created'] is False
2840 assert result['deleted'] is False
2841 assert result['updated'] is True
2842 assert result['password_updated'] is False
2843 assert result['subaccount'] == {
2844 'access_settings': {
2845 'reachable_externally': True,
2846 'readonly': False,
2847 'samba_enabled': True,
2848 'ssh_enabled': True,
2849 'webdav_enabled': False,
2850 },
2851 'username': 'u2342-sub1',
2852 'server': 'u12345-sub1.your-storagebox.de',
2853 'homedirectory': 'test',
2854 'home_directory': 'test',
2855 'samba': True,
2856 'ssh': True,
2857 'external_reachability': True,
2858 'webdav': False,
2859 'readonly': False,
2860 'created': '2017-05-24 00:00:00',
2861 'createtime': '2017-05-24 00:00:00',
2862 'comment': 'new comment',
2863 'description': 'new comment',
2864 'id': 1,
2865 'labels': {},
2866 'storage_box': 2342,
2867 }
2868
2869 def test_delete_existing_subaccount_CHECK_MODE(self, mocker):
2870 """Test successful deletion of an existing subaccount."""
2871 result = self.run_module_success(mocker, storagebox_subaccount, {
2872 'hetzner_token': '',
2873 'storagebox_id': 1234,
2874 'state': 'absent',
2875 'username': 'u2342-sub2',
2876 '_ansible_check_mode': True,
2877 }, [
2878 FetchUrlCall('GET', 200)
2879 .result_json({
2880 "subaccounts": STORAGEBOX_SUBACCOUNTS,
2881 })
2882 .expect_url('{0}/v1/storage_boxes/1234/subaccounts?username=u2342-sub2'.format(API_BASE_URL))
2883 ])
2884
2885 assert result['changed'] is True
2886 assert result['created'] is False
2887 assert result['deleted'] is True
2888 assert result['updated'] is False
2889 assert result['password_updated'] is False
2890 assert result['subaccount'] is None
2891
2892 def test_broken_idempotence_same_comment_multiple_accounts(self, mocker):
2893 result = self.run_module_failed(mocker, storagebox_subaccount, {
2894 'hetzner_token': '',
2895 'storagebox_id': 1234,
2896 'idempotence': 'comment',
2897 'comment': STORAGEBOX_SUBACCOUNTS[0]['description'],
2898 }, [
2899 FetchUrlCall('GET', 200)
2900 .result_json({
2901 "subaccounts": [STORAGEBOX_SUBACCOUNTS[0], STORAGEBOX_SUBACCOUNTS[0]],
2902 })
2903 .expect_url('{0}/v1/storage_boxes/1234/subaccounts'.format(API_BASE_URL))
2904 ])
2905
2906 assert result['msg'] == "More than one subaccount matched the idempotence criteria."