Refactors code and adds unit tests
[osm/MON.git] / osm_mon / tests / unit / server / test_server_service.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
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
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
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
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 import json
24 from unittest import TestCase, mock
25
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
30
31
32 @mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None)
33 class ServerServiceTest(TestCase):
34 def setUp(self):
35 super().setUp()
36 self.config = Config()
37
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')
44
45 decrypt_vim_password.side_effect = _mock_decrypt_vim_password
46
47 mock_config = {
48 'admin_password': 'encrypted_admin_password',
49 'nsx_password': 'encrypted_nsx_password',
50 'vcenter_password': 'encrypted_vcenter_password'
51 }
52
53 mock_expected_config = {
54 'admin_password': 'decrypted_admin_password',
55 'nsx_password': 'decrypted_nsx_password',
56 'vcenter_password': 'decrypted_vcenter_password'
57 }
58
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)
62
63 upsert_credentials.assert_called_with(
64 uuid=mock.ANY,
65 name='test_name',
66 type='test_type',
67 url='test_url',
68 user='test_user',
69 password='decrypted_password',
70 tenant_name='test_tenant_name',
71 config=json.dumps(mock_expected_config)
72 )
73
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
79
80 service = ServerService(self.config)
81 service.delete_vim_account('test_uuid')
82
83 get_credentials.assert_called_with(VimCredentials.uuid == 'test_uuid')
84 mock_creds.delete_instance.assert_called_with()