- Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathconftest.py
103 lines (82 loc) · 2.15 KB
/
conftest.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
importmock
importpytest
fromappimportappas_app, SQLAlchemyDB
fromapp.databaseimportmodel
fromapp.utilsimportRequestUtil
importapp.utils.SshUtilasssh
from .importsuccess, load_data
# =====================================
# base fixtures
# =====================================
@pytest.fixture
defapp():
SQLAlchemyDB.create_all()
yield_app
SQLAlchemyDB.session.close()
SQLAlchemyDB.drop_all()
@pytest.fixture
defclient(app):
withapp.test_client() asc:
yieldc
@pytest.fixture
defsql(app):
returnSQLAlchemyDB.session
@pytest.fixture
deftester(app, sql):
"""模拟已登录用户"""
withapp.test_client() asc:
user=create_user(sql)
mock_func=mock.MagicMock(return_value=user.dict())
withmock.patch.object(RequestUtil, 'get_login_user', new=mock_func):
yieldc
defcreate_user(sql):
user_id='tester'
user=model.User(
id=user_id,
name=user_id,
location='',
avatar=''
)
sql.add(user)
sql.commit()
returnuser
# =====================================
# server fixtures
# =====================================
SERVER_DATA= {
'ip': '127.0.0.1',
'name': 'dev',
'port': '22',
'account': 'root',
'pkey': 'asdfghjkl',
}
defmock_do_ssh_cmd(*args, **kwargs):
returnTrue, "OK"
@pytest.fixture
defcreate_server(tester):
deffunc(**kwargs):
data=SERVER_DATA.copy()
data.update(kwargs)
withmock.patch.object(ssh, 'do_ssh_cmd', new=mock_do_ssh_cmd):
resp=tester.post('/api/server/new', data=data)
assertsuccess(resp)
returnload_data(resp)
returnfunc
# =====================================
# webhook fixtures
# =====================================
WEBHOOK_DATA= {
'repo': 'git-webhook',
'branch': 'master',
'shell': 'echo hello',
}
@pytest.fixture
defcreate_webhook(tester):
deffunc(**kwargs):
data=WEBHOOK_DATA.copy()
data.update(kwargs)
resp=tester.post('/api/webhook/new', data=data)
assertsuccess(resp)
returnload_data(resp)
returnfunc