File: security_tests.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (154 lines) | stat: -rw-r--r-- 6,075 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
    SoftLayer.tests.CLI.modules.security_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :license: MIT, see LICENSE for more details.
"""
import json
import os.path
import sys
import tempfile

from unittest import mock as mock

from SoftLayer.CLI import exceptions
from SoftLayer import testing


class SecurityTests(testing.TestCase):
    def test_add_sshkey_without_key_errors(self):
        result = self.run_command(['security', 'sshkey-add', 'key1'])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_add_sshkey_with_key_file_and_key_argument_errors(self):
        path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub')
        result = self.run_command(['security', 'sshkey-add', 'key1',
                                   '--key=some_key',
                                   '--in-file=%s' % path])

        self.assertEqual(result.exit_code, 2)
        self.assertIsInstance(result.exception, exceptions.ArgumentError)

    def test_add_sshkey_by_option(self):
        service = self.client['Security_Ssh_Key']
        mock_key = service.getObject()['key']

        result = self.run_command(['security', 'sshkey-add', 'key1',
                                   '--key=%s' % mock_key,
                                   '--note=my key'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output),
                         "SSH key added: aa:bb:cc:dd")
        self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject',
                                args=({'notes': 'my key',
                                       'key': mock_key,
                                       'label': 'key1'},))

    def test_add_sshkey_by_file(self):
        path = os.path.join(testing.FIXTURE_PATH, 'id_rsa.pub')

        result = self.run_command(['security', 'sshkey-add', 'key1',
                                   '--in-file=%s' % path])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output),
                         "SSH key added: aa:bb:cc:dd")
        service = self.client['Security_Ssh_Key']
        mock_key = service.getObject()['key']
        self.assert_called_with('SoftLayer_Security_Ssh_Key', 'createObject',
                                args=({'notes': None,
                                       'key': mock_key,
                                       'label': 'key1'},))

    def test_remove_sshkey_key(self):
        result = self.run_command(['--really', 'security', 'sshkey-remove', '1234'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Security_Ssh_Key', 'deleteObject',
                                identifier=1234)

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_remove_sshkey_fail(self, ngb_mock):
        ngb_mock.return_value = False
        result = self.run_command(['security', 'sshkey-remove', '1234'])

        self.assertEqual(result.exit_code, 2)

    def test_edit_sshkey(self):
        result = self.run_command(['security', 'sshkey-edit', '1234',
                                   '--label=key1', '--note=my key'])

        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Security_Ssh_Key', 'editObject',
                                args=({'notes': 'my key',
                                       'label': 'key1'},),
                                identifier=1234)

    def test_edit_sshkey_fail(self):
        fixture = self.set_mock('SoftLayer_Security_Ssh_Key', 'editObject')
        fixture.return_value = False

        result = self.run_command(['security', 'sshkey-edit', '1234',
                                   '--label=key1', '--note=my key'])

        self.assertEqual(result.exit_code, 2)

    def test_list_sshkeys(self):
        result = self.run_command(['security', 'sshkey-list'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output),
                         [{'notes': '-',
                           'fingerprint': None,
                           'id': '100',
                           'label': 'Test 1'},
                          {'notes': 'my key',
                           'fingerprint': None,
                           'id': '101',
                           'label': 'Test 2'}])

    def test_print_sshkey(self):
        result = self.run_command(['security', 'sshkey-print', '1234'])

        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output),
                         {'id': 1234, 'label': 'label', 'notes': 'notes'})

    def test_print_sshkey_file(self):
        if sys.platform.startswith("win"):
            self.skipTest("Test doesn't work in Windows")
        with tempfile.NamedTemporaryFile() as sshkey_file:
            service = self.client['Security_Ssh_Key']
            mock_key = service.getObject()['key']
            result = self.run_command(['security', 'sshkey-print', '1234',
                                       '--out-file=%s' % sshkey_file.name])

            self.assert_no_fail(result)
            self.assertEqual(mock_key, sshkey_file.read().decode("utf-8"))

    def test_list_certficates(self):
        result = self.run_command(['security', 'cert-list', '--status', 'all'])
        self.assert_no_fail(result)
        self.assertEqual(json.loads(result.output), [
            {
                "id": 1234,
                "common_name": "cert",
                "days_until_expire": 0,
                "notes": None
            }
        ])

    @mock.patch('SoftLayer.CLI.formatting.no_going_back')
    def test_remove_certficate(self, confirm_mock):
        confirm_mock.return_value = True
        result = self.run_command(['security', 'cert-remove', '123456'])
        self.assert_no_fail(result)
        self.assertEqual(result.exit_code, 0)

    def test_download_certficate(self):
        result = self.run_command(['security', 'cert-download', '123456'])
        self.assert_no_fail(result)
        self.assertEqual(result.exit_code, 0)