# coding: utf-8
import cherrypy
import mock

from salt.netapi.rest_cherrypy import app

from . cptestcase import BaseCherryPyTestCase

class BaseRestCherryPyTest(BaseCherryPyTestCase):
    '''
    A base TestCase subclass for the rest_cherrypy module

    This mocks all interactions with Salt-core and sets up a dummy
    (unsubscribed) CherryPy web server.
    '''
    __opts__ = None

    @mock.patch('salt.netapi.NetapiClient', autospec=True)
    @mock.patch('salt.auth.Resolver', autospec=True)
    @mock.patch('salt.auth.LoadAuth', autospec=True)
    @mock.patch('salt.utils.event.get_event', autospec=True)
    def setUp(self, get_event, LoadAuth, Resolver, NetapiClient):
        app.salt.netapi.NetapiClient = NetapiClient
        app.salt.auth.Resolver = Resolver
        app.salt.auth.LoadAuth = LoadAuth
        app.salt.utils.event.get_event = get_event

        # Make local references to mocked objects so individual tests can
        # access and modify the mocked interfaces.
        self.Resolver = Resolver
        self.NetapiClient = NetapiClient
        self.get_event = get_event

        __opts__ = self.__opts__ or {
            'external_auth': {
                'auto': {
                    'saltdev': [
                        '@wheel',
                        '@runner',
                        '.*',
                    ],
                }
            },
            'rest_cherrypy': {
                'port': 8000,
                'debug': True,
            },
        }

        root, apiopts, conf = app.get_app(__opts__)

        cherrypy.tree.mount(root, '/', conf)
        cherrypy.server.unsubscribe()
        cherrypy.engine.start()

    def tearDown(self):
        cherrypy.engine.exit()

class Root(object):
    '''
    The simplest CherryPy app needed to test individual tools
    '''
    exposed = True

    _cp_config = {}

    def GET(self):
        return {'return': ['Hello world.']}

    def POST(self, *args, **kwargs):
        return {'return': [{'args': args}, {'kwargs': kwargs}]}

class BaseToolsTest(BaseCherryPyTestCase):
    '''
    A base class so tests can selectively turn individual tools on for testing
    '''
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
        },
    }

    def setUp(self):
        Root._cp_config = self._cp_config
        root = Root()

        cherrypy.tree.mount(root, '/', self.conf)
        cherrypy.server.unsubscribe()
        cherrypy.engine.start()

    def tearDown(self):
        cherrypy.engine.exit()


@@ -9,16 +9,26 @@
 : utf-8%0A
+%0Atry:%0A    
 import c
@@ -39,21 +39,162 @@
 ypy%0A
-import mock%0A%0A
+    HAS_CHERRYPY = True%0Aexcept ImportError:%0A    HAS_CHERRYPY = False%0A%0Aimport mock%0A%0Aif HAS_CHERRYPY:%0A    from . cptestcase import BaseCherryPyTestCase%0A    
 from
@@ -235,34 +235,233 @@
 app%0A
-%0Afrom . cptestcase import 
+else:%0A    from salttesting.unit import TestCase, skipIf%0A%0A    @skipIf(HAS_CHERRYPY is False, 'The CherryPy python package needs to be installed')%0A    class BaseCherryPyTestCase(TestCase):%0A        pass%0A%0A    class BaseToolsTest(
 Base
@@ -476,16 +476,32 @@
 TestCase
+):%0A        pass%0A
 %0A%0Aclass 
@@ -2088,16 +2088,17 @@
 exit()%0A%0A
+%0A
 class Ro
@@ -2394,16 +2394,38 @@
 rgs%7D%5D%7D%0A%0A
+%0Aif HAS_CHERRYPY:%0A    
 class Ba
@@ -2455,36 +2455,44 @@
 yTestCase):%0A    
-'''%0A
+    '''%0A    
     A base class
@@ -2554,16 +2554,20 @@
 testing%0A
+    
     '''%0A
@@ -2570,16 +2570,20 @@
 '''%0A    
+    
 conf = %7B
@@ -2591,16 +2591,20 @@
         
+    
 '/': %7B%0A 
@@ -2614,16 +2614,20 @@
         
+    
 'request
@@ -2688,22 +2688,34 @@
     
+    
 %7D,%0A    
+    
 %7D%0A%0A    
+    
 def 
@@ -2719,32 +2719,36 @@
 ef setUp(self):%0A
+    
         Root._cp
@@ -2773,16 +2773,20 @@
 _config%0A
+    
         
@@ -2800,32 +2800,36 @@
 Root()%0A%0A        
+    
 cherrypy.tree.mo
@@ -2854,32 +2854,36 @@
 f.conf)%0A        
+    
 cherrypy.server.
@@ -2896,32 +2896,36 @@
 cribe()%0A        
+    
 cherrypy.engine.
@@ -2925,32 +2925,36 @@
 engine.start()%0A%0A
+    
     def tearDown
@@ -2961,32 +2961,36 @@
 (self):%0A        
+    
 cherrypy.engine.
@@ -2976,29 +2976,28 @@
      cherrypy.engine.exit()%0A
-%0A

Make the NetAPI tests skip if CherryPy is not installed