-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
115 lines (91 loc) · 3.55 KB
/
tests.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
from unittest import TestCase
from datetime import date
from cpsim_app.extensions import app, db, bcrypt
from cpsim_app.models import User, SimDoc
"""
Run these tests with the command:
python -m unittest books_app.main.tests
"""
#################################################
# Setup
#################################################
def create_sim():
sim = SimDoc(doc=f'{os.path.join(app.config["SIM_FOLDER"])}1.pkl', cost=10000, time=107)
db.session.add(sim)
db.session.commit()
def create_user():
password_hash = bcrypt.generate_password_hash('password').decode('utf-8')
user = User(school_email='[email protected]', password=password_hash, class_code='abc', name='bob crump', is_teacher=False)
db.session.add(user)
db.session.commit()
#################################################
# Tests
#################################################
class Tests(TestCase):
"""Tests for authentication (login & signup)."""
def setUp(self):
"""Executed prior to each test."""
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['DEBUG'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
self.app = app.test_client()
db.drop_all()
db.create_all()
def test_signup(self):
post_data = {
'school_email':'[email protected]',
'password':'password',
'class_code':"abc"
}
self.app.post('/signup', data=post_data)
user = User.query.all()
self.assertIn(post_data['school_email'], f'{user}')
def test_signup_existing_user(self):
create_user()
post_data = {
'school_email':'[email protected]',
'password':'password',
'class_code':'abc'
}
response = self.app.post('/signup', data=post_data)
self.assertIn(f'That school_email is taken. Please choose a different one.', response.get_data(as_text=True))
def test_login_correct_password(self):
create_user()
post_data = {
'school_email':'[email protected]',
'password':'password'
}
response = self.app.post('/login', data=post_data)
self.assertNotIn('Login', response.get_data(as_text=True))
def test_login_nonexistent_user(self):
post_data = {
'school_email':'me3',
'password':'passwords'
}
response = self.app.post('/login', data=post_data)
self.assertIn("No user with that school_email. Please try again.", response.get_data(as_text=True))
def test_login_incorrect_password(self):
create_user()
post_data = {
'school_email':'[email protected]',
'password':'incorrect'
}
response = self.app.post('/login', data=post_data)
self.assertIn('Password doesn't match. Please try again', response.get_data(as_text=True))
def test_logout(self):
create_user()
post_data = {
'school_email':'[email protected]',
'password':'password'
}
self.app.post('/login', data=post_data)
response = self.app.get('/logout', follow_redirects=True)
self.assertIn('Log In', response.get_data(as_text=True))
def test_user_sim(self):
create_user()
create_sim()
user = User.query.filter_by(school_email='[email protected]').first()
result = SimDoc.query.filter_by(user_id=user.id).first()
self.assertIsNotNone(result)