|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2016 LasLabs Inc. |
| 3 | +# License MIT (https://opensource.org/licenses/MIT). |
| 4 | + |
| 5 | +import mock |
| 6 | +import unittest |
| 7 | + |
| 8 | +from ..cfssl import CFSSL, CFSSLRemoteException, requests |
| 9 | + |
| 10 | + |
| 11 | +class TestCFSSL(unittest.TestCase): |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + super(TestCFSSL, self).setUp() |
| 15 | + self.cfssl = CFSSL('test', 1) |
| 16 | + |
| 17 | + def test_uri_base_https(self): |
| 18 | + """ It should have an HTTP URI by default """ |
| 19 | + self.assertIn('https://', self.cfssl.uri_base) |
| 20 | + |
| 21 | + def test_uri_base_http(self): |
| 22 | + """ It should have an HTTP URI if someone decides to be crazy """ |
| 23 | + cfssl = CFSSL('test', 1, False) |
| 24 | + self.assertIn('http://', cfssl.uri_base) |
| 25 | + |
| 26 | + @mock.patch.object(CFSSL, 'call') |
| 27 | + def test_auth_sign(self, call): |
| 28 | + """ It should call with proper args """ |
| 29 | + expect = { |
| 30 | + 'token': 'token', |
| 31 | + 'request': 'request', |
| 32 | + } |
| 33 | + self.cfssl.auth_sign(**expect) |
| 34 | + call.assert_called_once_with( |
| 35 | + 'authsign', 'POST', data=expect |
| 36 | + ) |
| 37 | + |
| 38 | + @mock.patch.object(CFSSL, 'call') |
| 39 | + def test_bundle(self, call): |
| 40 | + """ It should call with proper args """ |
| 41 | + expect = { |
| 42 | + 'certificate': 'certificate', |
| 43 | + 'flavor': 'flavor', |
| 44 | + } |
| 45 | + self.cfssl.bundle(**expect) |
| 46 | + call.assert_called_once_with( |
| 47 | + 'bundle', 'POST', data=expect |
| 48 | + ) |
| 49 | + |
| 50 | + @mock.patch.object(CFSSL, 'call') |
| 51 | + def test_info(self, call): |
| 52 | + """ It should call with proper args """ |
| 53 | + expect = { |
| 54 | + 'label': 'label', |
| 55 | + } |
| 56 | + self.cfssl.info(**expect) |
| 57 | + call.assert_called_once_with( |
| 58 | + 'info', 'POST', data=expect |
| 59 | + ) |
| 60 | + |
| 61 | + @mock.patch.object(CFSSL, 'call') |
| 62 | + def test_init_ca(self, call): |
| 63 | + """ It should call with proper args """ |
| 64 | + expect = { |
| 65 | + 'hosts': 'hosts', |
| 66 | + 'names': 'names', |
| 67 | + 'common_name': 'cn' |
| 68 | + } |
| 69 | + self.cfssl.init_ca(**expect) |
| 70 | + expect['CN'] = 'cn' |
| 71 | + del expect['common_name'] |
| 72 | + call.assert_called_once_with( |
| 73 | + 'init_ca', 'POST', data=expect |
| 74 | + ) |
| 75 | + |
| 76 | + @mock.patch.object(CFSSL, 'call') |
| 77 | + def test_new_key(self, call): |
| 78 | + """ It should call with proper args """ |
| 79 | + expect = { |
| 80 | + 'hosts': 'hosts', |
| 81 | + 'names': 'names', |
| 82 | + 'common_name': 'cn' |
| 83 | + } |
| 84 | + self.cfssl.new_key(**expect) |
| 85 | + expect['CN'] = 'cn' |
| 86 | + del expect['common_name'] |
| 87 | + call.assert_called_once_with( |
| 88 | + 'newkey', 'POST', data=expect |
| 89 | + ) |
| 90 | + |
| 91 | + @mock.patch.object(CFSSL, 'call') |
| 92 | + def test_new_cert(self, call): |
| 93 | + """ It should call with proper args """ |
| 94 | + expect = { |
| 95 | + 'request': 'request', |
| 96 | + 'label': 'label', |
| 97 | + } |
| 98 | + self.cfssl.new_cert(**expect) |
| 99 | + call.assert_called_once_with( |
| 100 | + 'newcert', 'POST', data=expect |
| 101 | + ) |
| 102 | + |
| 103 | + @mock.patch.object(CFSSL, 'call') |
| 104 | + def test_revoke(self, call): |
| 105 | + """ It should call with proper args """ |
| 106 | + expect = { |
| 107 | + 'serial': 'Ben-S', |
| 108 | + 'authority_key_id': 'REVOKE!', |
| 109 | + 'reason': 'The derphead lost it', |
| 110 | + } |
| 111 | + self.cfssl.revoke(**expect) |
| 112 | + call.assert_called_once_with( |
| 113 | + 'revoke', 'POST', data=expect |
| 114 | + ) |
| 115 | + |
| 116 | + @mock.patch.object(CFSSL, 'call') |
| 117 | + def test_scan(self, call): |
| 118 | + """ It should call with proper args """ |
| 119 | + expect = { |
| 120 | + 'host': 'host', |
| 121 | + } |
| 122 | + self.cfssl.scan(**expect) |
| 123 | + call.assert_called_once_with( |
| 124 | + 'scan', params=expect |
| 125 | + ) |
| 126 | + |
| 127 | + @mock.patch.object(CFSSL, 'call') |
| 128 | + def test_scan_info(self, call): |
| 129 | + """ It should call with proper args """ |
| 130 | + self.cfssl.scan_info() |
| 131 | + call.assert_called_once_with('scaninfo') |
| 132 | + |
| 133 | + @mock.patch.object(CFSSL, 'call') |
| 134 | + def test_sign(self, call): |
| 135 | + """ It should call with proper args """ |
| 136 | + expect = { |
| 137 | + 'certificate_request': 'certificate_request', |
| 138 | + } |
| 139 | + self.cfssl.sign(**expect) |
| 140 | + call.assert_called_once_with( |
| 141 | + 'sign', 'POST', data=expect |
| 142 | + ) |
| 143 | + |
| 144 | + @mock.patch.object(requests, 'request') |
| 145 | + def test_call_request(self, requests): |
| 146 | + """ It should call requests with proper args """ |
| 147 | + self.cfssl.call('endpoint', 'method', 'params', 'data') |
| 148 | + requests.assert_called_once_with( |
| 149 | + method='method', |
| 150 | + url='https://test:1/api/v1/cfssl/endpoint', |
| 151 | + params='params', |
| 152 | + data='data', |
| 153 | + ) |
| 154 | + |
| 155 | + @mock.patch.object(requests, 'request') |
| 156 | + def test_call_error(self, requests): |
| 157 | + """ It should raise on non-success response """ |
| 158 | + requests().json.return_value = {'success': False} |
| 159 | + with self.assertRaises(CFSSLRemoteException): |
| 160 | + self.cfssl.call('None') |
| 161 | + |
| 162 | + @mock.patch.object(requests, 'request') |
| 163 | + def test_call_success(self, requests): |
| 164 | + """ It should reteurn result on success response """ |
| 165 | + requests().json.return_value = {'success': True, |
| 166 | + 'result': 'result'} |
| 167 | + res = self.cfssl.call(None) |
| 168 | + self.assertEqual(res, 'result') |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + unittest.main() |
0 commit comments