1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 from unittest
import TestCase
, mock
26 from osm_mon
.core
.common_db
import CommonDbClient
27 from osm_mon
.core
.config
import Config
28 from osm_mon
.core
.database
import VimCredentialsRepository
, VimCredentials
29 from osm_mon
.server
.service
import ServerService
32 @mock.patch
.object(CommonDbClient
, "__init__", lambda *args
, **kwargs
: None)
33 class ServerServiceTest(TestCase
):
36 self
.config
= Config()
38 @mock.patch
.object(CommonDbClient
, "decrypt_vim_password")
39 @mock.patch
.object(VimCredentialsRepository
, "upsert")
40 @mock.patch('osm_mon.core.database.db')
41 def test_upsert_vim_account(self
, db
, upsert_credentials
, decrypt_vim_password
):
42 def _mock_decrypt_vim_password(password
: str, schema_version
: str, vim_uuid
: str):
43 return password
.replace('encrypted', 'decrypted')
45 decrypt_vim_password
.side_effect
= _mock_decrypt_vim_password
48 'admin_password': 'encrypted_admin_password',
49 'nsx_password': 'encrypted_nsx_password',
50 'vcenter_password': 'encrypted_vcenter_password'
53 mock_expected_config
= {
54 'admin_password': 'decrypted_admin_password',
55 'nsx_password': 'decrypted_nsx_password',
56 'vcenter_password': 'decrypted_vcenter_password'
59 service
= ServerService(self
.config
)
60 service
.upsert_vim_account('test_uuid', 'test_name', 'test_type', 'test_url', 'test_user', 'encrypted_password',
61 'test_tenant_name', '1.1', mock_config
)
63 upsert_credentials
.assert_called_with(
69 password
='decrypted_password',
70 tenant_name
='test_tenant_name',
71 config
=json
.dumps(mock_expected_config
)
74 @mock.patch
.object(VimCredentialsRepository
, "get")
75 @mock.patch('osm_mon.core.database.db')
76 def test_delete_vim_account(self
, db
, get_credentials
):
77 mock_creds
= mock
.Mock()
78 get_credentials
.return_value
= mock_creds
80 service
= ServerService(self
.config
)
81 service
.delete_vim_account('test_uuid')
83 get_credentials
.assert_called_with(VimCredentials
.uuid
== 'test_uuid')
84 mock_creds
.delete_instance
.assert_called_with()