File: test_https_cert.py

package info (click to toggle)
python-proliantutils 2.16.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 3,684 kB
  • sloc: python: 58,655; makefile: 163; sh: 2
file content (131 lines) | stat: -rw-r--r-- 5,405 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2020 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Test Class for HttpsCertTestCase"""

import json
from unittest import mock

import sushy
import testtools

from proliantutils import exception
from proliantutils.redfish.resources.manager import https_cert


class HttpsCertTestCase(testtools.TestCase):

    def setUp(self):
        super(HttpsCertTestCase, self).setUp()
        self.conn = mock.Mock()
        https_cert_file = ('proliantutils/tests/redfish/json_samples/'
                           'https_cert.json')
        with open(https_cert_file) as f:
            self.json_doc = json.load(f)
            self.conn.get.return_value.json.return_value = (
                self.json_doc)

        path = ("/redfish/v1/Mangers/1/SecurityService/HttpsCert")
        self.https_cert_inst = https_cert.HttpsCert(
            self.conn, path, '1.0.2', None)

    def test__get_https_cert_uri_generate_csr(self):
        value = self.https_cert_inst._get_https_cert_uri('generate_csr')
        expected_url = ('/redfish/v1/Managers/1/SecurityService/HttpsCert/'
                        'Actions/HpeHttpsCert.GenerateCSR')
        self.assertEqual(expected_url, value.target_uri)

    def test__get_https_cert_uri_import_certificate(self):
        value = self.https_cert_inst._get_https_cert_uri('import_cert')
        expected_url = ('/redfish/v1/Managers/1/SecurityService/HttpsCert/'
                        'Actions/HpeHttpsCert.ImportCertificate')
        self.assertEqual(expected_url, value.target_uri)

    def test__get_https_cert_uri_missing_url(self):
        self.https_cert_inst._actions.generate_csr = None
        self.assertRaisesRegex(
            sushy.exceptions.MissingActionError,
            'action #HpeHttpsCert.GenerateCSR',
            self.https_cert_inst._get_https_cert_uri, 'generate_csr')

    @mock.patch.object(https_cert.HttpsCert, 'wait_for_csr_to_create',
                       autospec=True)
    def test_generate_csr(self, wait_for_csr_to_create_mock):
        target_uri = ('/redfish/v1/Managers/1/SecurityService/HttpsCert/'
                      'Actions/HpeHttpsCert.GenerateCSR')
        data = {
            "CommonName": '1.1.1.1',
            "Country": 'IN',
            "State": 'KA',
            "City": 'blr',
            "OrgName": 'HPE',
            "OrgUnit": None
        }
        self.https_cert_inst.generate_csr(data)
        self.https_cert_inst._conn.post.assert_called_once_with(
            target_uri, data=data)
        self.assertTrue(wait_for_csr_to_create_mock.called)

    def test_generate_csr_post_fails(self):
        data = {
            "CommonName": '1.1.1.1',
            "Country": 'IN',
            "State": 'KA',
            "City": 'blr',
            "OrgName": 'HPE',
            "OrgUnit": None
        }
        self.https_cert_inst._conn.post.side_effect = (
            sushy.exceptions.SushyError)
        self.assertRaisesRegex(
            exception.IloError,
            'The Redfish controller failed to generate CSR',
            self.https_cert_inst.generate_csr, data)

    def test_import_certificate(self):
        target_uri = ('/redfish/v1/Managers/1/SecurityService/HttpsCert/'
                      'Actions/HpeHttpsCert.ImportCertificate')
        data = {
            "Certificate": 'certificate'
        }
        self.https_cert_inst.import_certificate('certificate')
        self.https_cert_inst._conn.post.assert_called_once_with(
            target_uri, data=data)

    def test_import_certificate_post_fails(self):
        self.https_cert_inst._conn.post.side_effect = (
            sushy.exceptions.SushyError)
        self.assertRaisesRegex(
            exception.IloError,
            'The Redfish controller failed to import certificate. Error',
            self.https_cert_inst.import_certificate, 'certificate')

    @mock.patch('time.sleep', autospec=True)
    @mock.patch.object(https_cert.HttpsCert, 'get_generate_csr_refresh',
                       autospec=True)
    def test_wait_for_csr_to_create_ok(self, get_generate_csr_refresh_mock,
                                       sleep_mock):
        self.cert_sign_request = 'certificate'
        self.https_cert_inst.wait_for_csr_to_create()
        self.assertTrue(get_generate_csr_refresh_mock.called)

    @mock.patch('time.sleep', autospec=True)
    @mock.patch.object(https_cert.HttpsCert, 'get_generate_csr_refresh',
                       autospec=True)
    def test_wait_for_csr_to_create_fails(self, get_generate_csr_refresh_mock,
                                          sleep_mock):
        exc = exception.IloError('error')
        get_generate_csr_refresh_mock.side_effect = exc
        self.assertRaises(exception.IloError,
                          self.https_cert_inst.wait_for_csr_to_create)
        self.assertEqual(10, get_generate_csr_refresh_mock.call_count)