1
|
|
|
""" |
2
|
|
|
:Copyright: 2006-2019 Jochen Kupperschmidt |
3
|
|
|
:License: Modified BSD, see LICENSE for details. |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from byceps.services.authorization import service as authorization_service |
7
|
|
|
|
8
|
|
|
from tests.base import AbstractAppTestCase |
9
|
|
|
from tests.helpers import ( |
10
|
|
|
create_email_config, |
11
|
|
|
create_site, |
12
|
|
|
create_user, |
13
|
|
|
http_client, |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
from testfixtures.verification_token import ( |
17
|
|
|
create_verification_token_for_email_address_confirmation as create_confirmation_token, |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class EmailAddressConfirmationTestCase(AbstractAppTestCase): |
22
|
|
|
|
23
|
|
|
def setUp(self): |
24
|
|
|
super().setUp() |
25
|
|
|
|
26
|
|
|
create_email_config() |
27
|
|
|
create_site() |
28
|
|
|
|
29
|
|
|
self.user = create_user(initialized=False) |
30
|
|
|
assert not self.user.initialized |
31
|
|
|
|
32
|
|
|
def test_confirm_email_address_with_valid_token(self): |
33
|
|
|
authorization_service.create_role('board_user', 'Board User') |
34
|
|
|
|
35
|
|
|
verification_token = create_confirmation_token(self.user.id) |
36
|
|
|
self.db.session.add(verification_token) |
37
|
|
|
self.db.session.commit() |
38
|
|
|
|
39
|
|
|
# -------------------------------- # |
40
|
|
|
|
41
|
|
|
response = self._confirm(verification_token) |
42
|
|
|
|
43
|
|
|
# -------------------------------- # |
44
|
|
|
|
45
|
|
|
assert response.status_code == 302 |
46
|
|
|
assert self.user.initialized |
47
|
|
|
assert _get_role_ids(self.user.id) == {'board_user'} |
48
|
|
|
|
49
|
|
|
def test_confirm_email_address_with_unknown_token(self): |
50
|
|
|
verification_token = create_confirmation_token(self.user.id) |
51
|
|
|
verification_token.token = 'wZdSLzkT-zRf2x2T6AR7yGa3Nc_X3Nn3F3XGPvPtOhw' |
52
|
|
|
|
53
|
|
|
# -------------------------------- # |
54
|
|
|
|
55
|
|
|
response = self._confirm(verification_token) |
56
|
|
|
|
57
|
|
|
# -------------------------------- # |
58
|
|
|
|
59
|
|
|
assert response.status_code == 404 |
60
|
|
|
assert not self.user.initialized |
61
|
|
|
assert _get_role_ids(self.user.id) == set() |
62
|
|
|
|
63
|
|
|
def _confirm(self, verification_token): |
64
|
|
|
url = f'/users/email_address/confirmation/{verification_token.token}' |
65
|
|
|
with http_client(self.app) as client: |
66
|
|
|
return client.get(url) |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def _get_role_ids(user_id): |
70
|
|
|
return authorization_service.find_role_ids_for_user(user_id) |
71
|
|
|
|