Passed
Push — master ( bf54e8...3200ce )
by Mingyu
01:32
created

tests.views.TCBase.json_request()   A

Complexity

Conditions 2

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
nop 6
1
import ujson
2
from unittest import TestCase as TC
3
4
from flask import Response
5
6
from app import create_app
7
from config.test import TestConfig
8
9
app = create_app(TestConfig)
10
11
12
class TCBase(TC):
13
    def __init__(self, *args, **kwargs):
14
        self.client = app.test_client()
15
16
        super(TCBase, self).__init__(*args, **kwargs)
17
18
    def _create_fake_account(self):
19
        pass
20
21
    def _get_tokens(self):
22
        self.access_token = None
23
        self.refresh_token = None
24
25
    def setUp(self):
26
        self._create_fake_account()
27
        self._get_tokens()
28
29
    def tearDown(self):
30
        pass
31
32
    def request(self, method, target_url_rule, token=None, *args, **kwargs):
33
        """
34
        Helper for common request
35
36
        Args:
37
            method (func): Request method
38
            target_url_rule (str): URL rule for request
39
            token (str) : JWT or OAuth's access token with prefix(Bearer, JWT, ...)
40
41
        Returns:
42
            Response
43
        """
44
        return method(
45
            target_url_rule,
46
            headers={'Authorization': token or self.access_token},
47
            *args,
48
            **kwargs
49
        )
50
51
    def decode_response_data(self, resp):
52
        return resp.data.decode()
53
54
    def get_response_data_as_json(self, resp):
55
        return ujson.loads(self.decode_response_data(resp))
56