Change directory structure (move ./nopaque/* to ./)

This commit is contained in:
Patrick Jentsch
2021-07-20 15:07:42 +02:00
parent ff39d8d650
commit d6ab379418
231 changed files with 26 additions and 23 deletions

0
tests/__init__.py Normal file
View File

19
tests/test_basics.py Normal file
View File

@ -0,0 +1,19 @@
import unittest
from flask import current_app
from app import create_app, db
class BasicsTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('testing')
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_app_exists(self):
self.assertFalse(current_app is None)

98
tests/test_client.py Normal file
View File

@ -0,0 +1,98 @@
import unittest
from app import create_app, db
class FlaskClientTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('testing')
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
# Role.insert_roles()
self.client = self.app.test_client(use_cookies=True)
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_home_page(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
self.assertTrue('Stranger' in response.get_data(as_text=True))
def test_register(self):
# register a new account
response = self.client.post('/auth/register', data={
'email': 'john@example.com',
'username': 'john',
'password': 'cat',
'password2': 'cat'
})
self.assertEqual(response.status_code, 302)
def test_login(self):
# login with the new account
response = self.client.post('/auth/login', data={
'email': 'john@example.com',
'password': 'cat'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_register_false_username(self):
# register a new account with wrong username
response = self.client.post('/auth/register', data={
'email': 'john@example.com',
'username': 'john.,*Ä#ä+=?',
'password': 'cat',
'password2': 'cat'
})
self.assertEqual(response.status_code, 200)
self.assertTrue('Usernames must have only letters, numbers, dots or underscores' in response.get_data(as_text=True))
def test_register_false_email(self):
# register a new account with wrong username
response = self.client.post('/auth/register', data={
'email': 'john@example',
'username': 'john',
'password': 'cat',
'password2': 'cat'
})
self.assertEqual(response.status_code, 200)
self.assertTrue('Invalid email address.' in response.get_data(as_text=True))
def test_duplicates(self):
# tries to register an account that has already been registered
# test duplicate username and duplicate email
response = self.client.post('/auth/register', data={
'email': 'john@example.com',
'username': 'john',
'password': 'cat',
'password2': 'cat'
})
self.assertEqual(response.status_code, 302)
response = self.client.post('/auth/register', data={
'email': 'john@example2.com',
'username': 'john',
'password': 'cat',
'password2': 'cat'
})
self.assertEqual(response.status_code, 200)
self.assertTrue('Username already in use.' in response.get_data(as_text=True))
response = self.client.post('/auth/register', data={
'email': 'john@example.com',
'username': 'johnsmith',
'password': 'cat',
'password2': 'cat'
})
self.assertEqual(response.status_code, 200)
self.assertTrue('Email already registered.' in response.get_data(as_text=True))
def test_admin_forbidden(self):
response = self.client.post('/auth/login', data={
'email': 'john@example.com',
'password': 'cat'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200)
response = self.client.get('/admin')
self.assertEqual(response.status_code, 403)

71
tests/test_user_model.py Normal file
View File

@ -0,0 +1,71 @@
import unittest
import time
from app import create_app, db
from app.models import User, AnonymousUser, Role, Permission
class UserModelTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app('testing')
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
Role.insert_roles()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_password_setter(self):
u = User(password='cat')
self.assertTrue(u.password_hash is not None)
def test_no_password_getter(self):
u = User(password='cat')
with self.assertRaises(AttributeError):
u.password
def test_password_verification(self):
u = User(password='cat')
self.assertTrue(u.verify_password('cat'))
self.assertFalse(u.verify_password('dog'))
def test_password_salts_are_random(self):
u = User(password='cat')
u2 = User(password='cat')
self.assertTrue(u.password_hash != u2.password_hash)
def test_valid_confirmation_token(self):
u = User(password='cat')
db.session.add(u)
db.session.commit()
token = u.generate_confirmation_token()
self.assertTrue(u.confirm(token))
def test_invalid_confirmation_token(self):
u1 = User(password='cat')
u2 = User(password='dog')
db.session.add(u1)
db.session.add(u2)
db.session.commit()
token = u1.generate_confirmation_token()
self.assertFalse(u2.confirm(token))
def test_expired_confirmation_token(self):
u = User(password='cat')
db.session.add(u)
db.session.commit()
token = u.generate_confirmation_token(1)
time.sleep(2)
self.assertFalse(u.confirm(token))
def test_user_role(self):
u = User(email='john@example.com', password='cat')
self.assertTrue(u.can(Permission.CREATE_JOB))
self.assertFalse(u.can(Permission.ADMIN))
def test_anonymous_user(self):
u = AnonymousUser()
self.assertFalse(u.can(Permission.CREATE_JOB))
self.assertFalse(u.can(Permission.ADMIN))