# -*- 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
        '''
        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')
            mock.assert_called_once_with(
                'certutil.exe -silent -verify /path/to/cert.cer')
            self.assertEqual(expected, out)

    def test_get_serials(self):
        '''
            Test getting all the serial numbers from a store
        '''
        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')
            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
        '''
        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')
            cmd_mock.assert_called_once_with(
                'certutil.exe -addstore TrustedPublisher /tmp/cert.cer')
            cache_mock.assert_called_once_with('salt://path/to/file', 'base')

    def test_del_store(self):
        '''
            Test removing a certificate to a specific store
        '''
<<<<<<< HEAD:tests/unit/modules/test_win_certutil.py
        with patch('salt.modules.win_certutil.get_cert_serial') as cert_serial_mock:
            cmd_mock = MagicMock(return_value='CertInfo\r\nSerial: XYZABC\r\nOtherStuff')
            cache_mock = MagicMock(return_value='/tmp/cert.cer')
            cert_serial_mock.return_value = "ABCDEF"
            with patch.dict(certutil.__salt__, {'cmd.run': cmd_mock,
                                                'cp.cache_file': cache_mock}):
                certutil.del_store('salt://path/to/file', 'TrustedPublisher')
                cmd_mock.assert_called_once_with('certutil.exe -delstore TrustedPublisher ABCDEF')
                cache_mock.assert_called_once_with('salt://path/to/file', 'base')
=======
        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')
        cert_serial_mock.return_value = 'ABCDEF'
        with patch.dict(certutil.__salt__, {'cmd.run': cmd_mock,
                                            'cp.cache_file': cache_mock}):
            certutil.del_store('salt://path/to/file', 'TrustedPublisher')
            cmd_mock.assert_called_once_with(
                'certutil.exe -delstore TrustedPublisher ABCDEF')
            cache_mock.assert_called_once_with('salt://path/to/file', 'base')


if __name__ == '__main__':
    from integration import run_tests
    run_tests(CertUtilTestCase, needs_daemon=False)
>>>>>>> 2016.11:tests/unit/modules/win_certutil_test.py

@@ -2933,61 +2933,8 @@
 '''%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD:tests/unit/modules/test_win_certutil.py%0A
     
@@ -3064,39 +3064,198 @@
 lue=
-'CertInfo%5Cr%5CnSerial: XYZABC%5Cr%5Cn
+(%0A                'CertInfo%5Cr%5Cn'%0A                '================ Certificate 0 ================%5Cr%5Cn'%0A                'Serial Number: 180720d39cd2db3244ba037417241e90%5Cr%5Cn'%0A                '
 Othe
@@ -3262,16 +3262,17 @@
 rStuff')
+)
 %0A       
@@ -3377,16 +3377,16 @@
 e = 
-%22
+'
 ABCDEF
-%22
+'
 %0A   
@@ -3661,748 +3661,13 @@
 ith(
-'certutil.exe -delstore TrustedPublisher ABCDEF')%0A                cache_mock.assert_called_once_with('salt://path/to/file', 'base')%0A=======%0A        cmd_mock = MagicMock(return_value=(%0A            'CertInfo%5Cr%5Cn'%0A            '================ Certificate 0 ================%5Cr%5Cn'%0A            'Serial Number: 180720d39cd2db3244ba037417241e90%5Cr%5Cn'%0A            'OtherStuff'))%0A        cache_mock = MagicMock(return_value='/tmp/cert.cer')%0A        cert_serial_mock.return_value = 'ABCDEF'%0A        with patch.dict(certutil.__salt__, %7B'cmd.run': cmd_mock,%0A                                            'cp.cache_file': cache_mock%7D):%0A            certutil.del_store('salt://path/to/file', 'TrustedPublisher')%0A            cmd_mock.assert_called_once_with(%0A
+%0A    
     
@@ -3728,16 +3728,20 @@
 BCDEF')%0A
+    
         
@@ -3933,60 +3933,4 @@
 se)%0A
-%3E%3E%3E%3E%3E%3E%3E 2016.11:tests/unit/modules/win_certutil_test.py%0A

Fix win_certutil merge error