def test_invalid_confirmation_token(self): u1 = UserFactory.build() u2 = UserFactory.build() db.session.add(u1) db.session.add(u2) db.session.commit() token = u1.generate_confirmation_token() self.assertFalse(u2.confirm(token))
def test_user_role(self): u = UserFactory.build() self.assertTrue(u.can(Permission.FOLLOW)) self.assertTrue(u.can(Permission.COMMENT)) self.assertTrue(u.can(Permission.WRITE)) self.assertFalse(u.can(Permission.MODERATE)) self.assertFalse(u.can(Permission.ADMIN))
def test_expired_confirmation_token(self): u = UserFactory.build() db.session.add(u) db.session.commit() token = u.generate_confirmation_token(1) time.sleep(2) self.assertFalse(u.confirm(token))
def test_admin_only_for_admin(self): def texts(sel): return [el.text for el in self.b.find_elements_by_css_selector(sel)] def nav_texts(): return texts('.nav li a') def admin_page_not_found(): b.get("%s/admin" % self.app_url()) self.assertIn('Not Found', texts('h1')) b = self.b admin_page_not_found() b.get(self.app_url()) self.assertNotIn('Admin', nav_texts()) User.objects.create_superuser(username='******', password='******', email='') self.log_in('admin') b.get(self.app_url()) self.assertIn('Admin', nav_texts()) user = UserFactory.create() self.log_in(user.username) b.get(self.app_url()) self.assertNotIn('Admin', nav_texts()) admin_page_not_found()
def test_ping(self): u = UserFactory.build() db.session.add(u) db.session.commit() time.sleep(2) last_seen_before = u.last_seen u.ping() self.assertTrue(u.last_seen > last_seen_before)
def __init__(self, section=None, status=SS_STATUS_ACCEPTED): self.user = UserFactory.create() self.parent = ParentFactory() self.parent.users.add(self.user) self.stu = StudentFactory.create(parent=self.parent) if section: StudentSectionAssignment.objects.create(student=self.stu, section=section, status=status)
def admin_client(): admin = UserFactory( username='******', name='Admin', email='admin@localhost', is_active=True, is_admin=True, ) client = Client() client.force_login(admin) return client
def user_client(): user = UserFactory( username='******', name='User', phone='+15005551212', is_active=True, is_admin=False, ) client = Client() client.force_login(user) return client
def admin_client(): admin = UserFactory( username='******', name='Admin', phone='+15005551212', is_active=True, is_admin=True, ) client = Client() client.force_login(admin) return client
def user_client(): user = UserFactory( username='******', name='User', email='user@localhost', is_active=True, is_admin=False, ) client = Client() client.force_login(user) return client
def test_email_goes_to_right_parents(self): user1 = UserFactory.create() self.client.login(username=user1.username, password='******') parent1 = ParentFactory() parent1.users.add(user1) stu1a = StudentFactory.create(parent=parent1) stu1b = StudentFactory.create(parent=parent1) user2 = UserFactory.create() self.client.login(username=user2.username, password='******') parent2 = ParentFactory() parent2.users.add(user2) stu2a = StudentFactory.create(parent=parent2) stu2b = StudentFactory.create(parent=parent2) course1 = Course(name='Python') course1.save() stu1a.wants_courses.add(course1) py_sec = SectionFactory(course=course1) parents = email_parents(()) # No filters, so all parents should be included self.assertIn(parent1, parents) self.assertIn(parent2, parents) parents = email_parents((), send_only_upcoming=True) # No parents have students in upcoming sections self.assertNotIn(parent1, parents) self.assertNotIn(parent2, parents) StudentSectionAssignment(student=stu1a, section=py_sec, status=SS_STATUS_APPLIED).save() parents = email_parents((), send_only_upcoming=True) # parent1 now has a student in an upcoming section self.assertIn(parent1, parents) self.assertNotIn(parent2, parents) parents = email_parents((), send_only_wanted_upcoming=True) self.assertNotIn(parent1, parents) self.assertNotIn(parent2, parents) parents = email_parents((course1.id,), send_only_wanted_upcoming=True) self.assertIn(parent1, parents) self.assertNotIn(parent2, parents)
def test_gravatar(self): u = UserFactory.build(email="*****@*****.**") with self.app.test_request_context("/"): gravatar = u.gravatar() gravatar_256 = u.gravatar(size=256) gravatar_pg = u.gravatar(rating="pg") gravatar_retro = u.gravatar(default="retro") self.assertTrue("https://secure.gravatar.com/avatar/" + "d4c74594d841139328695756648b6bd6" in gravatar) self.assertTrue("s=256" in gravatar_256) self.assertTrue("r=pg" in gravatar_pg) self.assertTrue("d=retro" in gravatar_retro)
def test_can_view_students(self): user = UserFactory.create() self.client.login(username=user.username, password='******') parent = ParentFactory() parent.users.add(user) stu1 = StudentFactory.create(parent=parent) stu2 = StudentFactory.create(parent=parent) course1 = Course(name='Python') course1.save() stu1.wants_courses.add(course1) response = self.client.get(reverse('students') + '?wants=%i' % course1.id) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content, 'html.parser') text = soup.get_text() self.assertIn(stu1.name, text) self.assertNotIn(stu2.name, text)
def test_no_password_getter(self): u = UserFactory.build() with self.assertRaises(AttributeError): u.password
def test_password_verification(self): password1 = self.faker.password() password2 = self.faker.password() u = UserFactory.build(password=password1) self.assertTrue(u.verify_password(password1)) self.assertFalse(u.verify_password(password2))
def test_password_salts_are_random(self): password = self.faker.password() u1 = UserFactory.build(password=password) u2 = UserFactory.build(password=password) self.assertNotEqual(u1.password_hash, u2.password_hash)
def test_valid_confirmation_token(self): u = UserFactory.build() db.session.add(u) db.session.commit() token = u.generate_confirmation_token() self.assertTrue(u.confirm(token))
def test_password_setter(self): u = UserFactory.build() self.assertTrue(u.password_hash is not None)