def test_search_by_title_provided(self): project.Provided().post('foo', 'bar', User(id=1)) project.Provided().post('hello', 'world', User(id=1)) search_term = 'foo' clg = catalog.Catalog(search_term, True) clg.apply() self.assertEqual(len(clg.projects), 1) self.assertEqual(clg.projects[0].title, 'foo')
def test_apply_chooses_correct_table(self): project.Provided().post('foo', 'bar', User(id=1)) project.Solicited().post('your', 'mom', User(id=1)) clg = catalog.Catalog('', False) clg.apply() self.assertEqual(len(clg.projects), 1) self.assertEqual(clg.projects[0].title, 'your') self.assertEqual(clg.projects[0].description, 'mom')
def test_can_be_modified_by(self): u = User(can_post_provided=True) self.session.add(u) self.session.commit() p = project.Provided() p.post('foo', 'bar', u) p = project.Provided.get(1) self.assertTrue(p.can_be_modified_by(User(id=1, is_admin=False))) self.assertTrue(p.can_be_modified_by(User(id=2, is_admin=True))) self.assertFalse(p.can_be_modified_by(User(id=3, is_admin=False)))
def ycp_signup(self): form = YCPRegistrationForm() if request.method == 'POST': email = session['email'] try: if not any(char in form.password.data for char in string.printable): raise TypeError user = User.sign_up(form.username.data, form.password.data, form.confirm_password.data, email) login_user(user) return redirect(url_for('IndexView:get')) except IntegrityError: flash(Markup(f'<b>{form.username.data}</b> is taken')) except ValueError as e: flash(str(e)) except TypeError: flash( Markup('''<p>Password <b>MUST</b> have at least:</p> <ul> <li>1 Number Character</li> <li>1 Symbol Character</li> <li>1 UpperCase character</li> <li>1 LowerCase character</li> </ul>''')) except Exception as e: flash(str(e)) return render_template('signup.html', form=form, submit_url=url_for('UserView:ycp_signup'))
def test_selected_project_lookup(self): # tests to see that get method works s = project.Provided() s.post('cookie', 'biscuit', User(can_post_provided=True)) s = project.Provided.get(1) self.assertIsNotNone(s) self.assertTrue(s.title == 'cookie' and s.description == 'biscuit')
def forgotPassword(self): form = RecoveryForm() if request.method == 'POST': try: user = User.get_by_username(form.username.data) if user: # initializing size of string N = 8 # using secrets.choices() # generating random strings res = ''.join( secrets.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase + string.punctuation) for i in range(N)) user.update_password(res, res) flash('Password change successful!') mail = Mail() mail.init_app(current_app) mail.send_message(subject="PASSWORD CHANGED!", recipients=[user.email], html=render_template( 'pwd_forgot_email.html', username=form.username.data, res=res)) return redirect(url_for('UserView:login')) except TypeError as e: flash(str(e)) return render_template('forgot_password.html', form=form)
def setUp(self): self.session = self.Session(bind=self.engine) self.user_args = { 'username': '******', 'password': '******', 'confirm_password': '******', 'email': '*****@*****.**', 'name': 'baz', 'is_admin': True, 'bio': 'test', 'contact_info': '*****@*****.**' } self.user = User(id=1, can_post_provided=True, can_post_solicited=True) project.Provided().post('foo', 'barbarba', self.user) project.Provided().post('Awesome', 'Force of Gravity', self.user) project.Provided().post('Cannon', 'Electric Beast', self.user)
def test_post_permissions(self): pp = project.Provided() ps = project.Solicited() up = User(can_post_provided=True, can_post_solicited=False) us = User(can_post_provided=False, can_post_solicited=True) with self.assertRaises(PermissionError): ps.post('foo', 'bar', up) with self.assertRaises(PermissionError): pp.post('foo', 'bar', us) pp.post('foo', 'bar', up) ps.post('foo', 'bar', us) self.assertEqual(len(self.session.query(project.Provided).all()), 1) self.assertEqual(len(self.session.query(project.Solicited).all()), 1)
def test_apply_many_projects(self): project.Provided().post('foo', 'bar', User(id=1)) project.Provided().post('nobody expects', 'the spanish inquisition', User(id=1)) project.Provided().post('sperm whale', 'bowl of petunias', User(id=1)) clg = catalog.Catalog('', True) clg.apply() self.assertEqual(len(clg.projects), 3) projects = [] for proj in clg.projects: projects.append((proj.title, proj.description)) self.assertTrue(('foo', 'bar') in projects) self.assertTrue(('nobody expects', 'the spanish inquisition') in projects) self.assertTrue(('sperm whale', 'bowl of petunias') in projects)
def login(self): msg = '' form = LoginForm() user = None if form.validate_on_submit: try: user = User.log_in(username=form.username.data, password=form.password.data) login_user(user, remember=form.remember.data) return redirect(url_for('IndexView:get')) except Exception as e: return render_template('login.html', msg=str(e), form=form) return render_template('login.html', msg = msg, form=form)
def login(self): form = LoginForm() if request.method == 'POST': try: user = User.log_in(form.username.data, form.password.data) login_user(user, remember=form.remember.data, duration=timedelta(minutes=30.0)) return redirect(url_for('IndexView:get')) except ValueError as e: flash(str(e)) return render_template('login.html', form=form)
def signup(self): form = EmailForm() if request.method == 'POST': match = re.match('(.*)@(.*?\..+)', form.email.data) if match: try: print(form.email.data) User.get_by_email(form.email.data) except: session['email'] = form.email.data if match[2] == 'ycp.edu' and YCPData(match[1]).is_valid: return redirect(url_for('UserView:ycp_signup')) else: return redirect(url_for('UserView:company_signup')) else: flash('There is already an account with that email') else: flash('Please enter a valid email') return render_template('signup.html', form=form, submit_url=url_for('UserView:signup'))
def setUp(self): self.session = self.Session() self.fake_project_list = [ 'these', "don't", 'need', 'to', 'be', 'real', 'projects' ] self.fake_catalog = catalog.Catalog() self.fake_catalog.projects = self.fake_project_list self.user = User(id=1, can_post_provided=True, can_post_solicited=True) project.Provided().post('foo', 'bar', self.user) project.Provided().post('nobody expects', 'the spanish inquisition', self.user) project.Provided().post('sperm whale', 'bowl of petunias', self.user)
def test_project_post(self): p = project.Provided() p.post('foo', 'bar', User(can_post_provided=True)) results = self.session.query(project.Provided).all() self.assertEqual(len(results), 1) self.assertEqual(results[0].id, p.id) self.assertEqual(results[0].title, 'foo') self.assertEqual(results[0].description, 'bar') self.assertEqual(results[0].date, p.date) self.assertEqual(results[0].archived, False) self.assertEqual(results[0].needsReview, False)
def test_edit(self): u = User(can_post_provided=True) self.session.add(u) self.session.commit() project.Provided().post('foo', 'bar', u) p = project.Provided.get(1) p.edit(u, description='baz', this_isnt_real=4) p = project.Provided.get(1) self.assertEqual(p.title, 'foo') self.assertEqual(p.description, 'baz') with self.assertRaises(AttributeError): x = p.this_isnt_real with self.assertRaises(AttributeError): p.edit(u, id=3) u = User(can_post_provided=True, is_admin=False) self.session.add(u) self.session.commit() with self.assertRaises(PermissionError): p.edit(u, title='bar')
def test_get_user_projects(self): user.User.sign_up(**self.user_args) self.user = user.User.log_in('foo', 'barbarba') new_user = User(id=2, can_post_provided=True, can_post_solicited=True) project.Provided().post('Test', 'Fire', new_user) project.Provided().post('Interesting', 'Facts', new_user) project.Provided().post('Fall', 'Food', new_user) clg = catalog.Catalog('', True) clg.apply() projects = self.user.get_user_projects() self.assertEqual(len(clg.projects), 6) self.assertEqual(len(projects), 3) self.assertEqual('foo', projects[0].title) self.assertEqual('Awesome', projects[1].title) self.assertEqual('Cannon', projects[2].title)
def company_signup(self): form = CompanyRegistrationForm() if request.method == 'POST': email = session['email'] try: if not any(char in form.password.data for char in string.printable): raise TypeError user = User.sign_up(form.username.data, form.password.data, form.confirm_password.data, email, bio=form.bio.data, contact_info=form.contact.data, name=form.name.data) flash( "Welcome to YCP Project Database! Your account requires a review. We'll let you know when you can sign in" ) return redirect(url_for('IndexView:get')) except IntegrityError: flash(Markup(f'<b>{form.username.data}</b> is taken')) except ValueError as e: flash(str(e)) except TypeError: flash( Markup('''<p>Password <b>MUST</b> have at least:</p> <ul> <li>1 Number Character</li> <li>1 Symbol Character</li> <li>1 UpperCase character</li> <li>1 LowerCase character</li> </ul>''')) except Exception as e: flash(str(e)) return render_template('signup.html', form=form, submit_url=url_for('UserView:company_signup'))
def signup(self): msg = '' form = RegistrationForm() if form.validate_on_submit: # email = request.form['email'] option = form.user_types.data user = None try: user = User(username=form.username.data, password=form.password.data, name=form.username.data, is_admin=False) user.can_post_provided = (option == 'faculty' or option == 'company') user.can_post_solicited = (option == 'faculty' or option == 'student') user.sign_up() login_user(user, remember=True) msg = f"Welcome to the YCP Database {user.name}!" return redirect(url_for('IndexView:get')) except Exception as e: msg = e return render_template('signup.html', msg=str(e), form=form) return render_template('signup.html', form=form)
def setUp(self): self.user1 = User(id=1) self.user1_copy = User(id=1) self.user2 = User(id=2) self.provided = Provided(id=1) self.solicited = Solicited(id=1)
class TestUserPage(TestCase): @classmethod def setUpClass(self): self.engine = create_engine('sqlite:///') Base.metadata.create_all(self.engine) self.Session = sessionmaker(bind=self.engine, expire_on_commit=False) session_manager.Session = self.Session def setUp(self): self.session = self.Session(bind=self.engine) self.user_args = { 'username': '******', 'password': '******', 'confirm_password': '******', 'email': '*****@*****.**', 'name': 'baz', 'is_admin': True, 'bio': 'test', 'contact_info': '*****@*****.**' } self.user = User(id=1, can_post_provided=True, can_post_solicited=True) project.Provided().post('foo', 'barbarba', self.user) project.Provided().post('Awesome', 'Force of Gravity', self.user) project.Provided().post('Cannon', 'Electric Beast', self.user) def tearDown(self): users = self.session.query(user.User).all() for u in users: self.session.delete(u) self.session.query(Provided).delete() self.session.query(Solicited).delete() self.session.commit() self.session.close() def test_submit_bio(self): user.User.sign_up(**self.user_args) self.user = user.User.log_in('foo', 'barbarba') bio = 'Test bio' self.user.add_bio(bio) self.session.add(self.user) self.assertEqual(self.user.bio, bio) def test_submit_contact(self): user.User.sign_up(**self.user_args) self.user = user.User.log_in('foo', 'barbarba') contact = '*****@*****.**' self.user.add_contact(contact) self.session.add(self.user) self.assertEqual(self.user.contact_info, contact) def test_get_user_projects(self): user.User.sign_up(**self.user_args) self.user = user.User.log_in('foo', 'barbarba') new_user = User(id=2, can_post_provided=True, can_post_solicited=True) project.Provided().post('Test', 'Fire', new_user) project.Provided().post('Interesting', 'Facts', new_user) project.Provided().post('Fall', 'Food', new_user) clg = catalog.Catalog('', True) clg.apply() projects = self.user.get_user_projects() self.assertEqual(len(clg.projects), 6) self.assertEqual(len(projects), 3) self.assertEqual('foo', projects[0].title) self.assertEqual('Awesome', projects[1].title) self.assertEqual('Cannon', projects[2].title) def test_submit_image(self): user.User.sign_up(**self.user_args) self.user = user.User.log_in('foo', 'barbarba') self.assertFalse(self.user.image) self.user.add_image() self.assertTrue(self.user.image)