Skip to content

Commit b426102

Browse files
committed
Fixed test and updated client fixture
1 parent 036bdf5 commit b426102

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

lms/lmsweb/tools/registration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from wtforms.validators import Email, EqualTo, InputRequired, Length
1515

1616
from lms.lmsdb import models
17-
from lms.lmsweb import config, webmail
17+
from lms.lmsweb import config, webapp, webmail
1818
from lms.utils.log import log
1919

2020
import requests
@@ -179,7 +179,8 @@ def send_confirmation_mail(email: str, fullname: str) -> None:
179179
)
180180
link = url_for('confirm_email', token=token, _external=True)
181181
msg.body = f'Hey {fullname},\nYour confirmation link is: {link}'
182-
webmail.send(msg)
182+
if not webapp.config.get('TESTING'):
183+
webmail.send(msg)
183184

184185

185186
if __name__ == '__main__':

tests/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def db(db_in_memory):
4949
db_in_memory.rollback()
5050

5151

52-
@pytest.fixture(autouse=True, scope='session')
53-
def app():
54-
return webapp
52+
@pytest.fixture(autouse=True, scope='function')
53+
def client():
54+
return webapp.test_client()
5555

5656

5757
@pytest.fixture(autouse=True, scope='session')
@@ -179,17 +179,17 @@ def admin_user():
179179

180180

181181
@pytest.fixture(autouse=True, scope='session')
182-
def captured_templates(app):
182+
def captured_templates():
183183
recorded = []
184184

185185
def record(sender, template, context, **kwargs):
186186
recorded.append((template, context))
187187

188-
template_rendered.connect(record, app)
188+
template_rendered.connect(record, webapp)
189189
try:
190190
yield recorded
191191
finally:
192-
template_rendered.disconnect(record, app)
192+
template_rendered.disconnect(record, webapp)
193193

194194

195195
def create_notification(

tests/test_flask_limiter.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from flask.testing import FlaskClient
2+
13
from lms.lmsweb import routes
24
from lms.lmsdb.models import Solution, User
35
from lms.lmsweb import webapp
@@ -7,8 +9,7 @@
79
class TestLimiter:
810
@staticmethod
911
@conftest.use_limiter
10-
def test_limiter_login_fails(student_user: User):
11-
client = webapp.test_client()
12+
def test_limiter_login_fails(client: FlaskClient, student_user: User):
1213
for _ in range(webapp.config['LIMITS_PER_MINUTE'] - 1):
1314
response = client.post('/login', data={
1415
'username': student_user.username,
@@ -25,16 +26,14 @@ def test_limiter_login_fails(student_user: User):
2526

2627
@staticmethod
2728
@conftest.use_limiter
28-
def test_limiter_login_refreshes():
29-
client = webapp.test_client()
29+
def test_limiter_login_refreshes(client: FlaskClient):
3030
for _ in range(webapp.config['LIMITS_PER_MINUTE'] + 1):
3131
response = client.get('/login')
3232
assert response.status_code == 200
3333

3434
@staticmethod
3535
@conftest.use_limiter
36-
def test_limiter_login_success(student_user: User):
37-
client = webapp.test_client()
36+
def test_limiter_login_success(client: FlaskClient, student_user: User):
3837
client.post('/login', data={
3938
'username': student_user.username,
4039
'password': 'fake5',

tests/test_login.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
from flask.testing import FlaskClient
2+
13
from lms.lmsdb.models import User
2-
from lms.lmsweb import webapp
34

45

56
class TestLogin:
67
@staticmethod
7-
def test_login_password_fail(student_user: User):
8-
client = webapp.test_client()
8+
def test_login_password_fail(client: FlaskClient, student_user: User):
99
client.post('/login', data={
1010
'username': student_user.username,
1111
'password': 'wrong_pass',
@@ -14,8 +14,7 @@ def test_login_password_fail(student_user: User):
1414
assert fail_login_response.status_code == 302
1515

1616
@staticmethod
17-
def test_login_username_fail():
18-
client = webapp.test_client()
17+
def test_login_username_fail(client: FlaskClient):
1918
client.post('/login', data={
2019
'username': 'wrong_user',
2120
'password': 'fake pass',
@@ -25,10 +24,9 @@ def test_login_username_fail():
2524

2625
@staticmethod
2726
def test_login_not_confirmed_user(
28-
not_confirmed_user: User, captured_templates,
27+
client: FlaskClient, not_confirmed_user: User, captured_templates,
2928
):
30-
client = webapp.test_client()
31-
login_response = client.post('/login', data={
29+
client.post('/login', data={
3230
'username': not_confirmed_user.username,
3331
'password': 'fake pass',
3432
}, follow_redirects=True)
@@ -39,8 +37,7 @@ def test_login_not_confirmed_user(
3937
assert fail_login_response.status_code == 302
4038

4139
@staticmethod
42-
def test_login_success(student_user: User):
43-
client = webapp.test_client()
40+
def test_login_success(client: FlaskClient, student_user: User):
4441
client.post('/login', data={
4542
'username': student_user.username,
4643
'password': 'fake pass',

tests/test_registration.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
from flask.testing import FlaskClient
2+
13
from lms.lmsweb.tools.registration import generate_confirmation_token
24
from lms.lmsdb.models import User
3-
from lms.lmsweb import webapp
45

56

67
class TestRegistration:
78
@staticmethod
8-
def test_invalid_username(student_user: User, captured_templates):
9-
client = webapp.test_client()
9+
def test_invalid_username(
10+
client: FlaskClient, student_user: User, captured_templates,
11+
):
1012
client.post('/signup', data={
1113
'email': 'some_name@mail.com',
1214
'username': student_user.username,
@@ -25,8 +27,9 @@ def test_invalid_username(student_user: User, captured_templates):
2527
assert fail_login_response.status_code == 302
2628

2729
@staticmethod
28-
def test_invalid_email(student_user: User, captured_templates):
29-
client = webapp.test_client()
30+
def test_invalid_email(
31+
client: FlaskClient, student_user: User, captured_templates,
32+
):
3033
client.post('/signup', data={
3134
'email': student_user.mail_address,
3235
'username': 'some_user',
@@ -45,8 +48,7 @@ def test_invalid_email(student_user: User, captured_templates):
4548
assert fail_login_response.status_code == 302
4649

4750
@staticmethod
48-
def test_successful_registration(captured_templates):
49-
client = webapp.test_client()
51+
def test_successful_registration(client: FlaskClient, captured_templates):
5052
client.post('/signup', data={
5153
'email': 'some_user123@mail.com',
5254
'username': 'some_user',

tests/test_users.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from flask.testing import FlaskClient
2+
13
from lms.lmsdb.models import User
2-
from lms.lmsweb import webapp
34
from tests import conftest
45

56

@@ -61,8 +62,7 @@ def test_logout(student_user: User):
6162
assert logout_response.status_code == 200
6263

6364
@staticmethod
64-
def test_banned_user(banned_user: User):
65-
client = webapp.test_client()
65+
def test_banned_user(client: FlaskClient, banned_user: User):
6666
login_response = client.post('/login', data={
6767
'username': banned_user.username,
6868
'password': 'fake pass',

0 commit comments

Comments
 (0)