|
1
|
|
|
from datetime import datetime |
|
2
|
|
|
from unittest import TestCase as TC |
|
3
|
|
|
|
|
4
|
|
|
import pymongo |
|
5
|
|
|
import ujson |
|
6
|
|
|
from flask import Response |
|
7
|
|
|
|
|
8
|
|
|
from app import create_app |
|
9
|
|
|
from config.test import TestConfig |
|
10
|
|
|
|
|
11
|
|
|
app = create_app(TestConfig) |
|
12
|
|
|
|
|
13
|
|
|
mongo_setting = app.config['MONGODB_SETTINGS'] |
|
14
|
|
|
db_name = mongo_setting.pop('db') |
|
15
|
|
|
mongo_client = pymongo.MongoClient(mongo_setting) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class TCBase(TC): |
|
19
|
|
|
def __init__(self, *args, **kwargs): |
|
20
|
|
|
self.client = app.test_client() |
|
21
|
|
|
self.today = datetime.now().strftime('%Y-%m-%d') |
|
22
|
|
|
|
|
23
|
|
|
super(TCBase, self).__init__(*args, **kwargs) |
|
24
|
|
|
|
|
25
|
|
|
def _create_fake_account(self): |
|
26
|
|
|
pass |
|
27
|
|
|
|
|
28
|
|
|
def _get_tokens(self): |
|
29
|
|
|
self.access_token = None |
|
30
|
|
|
self.refresh_token = None |
|
31
|
|
|
|
|
32
|
|
|
def setUp(self): |
|
33
|
|
|
self._create_fake_account() |
|
34
|
|
|
self._get_tokens() |
|
35
|
|
|
|
|
36
|
|
|
def tearDown(self): |
|
37
|
|
|
mongo_client.drop_database(db_name) |
|
38
|
|
|
|
|
39
|
|
|
def request(self, method, target_url_rule, token=None, *args, **kwargs): |
|
40
|
|
|
""" |
|
41
|
|
|
Helper for common request |
|
42
|
|
|
|
|
43
|
|
|
Args: |
|
44
|
|
|
method (func): Request method |
|
45
|
|
|
target_url_rule (str): URL rule for request |
|
46
|
|
|
token (str) : JWT or OAuth's access token with prefix(Bearer, JWT, ...) |
|
47
|
|
|
|
|
48
|
|
|
Returns: |
|
49
|
|
|
Response |
|
50
|
|
|
""" |
|
51
|
|
|
return method( |
|
52
|
|
|
target_url_rule, |
|
53
|
|
|
headers={'Authorization': token or self.access_token}, |
|
54
|
|
|
*args, |
|
55
|
|
|
**kwargs |
|
56
|
|
|
) |
|
57
|
|
|
|