Beispiel #1
0
    def testSanitiseAssertFail(self):
        """
        No server set up required, the purpose of the test is just
        demonstrating how validation is used.
        """
        drm = DummyRESTModel(self.config.getModelConfig())

        def func(*args, **kwargs):
            sanitised_input = drm._sanitise_input(args, kwargs, "list")
            return drm.list(**sanitised_input)

        # Wrong type for input args
        self.assertRaises(HTTPError, func, 123, 123)
        self.assertRaises(HTTPError, func, 'abc', 'abc')
        self.assertRaises(HTTPError, func, input_str = 'abc', input_int = 123, other='dfe')
        self.assertRaises(HTTPError, func, str = 123, int ='abc')
        self.assertRaises(HTTPError, func, str =' abc', int = 'abc')
        self.assertRaises(HTTPError, func, 'abc', 123)
        self.assertRaises(HTTPError, func, 'abc', 'abc')
        self.assertRaises(HTTPError, func, str = 123, int = 'abc')
        self.assertRaises(HTTPError, func, str =123, int = 123)
        self.assertRaises(HTTPError, func, str = 'abc', int ='abc')

        # Incorrect values for input args
        self.assertRaises(HTTPError, func, 1234, 'abc')
        self.assertRaises(HTTPError, func, 123, 'abcd')

        # Empty input data, when data is required
        self.assertRaises(HTTPError, func)
Beispiel #2
0
    def testSanitisePass(self):
        """
        Emulate how CherryPy passes arguments to a method, check that the data
        returned is correct.

        No server setup required
        """
        drm = DummyRESTModel(self.config.getModelConfig())

        def func(*args, **kwargs):
            sanitised_input = drm._sanitise_input(args, kwargs, "list")
            return drm.list(**sanitised_input)

        # 2 positional args (e.g. url/arg1/arg2)
        result = func(123, 'abc')
        assert result == {'input_int':123, 'input_str':'abc'},\
                                'list with 2 positional args failed: %s' % result
        # 2 query string args (e.g. url?int=arg1&str=arg2)
        result = func(input_int=123, input_str='abc')
        assert result == {'input_int':123, 'input_str':'abc'},\
                               'list with 2 query string args failed: %s' % result

        # 1 positional, 1 keyword  (e.g. url/arg1/?str=arg2)
        result = func(123, input_str='abc')
        assert result == {'input_int':123, 'input_str':'abc'},\
               'list with 1 positional, 1 keyword failed: %s' % result
Beispiel #3
0
    def testDAOBased(self):
        drm = DummyRESTModel(self.config.getModelConfig())

        result = drm.methods['GET']['data1']['call']()
        self.assertEqual( result ,  123, 'Error default value is set to 123 but returns %s' % result )

        result =  drm.methods['GET']['data2']['call'](456)
        self.assertEqual( result['num'] ,  456 )

        result =  drm.methods['GET']['data2']['call'](num = 456)
        self.assertEqual( result['num'] ,  456 )

        result =  drm.methods['GET']['data3']['call'](num = 456, thing="TEST")
        self.assertEqual( result['num'] == 456 and result['thing'] ,  "TEST" )