summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/tests/unit/utils.py
blob: 21f98c445f1c1485fa75bdcb1fd592aea05fe318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import httplib

from mock import mock
from six.moves.urllib import parse
import testtools

from testapiclient.tests.unit import fakes
from testapiclient.utils import clientmanager

FAKE_FAILURE = fakes.FakeResponse(status_code=httplib.FORBIDDEN, data='Error')


class ParserException(Exception):
    pass


class TestCommand(testtools.TestCase):
    api_url = 'http://localhost:8000/api/v1/'

    def setUp(self):
        super(TestCommand, self).setUp()
        env_variables = {
            'testapi_url': 'http://localhost:8000/api/v1',
            'testapi_cas_auth_url':
            (
                'https://identity.linuxfoundation.org/user' +
                '/login?destination=cas/login%3Fservice%3D'
            ),
            'testapi_cas_signin_return': '/auth/signin_return'
        }
        self.config_mock = mock.patch.dict(
            'os.environ', env_variables).start()
        self.fake_stdout = fakes.FakeStdout()
        self.fake_log = fakes.FakeLog()
        self.app = fakes.FakeApp(self.fake_stdout, self.fake_log)
        self.app.client_manager = clientmanager.ClientManager()
        self.get_mock = mock.patch('requests.Session.get').start()
        self.post_mock = mock.patch('requests.Session.post').start()
        self.delete_mock = mock.patch('requests.Session.delete').start()
        self.put_mock = mock.patch('requests.Session.put').start()

    def check_parser(self, cmd, args, verify_args):
        cmd_parser = cmd.get_parser('check_parser')
        try:
            parsed_args = cmd_parser.parse_args(args)
        except SystemExit:
            raise ParserException("Argument parse failed")
        for av in verify_args:
            attr, value = av
            if attr:
                self.assertIn(attr, parsed_args)
                self.assertEqual(value, getattr(parsed_args, attr))
        return parsed_args

    def assert_url(self, actual_url, expected_url):
        actual_parsed = parse.parse_qs(parse.urlparse(actual_url).query)
        expected_parsed = parse.parse_qs(parse.urlparse(expected_url).query)
        assert actual_parsed == expected_parsed