Beispiel #1
0
    def test_home_page_renders_template_with_correct_values_for_existing_match_url(
            self):

        match_url = self.add_new_round(FIRST_MOVE_FEN)
        self.submit_move(match_url, SECOND_MOVE_FEN, 'b', '')

        match = Matches.get_match_by_url(match_url)
        self.assertEqual(len(match.rounds.all()), 3)

        self.client.get('/' + match_url)
        expected_values = {
            'fen': SECOND_MOVE_FEN,
            'match_url': match_url,
            'round_date': str(match.rounds.all()[-1].date_of_turn),
            'taken_players': {
                'w': "Guest",
                'b': "Guest"
            },
            'current_match': match,
            'notify': False,
            'posts': []
        }

        context_variables = self.get_multiple_context_variables(
            expected_values)
        self.assertEqual(context_variables, sorted(expected_values.items()))
Beispiel #2
0
def toggle_options():
    """Toggle save/notify options based on requested action"""

    # requested action parameters
    data = {k:v for k,v in request.args.items()}

    match = Matches.get_match_by_url(data['match_url'])

    if not match:
        flash('{} is not a valid match url'.format(match_url))
        return redirect(url_for('index')) 

    options = OptionToggler(g.user, data['current_player'], match)   

    action_dict = {'save': options.save_game,
                   'unsave': options.unsave_game,
                   'notify': options.notify,
                   'unnotify': options.unnotify}

    resp = action_dict[data['action']]()
    if not resp == 'need confirmation':
        options.commit_to_db()

    white_player_name = match.white_player.username if match.white_player else 'Guest'
    black_player_name = match.black_player.username if match.black_player else 'Guest'

    return jsonify(white_player_name=white_player_name, 
                   black_player_name=black_player_name,
                   resp=resp)
Beispiel #3
0
 def test_get_match_by_url_method_returns_Match_if_passed_existing_match_url(self):
     match1_url = self.add_new_round(FIRST_MOVE_FEN)
     match2_url = self.add_new_round(FIRST_MOVE_FEN)
     self.assertNotEqual(match1_url, match2_url)
     self.assertEqual(len(Matches.query.all()), 2)
     searched_match = Matches.get_match_by_url(match2_url)
     self.assertIn(searched_match, Matches.query.all())
     self.assertEqual(searched_match.match_url, match2_url)
    def test_cannot_set_notify_True_if_email_unconfirmed(self):

        user = self.add_fake_users(1)[0]
        _match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(_match_url)

        ot = OptionToggler(user, 'w', match)
        ot.save_game()

        self.assertEqual('need confirmation', ot.notify())
Beispiel #5
0
    def test_user_can_save_match_successfully(self):
        
        user = self.add_fake_users(1)[0]
        self.assertEqual(len(user.matches.all()), 0)

        match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(match_url)

        user.save_match('w', match)
        self.assertEqual(len(user.matches.all()), 1)
        self.assertEqual(match.white_player, user)
    def test_can_save_open_game(self):

        user = self.add_fake_users(1)[0]
        _match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(_match_url)

        ot = OptionToggler(user, 'w', match)
        ot.save_game()

        self.assertEqual(user.matches.first(), match)
        self.assertEqual(match.white_player, user)
Beispiel #7
0
def index(match_url='/'):
    """Main game view function"""

    match_url = match_url.strip('/')
    existing_game = Matches.get_match_by_url(match_url) if match_url else None
    if not existing_game and len(match_url) > 1:
        flash('Unable to locate game the game "{}"'.format(match_url))
        return redirect(url_for('index'))

    # Returns either values for existing game state or initialization values
    state = Matches.get_state(existing_game)

    # We assume that current user has not played this particular match before
    # and update state['current_player'] if assumption is wrong
    player = False
    if g.user.is_authenticated:
        player, state['notify'] = g.user.get_color_and_notify(existing_game)
    
    state['current_player'] = assign_current_player(player, match_url)

    return render_template('index.html', root_path=request.url_root, **state)
Beispiel #8
0
    def test_match_object_knows_which_color_is_played_by_which_user(self):
        
        user1, user2 = self.add_fake_users(2)
        
        match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(match_url)
        user1.save_match('w', match)
        user2.save_match('b', match)

        self.assertNotEqual(match.white_player, user2)
        self.assertEqual(match.black_player, user2)
        self.assertEqual(match.white_player, user1)
Beispiel #9
0
    def test_get_state_returns_correct_values_when_passed_existing_game(self):

        match_url1 = self.add_new_round(FIRST_MOVE_FEN)
        match_url2 = self.add_new_round(SECOND_MOVE_FEN, match_url1)
        match = Matches.get_match_by_url(match_url1)
        user = self.add_fake_users(1)[0]
        user.save_match('w', match)

        latest_round_date = Rounds.query.all()[-1].date_of_turn
        expected_values = {'fen': SECOND_MOVE_FEN,
                           'match_url': match_url1,
                           'round_date': str(latest_round_date),
                           'taken_players': frozenset({'w': user.username, 
                                                       'b': "Guest"}.items()), 
                           'current_match': match,
                           'notify': False,
                           'posts': frozenset([])}

        state = Matches.get_state(match)
        state['taken_players'] = frozenset(state['taken_players'].items())
        state['posts'] = frozenset(state['posts'])
        self.assertFalse(set(state.items()).symmetric_difference(set(expected_values.items())))
Beispiel #10
0
    def test_can_set_notify_True_if_email_unconfirmed(self):

        user = self.add_fake_users(1)[0]
        user.email_confirmed = True
        _match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(_match_url)
        user.save_match('w', match)

        ot = OptionToggler(user, 'w', match)
        self.assertEqual(match.white_notify, False)
        ot.notify()

        self.assertEqual(match.white_notify, True)
Beispiel #11
0
    def test_cannot_save_closed_game(self):

        user1, user2, user3 = self.add_fake_users(3)
        _match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(_match_url)

        user1.save_match('w', match)
        user3.save_match('b', match)

        ot = OptionToggler(user2, 'w', match)
        ot.save_game()

        self.assertIsNone(user2.matches.first())
Beispiel #12
0
    def test_user_get_recent_matches(self):
        matchurl1 = self.add_new_round(FIRST_MOVE_FEN)
        matchurl2 = self.add_new_round(FIRST_MOVE_FEN)
        matchurl3 = self.add_new_round(FIRST_MOVE_FEN)
        self.add_new_round(SECOND_MOVE_FEN, matchurl2)
        matchurl4 = self.add_new_round(FIRST_MOVE_FEN)
        matchurl5 = self.add_new_round(FIRST_MOVE_FEN)

        match1 = Matches.get_match_by_url(matchurl1)
        match2 = Matches.get_match_by_url(matchurl2)
        match3 = Matches.get_match_by_url(matchurl3)
        match4 = Matches.get_match_by_url(matchurl4)
        match5 = Matches.get_match_by_url(matchurl5)

        user = self.add_fake_users(1)[0]
        user.save_match('w', match1)
        user.save_match('b', match2)
        user.save_match('w', match3)
        user.save_match('w', match4)

        recent_matches = [m[0] for m in user.get_recent_matches()]
        expected_values = [match1, match3, match2, match4]
        self.assertTrue(recent_matches == expected_values, 
                        "{} \n {}".format(recent_matches, expected_values))
Beispiel #13
0
    def test_get_state_returns_initialization_values_when_passed_None(self):
        """
        frozenset used to make dict hashable for use of set operations
        """
        expected_values = {'fen': INITIAL_FEN,
                           'match_url': '',
                           'round_date': None,
                           'taken_players': frozenset({'w': "Guest", 
                                                       'b': "Guest"}.items()), 
                           'current_match': None,
                           'current_player': 'w',
                           'notify': False,
                           'posts': frozenset([])}

        state = Matches.get_state(None)
        state['taken_players'] = frozenset(state['taken_players'].items())
        state['posts'] = frozenset(state['posts'])
        self.assertFalse(set(state.items()).symmetric_difference(set(expected_values.items())))
Beispiel #14
0
    def test_can_unsave_game(self):

        user1, user2 = self.add_fake_users(2)
        _match_url = self.add_new_round(FIRST_MOVE_FEN)
        match = Matches.get_match_by_url(_match_url)

        user1.save_match('w', match)
        user2.save_match('b', match)

        self.assertEqual(user1.matches.first(), match)
        self.assertEqual(user2.matches.first(), match)
        self.assertEqual(match.white_player, user1)
        self.assertEqual(match.black_player, user2)

        ot = OptionToggler(user2, 'w', match)
        ot.unsave_game()

        self.assertEqual(user1.matches.first(), match)
        self.assertNotEqual(user2.matches.first(), match)
        self.assertEqual(match.white_player, user1)
        self.assertNotEqual(match.black_player, user2)
Beispiel #15
0
def submit_move():
    """Add new move to database. Notify opponent"""

    data = {k:v for k,v in request.args.items()}
   
    time_of_turn = datetime.datetime.utcnow()
    post = Posts.add_post(data['message'], g.user)
    match_url = Rounds.add_turn_to_game(data['match_url'].strip('/'), 
                                        data['fen_move'], 
                                        time_of_turn,
                                        post)

    current_match = Matches.get_match_by_url(match_url)
    emails.process_email(data['current_player'], 
                         current_match, 
                         request.url_root + match_url, 
                         g.user,
                         post)

    # Now add current player ('w' or 'b') to session to keep players the same
    session[match_url] = data['current_player']
    return jsonify(match_url=match_url)
Beispiel #16
0
 def test_get_match_by_url_method_returns_None_if_passed_empty_string(self):
     match = Matches.get_match_by_url('')
     self.assertEqual(match, None)