summaryrefslogtreecommitdiff
path: root/tests/test_auth_account.py
blob: 365a53cf869c66506b77cad7a5b10bb36a45afd4 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import os
import pytest
from fxa.errors import ClientError

from api import *

def test_create_no_args(client):
    with pytest.raises(ClientError) as e:
        client.post("/account/create")
    assert e.value.details == {
        'code': 400,
        'errno': 106,
        'error': 'Bad Request',
        'message': 'invalid json in request body'
    }

@pytest.mark.parametrize("args", [
    {"email": "", "authPW": "", "extra": ""},
    {"email": "", "authPW": "00"},
    {"email": "a" * 257, "authPW": "0" * 64},
    {"email": "a@test", "authPW": "00"},
    {"email": "a@test", "authPW": "00" * 32, "style": "foo"},
])
def test_create_invalid_args(client, args):
    with pytest.raises(ClientError) as e:
        client.post("/account/create", args)
    assert e.value.details == {
        'code': 400,
        'errno': 107,
        'error': 'Bad Request',
        'message': 'invalid parameter in request body'
    }

def test_create_nomail(client):
    with pytest.raises(ClientError) as e:
        client.create_account("test.account@test-auth", "")
    assert e.value.details == {
        'code': 422,
        'errno': 151,
        'error': 'Unprocessable Entity',
        'message': 'failed to send email'
    }

class TestCreateExists:
    def test_create_exists(self, account):
        with pytest.raises(ClientError) as e:
            account.create_account(account.email, "")
        assert e.value.details == {
            'code': 400,
            'errno': 101,
            'error': 'Bad Request',
            'message': 'account already exists'
        }

@pytest.mark.parametrize("keys", [None, False, True])
@pytest.mark.parametrize("verify", [False, True])
def test_create_destroy(client, keys, verify, mail_server):
    s = client.create_account("test.create.destroy@test-auth", "", keys=keys)
    try:
        if verify:
            (to, body) = mail_server.wait()
            assert to == [s.email]
            data = json.loads(base64.urlsafe_b64decode(body.split("#/verify/", maxsplit=1)[1]).decode('utf8'))
            s.post_a('/recovery_email/verify_code', { 'uid': data['uid'], 'code': data['code'] })
        if keys:
            k = s.fetch_keys(s.props['keyFetchToken'], "")
            with pytest.raises(ClientError) as e:
                s.fetch_keys(s.props['keyFetchToken'], "")
            assert e.value.details == {
                'code': 401,
                'errno': 109,
                'error': 'Unauthorized',
                'message': 'invalid request signature'
            }
    finally:
        client.destroy_account("test.create.destroy@test-auth", "")

def test_login_no_args(client):
    with pytest.raises(ClientError) as e:
        client.post("/account/login")
    assert e.value.details == {
        'code': 400,
        'errno': 106,
        'error': 'Bad Request',
        'message': 'invalid json in request body'
    }

@pytest.mark.parametrize("args", [
    {"email": "", "authPW": "", "extra": ""},
    {"email": "", "authPW": "00"},
    {"email": "a" * 257, "authPW": "0" * 64},
    {"email": "a@test", "authPW": "00"},
])
def test_login_invalid_args(client, args):
    with pytest.raises(ClientError) as e:
        client.post("/account/login", args)
    assert e.value.details == {
        'code': 400,
        'errno': 107,
        'error': 'Bad Request',
        'message': 'invalid parameter in request body'
    }

def test_login_noaccount(client):
    with pytest.raises(ClientError) as e:
        client.login("test@test", "")
    assert e.value.details == {
        'code': 400,
        'errno': 102,
        'error': 'Bad Request',
        'message': 'unknown account'
    }

def test_login_unverified(client, unverified_account):
    with pytest.raises(ClientError) as e:
        client.login(unverified_account.email, "")
    assert e.value.details == {
        'code': 400,
        'errno': 104,
        'error': 'Bad Request',
        'message': 'unverified account'
    }

class TestBadLogin:
    def test_login_badcase(self, account):
        with pytest.raises(ClientError) as e:
            account.login(account.email.upper(), "")
        assert e.value.details == {
            'code': 400,
            'errno': 120,
            'error': 'Bad Request',
            'message': 'incorrect email case'
        }

    def test_login_badpasswd(self, account):
        with pytest.raises(ClientError) as e:
            account.login(account.email, "ca0cb780-f114-405a-a5c2-1a4deb933a51")
        assert e.value.details == {
            'code': 400,
            'errno': 103,
            'error': 'Bad Request',
            'message': 'incorrect password'
        }

@pytest.mark.parametrize("keys", [None, False, True])
def test_login(client, account, keys):
    s = client.login(account.email, "", keys=keys)
    try:
        if keys:
            s.fetch_keys(s.props['keyFetchToken'], "")
            with pytest.raises(ClientError) as e:
                s.fetch_keys(s.props['keyFetchToken'], "")
            assert e.value.details == {
                'code': 401,
                'errno': 109,
                'error': 'Unauthorized',
                'message': 'invalid request signature'
            }
    finally:
        s.destroy_session()

@pytest.mark.parametrize("args", [
    {"email": "", "authPW": "", "extra": ""},
    {"email": "", "authPW": "00"},
    {"email": "a" * 257, "authPW": "0" * 64},
    {"email": "a@test", "authPW": "00"},
])
def test_destroy_invalid_args(client, args):
    with pytest.raises(ClientError) as e:
        client.post("/account/destroy", args)
    assert e.value.details == {
        'code': 400,
        'errno': 107,
        'error': 'Bad Request',
        'message': 'invalid parameter in request body'
    }

def test_destroy_noaccount(client):
    with pytest.raises(ClientError) as e:
        client.destroy_account("test@test", "")
    assert e.value.details == {
        'code': 400,
        'errno': 102,
        'error': 'Bad Request',
        'message': 'unknown account'
    }

class TestBadDestroy:
    def test_destroy_badcase(self, account):
        with pytest.raises(ClientError) as e:
            account.destroy_account(account.email.upper(), "")
        assert e.value.details == {
            'code': 400,
            'errno': 120,
            'error': 'Bad Request',
            'message': 'incorrect email case'
        }

    def test_destroy_badpasswd(self, account):
        with pytest.raises(ClientError) as e:
            account.destroy_account(account.email, "ca0cb780-f114-405a-a5c2-1a4deb933a51")
        assert e.value.details == {
            'code': 400,
            'errno': 103,
            'error': 'Bad Request',
            'message': 'incorrect password'
        }

@pytest.mark.parametrize("auth", [
    {},
    {"authorization": "bearer invalid"},
    {"authorization": "hawk id=invalid"},
])
def test_keys_invalid(client, auth):
    with pytest.raises(ClientError) as e:
        client.get("/account/keys", headers=auth)
    assert e.value.details == {
        'code': 401,
        'errno': 109,
        'error': 'Unauthorized',
        'message': 'invalid request signature'
    }

# create and login do the remaining keyfetch tests.
# we don't check whether the bundle is actually *valid* because we'd have to look
# into the db to do a full test, so we'll trust it is correct if sync works.

@pytest.fixture
def reset_token(account, forgot_token, mail_server):
    resp = forgot_token.post_a("/password/forgot/verify_code", { 'code': forgot_token.code })
    return AccountReset(account.client, resp['accountResetToken'])

@pytest.mark.parametrize("args", [
    { 'authPW': '00', },
    { 'authPW': '00' * 32, 'extra': 0, },
])
def test_reset_invalid(reset_token, args):
    with pytest.raises(ClientError) as e:
        reset_token.post_a("/account/reset", args)
    assert e.value.details == {
        'code': 400,
        'errno': 107,
        'error': 'Bad Request',
        'message': 'invalid parameter in request body'
    }

def test_reset(account, reset_token, mail_server, push_server):
    dev = Device(account, "dev", pcb=push_server.good("d4105515-f4f0-4d26-85c1-f48c5c348edb"))
    resp = reset_token.post_a("/account/reset", { 'authPW': auth_pw(account.email, "") })
    assert resp == {}
    (to, body) = mail_server.wait()
    assert account.email in to
    assert 'account has been reset' in body
    p = push_server.wait()
    assert p[0] == "/d4105515-f4f0-4d26-85c1-f48c5c348edb"
    assert dev.decrypt(p[2]) == {'command': 'fxaccounts:password_reset', 'version': 1}
    p = push_server.wait()
    assert p[0] == "/d4105515-f4f0-4d26-85c1-f48c5c348edb"
    assert dev.decrypt(p[2]) == {
        'command': 'fxaccounts:device_disconnected',
        'data': {'id': dev.id},
        'version': 1
    }
    assert push_server.done()

def test_reset_twice(account, reset_token, mail_server, push_server):
    reset_token.post_a("/account/reset", { 'authPW': auth_pw(account.email, "") })
    with pytest.raises(ClientError) as e:
        reset_token.post_a("/account/reset", { 'authPW': auth_pw(account.email, "") })
    assert e.value.details == {
        'code': 401,
        'errno': 109,
        'error': 'Unauthorized',
        'message': 'invalid request signature'
    }

@pytest.mark.invite
def test_create_noinvite(client):
    with pytest.raises(ClientError) as e:
        client.create_account("test.create.destroy@test-auth", "")
    assert e.value.details == {
        'code': 400,
        'errno': -1,
        'error': 'Bad Request',
        'message': 'invite code required'
    }

@pytest.mark.invite
def test_create_badinvite(client):
    with pytest.raises(ClientError) as e:
        client.create_account("test.create.destroy@test-auth", "", { 'style': '' })
    assert e.value.details == {
        'code': 400,
        'errno': -1,
        'error': 'Bad Request',
        'message': 'invite code required'
    }

@pytest.mark.invite
def test_create_invite(client, mail_server):
    # all in one test because restarting the server from python would be a pain
    c = client.create_account("test.account.invite@test-auth", "", invite=os.environ['INVITE_CODE'])
    c2 = None
    try:
        invite = Invite(c.props['sessionToken'])
        # unverified sessions fail
        with pytest.raises(ClientError) as e:
            code = invite.post_a('/generate', { 'ttl_hours': 1 })
        assert e.value.details == {
            'code': 400,
            'errno': 138,
            'error': 'Bad Request',
            'message': 'unverified session'
        }
        # verified session works
        (to, body) = mail_server.wait()
        data = json.loads(base64.urlsafe_b64decode(body.split("#/verify/", maxsplit=1)[1]).decode('utf8'))
        c.post_a('/recovery_email/verify_code', { 'uid': data['uid'], 'code': data['code'] })
        code = invite.post_a('/generate', { 'ttl_hours': 1 })
        assert 'url' in code
        code = code['url'].split('/')[-1]
        # code allows registration
        c2 = client.create_account("test.account.invite2@test-auth", "", invite=code)
        (to, body) = mail_server.wait()
        data = json.loads(base64.urlsafe_b64decode(body.split("#/verify/", maxsplit=1)[1]).decode('utf8'))
        c2.post_a('/recovery_email/verify_code', { 'uid': data['uid'], 'code': data['code'] })
        # token is consumed
        with pytest.raises(ClientError) as e:
            client.create_account("test.account3@test-auth", "", invite=code)
        assert e.value.details == {
            'code': 400,
            'errno': -2,
            'error': 'Bad Request',
            'message': 'invite code not found'
        }
        # non-admin accounts can't generate tokens
        with pytest.raises(ClientError) as e:
            invite2 = Invite(c2.props['sessionToken'])
            invite2.post_a('/generate', { 'ttl_hours': 1 })
        assert e.value.details == {
            'code': 401,
            'errno': 110,
            'error': 'Unauthorized',
            'message': 'invalid authentication token'
        }
    finally:
        c.destroy_account(c.email, "")
        if c2 is not None:
            c2.destroy_account(c2.email, "")