salt/tests/unit/modules/test_win_certutil.py

95 lines
3.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import
# Import Salt Libs
import salt.modules.win_certutil as certutil
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase
from tests.support.mock import (
MagicMock,
patch
)
class CertUtilTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
return {certutil: {}}
def test_get_serial(self):
'''
Test getting the serial number from a certificate
'''
2017-06-21 20:51:02 +00:00
expected = '180720d39cd2db3244ba037417241e90'
mock = MagicMock(return_value=(
'CertInfo\r\n'
'Cert Serial Number: 180720d39cd2db3244ba037417241e90\r\n'
'\r\n'
'OtherStuff'))
with patch.dict(certutil.__salt__, {'cmd.run': mock}):
out = certutil.get_cert_serial('/path/to/cert.cer')
2017-06-21 20:51:02 +00:00
mock.assert_called_once_with(
'certutil.exe -silent -verify /path/to/cert.cer')
self.assertEqual(expected, out)
def test_get_serials(self):
'''
2017-06-21 20:51:02 +00:00
Test getting all the serial numbers from a store
'''
2017-06-21 20:51:02 +00:00
expected = ['180720d39cd2db3244ba037417241e90',
'1768ac4e5b72bf1d0df0df118b34b959']
mock = MagicMock(return_value=(
'CertInfo\r\n'
'================ Certificate 0 ================\r\n'
'Serial Number: 180720d39cd2db3244ba037417241e90\r\n'
'OtherStuff\r\n'
'\r\n'
'================ Certificate 1 ================\r\n'
'Serial Number: 1768ac4e5b72bf1d0df0df118b34b959\r\n'
'OtherStuff'))
with patch.dict(certutil.__salt__, {'cmd.run': mock}):
out = certutil.get_stored_cert_serials('TrustedPublisher')
2017-06-21 20:51:02 +00:00
mock.assert_called_once_with(
'certutil.exe -store TrustedPublisher')
self.assertEqual(expected, out)
def test_add_store(self):
'''
Test adding a certificate to a specific store
'''
2017-06-21 20:51:02 +00:00
cmd_mock = MagicMock(return_value=(
'CertInfo\r\n'
'================ Certificate 0 ================\r\n'
'Serial Number: 180720d39cd2db3244ba037417241e90\r\n'
'OtherStuff'))
cache_mock = MagicMock(return_value='/tmp/cert.cer')
with patch.dict(certutil.__salt__, {'cmd.run': cmd_mock,
'cp.cache_file': cache_mock}):
certutil.add_store('salt://path/to/file', 'TrustedPublisher')
2017-06-21 20:51:02 +00:00
cmd_mock.assert_called_once_with(
'certutil.exe -addstore TrustedPublisher /tmp/cert.cer')
cache_mock.assert_called_once_with('salt://path/to/file', 'base')
2017-04-10 13:00:57 +00:00
def test_del_store(self):
'''
Test removing a certificate to a specific store
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.win_certutil.get_cert_serial') as cert_serial_mock:
2017-06-22 17:40:56 +00:00
cmd_mock = MagicMock(return_value=(
'CertInfo\r\n'
'================ Certificate 0 ================\r\n'
'Serial Number: 180720d39cd2db3244ba037417241e90\r\n'
'OtherStuff'))
2017-04-10 13:00:57 +00:00
cache_mock = MagicMock(return_value='/tmp/cert.cer')
2017-06-22 17:40:56 +00:00
cert_serial_mock.return_value = 'ABCDEF'
2017-04-10 13:00:57 +00:00
with patch.dict(certutil.__salt__, {'cmd.run': cmd_mock,
'cp.cache_file': cache_mock}):
certutil.del_store('salt://path/to/file', 'TrustedPublisher')
2017-06-22 17:40:56 +00:00
cmd_mock.assert_called_once_with(
'certutil.exe -delstore TrustedPublisher ABCDEF')
2017-04-10 13:00:57 +00:00
cache_mock.assert_called_once_with('salt://path/to/file', 'base')