-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_cfssl.py
More file actions
207 lines (184 loc) · 6.51 KB
/
test_cfssl.py
File metadata and controls
207 lines (184 loc) · 6.51 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc.
# License MIT (https://opensource.org/licenses/MIT).
import logging
import mock
import unittest
from ..cfssl import (CFSSL,
CFSSLRemoteException,
requests,
)
_logger = logging.getLogger(__name__)
try:
from cfssl import CertificateRequest
except ImportError:
_logger.info('CFSSL Python library not installed.')
class TestCFSSL(unittest.TestCase):
def setUp(self):
super(TestCFSSL, self).setUp()
self.cfssl = CFSSL('test', 1)
def test_uri_base_https(self):
""" It should have an HTTP URI by default """
self.assertIn('https://', self.cfssl.uri_base)
def test_uri_base_http(self):
""" It should have an HTTP URI if someone decides to be crazy """
cfssl = CFSSL('test', 1, False)
self.assertIn('http://', cfssl.uri_base)
@mock.patch.object(CFSSL, 'call')
def test_auth_sign(self, call):
""" It should call with proper args """
expect = {
'token': 'token',
'request': mock.MagicMock(),
}
self.cfssl.auth_sign(**expect)
expect['request'] = expect['request'].to_api()
call.assert_called_once_with(
'authsign', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_bundle(self, call):
""" It should call with proper args """
expect = {
'certificate': 'certificate',
'flavor': 'flavor',
}
self.cfssl.bundle(**expect)
call.assert_called_once_with(
'bundle', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_info(self, call):
""" It should call with proper args """
expect = {
'label': 'label',
}
self.cfssl.info(**expect)
call.assert_called_once_with(
'info', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_init_ca(self, call):
""" It should call with proper args """
csr_vals = {
'hosts': [mock.MagicMock()],
'names': [mock.MagicMock()],
'common_name': 'cn',
'key': mock.MagicMock(),
}
csr = CertificateRequest(**csr_vals)
expect = {'ca': mock.MagicMock(),
'certificate_request': csr}
self.cfssl.init_ca(**expect)
expect.update(csr_vals)
expect['CN'] = 'cn'
del expect['common_name']
del expect['certificate_request']
expect['hosts'][0]= expect['hosts'][0].to_api()
expect['names'][0] = expect['names'][0].to_api()
expect['key'] = expect['key'].to_api()
expect['ca'] = expect['ca'].to_api()
call.assert_called_once_with(
'init_ca', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_new_key(self, call):
""" It should call with proper args """
expect = {
'hosts': [mock.MagicMock()],
'names': [mock.MagicMock()],
'common_name': 'cn',
'ca': mock.MagicMock(),
'key': mock.MagicMock(),
}
self.cfssl.new_key(**expect)
expect['CN'] = 'cn'
del expect['common_name']
expect['hosts'][0]= expect['hosts'][0].to_api()
expect['names'][0] = expect['names'][0].to_api()
expect['ca'] = expect['ca'].to_api()
expect['key'] = expect['key'].to_api()
call.assert_called_once_with(
'newkey', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_new_cert(self, call):
""" It should call with proper args """
expect = {
'request': mock.MagicMock(),
'label': 'label',
}
self.cfssl.new_cert(**expect)
expect['request'] = expect['request'].to_api()
call.assert_called_once_with(
'newcert', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_revoke(self, call):
""" It should call with proper args """
expect = {
'serial': 'Ben-S',
'authority_key_id': 'REVOKE!',
'reason': 'The derphead lost it',
}
self.cfssl.revoke(**expect)
call.assert_called_once_with(
'revoke', 'POST', data=expect
)
@mock.patch.object(CFSSL, 'call')
def test_scan(self, call):
""" It should call with proper args """
expect = {
'host': mock.MagicMock(),
}
self.cfssl.scan(**expect)
expect['host'] = expect['host'].to_api()
call.assert_called_once_with(
'scan', params=expect
)
@mock.patch.object(CFSSL, 'call')
def test_scan_info(self, call):
""" It should call with proper args """
self.cfssl.scan_info()
call.assert_called_once_with('scaninfo')
@mock.patch.object(CFSSL, 'call')
def test_sign(self, call):
""" It should call with proper args """
expect = {
'certificate_request': mock.MagicMock(),
'hosts': [mock.MagicMock()],
'profile': mock.MagicMock(),
}
self.cfssl.sign(**expect)
expect['certificate_request'] = expect['certificate_request'].to_api()
expect['hosts'][0] = expect['hosts'][0].to_api()
expect['profile'] = expect['profile'].to_api()
call.assert_called_once_with(
'sign', 'POST', data=expect
)
@mock.patch.object(requests, 'request')
def test_call_request(self, requests):
""" It should call requests with proper args """
self.cfssl.call('endpoint', 'method', 'params', 'data')
requests.assert_called_once_with(
method='method',
url='https://test:1/api/v1/cfssl/endpoint',
params='params',
data='data',
verify=True,
)
@mock.patch.object(requests, 'request')
def test_call_error(self, requests):
""" It should raise on non-success response """
requests().json.return_value = {'success': False}
with self.assertRaises(CFSSLRemoteException):
self.cfssl.call('None')
@mock.patch.object(requests, 'request')
def test_call_success(self, requests):
""" It should reteurn result on success response """
requests().json.return_value = {'success': True,
'result': 'result'}
res = self.cfssl.call(None)
self.assertEqual(res, 'result')
if __name__ == '__main__':
unittest.main()