Esempio n. 1
0
class TestAuthRestSM(unittest.TestCase):
    def setUp(self):
        self.aeon = Aeon(namespace={
            r'^/$': AuthRestSiteModule(),
        }, )

    def request(
        self,
        url,
        headers=None,
        args=None,
        data=None,
        method='GET',
        http_version='HTTP/1.1',
        code=200,
    ):
        resp = LOOP.run_until_complete(
            self.aeon.emulate_request(url, headers, args, data, method,
                                      http_version))
        self.assertEqual(resp.code, code)
        return resp

    def test_ok(self):
        resp = self.request('/',
                            headers={
                                'x-auth': 'test-token',
                            },
                            args={
                                'user_id': '123',
                            })
        self.assertEqual(json.loads(resp.data), {'response': '123'})

    def test_auth_fail(self):
        self.request('/',
                     headers={
                         'x-auth': 'no-valid-token',
                     },
                     args={
                         'user_id': '123',
                     },
                     code=403)

    def test_rest_fail(self):
        self.request('/',
                     headers={
                         'x-auth': 'test-token',
                     },
                     args={
                         'user': '******',
                     },
                     code=400)
Esempio n. 2
0
class TestSM(unittest.TestCase):
    def setUp(self):
        self.aeon = Aeon(
            namespace={
                r'^/': SimpleSiteModule(),
                r'^/not_found/': Response(code=404),
            },
        )

    def request(
        self,
        url,
        headers=None,
        args=None,
        data=None,
        method='GET',
        http_version='HTTP/1.1',
        code=200,
    ):
        resp = LOOP.run_until_complete(self.aeon.emulate_request(url, headers, args, data, method, http_version))
        self.assertEqual(resp.code, code)
        data = '{method}: url={url},args={args},data={data},headers={headers}'.format(
            method=method,
            url=url,
            args=str(args or {}),
            data=str(data or b''),
            headers=str(headers or {}),
        )
        if 200 <= resp.code < 300:
            self.assertEqual(resp.data, data)
        return resp

    def test_url(self):
        self.request('/abc')

    def test_code(self):
        self.request('/not_found/', code=404)

    def test_method(self):
        self.request('/', method='POST')

    def test_args(self):
        self.request('/', args={'a': 123})

    def test_data(self):
        self.request('/', data='-- Some Data --')

    def test_headers(self):
        self.request('/', headers={'x-test-header': 'test-value; ???'})
Esempio n. 3
0
File: restsm.py Progetto: moff4/k2
class TestRestSM(unittest.TestCase):
    def setUp(self):
        self.aeon = Aeon(namespace={
            r'^/': RestSiteModule(),
        }, )

    def request(
        self,
        url,
        headers=None,
        args=None,
        data=None,
        method='GET',
        http_version='HTTP/1.1',
        code=200,
    ):
        resp = LOOP.run_until_complete(
            self.aeon.emulate_request(url, headers, args, data, method,
                                      http_version))
        self.assertEqual(resp.code, code)
        data = json.dumps('{method}: name={name}, age={age}'.format(
            method=method,
            name=args['name'],
            age=str(args['age']),
        ))
        if 200 <= resp.code < 300:
            self.assertEqual(resp.data, data)
        return resp

    def test_ok(self):
        self.request('/abc', args={
            'name': 'Ivan Petrov',
            'age': '22',
        })

    def test_fail(self):
        self.request(
            '/abc',
            args={
                'name': 'Ivan Petrov',
                'age': 'not-integer',
            },
            code=400,
        )
Esempio n. 4
0
File: authsm.py Progetto: moff4/k2
class TestAuthSM(unittest.TestCase):
    def setUp(self):
        self.aeon = Aeon(namespace={
            r'^/$': AuthSiteModule(),
            r'^/(?P<username>\w+)/$': AuthSiteModule(),
        }, )

    def request(
        self,
        url,
        headers=None,
        args=None,
        data=None,
        method='GET',
        http_version='HTTP/1.1',
        code=200,
    ):
        resp = LOOP.run_until_complete(
            self.aeon.emulate_request(url, headers, args, data, method,
                                      http_version))
        self.assertEqual(resp.code, code)
        if 200 <= resp.code < 300:
            self.assertEqual(resp.data, 'ok')
        return resp

    def test_ok(self):
        self.request('/', headers={
            'x-auth': 'test-token',
        })

    def test_fail(self):
        self.request(
            '/',
            headers={
                'x-auth': 'not-valid-test-token',
            },
            code=403,
        )

    def test_params_ok(self):
        self.request(
            '/testuser/',
            args={
                'token': 'testtoken',
            },
            headers={
                'x-auth': 'test-token',
            },
        )

    def test_params_fail_1(self):
        self.request(
            '/testuser/',
            args={'token': 'testuser'},
            headers={
                'x-auth': 'not-valid-test-token',
            },
            code=403,
        )

    def test_params_fail_2(self):
        self.request(
            '/unknownuser/',
            args={'token': 'testtoken'},
            headers={
                'x-auth': 'test-token',
            },
            code=403,
        )

    def test_params_fail_3(self):
        self.request(
            '/unknownuser/',
            args={'token': 'invalidtoken'},
            headers={
                'x-auth': 'test-token',
            },
            code=403,
        )