def test_valid_confirmation_token(self):
     u = User(password='******')
     db.session.add(u)
     db.session.commit()
     token = u.generate_confirmation_token()
     self.assertTrue(u.confirm(token))
     self.assertTrue(u.confirmed)
 def test_expired_confirmation_token(self):
     u = User(password='******')
     db.session.add(u)
     db.session.commit()
     token = u.generate_confirmation_token(1)
     time.sleep(2)
     self.assertFalse(u.confirm(token))
Example #3
0
    def test_ask_question(self):
        # While logged out, we should be redirected from the ask question page
        response = self.client.get(url_for('main.ask_question'), follow_redirects=True)
        self.assertTrue('Please log in to access this page.' in response.data)

        # Make a user and log in
        u = User(username='******', password='******', confirmed=True)
        db.session.add(u)
        db.session.commit()
        self.client.post(url_for('auth.login'), data={
            'username': '******',
            'password': '******'
        })

        # While logged in, we should be able to get to the ask question page
        response = self.client.get(url_for('main.ask_question'), follow_redirects=True)
        self.assertTrue('Ask a question' in response.data)

        # Ask a question
        response = self.client.post(url_for('main.ask_question'), data={
            'title': 'test_ask_question title',
            'content': 'test_ask_question content'
        }, follow_redirects=True)
        self.assertTrue('test_ask_question title' in response.data)
        self.assertTrue('test_ask_question content' in response.data)
Example #4
0
    def test_get_question(self):
        q = Question(title='test_get_question title', content='test_get_question content')
        db.session.add(q)
        db.session.commit()

        # Make sure we can get the question
        response = self.client.get(url_for('main.get_question', question_id=q.id))
        self.assertTrue('test_get_question title' in response.data)
        self.assertTrue('test_get_question content' in response.data)

        # We're not logged in, so make sure we can't answer the question
        self.assertTrue('to answer this question' in response.data)
        # Log in or      ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
        #       register to answer this question.

        # Make a user and log in
        u = User(username='******', password='******')
        db.session.add(u)
        db.session.commit()
        self.client.post(url_for('auth.login'), data={
            'username': '******',
            'password': '******'
        })

        response = self.client.get(url_for('main.get_question', question_id=q.id))
        # Make sure we can answer the question
        self.assertFalse('to answer this question' in response.data)
Example #5
0
    def test_answer_question(self):
        # Make and add a question
        q = Question(title='test_answer_question title', content='test_answer_question content')
        db.session.add(q)
        db.session.commit()

        # While not logged in, we should not be able to answer a question
        response = self.client.post(url_for('main.answer_question', question_id=q.id), data={
            'content': 'test_answer_question content'
        })
        self.assertTrue(response.status_code == 302) # Redirect

        # Make a user and log in
        u = User(username='******', password='******', confirmed=True)
        db.session.add(u)
        db.session.commit()
        self.client.post(url_for('auth.login'), data={
            'username': '******',
            'password': '******'
        })

        response = self.client.post(url_for('main.answer_question', question_id=q.id), data={
            'content': 'test_answer_question content'
        }, follow_redirects=True)
        self.assertTrue('test_answer_question content' in response.data)
 def test_invalid_confirmation_token(self):
     u1 = User(password='******')
     u2 = User(password='******')
     db.session.add(u1)
     db.session.add(u2)
     db.session.commit()
     token = u1.generate_confirmation_token()
     self.assertFalse(u2.confirm(token))
Example #7
0
 def test_show_user(self):
     u = User(username='******', password='******', email='*****@*****.**')
     db.session.add(u)
     db.session.commit()
     response = self.client.get(url_for('main.show_user', username=u.username))
     self.assertTrue(('Username: ' + u.username) in response.data)
 def test_password_salts_are_random(self):
     u1 = User(password='******')
     u2 = User(password='******')
     self.assertTrue(u1.password_hash != u2.password_hash)
 def test_password_verification(self):
     u = User(password='******')
     self.assertTrue(u.verify_password('cat'))
     self.assertFalse(u.verify_password('dog'))
 def test_no_password_getter(self):
     u = User(password='******')
     with self.assertRaises(AttributeError):
         u.password
 def test_password_setter(self):
     u = User(password='******')
     self.assertTrue(u.password_hash is not None)