-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_auth.py
More file actions
executable file
·85 lines (76 loc) · 4.03 KB
/
test_auth.py
File metadata and controls
executable file
·85 lines (76 loc) · 4.03 KB
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
import json
from flask import current_app
from groundstation import db
from groundstation.backend_api.utils import add_user
from groundstation.tests.base import BaseTestCase
class TestAuthentication(BaseTestCase):
def setUp(self):
db.create_all()
db.session.commit()
current_app.config.update(BYPASS_AUTH=False)
def test_login_happy(self):
user = add_user('Nick', 'testing123')
with self.client:
login_data = {'username':'Nick', 'password':'testing123'}
post_data = json.dumps(login_data)
kw_args = {'data':post_data, 'content_type':'application/json'}
response = self.client.post('/api/auth/login', **kw_args)
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertIn('success', response_data['status'])
auth_token = response_data.get('auth_token')
self.assertTrue(auth_token is not None)
def test_login_invalid_password(self):
user = add_user('Nick', 'testing123')
with self.client:
login_data = {'username':'Nick', 'password':'wrong-password'}
post_data = json.dumps(login_data)
kw_args = {'data':post_data, 'content_type':'application/json'}
response = self.client.post('/api/auth/login', **kw_args)
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 400)
self.assertIn('fail', response_data['status'])
self.assertIn('Username and/or password is incorrect', response_data['message'])
def test_login_invalid_username(self):
user = add_user('Nick', 'testing123')
with self.client:
login_data = {'username':'wrong-username', 'password':'testing123'}
post_data = json.dumps(login_data)
kw_args = {'data':post_data, 'content_type':'application/json'}
response = self.client.post('/api/auth/login', **kw_args)
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 400)
self.assertIn('fail', response_data['status'])
self.assertIn('Username and/or password is incorrect', response_data['message'])
def test_logout_happy(self):
user = add_user('Nick', 'testing123')
auth_token = user.encode_auth_token_by_id().decode()
with self.client:
response = self.client.get('/api/auth/logout', headers={'Authorization': f'Bearer {auth_token}'})
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 200)
self.assertIn('success', response_data['status'])
def test_logout_no_token(self):
user = add_user('Nick', 'testing123')
with self.client:
response = self.client.get('/api/auth/logout')
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 401)
def test_logout_invalid_token(self):
user = add_user('Nick', 'testing123')
auth_token = user.encode_auth_token_by_id()
with self.client:
response = self.client.get('/api/auth/logout', headers={'Authorization': f'Bearer INVALIDTOKEN'})
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 401)
self.assertIn('fail', response_data['status'])
def test_logout_expired_token(self):
current_app.config['TOKEN_EXPIRATION_SECONDS'] = -1
user = add_user('Nick', 'testing123')
auth_token = user.encode_auth_token_by_id().decode()
with self.client:
response = self.client.get('/api/auth/logout', headers={'Authorization': f'Bearer {auth_token}'})
response_data = json.loads(response.data.decode())
self.assertEqual(response.status_code, 401)
self.assertIn('fail', response_data['status'])
self.assertIn('Signature expired. Please log in again.', response_data['message'])