summaryrefslogtreecommitdiff
path: root/tests/test_auth_password.py
blob: fc7bb2b644b2307df5f8d2708bb28ea7739bc987 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import pytest
from fxa.crypto import derive_key, quick_stretch_password
from fxa.errors import ClientError

from api import *

class TestPasswordInvalid:
    @pytest.mark.parametrize("args", [
        { 'email': "", 'oldAuthPW': '00' * 32 },
        { 'email': "test0@test", 'oldAuthPW': '00' },
        { 'email': "test0@test", 'oldAuthPW': '00' * 32, 'extra': 0 },
    ])
    def test_change_start_invalid(self, account, args):
        with pytest.raises(ClientError) as e:
            account.post_a("/password/change/start", args)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

    def test_change_start_badaccount(self, account):
        with pytest.raises(ClientError) as e:
            account.post_a("/password/change/start", { 'email': "test0@test", 'oldAuthPW': '00' * 32 })
        assert e.value.details == {
            'code': 400,
            'errno': 102,
            'error': 'Bad Request',
            'message': 'unknown account'
        }
        with pytest.raises(ClientError) as e:
            account.post_a("/password/change/start", { 'email': account.email.upper(), 'oldAuthPW': '00' * 32 })
        assert e.value.details == {
            'code': 400,
            'errno': 120,
            'error': 'Bad Request',
            'message': 'incorrect email case'
        }

    def test_change_start_badpw(self, account):
        with pytest.raises(ClientError) as e:
            account.post_a("/password/change/start", { 'email': account.email, 'oldAuthPW': '00' * 32 })
        assert e.value.details == {
            'code': 400,
            'errno': 103,
            'error': 'Bad Request',
            'message': 'incorrect password'
        }

    @pytest.mark.parametrize("args", [
        { 'email': "" },
        { 'email': "test0@test", 'extra': 0 },
    ])
    def test_forgot_start_invalid(self, account, args):
        with pytest.raises(ClientError) as e:
            account.post_a("/password/forgot/send_code", args)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

    def test_change_forgot_badaccount(self, account):
        with pytest.raises(ClientError) as e:
            account.post_a("/password/forgot/send_code", { 'email': "test0@test" })
        assert e.value.details == {
            'code': 400,
            'errno': 102,
            'error': 'Bad Request',
            'message': 'unknown account'
        }
        with pytest.raises(ClientError) as e:
            account.post_a("/password/forgot/send_code", { 'email': account.email.upper() })
        assert e.value.details == {
            'code': 400,
            'errno': 120,
            'error': 'Bad Request',
            'message': 'incorrect email case'
        }

def test_change_start_unverified(unverified_account):
    with pytest.raises(ClientError) as e:
        unverified_account.post_a("/password/change/start", {
            'email': unverified_account.email,
            'oldAuthPW': '00' * 32
        })
    assert e.value.details == {
        'code': 400,
        'errno': 104,
        'error': 'Bad Request',
        'message': 'unverified account'
    }

@pytest.fixture
def change_token(account):
    pw = auth_pw(account.email, "")
    resp = account.post_a("/password/change/start", { 'email': account.email, 'oldAuthPW': pw })
    assert 'keyFetchToken' in resp
    return PasswordChange(account.client, resp['passwordChangeToken'])

class TestChangeInvalid:
    @pytest.mark.parametrize("args", [
        { 'authPW': '00', 'wrapKb': '00' * 32, 'sessionToken': '00' * 32, },
        { 'authPW': '00' * 32, 'wrapKb': '00', 'sessionToken': '00' * 32, },
        { 'authPW': '00' * 32, 'wrapKb': '00' * 32, 'sessionToken': '00', },
    ])
    def test_change_finish_invalid(self, change_token, args):
        with pytest.raises(ClientError) as e:
            change_token.post_a("/password/change/finish", args)
        assert e.value.details == {
            'code': 400,
            'errno': 107,
            'error': 'Bad Request',
            'message': 'invalid parameter in request body'
        }

def test_change_finish(account, change_token, mail_server):
    pw = auth_pw(account.email, "new")
    change_token.post_a("/password/change/finish", {
        'authPW': pw,
        'wrapKb': '00' * 32,
    })
    account.password = "new" # for fixture teardown
    (to, body) = mail_server.wait()
    assert account.email in to
    assert 'password has been changed' in body

    # just do a login test to see that the password was really changed
    account.login(account.email, "new")
    with pytest.raises(ClientError) as e:
        account.login(account.email, "")
    assert e.value.details == {
        'code': 400,
        'errno': 103,
        'error': 'Bad Request',
        'message': 'incorrect password'
    }

def test_change_finish_twice(account, change_token, mail_server):
    pw = auth_pw(account.email, "new")
    change_token.post_a("/password/change/finish", {
        'authPW': pw,
        'wrapKb': '00' * 32,
    })
    account.password = "new" # for fixture teardown

    with pytest.raises(ClientError) as e:
        change_token.post_a("/password/change/finish", {
                'authPW': pw,
                'wrapKb': '00' * 32,
        })
    assert e.value.details == {
        'code': 401,
        'errno': 109,
        'error': 'Unauthorized',
        'message': 'invalid request signature'
    }

def test_change_forgot_unverified(unverified_account):
    with pytest.raises(ClientError) as e:
        unverified_account.post_a("/password/forgot/send_code", { 'email': unverified_account.email })
    assert e.value.details == {
        'code': 400,
        'errno': 104,
        'error': 'Bad Request',
        'message': 'unverified account'
    }

@pytest.mark.parametrize("args", [
    { 'code': '', 'extra': 0, },
])
def test_forgot_finish_invalid(change_token, args):
    with pytest.raises(ClientError) as e:
        change_token.post_a("/password/forgot/send_code", args)
    assert e.value.details == {
        'code': 400,
        'errno': 107,
        'error': 'Bad Request',
        'message': 'invalid parameter in request body'
    }

def test_forgot_finish_badcode(account, forgot_token, mail_server):
    with pytest.raises(ClientError) as e:
        resp = forgot_token.post_a("/password/forgot/verify_code", { 'code': '' })
    assert e.value.details == {
        'code': 400,
        'errno': 105,
        'error': 'Bad Request',
        'message': 'invalid verification code'
    }

def test_forgot_finish(account, forgot_token, mail_server):
    resp = forgot_token.post_a("/password/forgot/verify_code", { 'code': forgot_token.code })
    assert 'accountResetToken' in resp

def test_forgot_finish_twice(account, forgot_token, mail_server):
    forgot_token.post_a("/password/forgot/verify_code", { 'code': forgot_token.code })

    with pytest.raises(ClientError) as e:
        forgot_token.post_a("/password/forgot/verify_code", { 'code': forgot_token.code })
    assert e.value.details == {
        'code': 401,
        'errno': 109,
        'error': 'Unauthorized',
        'message': 'invalid request signature'
    }