Add tests for registration

This commit is contained in:
Gergely Polonkai 2018-07-10 15:34:52 +02:00
parent 66659dfe51
commit f782ece010

View File

@ -1,13 +1,14 @@
import pytest import pytest
import calsocial import calsocial
from calsocial.models import db from calsocial.models import db, User
@pytest.fixture @pytest.fixture
def client(): def client():
calsocial.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' calsocial.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
calsocial.app.config['TESTING'] = True calsocial.app.config['TESTING'] = True
calsocial.app.config['WTF_CSRF_ENABLED'] = False
client = calsocial.app.test_client() client = calsocial.app.test_client()
with calsocial.app.app_context(): with calsocial.app.app_context():
@ -15,6 +16,9 @@ def client():
yield client yield client
with calsocial.app.app_context():
db.drop_all()
def test_index_no_login(client): def test_index_no_login(client):
"""Test the main page without logging in """Test the main page without logging in
@ -22,3 +26,96 @@ def test_index_no_login(client):
page = client.get('/') page = client.get('/')
assert b'Welcome to Calendar.social' in page.data assert b'Welcome to Calendar.social' in page.data
def test_register_page(client):
"""Test the registration page
"""
page = client.get('/register')
assert b'Register</button>' in page.data
def test_register_post_empty(client):
"""Test sending empty registration data
"""
page = client.post('/register', data={})
assert b'This field is required' in page.data
def test_register_invalid_email(client):
"""Test sending an invalid email address
"""
page = client.post('/register', data={
'username': 'test',
'email': 'test',
'password': 'password',
'password_retype': 'password',
})
assert b'Invalid email address' in page.data
def test_register_password_mismatch(client):
"""Test sending different password for registration
"""
page = client.post('/register', data={
'username': 'test',
'email': 'test@example.com',
'password': 'password',
'password_retype': 'something',
})
assert b'The two passwords must match' in page.data
def test_register(client):
"""Test user registration
"""
page = client.post('/register', data={
'username': 'test',
'email': 'test@example.com',
'password': 'password',
'password_retype': 'password',
})
print(page.data)
assert page.status_code == 302
assert page.location == 'http://localhost/'
with calsocial.app.app_context():
user = User.query.one()
assert user.username == 'test'
assert user.email == 'test@example.com'
def test_register_existing_username(client):
"""Test registering an existing username
"""
with calsocial.app.app_context():
user = User(username='test', email='test@example.com')
db.session.add(user)
db.session.commit()
page = client.post('/register', data={
'username': 'test',
'email': 'test2@example.com',
'password': 'password',
'password_retype': 'password',
})
assert b'This username is not available' in page.data
def test_register_existing_email(client):
"""Test registering an existing email address
"""
with calsocial.app.app_context():
user = User(username='test', email='test@example.com')
db.session.add(user)
db.session.commit()
page = client.post('/register', data={
'username': 'tester',
'email': 'test@example.com',
'password': 'password',
'password_retype': 'password',
})
assert b'This email address can not be used' in page.data