2 # -*- coding: utf-8 -*-
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 __author__
= "Pedro de la Cruz Ramos, pedro.delacruzramos@altran.com"
18 __date__
= "$2019-10-019"
21 from unittest
import TestCase
22 from unittest
.mock
import Mock
, patch
, call
23 from uuid
import uuid4
24 from http
import HTTPStatus
26 from random
import randint
27 from osm_common
import dbbase
, fsbase
, msgbase
28 from osm_nbi
import authconn
, validation
29 from osm_nbi
.admin_topics
import (
37 from osm_nbi
.engine
import EngineException
38 from osm_nbi
.authconn
import AuthconnNotFoundException
41 test_pid
= str(uuid4())
42 test_name
= "test-user"
46 """Normalize string for checking"""
47 return " ".join(str.strip().split()).lower()
50 class TestVcaTopic(TestCase
):
52 self
.db
= Mock(dbbase
.DbBase())
53 self
.fs
= Mock(fsbase
.FsBase())
54 self
.msg
= Mock(msgbase
.MsgBase())
55 self
.auth
= Mock(authconn
.Authconn(None, None, None))
56 self
.vca_topic
= VcaTopic(self
.db
, self
.fs
, self
.msg
, self
.auth
)
58 @patch("osm_nbi.admin_topics.CommonVimWimSdn.format_on_new")
59 def test_format_on_new(self
, mock_super_format_on_new
):
62 "secret": "encrypted_secret",
63 "cacert": "encrypted_cacert",
65 self
.db
.encrypt
.side_effect
= ["secret", "cacert"]
66 mock_super_format_on_new
.return_value
= "1234"
68 oid
= self
.vca_topic
.format_on_new(content
)
70 self
.assertEqual(oid
, "1234")
71 self
.assertEqual(content
["secret"], "secret")
72 self
.assertEqual(content
["cacert"], "cacert")
73 self
.db
.encrypt
.assert_has_calls(
75 call("encrypted_secret", schema_version
="1.11", salt
="id"),
76 call("encrypted_cacert", schema_version
="1.11", salt
="id"),
79 mock_super_format_on_new
.assert_called_with(content
, None, False)
81 @patch("osm_nbi.admin_topics.CommonVimWimSdn.format_on_edit")
82 def test_format_on_edit(self
, mock_super_format_on_edit
):
85 "secret": "encrypted_secret",
86 "cacert": "encrypted_cacert",
90 "schema_version": "1.11",
92 self
.db
.encrypt
.side_effect
= ["secret", "cacert"]
93 mock_super_format_on_edit
.return_value
= "1234"
95 oid
= self
.vca_topic
.format_on_edit(final_content
, edit_content
)
97 self
.assertEqual(oid
, "1234")
98 self
.assertEqual(final_content
["secret"], "secret")
99 self
.assertEqual(final_content
["cacert"], "cacert")
100 self
.db
.encrypt
.assert_has_calls(
102 call("encrypted_secret", schema_version
="1.11", salt
="id"),
103 call("encrypted_cacert", schema_version
="1.11", salt
="id"),
106 mock_super_format_on_edit
.assert_called()
108 @patch("osm_nbi.admin_topics.CommonVimWimSdn.check_conflict_on_del")
109 def test_check_conflict_on_del(self
, mock_check_conflict_on_del
):
111 "project_id": "project-id",
117 self
.db
.get_list
.return_value
= None
119 self
.vca_topic
.check_conflict_on_del(session
, _id
, db_content
)
121 self
.db
.get_list
.assert_called_with(
123 {"vca": _id
, "_admin.projects_read.cont": "project-id"},
125 mock_check_conflict_on_del
.assert_called_with(session
, _id
, db_content
)
127 @patch("osm_nbi.admin_topics.CommonVimWimSdn.check_conflict_on_del")
128 def test_check_conflict_on_del_force(self
, mock_check_conflict_on_del
):
130 "project_id": "project-id",
136 self
.vca_topic
.check_conflict_on_del(session
, _id
, db_content
)
138 self
.db
.get_list
.assert_not_called()
139 mock_check_conflict_on_del
.assert_not_called()
141 @patch("osm_nbi.admin_topics.CommonVimWimSdn.check_conflict_on_del")
142 def test_check_conflict_on_del_with_conflict(self
, mock_check_conflict_on_del
):
144 "project_id": "project-id",
150 self
.db
.get_list
.return_value
= {"_id": "vim", "vca": "vca-id"}
152 with self
.assertRaises(EngineException
) as context
:
153 self
.vca_topic
.check_conflict_on_del(session
, _id
, db_content
)
157 "There is at least one VIM account using this vca",
158 http_code
=HTTPStatus
.CONFLICT
,
162 self
.db
.get_list
.assert_called_with(
164 {"vca": _id
, "_admin.projects_read.cont": "project-id"},
166 mock_check_conflict_on_del
.assert_not_called()
169 class TestPaaSTopic(TestCase
):
171 self
.db
= Mock(dbbase
.DbBase())
172 self
.fs
= Mock(fsbase
.FsBase())
173 self
.msg
= Mock(msgbase
.MsgBase())
174 self
.auth
= Mock(authconn
.Authconn(None, None, None))
175 self
.paas_topic
= PaasTopic(self
.db
, self
.fs
, self
.msg
, self
.auth
)
177 def test_format_on_new(self
):
180 "secret": "secret_to_encrypt",
182 self
.db
.encrypt
.side_effect
= ["encrypted_secret"]
185 expected_num_operations
= 1
186 oid
= self
.paas_topic
.format_on_new(content
)
188 self
.assertEqual(oid
, expecte_oid
)
189 self
.assertEqual(content
["secret"], "encrypted_secret")
190 self
.assertEqual(content
["_admin"]["operationalState"], "PROCESSING")
191 self
.assertEqual(content
["_admin"]["current_operation"], None)
192 self
.assertEqual(len(content
["_admin"]["operations"]), expected_num_operations
)
194 content
["_admin"]["operations"][0]["lcmOperationType"], "create"
196 self
.db
.encrypt
.assert_called_with(
197 "secret_to_encrypt", schema_version
="1.11", salt
="id"
200 @patch("osm_nbi.base_topic.BaseTopic._get_project_filter")
201 def test_check_conflict_on_new(self
, mock_get_project_filter
):
202 indata
= {"name": "new_paas_name"}
204 mock_get_project_filter
.return_value
= {}
205 self
.db
.get_one
.return_value
= None
206 self
.paas_topic
.check_conflict_on_new(session
, indata
)
208 @patch("osm_nbi.base_topic.BaseTopic._get_project_filter")
209 def test_check_conflict_on_new_raise_exception(self
, mock_get_project_filter
):
210 indata
= {"name": "new_paas_name"}
212 mock_get_project_filter
.return_value
= {}
213 self
.db
.get_one
.return_value
= ["Found_PaaS"]
214 with self
.assertRaises(EngineException
):
215 self
.paas_topic
.check_conflict_on_new(session
, indata
)
217 @patch("osm_nbi.base_topic.BaseTopic._get_project_filter")
218 def test_check_conflict_on_edit(self
, mock_get_project_filter
):
219 edit_content
= {"name": "new_paas_name"}
221 session
= {"force": None}
222 mock_get_project_filter
.return_value
= {}
223 self
.db
.get_one
.return_value
= None
224 self
.paas_topic
.check_conflict_on_edit(
225 session
, final_content
, edit_content
, "id"
228 @patch("osm_nbi.base_topic.BaseTopic._get_project_filter")
229 def test_check_conflict_on_edit_raise_exception(self
, mock_get_project_filter
):
230 edit_content
= {"name": "new_paas_name"}
232 session
= {"force": None}
233 mock_get_project_filter
.return_value
= {}
234 self
.db
.get_one
.return_value
= ["Found_PaaS"]
235 with self
.assertRaises(EngineException
):
236 self
.paas_topic
.check_conflict_on_edit(
237 session
, final_content
, edit_content
, "id"
240 def test_format_on_edit(self
):
243 "secret": "secret_to_encrypt",
247 "_admin": {"operations": [{"lcmOperationType": "create"}]},
248 "schema_version": "1.11",
250 self
.db
.encrypt
.side_effect
= ["encrypted_secret"]
251 expected_oid
= "id:1"
252 expected_num_operations
= 2
253 print(self
.paas_topic
.password_to_encrypt
)
254 oid
= self
.paas_topic
.format_on_edit(final_content
, edit_content
)
256 self
.assertEqual(oid
, expected_oid
)
257 self
.assertEqual(final_content
["secret"], "encrypted_secret")
259 len(final_content
["_admin"]["operations"]), expected_num_operations
261 self
.assertEqual(final_content
["_admin"]["operationalState"], "PROCESSING")
262 self
.assertEqual(final_content
["_admin"]["detailed-status"], "Editing")
263 self
.db
.encrypt
.assert_called_with(
264 "secret_to_encrypt", schema_version
="1.11", salt
="id"
268 class Test_ProjectTopicAuth(TestCase
):
271 cls
.test_name
= "test-project-topic"
274 self
.db
= Mock(dbbase
.DbBase())
275 self
.fs
= Mock(fsbase
.FsBase())
276 self
.msg
= Mock(msgbase
.MsgBase())
277 self
.auth
= Mock(authconn
.Authconn(None, None, None))
278 self
.topic
= ProjectTopicAuth(self
.db
, self
.fs
, self
.msg
, self
.auth
)
279 self
.fake_session
= {
280 "username": self
.test_name
,
281 "project_id": (test_pid
,),
286 "allow_show_user_project_role": True,
288 self
.topic
.check_quota
= Mock(return_value
=None) # skip quota
290 def test_new_project(self
):
291 with self
.subTest(i
=1):
294 self
.auth
.get_project_list
.return_value
= []
295 self
.auth
.create_project
.return_value
= pid1
296 pid2
, oid
= self
.topic
.new(
297 rollback
, self
.fake_session
, {"name": self
.test_name
, "quotas": {}}
299 self
.assertEqual(len(rollback
), 1, "Wrong rollback length")
300 self
.assertEqual(pid2
, pid1
, "Wrong project identifier")
301 content
= self
.auth
.create_project
.call_args
[0][0]
302 self
.assertEqual(content
["name"], self
.test_name
, "Wrong project name")
303 self
.assertEqual(content
["quotas"], {}, "Wrong quotas")
304 self
.assertIsNotNone(content
["_admin"]["created"], "Wrong creation time")
306 content
["_admin"]["modified"],
307 content
["_admin"]["created"],
308 "Wrong modification time",
310 with self
.subTest(i
=2):
312 with self
.assertRaises(EngineException
, msg
="Accepted wrong quotas") as e
:
316 {"name": "other-project-name", "quotas": {"baditems": 10}},
318 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
320 e
.exception
.http_code
,
321 HTTPStatus
.UNPROCESSABLE_ENTITY
,
322 "Wrong HTTP status code",
325 "format error at 'quotas' 'additional properties are not allowed ('{}' was unexpected)'".format(
328 norm(str(e
.exception
)),
329 "Wrong exception text",
332 def test_edit_project(self
):
337 "name": self
.test_name
,
338 "_admin": {"created": now
, "modified": now
},
340 with self
.subTest(i
=1):
341 self
.auth
.get_project_list
.side_effect
= [[proj
], []]
342 new_name
= "new-project-name"
343 quotas
= {"vnfds": randint(0, 100), "nsds": randint(0, 100)}
345 self
.fake_session
, pid
, {"name": new_name
, "quotas": quotas
}
347 _id
, content
= self
.auth
.update_project
.call_args
[0]
348 self
.assertEqual(_id
, pid
, "Wrong project identifier")
349 self
.assertEqual(content
["_id"], pid
, "Wrong project identifier")
350 self
.assertEqual(content
["_admin"]["created"], now
, "Wrong creation time")
352 content
["_admin"]["modified"], now
, "Wrong modification time"
354 self
.assertEqual(content
["name"], new_name
, "Wrong project name")
355 self
.assertEqual(content
["quotas"], quotas
, "Wrong quotas")
356 with self
.subTest(i
=2):
357 new_name
= "other-project-name"
358 quotas
= {"baditems": randint(0, 100)}
359 self
.auth
.get_project_list
.side_effect
= [[proj
], []]
360 with self
.assertRaises(EngineException
, msg
="Accepted wrong quotas") as e
:
362 self
.fake_session
, pid
, {"name": new_name
, "quotas": quotas
}
365 e
.exception
.http_code
,
366 HTTPStatus
.UNPROCESSABLE_ENTITY
,
367 "Wrong HTTP status code",
370 "format error at 'quotas' 'additional properties are not allowed ('{}' was unexpected)'".format(
373 norm(str(e
.exception
)),
374 "Wrong exception text",
377 def test_conflict_on_new(self
):
378 with self
.subTest(i
=1):
381 with self
.assertRaises(
382 EngineException
, msg
="Accepted uuid as project name"
384 self
.topic
.new(rollback
, self
.fake_session
, {"name": pid
})
385 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
387 e
.exception
.http_code
,
388 HTTPStatus
.UNPROCESSABLE_ENTITY
,
389 "Wrong HTTP status code",
392 "project name '{}' cannot have an uuid format".format(pid
),
393 norm(str(e
.exception
)),
394 "Wrong exception text",
396 with self
.subTest(i
=2):
398 self
.auth
.get_project_list
.return_value
= [
399 {"_id": test_pid
, "name": self
.test_name
}
401 with self
.assertRaises(
402 EngineException
, msg
="Accepted existing project name"
404 self
.topic
.new(rollback
, self
.fake_session
, {"name": self
.test_name
})
405 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
407 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
410 "project '{}' exists".format(self
.test_name
),
411 norm(str(e
.exception
)),
412 "Wrong exception text",
415 def test_conflict_on_edit(self
):
416 with self
.subTest(i
=1):
417 self
.auth
.get_project_list
.return_value
= [
418 {"_id": test_pid
, "name": self
.test_name
}
420 new_name
= str(uuid4())
421 with self
.assertRaises(
422 EngineException
, msg
="Accepted uuid as project name"
424 self
.topic
.edit(self
.fake_session
, test_pid
, {"name": new_name
})
426 e
.exception
.http_code
,
427 HTTPStatus
.UNPROCESSABLE_ENTITY
,
428 "Wrong HTTP status code",
431 "project name '{}' cannot have an uuid format".format(new_name
),
432 norm(str(e
.exception
)),
433 "Wrong exception text",
435 with self
.subTest(i
=2):
437 self
.auth
.get_project_list
.return_value
= [{"_id": pid
, "name": "admin"}]
438 with self
.assertRaises(
439 EngineException
, msg
="Accepted renaming of project 'admin'"
441 self
.topic
.edit(self
.fake_session
, pid
, {"name": "new-name"})
443 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
446 "you cannot rename project 'admin'",
447 norm(str(e
.exception
)),
448 "Wrong exception text",
450 with self
.subTest(i
=3):
451 new_name
= "new-project-name"
452 self
.auth
.get_project_list
.side_effect
= [
453 [{"_id": test_pid
, "name": self
.test_name
}],
454 [{"_id": str(uuid4()), "name": new_name
}],
456 with self
.assertRaises(
457 EngineException
, msg
="Accepted existing project name"
459 self
.topic
.edit(self
.fake_session
, pid
, {"name": new_name
})
461 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
464 "project '{}' is already used".format(new_name
),
465 norm(str(e
.exception
)),
466 "Wrong exception text",
469 def test_delete_project(self
):
470 with self
.subTest(i
=1):
472 self
.auth
.get_project
.return_value
= {
474 "name": "other-project-name",
476 self
.auth
.delete_project
.return_value
= {"deleted": 1}
477 self
.auth
.get_user_list
.return_value
= []
478 self
.db
.get_list
.return_value
= []
479 rc
= self
.topic
.delete(self
.fake_session
, pid
)
480 self
.assertEqual(rc
, {"deleted": 1}, "Wrong project deletion return info")
482 self
.auth
.get_project
.call_args
[0][0], pid
, "Wrong project identifier"
485 self
.auth
.delete_project
.call_args
[0][0],
487 "Wrong project identifier",
490 def test_conflict_on_del(self
):
491 with self
.subTest(i
=1):
492 self
.auth
.get_project
.return_value
= {
494 "name": self
.test_name
,
496 with self
.assertRaises(
497 EngineException
, msg
="Accepted deletion of own project"
499 self
.topic
.delete(self
.fake_session
, self
.test_name
)
501 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
504 "you cannot delete your own project",
505 norm(str(e
.exception
)),
506 "Wrong exception text",
508 with self
.subTest(i
=2):
509 self
.auth
.get_project
.return_value
= {"_id": str(uuid4()), "name": "admin"}
510 with self
.assertRaises(
511 EngineException
, msg
="Accepted deletion of project 'admin'"
513 self
.topic
.delete(self
.fake_session
, "admin")
515 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
518 "you cannot delete project 'admin'",
519 norm(str(e
.exception
)),
520 "Wrong exception text",
522 with self
.subTest(i
=3):
524 name
= "other-project-name"
525 self
.auth
.get_project
.return_value
= {"_id": pid
, "name": name
}
526 self
.auth
.get_user_list
.return_value
= [
529 "username": self
.test_name
,
530 "project_role_mappings": [{"project": pid
, "role": str(uuid4())}],
533 with self
.assertRaises(
534 EngineException
, msg
="Accepted deletion of used project"
536 self
.topic
.delete(self
.fake_session
, pid
)
538 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
541 "project '{}' ({}) is being used by user '{}'".format(
542 name
, pid
, self
.test_name
544 norm(str(e
.exception
)),
545 "Wrong exception text",
547 with self
.subTest(i
=4):
548 self
.auth
.get_user_list
.return_value
= []
549 self
.db
.get_list
.return_value
= [
552 "id": self
.test_name
,
553 "_admin": {"projects_read": [pid
], "projects_write": []},
556 with self
.assertRaises(
557 EngineException
, msg
="Accepted deletion of used project"
559 self
.topic
.delete(self
.fake_session
, pid
)
561 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
564 "project '{}' ({}) is being used by {} '{}'".format(
565 name
, pid
, "vnf descriptor", self
.test_name
567 norm(str(e
.exception
)),
568 "Wrong exception text",
572 class Test_RoleTopicAuth(TestCase
):
575 cls
.test_name
= "test-role-topic"
576 cls
.test_operations
= ["tokens:get"]
579 self
.db
= Mock(dbbase
.DbBase())
580 self
.fs
= Mock(fsbase
.FsBase())
581 self
.msg
= Mock(msgbase
.MsgBase())
582 self
.auth
= Mock(authconn
.Authconn(None, None, None))
583 self
.auth
.role_permissions
= self
.test_operations
584 self
.topic
= RoleTopicAuth(self
.db
, self
.fs
, self
.msg
, self
.auth
)
585 self
.fake_session
= {
586 "username": test_name
,
587 "project_id": (test_pid
,),
592 "allow_show_user_project_role": True,
594 self
.topic
.check_quota
= Mock(return_value
=None) # skip quota
596 def test_new_role(self
):
597 with self
.subTest(i
=1):
600 perms_in
= {"tokens": True}
601 perms_out
= {"default": False, "admin": False, "tokens": True}
602 self
.auth
.get_role_list
.return_value
= []
603 self
.auth
.create_role
.return_value
= rid1
604 rid2
, oid
= self
.topic
.new(
607 {"name": self
.test_name
, "permissions": perms_in
},
609 self
.assertEqual(len(rollback
), 1, "Wrong rollback length")
610 self
.assertEqual(rid2
, rid1
, "Wrong project identifier")
611 content
= self
.auth
.create_role
.call_args
[0][0]
612 self
.assertEqual(content
["name"], self
.test_name
, "Wrong role name")
613 self
.assertEqual(content
["permissions"], perms_out
, "Wrong permissions")
614 self
.assertIsNotNone(content
["_admin"]["created"], "Wrong creation time")
616 content
["_admin"]["modified"],
617 content
["_admin"]["created"],
618 "Wrong modification time",
620 with self
.subTest(i
=2):
622 with self
.assertRaises(
623 EngineException
, msg
="Accepted wrong permissions"
628 {"name": "other-role-name", "permissions": {"projects": True}},
630 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
632 e
.exception
.http_code
,
633 HTTPStatus
.UNPROCESSABLE_ENTITY
,
634 "Wrong HTTP status code",
637 "invalid permission '{}'".format("projects"),
638 norm(str(e
.exception
)),
639 "Wrong exception text",
642 def test_edit_role(self
):
647 "name": self
.test_name
,
648 "permissions": {"tokens": True},
649 "_admin": {"created": now
, "modified": now
},
651 with self
.subTest(i
=1):
652 self
.auth
.get_role_list
.side_effect
= [[role
], []]
653 self
.auth
.get_role
.return_value
= role
654 new_name
= "new-role-name"
655 perms_in
= {"tokens": False, "tokens:get": True}
663 self
.fake_session
, rid
, {"name": new_name
, "permissions": perms_in
}
665 content
= self
.auth
.update_role
.call_args
[0][0]
666 self
.assertEqual(content
["_id"], rid
, "Wrong role identifier")
667 self
.assertEqual(content
["_admin"]["created"], now
, "Wrong creation time")
669 content
["_admin"]["modified"], now
, "Wrong modification time"
671 self
.assertEqual(content
["name"], new_name
, "Wrong role name")
672 self
.assertEqual(content
["permissions"], perms_out
, "Wrong permissions")
673 with self
.subTest(i
=2):
674 new_name
= "other-role-name"
675 perms_in
= {"tokens": False, "tokens:post": True}
676 self
.auth
.get_role_list
.side_effect
= [[role
], []]
677 with self
.assertRaises(
678 EngineException
, msg
="Accepted wrong permissions"
681 self
.fake_session
, rid
, {"name": new_name
, "permissions": perms_in
}
684 e
.exception
.http_code
,
685 HTTPStatus
.UNPROCESSABLE_ENTITY
,
686 "Wrong HTTP status code",
689 "invalid permission '{}'".format("tokens:post"),
690 norm(str(e
.exception
)),
691 "Wrong exception text",
694 def test_delete_role(self
):
695 with self
.subTest(i
=1):
697 role
= {"_id": rid
, "name": "other-role-name"}
698 self
.auth
.get_role_list
.return_value
= [role
]
699 self
.auth
.get_role
.return_value
= role
700 self
.auth
.delete_role
.return_value
= {"deleted": 1}
701 self
.auth
.get_user_list
.return_value
= []
702 rc
= self
.topic
.delete(self
.fake_session
, rid
)
703 self
.assertEqual(rc
, {"deleted": 1}, "Wrong role deletion return info")
705 self
.auth
.get_role_list
.call_args
[0][0]["_id"],
707 "Wrong role identifier",
710 self
.auth
.get_role
.call_args
[0][0], rid
, "Wrong role identifier"
713 self
.auth
.delete_role
.call_args
[0][0], rid
, "Wrong role identifier"
716 def test_conflict_on_new(self
):
717 with self
.subTest(i
=1):
720 with self
.assertRaises(
721 EngineException
, msg
="Accepted uuid as role name"
723 self
.topic
.new(rollback
, self
.fake_session
, {"name": rid
})
724 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
726 e
.exception
.http_code
,
727 HTTPStatus
.UNPROCESSABLE_ENTITY
,
728 "Wrong HTTP status code",
731 "role name '{}' cannot have an uuid format".format(rid
),
732 norm(str(e
.exception
)),
733 "Wrong exception text",
735 with self
.subTest(i
=2):
737 self
.auth
.get_role_list
.return_value
= [
738 {"_id": str(uuid4()), "name": self
.test_name
}
740 with self
.assertRaises(
741 EngineException
, msg
="Accepted existing role name"
743 self
.topic
.new(rollback
, self
.fake_session
, {"name": self
.test_name
})
744 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
746 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
749 "role name '{}' exists".format(self
.test_name
),
750 norm(str(e
.exception
)),
751 "Wrong exception text",
754 def test_conflict_on_edit(self
):
756 with self
.subTest(i
=1):
757 self
.auth
.get_role_list
.return_value
= [
758 {"_id": rid
, "name": self
.test_name
, "permissions": {}}
760 new_name
= str(uuid4())
761 with self
.assertRaises(
762 EngineException
, msg
="Accepted uuid as role name"
764 self
.topic
.edit(self
.fake_session
, rid
, {"name": new_name
})
766 e
.exception
.http_code
,
767 HTTPStatus
.UNPROCESSABLE_ENTITY
,
768 "Wrong HTTP status code",
771 "role name '{}' cannot have an uuid format".format(new_name
),
772 norm(str(e
.exception
)),
773 "Wrong exception text",
775 for i
, role_name
in enumerate(["system_admin", "project_admin"], start
=2):
776 with self
.subTest(i
=i
):
778 self
.auth
.get_role
.return_value
= {
783 with self
.assertRaises(
785 msg
="Accepted renaming of role '{}'".format(role_name
),
787 self
.topic
.edit(self
.fake_session
, rid
, {"name": "new-name"})
789 e
.exception
.http_code
,
790 HTTPStatus
.FORBIDDEN
,
791 "Wrong HTTP status code",
794 "you cannot rename role '{}'".format(role_name
),
795 norm(str(e
.exception
)),
796 "Wrong exception text",
798 with self
.subTest(i
=i
+ 1):
799 new_name
= "new-role-name"
800 self
.auth
.get_role_list
.side_effect
= [
801 [{"_id": rid
, "name": self
.test_name
, "permissions": {}}],
802 [{"_id": str(uuid4()), "name": new_name
, "permissions": {}}],
804 self
.auth
.get_role
.return_value
= {
806 "name": self
.test_name
,
809 with self
.assertRaises(
810 EngineException
, msg
="Accepted existing role name"
812 self
.topic
.edit(self
.fake_session
, rid
, {"name": new_name
})
814 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
817 "role name '{}' exists".format(new_name
),
818 norm(str(e
.exception
)),
819 "Wrong exception text",
822 def test_conflict_on_del(self
):
823 for i
, role_name
in enumerate(["system_admin", "project_admin"], start
=1):
824 with self
.subTest(i
=i
):
826 role
= {"_id": rid
, "name": role_name
}
827 self
.auth
.get_role_list
.return_value
= [role
]
828 self
.auth
.get_role
.return_value
= role
829 with self
.assertRaises(
831 msg
="Accepted deletion of role '{}'".format(role_name
),
833 self
.topic
.delete(self
.fake_session
, rid
)
835 e
.exception
.http_code
,
836 HTTPStatus
.FORBIDDEN
,
837 "Wrong HTTP status code",
840 "you cannot delete role '{}'".format(role_name
),
841 norm(str(e
.exception
)),
842 "Wrong exception text",
844 with self
.subTest(i
=i
+ 1):
846 name
= "other-role-name"
847 role
= {"_id": rid
, "name": name
}
848 self
.auth
.get_role_list
.return_value
= [role
]
849 self
.auth
.get_role
.return_value
= role
850 self
.auth
.get_user_list
.return_value
= [
853 "username": self
.test_name
,
854 "project_role_mappings": [{"project": str(uuid4()), "role": rid
}],
857 with self
.assertRaises(
858 EngineException
, msg
="Accepted deletion of used role"
860 self
.topic
.delete(self
.fake_session
, rid
)
862 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
865 "role '{}' ({}) is being used by user '{}'".format(
866 name
, rid
, self
.test_name
868 norm(str(e
.exception
)),
869 "Wrong exception text",
873 class Test_UserTopicAuth(TestCase
):
876 cls
.test_name
= "test-user-topic"
879 self
.db
= Mock(dbbase
.DbBase())
880 self
.fs
= Mock(fsbase
.FsBase())
881 self
.msg
= Mock(msgbase
.MsgBase())
882 self
.auth
= Mock(authconn
.Authconn(None, None, None))
883 self
.topic
= UserTopicAuth(self
.db
, self
.fs
, self
.msg
, self
.auth
)
884 self
.fake_session
= {
885 "username": test_name
,
886 "project_id": (test_pid
,),
891 "allow_show_user_project_role": True,
893 self
.topic
.check_quota
= Mock(return_value
=None) # skip quota
895 def test_new_user(self
):
898 self
.auth
.get_user_list
.return_value
= []
899 self
.auth
.get_project
.return_value
= {"_id": pid
, "name": "some_project"}
900 self
.auth
.create_user
.return_value
= {"_id": uid1
, "username": self
.test_name
}
901 with self
.subTest(i
=1):
904 self
.auth
.get_role
.return_value
= {"_id": rid
, "name": "some_role"}
905 prms_in
= [{"project": "some_project", "role": "some_role"}]
906 prms_out
= [{"project": pid
, "role": rid
}]
907 uid2
, oid
= self
.topic
.new(
911 "username": self
.test_name
,
912 "password": self
.test_name
,
913 "project_role_mappings": prms_in
,
916 self
.assertEqual(len(rollback
), 1, "Wrong rollback length")
917 self
.assertEqual(uid2
, uid1
, "Wrong project identifier")
918 content
= self
.auth
.create_user
.call_args
[0][0]
919 self
.assertEqual(content
["username"], self
.test_name
, "Wrong project name")
920 self
.assertEqual(content
["password"], self
.test_name
, "Wrong password")
922 content
["project_role_mappings"],
924 "Wrong project-role mappings",
926 self
.assertIsNotNone(content
["_admin"]["created"], "Wrong creation time")
928 content
["_admin"]["modified"],
929 content
["_admin"]["created"],
930 "Wrong modification time",
932 with self
.subTest(i
=2):
934 def_rid
= str(uuid4())
935 def_role
= {"_id": def_rid
, "name": "project_admin"}
936 self
.auth
.get_role
.return_value
= def_role
937 self
.auth
.get_role_list
.return_value
= [def_role
]
938 prms_out
= [{"project": pid
, "role": def_rid
}]
939 uid2
, oid
= self
.topic
.new(
943 "username": self
.test_name
,
944 "password": self
.test_name
,
945 "projects": ["some_project"],
948 self
.assertEqual(len(rollback
), 1, "Wrong rollback length")
949 self
.assertEqual(uid2
, uid1
, "Wrong project identifier")
950 content
= self
.auth
.create_user
.call_args
[0][0]
951 self
.assertEqual(content
["username"], self
.test_name
, "Wrong project name")
952 self
.assertEqual(content
["password"], self
.test_name
, "Wrong password")
954 content
["project_role_mappings"],
956 "Wrong project-role mappings",
958 self
.assertIsNotNone(content
["_admin"]["created"], "Wrong creation time")
960 content
["_admin"]["modified"],
961 content
["_admin"]["created"],
962 "Wrong modification time",
964 with self
.subTest(i
=3):
966 with self
.assertRaises(
967 EngineException
, msg
="Accepted wrong project-role mappings"
973 "username": "other-project-name",
974 "password": "other-password",
975 "project_role_mappings": [{}],
978 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
980 e
.exception
.http_code
,
981 HTTPStatus
.UNPROCESSABLE_ENTITY
,
982 "Wrong HTTP status code",
985 "format error at '{}' '{}'".format(
986 "project_role_mappings:{}", "'{}' is a required property"
987 ).format(0, "project"),
988 norm(str(e
.exception
)),
989 "Wrong exception text",
991 with self
.subTest(i
=4):
993 with self
.assertRaises(EngineException
, msg
="Accepted wrong projects") as e
:
998 "username": "other-project-name",
999 "password": "other-password",
1003 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
1005 e
.exception
.http_code
,
1006 HTTPStatus
.UNPROCESSABLE_ENTITY
,
1007 "Wrong HTTP status code",
1010 "format error at '{}' '{}'".format(
1011 "projects", "{} is too short"
1013 norm(str(e
.exception
)),
1014 "Wrong exception text",
1017 def test_edit_user(self
):
1025 "project_name": "project-1",
1027 "role_name": "role-1",
1032 "username": self
.test_name
,
1033 "project_role_mappings": prms
,
1034 "_admin": {"created": now
, "modified": now
},
1036 with self
.subTest(i
=1):
1037 self
.auth
.get_user_list
.side_effect
= [[user
], []]
1038 self
.auth
.get_user
.return_value
= user
1041 self
.auth
.get_project
.side_effect
= [
1042 {"_id": pid2
, "name": "project-2"},
1043 {"_id": pid1
, "name": "project-1"},
1045 self
.auth
.get_role
.side_effect
= [
1046 {"_id": rid2
, "name": "role-2"},
1047 {"_id": rid1
, "name": "role-1"},
1049 new_name
= "new-user-name"
1050 new_pasw
= "new-password"
1051 add_prms
= [{"project": pid2
, "role": rid2
}]
1052 rem_prms
= [{"project": pid1
, "role": rid1
}]
1057 "username": new_name
,
1058 "password": new_pasw
,
1059 "add_project_role_mappings": add_prms
,
1060 "remove_project_role_mappings": rem_prms
,
1063 content
= self
.auth
.update_user
.call_args
[0][0]
1064 self
.assertEqual(content
["_id"], uid
, "Wrong user identifier")
1065 self
.assertEqual(content
["username"], new_name
, "Wrong user name")
1066 self
.assertEqual(content
["password"], new_pasw
, "Wrong user password")
1068 content
["add_project_role_mappings"],
1070 "Wrong project-role mappings to add",
1073 content
["remove_project_role_mappings"],
1075 "Wrong project-role mappings to remove",
1077 with self
.subTest(i
=2):
1078 new_name
= "other-user-name"
1080 self
.auth
.get_role_list
.side_effect
= [[user
], []]
1081 self
.auth
.get_user_list
.side_effect
= [[user
]]
1082 with self
.assertRaises(
1083 EngineException
, msg
="Accepted wrong project-role mappings"
1088 {"username": new_name
, "project_role_mappings": new_prms
},
1091 e
.exception
.http_code
,
1092 HTTPStatus
.UNPROCESSABLE_ENTITY
,
1093 "Wrong HTTP status code",
1096 "format error at '{}' '{}'".format(
1097 "project_role_mappings:{}", "'{}' is a required property"
1098 ).format(0, "project"),
1099 norm(str(e
.exception
)),
1100 "Wrong exception text",
1102 with self
.subTest(i
=3):
1103 self
.auth
.get_user_list
.side_effect
= [[user
], []]
1104 self
.auth
.get_user
.return_value
= user
1105 old_password
= self
.test_name
1106 new_pasw
= "new-password"
1111 "old_password": old_password
,
1112 "password": new_pasw
,
1115 content
= self
.auth
.update_user
.call_args
[0][0]
1117 content
["old_password"], old_password
, "Wrong old password"
1119 self
.assertEqual(content
["password"], new_pasw
, "Wrong user password")
1121 def test_delete_user(self
):
1122 with self
.subTest(i
=1):
1124 self
.fake_session
["username"] = self
.test_name
1127 "username": "other-user-name",
1128 "project_role_mappings": [],
1130 self
.auth
.get_user
.return_value
= user
1131 self
.auth
.delete_user
.return_value
= {"deleted": 1}
1132 rc
= self
.topic
.delete(self
.fake_session
, uid
)
1133 self
.assertEqual(rc
, {"deleted": 1}, "Wrong user deletion return info")
1135 self
.auth
.get_user
.call_args
[0][0], uid
, "Wrong user identifier"
1138 self
.auth
.delete_user
.call_args
[0][0], uid
, "Wrong user identifier"
1141 def test_conflict_on_new(self
):
1142 with self
.subTest(i
=1):
1145 with self
.assertRaises(
1146 EngineException
, msg
="Accepted uuid as username"
1153 "password": self
.test_name
,
1154 "projects": [test_pid
],
1157 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
1159 e
.exception
.http_code
,
1160 HTTPStatus
.UNPROCESSABLE_ENTITY
,
1161 "Wrong HTTP status code",
1164 "username '{}' cannot have a uuid format".format(uid
),
1165 norm(str(e
.exception
)),
1166 "Wrong exception text",
1168 with self
.subTest(i
=2):
1170 self
.auth
.get_user_list
.return_value
= [
1171 {"_id": str(uuid4()), "username": self
.test_name
}
1173 with self
.assertRaises(
1174 EngineException
, msg
="Accepted existing username"
1180 "username": self
.test_name
,
1181 "password": self
.test_name
,
1182 "projects": [test_pid
],
1185 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
1187 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
1190 "username '{}' is already used".format(self
.test_name
),
1191 norm(str(e
.exception
)),
1192 "Wrong exception text",
1194 with self
.subTest(i
=3):
1196 self
.auth
.get_user_list
.return_value
= []
1197 self
.auth
.get_role_list
.side_effect
= [[], []]
1198 with self
.assertRaises(
1199 AuthconnNotFoundException
, msg
="Accepted user without default role"
1205 "username": self
.test_name
,
1206 "password": self
.test_name
,
1207 "projects": [str(uuid4())],
1210 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
1212 e
.exception
.http_code
, HTTPStatus
.NOT_FOUND
, "Wrong HTTP status code"
1215 "can't find default role for user '{}'".format(self
.test_name
),
1216 norm(str(e
.exception
)),
1217 "Wrong exception text",
1220 def test_conflict_on_edit(self
):
1222 with self
.subTest(i
=1):
1223 self
.auth
.get_user_list
.return_value
= [
1224 {"_id": uid
, "username": self
.test_name
}
1226 new_name
= str(uuid4())
1227 with self
.assertRaises(
1228 EngineException
, msg
="Accepted uuid as username"
1230 self
.topic
.edit(self
.fake_session
, uid
, {"username": new_name
})
1232 e
.exception
.http_code
,
1233 HTTPStatus
.UNPROCESSABLE_ENTITY
,
1234 "Wrong HTTP status code",
1237 "username '{}' cannot have an uuid format".format(new_name
),
1238 norm(str(e
.exception
)),
1239 "Wrong exception text",
1241 with self
.subTest(i
=2):
1242 self
.auth
.get_user_list
.return_value
= [
1243 {"_id": uid
, "username": self
.test_name
}
1245 self
.auth
.get_role_list
.side_effect
= [[], []]
1246 with self
.assertRaises(
1247 AuthconnNotFoundException
, msg
="Accepted user without default role"
1249 self
.topic
.edit(self
.fake_session
, uid
, {"projects": [str(uuid4())]})
1251 e
.exception
.http_code
, HTTPStatus
.NOT_FOUND
, "Wrong HTTP status code"
1254 "can't find a default role for user '{}'".format(self
.test_name
),
1255 norm(str(e
.exception
)),
1256 "Wrong exception text",
1258 with self
.subTest(i
=3):
1259 admin_uid
= str(uuid4())
1260 self
.auth
.get_user_list
.return_value
= [
1261 {"_id": admin_uid
, "username": "admin"}
1263 with self
.assertRaises(
1265 msg
="Accepted removing system_admin role from admin user",
1271 "remove_project_role_mappings": [
1272 {"project": "admin", "role": "system_admin"}
1277 e
.exception
.http_code
, HTTPStatus
.FORBIDDEN
, "Wrong HTTP status code"
1280 "you cannot remove system_admin role from admin user",
1281 norm(str(e
.exception
)),
1282 "Wrong exception text",
1284 with self
.subTest(i
=4):
1285 new_name
= "new-user-name"
1286 self
.auth
.get_user_list
.side_effect
= [
1287 [{"_id": uid
, "name": self
.test_name
}],
1288 [{"_id": str(uuid4()), "name": new_name
}],
1290 with self
.assertRaises(
1291 EngineException
, msg
="Accepted existing username"
1293 self
.topic
.edit(self
.fake_session
, uid
, {"username": new_name
})
1295 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
1298 "username '{}' is already used".format(new_name
),
1299 norm(str(e
.exception
)),
1300 "Wrong exception text",
1303 def test_conflict_on_del(self
):
1304 with self
.subTest(i
=1):
1306 self
.fake_session
["username"] = self
.test_name
1309 "username": self
.test_name
,
1310 "project_role_mappings": [],
1312 self
.auth
.get_user
.return_value
= user
1313 with self
.assertRaises(
1314 EngineException
, msg
="Accepted deletion of own user"
1316 self
.topic
.delete(self
.fake_session
, uid
)
1318 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
1321 "you cannot delete your own login user",
1322 norm(str(e
.exception
)),
1323 "Wrong exception text",
1327 class Test_CommonVimWimSdn(TestCase
):
1329 def setUpClass(cls
):
1330 cls
.test_name
= "test-cim-topic" # CIM = Common Infrastructure Manager
1333 self
.db
= Mock(dbbase
.DbBase())
1334 self
.fs
= Mock(fsbase
.FsBase())
1335 self
.msg
= Mock(msgbase
.MsgBase())
1336 self
.auth
= Mock(authconn
.Authconn(None, None, None))
1337 self
.topic
= CommonVimWimSdn(self
.db
, self
.fs
, self
.msg
, self
.auth
)
1338 # Use WIM schemas for testing because they are the simplest
1339 self
.topic
._send
_msg
= Mock()
1340 self
.topic
.topic
= "wims"
1341 self
.topic
.schema_new
= validation
.wim_account_new_schema
1342 self
.topic
.schema_edit
= validation
.wim_account_edit_schema
1343 self
.fake_session
= {
1344 "username": test_name
,
1345 "project_id": (test_pid
,),
1350 "allow_show_user_project_role": True,
1352 self
.topic
.check_quota
= Mock(return_value
=None) # skip quota
1354 def test_new_cvws(self
):
1355 test_url
= "http://0.0.0.0:0"
1356 with self
.subTest(i
=1):
1359 self
.db
.get_one
.return_value
= None
1360 self
.db
.create
.side_effect
= lambda self
, content
: content
["_id"]
1361 cid
, oid
= self
.topic
.new(
1364 {"name": self
.test_name
, "wim_url": test_url
, "wim_type": test_type
},
1366 self
.assertEqual(len(rollback
), 1, "Wrong rollback length")
1367 args
= self
.db
.create
.call_args
[0]
1369 self
.assertEqual(args
[0], self
.topic
.topic
, "Wrong topic")
1370 self
.assertEqual(content
["_id"], cid
, "Wrong CIM identifier")
1371 self
.assertEqual(content
["name"], self
.test_name
, "Wrong CIM name")
1372 self
.assertEqual(content
["wim_url"], test_url
, "Wrong URL")
1373 self
.assertEqual(content
["wim_type"], test_type
, "Wrong CIM type")
1374 self
.assertEqual(content
["schema_version"], "1.11", "Wrong schema version")
1375 self
.assertEqual(content
["op_id"], oid
, "Wrong operation identifier")
1376 self
.assertIsNotNone(content
["_admin"]["created"], "Wrong creation time")
1378 content
["_admin"]["modified"],
1379 content
["_admin"]["created"],
1380 "Wrong modification time",
1383 content
["_admin"]["operationalState"],
1385 "Wrong operational state",
1388 content
["_admin"]["projects_read"],
1390 "Wrong read-only projects",
1393 content
["_admin"]["projects_write"],
1395 "Wrong read/write projects",
1398 content
["_admin"]["current_operation"], "Wrong current operation"
1401 len(content
["_admin"]["operations"]), 1, "Wrong number of operations"
1403 operation
= content
["_admin"]["operations"][0]
1405 operation
["lcmOperationType"], "create", "Wrong operation type"
1408 operation
["operationState"], "PROCESSING", "Wrong operation state"
1411 operation
["startTime"],
1412 content
["_admin"]["created"],
1413 "Wrong operation start time",
1416 operation
["statusEnteredTime"],
1417 content
["_admin"]["created"],
1418 "Wrong operation status enter time",
1421 operation
["detailed-status"], "", "Wrong operation detailed status info"
1424 operation
["operationParams"], "Wrong operation parameters"
1426 # This test is disabled. From Feature 8030 we admit all WIM/SDN types
1427 # with self.subTest(i=2):
1429 # test_type = "bad_type"
1430 # with self.assertRaises(EngineException, msg="Accepted wrong CIM type") as e:
1431 # self.topic.new(rollback, self.fake_session,
1432 # {"name": self.test_name, "wim_url": test_url, "wim_type": test_type})
1433 # self.assertEqual(len(rollback), 0, "Wrong rollback length")
1434 # self.assertEqual(e.exception.http_code, HTTPStatus.UNPROCESSABLE_ENTITY, "Wrong HTTP status code")
1435 # self.assertIn("format error at '{}' '{}".format("wim_type", "'{}' is not one of {}").format(test_type,""),
1436 # norm(str(e.exception)), "Wrong exception text")
1438 def test_conflict_on_new(self
):
1439 with self
.subTest(i
=1):
1441 test_url
= "http://0.0.0.0:0"
1443 self
.db
.get_one
.return_value
= {"_id": str(uuid4()), "name": self
.test_name
}
1444 with self
.assertRaises(
1445 EngineException
, msg
="Accepted existing CIM name"
1451 "name": self
.test_name
,
1452 "wim_url": test_url
,
1453 "wim_type": test_type
,
1456 self
.assertEqual(len(rollback
), 0, "Wrong rollback length")
1458 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
1461 "name '{}' already exists for {}".format(
1462 self
.test_name
, self
.topic
.topic
1464 norm(str(e
.exception
)),
1465 "Wrong exception text",
1468 def test_edit_cvws(self
):
1471 test_url
= "http://0.0.0.0:0"
1475 "name": self
.test_name
,
1476 "wim_url": test_url
,
1477 "wim_type": test_type
,
1481 "operations": [{"lcmOperationType": "create"}],
1484 with self
.subTest(i
=1):
1485 new_name
= "new-cim-name"
1486 new_url
= "https://1.1.1.1:1"
1488 self
.db
.get_one
.side_effect
= [cvws
, None]
1489 self
.db
.replace
.return_value
= {"updated": 1}
1490 # self.db.encrypt.side_effect = [b64str(), b64str()]
1494 {"name": new_name
, "wim_url": new_url
, "wim_type": new_type
},
1496 args
= self
.db
.replace
.call_args
[0]
1498 self
.assertEqual(args
[0], self
.topic
.topic
, "Wrong topic")
1499 self
.assertEqual(args
[1], cid
, "Wrong CIM identifier")
1500 self
.assertEqual(content
["_id"], cid
, "Wrong CIM identifier")
1501 self
.assertEqual(content
["name"], new_name
, "Wrong CIM name")
1502 self
.assertEqual(content
["wim_type"], new_type
, "Wrong CIM type")
1503 self
.assertEqual(content
["wim_url"], new_url
, "Wrong URL")
1504 self
.assertEqual(content
["_admin"]["created"], now
, "Wrong creation time")
1506 content
["_admin"]["modified"],
1507 content
["_admin"]["created"],
1508 "Wrong modification time",
1511 len(content
["_admin"]["operations"]), 2, "Wrong number of operations"
1513 operation
= content
["_admin"]["operations"][1]
1515 operation
["lcmOperationType"], "edit", "Wrong operation type"
1518 operation
["operationState"], "PROCESSING", "Wrong operation state"
1521 operation
["startTime"],
1522 content
["_admin"]["modified"],
1523 "Wrong operation start time",
1526 operation
["statusEnteredTime"],
1527 content
["_admin"]["modified"],
1528 "Wrong operation status enter time",
1531 operation
["detailed-status"], "", "Wrong operation detailed status info"
1534 operation
["operationParams"], "Wrong operation parameters"
1536 with self
.subTest(i
=2):
1537 self
.db
.get_one
.side_effect
= [cvws
]
1538 with self
.assertRaises(EngineException
, msg
="Accepted wrong property") as e
:
1542 {"name": "new-name", "extra_prop": "anything"},
1545 e
.exception
.http_code
,
1546 HTTPStatus
.UNPROCESSABLE_ENTITY
,
1547 "Wrong HTTP status code",
1550 "format error '{}'".format(
1551 "additional properties are not allowed ('{}' was unexpected)"
1552 ).format("extra_prop"),
1553 norm(str(e
.exception
)),
1554 "Wrong exception text",
1557 def test_conflict_on_edit(self
):
1558 with self
.subTest(i
=1):
1560 new_name
= "new-cim-name"
1561 self
.db
.get_one
.side_effect
= [
1562 {"_id": cid
, "name": self
.test_name
},
1563 {"_id": str(uuid4()), "name": new_name
},
1565 with self
.assertRaises(
1566 EngineException
, msg
="Accepted existing CIM name"
1568 self
.topic
.edit(self
.fake_session
, cid
, {"name": new_name
})
1570 e
.exception
.http_code
, HTTPStatus
.CONFLICT
, "Wrong HTTP status code"
1573 "name '{}' already exists for {}".format(new_name
, self
.topic
.topic
),
1574 norm(str(e
.exception
)),
1575 "Wrong exception text",
1578 def test_delete_cvws(self
):
1580 ro_pid
= str(uuid4())
1581 rw_pid
= str(uuid4())
1582 cvws
= {"_id": cid
, "name": self
.test_name
}
1583 self
.db
.get_list
.return_value
= []
1584 with self
.subTest(i
=1):
1586 "projects_read": [test_pid
, ro_pid
, rw_pid
],
1587 "projects_write": [test_pid
, rw_pid
],
1589 self
.db
.get_one
.return_value
= cvws
1590 oid
= self
.topic
.delete(self
.fake_session
, cid
)
1591 self
.assertIsNone(oid
, "Wrong operation identifier")
1593 self
.db
.get_one
.call_args
[0][0], self
.topic
.topic
, "Wrong topic"
1596 self
.db
.get_one
.call_args
[0][1]["_id"], cid
, "Wrong CIM identifier"
1599 self
.db
.set_one
.call_args
[0][0], self
.topic
.topic
, "Wrong topic"
1602 self
.db
.set_one
.call_args
[0][1]["_id"], cid
, "Wrong CIM identifier"
1605 self
.db
.set_one
.call_args
[1]["update_dict"],
1607 "Wrong read-only projects update",
1610 self
.db
.set_one
.call_args
[1]["pull_list"],
1612 "_admin.projects_read": (test_pid
,),
1613 "_admin.projects_write": (test_pid
,),
1615 "Wrong read/write projects update",
1617 self
.topic
._send
_msg
.assert_not_called()
1618 with self
.subTest(i
=2):
1621 "projects_read": [test_pid
],
1622 "projects_write": [test_pid
],
1625 self
.db
.get_one
.return_value
= cvws
1626 oid
= self
.topic
.delete(self
.fake_session
, cid
)
1627 self
.assertEqual(oid
, cid
+ ":0", "Wrong operation identifier")
1629 self
.db
.get_one
.call_args
[0][0], self
.topic
.topic
, "Wrong topic"
1632 self
.db
.get_one
.call_args
[0][1]["_id"], cid
, "Wrong CIM identifier"
1635 self
.db
.set_one
.call_args
[0][0], self
.topic
.topic
, "Wrong topic"
1638 self
.db
.set_one
.call_args
[0][1]["_id"], cid
, "Wrong user identifier"
1641 self
.db
.set_one
.call_args
[1]["update_dict"],
1642 {"_admin.to_delete": True},
1643 "Wrong _admin.to_delete update",
1645 operation
= self
.db
.set_one
.call_args
[1]["push"]["_admin.operations"]
1647 operation
["lcmOperationType"], "delete", "Wrong operation type"
1650 operation
["operationState"], "PROCESSING", "Wrong operation state"
1653 operation
["detailed-status"], "", "Wrong operation detailed status"
1656 operation
["operationParams"], "Wrong operation parameters"
1659 operation
["startTime"], now
, "Wrong operation start time"
1662 operation
["statusEnteredTime"], now
, "Wrong operation status enter time"
1664 self
.topic
._send
_msg
.assert_called_once_with(
1665 "delete", {"_id": cid
, "op_id": cid
+ ":0"}, not_send_msg
=None
1667 with self
.subTest(i
=3):
1669 "projects_read": [],
1670 "projects_write": [],
1673 self
.db
.get_one
.return_value
= cvws
1674 self
.topic
._send
_msg
.reset_mock()
1675 self
.db
.get_one
.reset_mock()
1676 self
.db
.del_one
.reset_mock()
1677 self
.fake_session
["force"] = True # to force deletion
1678 self
.fake_session
["admin"] = True # to force deletion
1679 self
.fake_session
["project_id"] = [] # to force deletion
1680 oid
= self
.topic
.delete(self
.fake_session
, cid
)
1681 self
.assertIsNone(oid
, "Wrong operation identifier")
1683 self
.db
.get_one
.call_args
[0][0], self
.topic
.topic
, "Wrong topic"
1686 self
.db
.get_one
.call_args
[0][1]["_id"], cid
, "Wrong CIM identifier"
1689 self
.db
.del_one
.call_args
[0][0], self
.topic
.topic
, "Wrong topic"
1692 self
.db
.del_one
.call_args
[0][1]["_id"], cid
, "Wrong CIM identifier"
1694 self
.topic
._send
_msg
.assert_called_once_with(
1695 "deleted", {"_id": cid
, "op_id": None}, not_send_msg
=None
1699 if __name__
== "__main__":