1
|
|
|
""" |
2
|
|
|
tests.helpers |
3
|
|
|
~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from contextlib import contextmanager |
10
|
|
|
|
11
|
|
|
from flask import appcontext_pushed, g |
12
|
|
|
|
13
|
|
|
from byceps.application import create_app |
14
|
|
|
from byceps.database import db |
15
|
|
|
from byceps.services.authentication.session import service as session_service |
16
|
|
|
from byceps.services.authorization import service as authorization_service |
17
|
|
|
from byceps.services.party import service as party_service |
18
|
|
|
from byceps.services.site import service as site_service |
19
|
|
|
|
20
|
|
|
from testfixtures.brand import create_brand as _create_brand |
21
|
|
|
from testfixtures.party import create_party as _create_party |
22
|
|
|
from testfixtures.user import create_user as _create_user, \ |
23
|
|
|
create_user_with_detail as _create_user_with_detail |
24
|
|
|
|
25
|
|
|
from .base import CONFIG_FILENAME_TEST_PARTY |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
@contextmanager |
29
|
|
|
def app_context(*, config_filename=CONFIG_FILENAME_TEST_PARTY): |
30
|
|
|
app = create_app(config_filename) |
31
|
|
|
|
32
|
|
|
with app.app_context(): |
33
|
|
|
yield app |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
@contextmanager |
37
|
|
|
def current_party_set(app, party): |
38
|
|
|
def handler(sender, **kwargs): |
39
|
|
|
g.party_id = party.id |
40
|
|
|
g.brand_id = party.brand_id |
41
|
|
|
|
42
|
|
|
with appcontext_pushed.connected_to(handler, app): |
43
|
|
|
yield |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
@contextmanager |
47
|
|
|
def current_user_set(app, user): |
48
|
|
|
def handler(sender, **kwargs): |
49
|
|
|
g.current_user = user |
50
|
|
|
|
51
|
|
|
with appcontext_pushed.connected_to(handler, app): |
52
|
|
|
yield |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def create_user(*args, **kwargs): |
56
|
|
|
user = _create_user(*args, **kwargs) |
57
|
|
|
|
58
|
|
|
db.session.add(user) |
59
|
|
|
db.session.commit() |
60
|
|
|
|
61
|
|
|
return user |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def create_user_with_detail(*args, **kwargs): |
65
|
|
|
user = _create_user_with_detail(*args, **kwargs) |
66
|
|
|
|
67
|
|
|
db.session.add(user) |
68
|
|
|
db.session.commit() |
69
|
|
|
|
70
|
|
|
return user |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
def assign_permissions_to_user(user_id, role_id, permission_ids, |
74
|
|
|
*, initiator_id=None): |
75
|
|
|
"""Create the role and permissions, assign the permissions to the |
76
|
|
|
role, and assign the role to the user. |
77
|
|
|
""" |
78
|
|
|
role = authorization_service.create_role(role_id, role_id) |
79
|
|
|
|
80
|
|
|
for permission_id in permission_ids: |
81
|
|
|
permission = authorization_service.create_permission(permission_id, |
82
|
|
|
permission_id) |
83
|
|
|
authorization_service.assign_permission_to_role(permission.id, role.id) |
84
|
|
|
|
85
|
|
|
authorization_service.assign_role_to_user(user_id, role.id, |
86
|
|
|
initiator_id=initiator_id) |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def create_brand(brand_id='acmecon', title='ACME Entertainment Convention'): |
90
|
|
|
brand = _create_brand(id=brand_id, title=title) |
91
|
|
|
|
92
|
|
|
db.session.add(brand) |
93
|
|
|
db.session.commit() |
94
|
|
|
|
95
|
|
|
return brand |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def create_party(brand_id, party_id='acmecon-2014', title='ACMECon 2014'): |
99
|
|
|
party = _create_party(id=party_id, title=title, brand_id=brand_id) |
100
|
|
|
|
101
|
|
|
db.session.add(party) |
102
|
|
|
db.session.commit() |
103
|
|
|
|
104
|
|
|
return party_service._db_entity_to_party(party) |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
def create_site(party_id, *, site_id='acmecon-2014-website', title='Website', |
108
|
|
|
server_name='www.example.com'): |
109
|
|
|
return site_service.create_site(site_id, party_id, title, server_name) |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
@contextmanager |
113
|
|
|
def http_client(app, *, user_id=None): |
114
|
|
|
"""Provide an HTTP client. |
115
|
|
|
|
116
|
|
|
If a user ID is given, the client authenticates with the user's |
117
|
|
|
credentials. |
118
|
|
|
""" |
119
|
|
|
client = app.test_client() |
120
|
|
|
|
121
|
|
|
if user_id is not None: |
122
|
|
|
_add_user_credentials_to_session(client, user_id) |
123
|
|
|
|
124
|
|
|
yield client |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def _add_user_credentials_to_session(client, user_id): |
128
|
|
|
session_token = session_service.find_session_token_for_user(user_id) |
129
|
|
|
|
130
|
|
|
with client.session_transaction() as session: |
131
|
|
|
session['user_id'] = str(user_id) |
132
|
|
|
session['user_auth_token'] = str(session_token.token) |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
def login_user(user_id): |
136
|
|
|
"""Authenticate the user to create a session.""" |
137
|
|
|
session_service.get_session_token(user_id) |
138
|
|
|
|