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
|
from datetime import datetime
from unittest import mock
import pytest
from szurubooru import api, db, errors
from szurubooru.func import auth, mailer
@pytest.fixture
def password_reset_api(config_injector):
config_injector({
'secret': 'x',
'base_url': 'http://example.com/',
'name': 'Test instance',
})
return api.PasswordResetApi()
def test_reset_sending_email(
password_reset_api, context_factory, user_factory):
db.session.add(user_factory(
name='u1', rank='regular_user', email='user@example.com'))
for getter in ['u1', 'user@example.com']:
mailer.send_mail = mock.MagicMock()
assert password_reset_api.get(context_factory(), getter) == {}
mailer.send_mail.assert_called_once_with(
'noreply@Test instance',
'user@example.com',
'Password reset for Test instance',
'You (or someone else) requested to reset your password ' +
'on Test instance.\nIf you wish to proceed, click this l' +
'ink: http://example.com/password-reset/u1:4ac0be176fb36' +
'4f13ee6b634c43220e2\nOtherwise, please ignore this email.')
def test_trying_to_reset_non_existing(password_reset_api, context_factory):
with pytest.raises(errors.NotFoundError):
password_reset_api.get(context_factory(), 'u1')
def test_trying_to_reset_without_email(
password_reset_api, context_factory, user_factory):
db.session.add(user_factory(name='u1', rank='regular_user', email=None))
with pytest.raises(errors.ValidationError):
password_reset_api.get(context_factory(), 'u1')
def test_confirming_with_good_token(
password_reset_api, context_factory, user_factory):
user = user_factory(
name='u1', rank='regular_user', email='user@example.com')
old_hash = user.password_hash
db.session.add(user)
context = context_factory(
input={'token': '4ac0be176fb364f13ee6b634c43220e2'})
result = password_reset_api.post(context, 'u1')
assert user.password_hash != old_hash
assert auth.is_valid_password(user, result['password']) is True
def test_trying_to_confirm_non_existing(password_reset_api, context_factory):
with pytest.raises(errors.NotFoundError):
password_reset_api.post(context_factory(), 'u1')
def test_trying_to_confirm_without_token(
password_reset_api, context_factory, user_factory):
db.session.add(user_factory(
name='u1', rank='regular_user', email='user@example.com'))
with pytest.raises(errors.ValidationError):
password_reset_api.post(context_factory(input={}), 'u1')
def test_trying_to_confirm_with_bad_token(
password_reset_api, context_factory, user_factory):
db.session.add(user_factory(
name='u1', rank='regular_user', email='user@example.com'))
with pytest.raises(errors.ValidationError):
password_reset_api.post(
context_factory(input={'token': 'bad'}), 'u1')
|