Skip to content

Commit 4b6daf8

Browse files
Format files
1 parent 37e7ff2 commit 4b6daf8

File tree

5 files changed

+192
-183
lines changed

5 files changed

+192
-183
lines changed

tests/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

tests/conftest.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import pytest
21
import os
32

3+
import pytest
4+
45
# TODO: Fix hack. Changes the env var before initializing the db for testing
5-
os.environ['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
6+
os.environ["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
67

78
from app import app, db
89

910

1011
@pytest.fixture
1112
def client():
12-
app.config['TESTING'] = True
13+
app.config["TESTING"] = True
1314
with app.test_client() as client:
1415
with app.app_context():
1516
db.create_all()
@@ -21,12 +22,16 @@ def client():
2122
@pytest.fixture
2223
def register_user(client):
2324
def _register(email, password):
24-
return client.post('/auth/register', json={'email': email, 'password': password})
25+
return client.post(
26+
"/auth/register", json={"email": email, "password": password}
27+
)
28+
2529
return _register
2630

2731

2832
@pytest.fixture
2933
def login_user(client):
3034
def _login(email, password):
31-
return client.post('/auth/login', json={'email': email, 'password': password})
35+
return client.post("/auth/login", json={"email": email, "password": password})
36+
3237
return _login

tests/test_auth.py

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66

77
class TestAuth:
8-
TEST_EMAIL = '[email protected]'
9-
TEST_PASSWORD = 'testpassword'
10-
11-
8+
TEST_EMAIL = "[email protected]"
9+
TEST_PASSWORD = "testpassword"
10+
1211
@pytest.fixture(autouse=True)
1312
def setup(self, client):
1413
self.client = client
1514
with client.application.app_context():
1615
assert User.query.count() == 0
17-
1816

1917
def _verify_user_in_db(self, email, should_exist=True):
2018
with self.client.application.app_context():
@@ -25,123 +23,112 @@ def _verify_user_in_db(self, email, should_exist=True):
2523
return user
2624
else:
2725
assert user is None
28-
2926

3027
def _count_users(self):
3128
with self.client.application.app_context():
3229
return User.query.count()
33-
3430

3531
def _test_invalid_request_data(self, endpoint, expected_status=400):
3632
response = self.client.post(endpoint, json={})
3733
assert response.status_code == expected_status
38-
39-
response = self.client.post(endpoint, json={'email': self.TEST_EMAIL})
34+
35+
response = self.client.post(endpoint, json={"email": self.TEST_EMAIL})
4036
assert response.status_code == expected_status
41-
42-
response = self.client.post(endpoint, json={'password': self.TEST_PASSWORD})
37+
38+
response = self.client.post(endpoint, json={"password": self.TEST_PASSWORD})
4339
assert response.status_code == expected_status
44-
45-
response = self.client.post(endpoint, data='not json data')
40+
41+
response = self.client.post(endpoint, data="not json data")
4642
assert response.status_code == 415
47-
4843

4944
def test_register_success(self, register_user):
5045
response = register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
5146

5247
assert response.status_code == 201
53-
assert b'Registered!' in response.data
48+
assert b"Registered!" in response.data
5449
assert self._count_users() == 1
5550
user = self._verify_user_in_db(self.TEST_EMAIL)
5651
assert user.password_hash != self.TEST_PASSWORD
5752

58-
5953
def test_register_duplicate_email(self, register_user):
6054
register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
6155
response = register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
6256

6357
assert response.status_code == 409
64-
assert b'Email already exists' in response.data
58+
assert b"Email already exists" in response.data
6559
assert self._count_users() == 1
6660
self._verify_user_in_db(self.TEST_EMAIL)
6761

68-
6962
def test_register_invalid_email_format(self, register_user):
70-
invalid_email = 'not-an-email'
63+
invalid_email = "not-an-email"
7164
response = register_user(invalid_email, self.TEST_PASSWORD)
7265

7366
assert response.status_code == 400
7467
data = response.get_json()
75-
assert data['code'] == 'invalid_email_format'
76-
assert 'error' in data
68+
assert data["code"] == "invalid_email_format"
69+
assert "error" in data
7770
self._verify_user_in_db(invalid_email, should_exist=False)
7871

79-
8072
def test_register_invalid_data(self):
81-
self._test_invalid_request_data('/auth/register')
73+
self._test_invalid_request_data("/auth/register")
8274
assert self._count_users() == 0
8375

84-
8576
def test_login_success(self, register_user, login_user):
8677
register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
8778
response = login_user(self.TEST_EMAIL, self.TEST_PASSWORD)
8879

8980
assert response.status_code == 200
9081
data = response.get_json()
91-
assert 'access_token' in data
92-
assert 'refresh_token' in data
93-
assert len(data['access_token']) > 0
94-
assert len(data['refresh_token']) > 0
95-
82+
assert "access_token" in data
83+
assert "refresh_token" in data
84+
assert len(data["access_token"]) > 0
85+
assert len(data["refresh_token"]) > 0
9686

9787
def test_login_invalid_password(self, register_user, login_user):
9888
register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
99-
response = login_user(self.TEST_EMAIL, 'wrongpassword')
89+
response = login_user(self.TEST_EMAIL, "wrongpassword")
10090

10191
assert response.status_code == 401
102-
assert b'Invalid username or password' in response.data
103-
92+
assert b"Invalid username or password" in response.data
10493

10594
def test_login_invalid_data(self):
106-
self._test_invalid_request_data('/auth/login')
107-
95+
self._test_invalid_request_data("/auth/login")
10896

10997
def test_refresh_token(self, register_user, login_user):
11098
register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
11199
login_resp = login_user(self.TEST_EMAIL, self.TEST_PASSWORD)
112100
tokens = login_resp.get_json()
113-
refresh_token = tokens['refresh_token']
114-
101+
refresh_token = tokens["refresh_token"]
102+
115103
headers = utils.get_auth_header(refresh_token)
116-
refresh_resp = self.client.post('/auth/refresh', headers=headers)
104+
refresh_resp = self.client.post("/auth/refresh", headers=headers)
117105
assert refresh_resp.status_code == 200
118106
data = refresh_resp.get_json()
119-
assert 'access_token' in data
120-
107+
assert "access_token" in data
121108

122109
def test_refresh_token_invalid(self, register_user, login_user):
123110
# Access token test
124111
register_user(self.TEST_EMAIL, self.TEST_PASSWORD)
125112
login_resp = login_user(self.TEST_EMAIL, self.TEST_PASSWORD)
126113
tokens = login_resp.get_json()
127-
access_token = tokens['access_token']
114+
access_token = tokens["access_token"]
128115
headers = utils.get_auth_header(access_token)
129-
response = self.client.post('/auth/refresh', headers=headers)
116+
response = self.client.post("/auth/refresh", headers=headers)
130117

131-
utils.verify_token_error_response(response, 'invalid_token')
118+
utils.verify_token_error_response(response, "invalid_token")
132119

133120
# Malformed token test
134121
malformed_headers = utils.get_invalid_token_headers()
135-
response = self.client.post('/auth/refresh', headers=malformed_headers)
136-
utils.verify_token_error_response(response, 'invalid_token')
137-
122+
response = self.client.post("/auth/refresh", headers=malformed_headers)
123+
utils.verify_token_error_response(response, "invalid_token")
138124

139125
def test_refresh_token_missing_auth(self):
140-
response = self.client.post('/auth/refresh')
141-
utils.verify_token_error_response(response, 'authorization_required')
142-
126+
response = self.client.post("/auth/refresh")
127+
utils.verify_token_error_response(response, "authorization_required")
143128

144129
def test_refresh_token_expired(self):
145-
expired_headers = utils.get_expired_token_headers(self.client.application.app_context())
146-
response = self.client.post('/auth/refresh', headers=expired_headers)
147-
utils.verify_token_error_response(response, 'token_expired')
130+
expired_headers = utils.get_expired_token_headers(
131+
self.client.application.app_context()
132+
)
133+
response = self.client.post("/auth/refresh", headers=expired_headers)
134+
utils.verify_token_error_response(response, "token_expired")

0 commit comments

Comments
 (0)