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