Beispiel #1
0
    def test_list(self):
        u1 = User(username='******', email='*****@*****.**', password='******')
        u2 = User(username='******', email='*****@*****.**', password='******')
        u3 = User(username='******', email='*****@*****.**', password='******')
        q1 = Question(answer='q1')
        q2 = Question(answer='q2')
        g1 = Game()
        q1.author = u2
        q2.author = u3
        q1.recipient = u1
        q2.recipient = u1
        q1.game = g1
        q2.game = g1

        db.session.add_all([u1, u2, u3, q1, q2, g1])
        db.session.commit()
        u1.last_question_read_time = datetime.utcnow()
        q2.timestamp = datetime.utcnow()
        db.session.commit()

        with self.client:
            self.client.post(url_for('auth.login'),
                             data={
                                 'email': '*****@*****.**',
                                 'password': '******'
                             })

            # test that only q2 appears (since q1.timestamp  < u1.last_question_read_time)
            response = self.client.get(url_for('questions.list'))
            data = response.get_data(as_text=True)
            self.assertTrue('Game Against three' in data)
            self.assertFalse('Game Against two' in data)
Beispiel #2
0
def send_question(recipient,drawing_id,game_id):
  user = User.query.filter_by(username=recipient).first_or_404()
  drawing = Drawing.query.get_or_404(drawing_id)
  game = Game.query.get_or_404(game_id)

  # validate user's drawing
  if not drawing in current_user.drawings.all():
    abort(403)
  
  # validate game
  if not game.validate_players(current_user,user):
    abort(403)

  # Check if current user's turn
  if not game.is_turn(current_user):
    flash('It is currently not your turn.')
    return redirect(url_for('games.game',game_id=game.id))
  
  form = QuestionForm()

  if form.validate_on_submit():
    # Update user questions count notification
    user.add_notifications('unread_question_count',user.new_questions())
    db.session.commit()

    # update game
    game.turn_update()

    # add new Question instance
    question = Question(author=current_user, recipient=user,answer=form.answer.data,drawing=drawing)
    if form.max_tries.data:
      question.max_tries = form.max_tries.data
    question.game = game
    db.session.add(question)
    db.session.commit()
    flash('Your question has been sent.')
    return redirect(url_for('games.game',game_id=question.game.id))

  return render_template('questions/send_question.html',form=form,recipient=recipient)
Beispiel #3
0
    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')
Beispiel #4
0
    def test_game(self):
        u1 = User(username='******', email='*****@*****.**', password='******')
        u2 = User(username='******', email='*****@*****.**', password='******')
        u3 = User(username='******', email='*****@*****.**', password='******')
        u4 = User(username='******', email='*****@*****.**', password='******')

        g1 = Game(author=u3, guest=u4, max_points=2)
        g2 = Game(author=u1, guest=u2, max_points=10)
        q1 = Question(answer='q1')
        q1.game = g2

        db.session.add_all([u1, u2, u3, u4, g1, g2, q1])
        db.session.commit()

        with self.client:
            self.client.post(url_for('auth.login'),
                             data={
                                 'email': '*****@*****.**',
                                 'password': '******'
                             })

            # invalid game
            response = self.client.get(url_for('games.game', game_id=100))
            self.assertEqual(response.status_code, 404)

            # game not belonging to user
            response = self.client.get(url_for('games.game', game_id=g1.id))
            self.assertEqual(response.status_code, 403)

            # test valid game page
            # game status
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue('Waiting for Game Confirmation' in data)

            g2.status = 'author_turn_to_ask'
            g2.turn = 'author'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue('Ask Your Opponent A Question!' in data)

            g2.status = 'rejected'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue('Game Has Been Rejected.' in data)

            g2.status = 'not_confirmed'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue('Waiting for Game Confirmation' in data)

            g2.status = 'author_win'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue('Congratulations, You Have Won!' in data)
            self.assertFalse('Send Question' in data)

            g2.status = 'guest_win'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue('You Have Lost.' in data)

            g2.status = 'waiting_author_answer'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue("Answer Your Opponent's Question!" in data)

            g2.status = 'guest_turn_to_ask'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue("Waiting For Opponent to Ask A Question!" in data)

            g2.status = 'waiting_guest_answer'
            db.session.commit()
            response = self.client.get(url_for('games.game', game_id=g2.id))
            data = response.get_data(as_text=True)
            self.assertTrue(
                "Waiting For Opponent Answer Your Question!" in data)

            # test game information
            self.assertTrue('10 Points' in data)
            self.assertTrue('10 Points' in data)
            self.assertTrue('Question' in data)