def test_send_hint(self): u1 = User(username='******', email='*****@*****.**', password='******') q1 = Question(answer='q1') q2 = Question(answer='q2') d1 = Drawing(filename='d1') q1.drawing = d1 q1.author = u1 db.session.add_all([q1, q2, u1, d1]) db.session.commit() with self.client: self.client.post(url_for('auth.login'), data={ 'email': '*****@*****.**', 'password': '******' }) # invalid question response = self.client.get( url_for('questions.send_hint', question_id=100)) self.assertEqual(response.status_code, 404) # question does not belong to user response = self.client.get( url_for('questions.send_hint', question_id=q2.id)) self.assertEqual(response.status_code, 403) # successful post response = self.client.post(url_for('questions.send_hint', question_id=q1.id), data={'body': 'hint1'}, follow_redirects=True) data = response.get_data(as_text=True) self.assertTrue('Hint Successfully Submitted' in data) hint1 = Hint.query.get(1) self.assertEqual(hint1.body, 'hint1') self.assertEqual(hint1.question, q1)
def test_question(self): u1 = User(username='******', email='*****@*****.**', password='******') u2 = User(username='******', email='*****@*****.**', password='******') q1 = Question(answer='q1') q2 = Question(answer='q2') q3 = Question(answer='q3') g1 = Game(author=u1, guest=u2, max_points=2, status='waiting_author_answer') d1 = Drawing(filename='d1') q1.drawing = d1 q1.author = u2 q1.recipient = u1 q1.game = g1 q1.max_tries = 2 q3.drawing = d1 q3.author = u2 q3.recipient = u1 q3.game = g1 q3.max_tries = 2 q3.status = 'lost' db.session.add_all([u1, u2, q1, q2, q3, g1]) db.session.commit() with self.client: self.client.post(url_for('auth.login'), data={ 'email': '*****@*****.**', 'password': '******' }) # invalid question response = self.client.get(url_for('questions.question', id=100)) self.assertEqual(response.status_code, 404) # question does not belong to user response = self.client.get(url_for('questions.question', id=q2.id)) self.assertEqual(response.status_code, 403) # question not in progress self.assertEqual(q3.num_of_tries, 0) response = self.client.get(url_for('questions.question', id=q3.id)) self.assertEqual(q3.num_of_tries, 0) # if question answered incorrectly (but no loss or win) response = self.client.post(url_for('questions.question', id=q1.id), data={'answer': 'incorrect'}, follow_redirects=True) data = response.get_data(as_text=True) self.assertTrue('Incorrect. Please Try Again.' in data) self.assertEqual(q1.num_of_tries, 1) # if user runs out of tries on question response = self.client.post(url_for('questions.question', id=q1.id), data={'answer': 'incorrect'}, follow_redirects=True) data = response.get_data(as_text=True) self.assertTrue( 'Incorrect. You Have Run Out of Tries On This Question' in data) self.assertEqual(q1.num_of_tries, 2) self.assertEqual(q1.status, 'lost') # check that game was updated correctly self.assertEqual(g1.status, 'author_turn_to_ask') # if user answers question correctly g1.status = 'waiting_author_answer' q1.status = 'in_progress' q1.num_of_tries = 1 db.session.commit() response = self.client.post(url_for('questions.question', id=q1.id), data={'answer': 'q1'}, follow_redirects=True) data = response.get_data(as_text=True) self.assertTrue('Question Answered Correctly' in data) self.assertEqual(q1.num_of_tries, 2) self.assertEqual(q1.status, 'complete') # check that game was updated correctly self.assertEqual(g1.status, 'author_turn_to_ask') # if user answers question correctly (leading to game win) g1.status = 'waiting_author_answer' q1.status = 'in_progress' q1.num_of_tries = 1 db.session.commit() response = self.client.post(url_for('questions.question', id=q1.id), data={'answer': 'q1'}, follow_redirects=True) data = response.get_data(as_text=True) self.assertTrue('Question Answered Correctly' in data) self.assertEqual(q1.num_of_tries, 2) self.assertEqual(q1.status, 'complete') # check that game was updated correctly self.assertEqual(g1.status, 'author_win')