Example #1
0
 def setUp(self):
     # board is constructed  "visually" so the test cases are easier to understand
     board = [
         '       ', '       ', '  2    ', '  21   ', '  21   ', ' 12121 '
     ]
     self.session = C4Session(board=''.join(reversed(board)),
                              player_1='Joe',
                              player_2='Moe')
Example #2
0
 def test_move_session_no_column(self):
     """
     move request does not specify a column
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     session = C4Session(player_1='joe', status=1)
     db.set_backend([session])
     resp = controller.move(None, {'username': ['joe']}, '123')
     self.assertEqual(400, resp.status_code)
     self.assertEqual({'error': 'Username and column are required.'},
                      resp.content)
Example #3
0
 def test_move_session_ended(self):
     """
     move request is made for a session that ended in draw (status=3)
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     session = C4Session(player_1='joe', status=3)
     db.set_backend([session])
     resp = controller.move(None, {
         'username': ['joe'],
         'column': ['1']
     }, '123')
     self.assertEqual(400, resp.status_code)
     self.assertEqual({'error': 'Session ended'}, resp.content)
Example #4
0
 def test_join_previous_session_same_user(self):
     """
     join with a request made with a user that already has an open session
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     session = C4Session(id='123', player_1='joe')
     db.set_backend([session])
     resp = controller.join(None, {'username': ['joe']}, None)
     self.assertEqual(len(db.saved), 0)
     self.assertEqual(200, resp.status_code)
     self.assertEqual(
         {
             'msg': 'Existing Session found',
             'session': '123',
             'player': 1
         }, resp.content)
Example #5
0
 def test_join_new_session(self):
     """
     a requests is assigned a newly created session
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     session = C4Session(id='blablabla', board='', player_1='joe')
     db.set_backend(None)
     resp = controller.join(None, {'username': ['moe']}, None)
     self.assertEqual(db.saved[0].player_1, 'moe')
     self.assertEqual(201, resp.status_code)
     # since the session is new, a new id is assigned, so we have to assert on individual fields
     self.assertEqual('Session created', resp.content['msg'])
     self.assertEqual(1, resp.content['player'])
     # since the uuid is random, we can't assert on its value.
     # however, and auto generated id should be an uuid, which is a 36 characters long string
     self.assertEqual(36, len(resp.content['session']))
Example #6
0
 def test_join_previous_session(self):
     """
     a request is assigned a previously created session that was open
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     session = C4Session(id='blablabla', board='', player_1='joe')
     db.set_backend([session])
     resp = controller.join(None, {'username': ['moe']}, None)
     self.assertEqual(db.saved[0].player_2, 'moe')
     self.assertEqual(209, resp.status_code)
     self.assertEqual(
         {
             'msg': 'Joined session',
             'session': 'blablabla',
             'player': 2
         }, resp.content)
Example #7
0
 def test_move_invalid_username(self):
     """
     move request is made for a user that is not either player 1 or 2
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     board = '                                          '
     session = C4Session(id='123',
                         board=board,
                         player_1='joe',
                         player_2='moe',
                         status=0)
     db.set_backend([session])
     resp = controller.move(None, {
         'username': ['noe'],
         'column': ['1']
     }, '123')
     self.assertEqual(400, resp.status_code)
     self.assertEqual({'error': 'Invalid Username'}, resp.content)
Example #8
0
 def test_move_column_full(self):
     """
     move request is made for a column that is already full
     test sets up column 0 as full.
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     board_with_c1_full = '1      2      1      2      1      2      '
     session = C4Session(id='123',
                         board=board_with_c1_full,
                         player_1='joe',
                         player_2='moe',
                         status=0)
     db.set_backend([session])
     resp = controller.move(None, {
         'username': ['joe'],
         'column': ['0']
     }, '123')
     self.assertEqual(400, resp.status_code)
     self.assertEqual({'error': 'Invalid Move'}, resp.content)
Example #9
0
 def test_move_invalid_turn(self):
     """
     move request is made for by a player whose turn is not ready yet
     """
     db = FakeBackend()
     controller = ConnectFourController(db)
     board = '                                          '
     session = C4Session(id='123',
                         board=board,
                         player_1='joe',
                         player_2='moe',
                         status=0)
     db.set_backend([session])
     resp = controller.move(None, {
         'username': ['moe'],
         'column': ['1']
     }, '123')
     self.assertEqual(400, resp.status_code)
     self.assertEqual(
         {'error': 'Player not allowed to make a move at this time.'},
         resp.content)
Example #10
0
 def join(self, path, query, _):
     """
     Handles a request to join a ConnectFour session
     Requires the username url query parameter
     If there are no open sessions available a new session is created.
     """
     username = query.get('username', [None])[0]
     if username:
         open_sessions = self.db.find_open_sessions()
         if open_sessions:
             row = open_sessions[0]
             if row.player_1 == username:
                 return Response(
                     200, {
                         "msg": "Existing Session found",
                         "session": row.id,
                         "player": 1
                     })
             else:
                 row.player_2 = username
                 self.db.save_session(row)
                 return Response(209, {
                     "msg": "Joined session",
                     "session": row.id,
                     "player": 2
                 })
         else:
             row = C4Session(player_1=username)
             self.db.save_session(row)
             return Response(201, {
                 "msg": "Session created",
                 "session": row.id,
                 "player": 1
             })
     else:
         return Response(400, {"error": "Missing username"})
Example #11
0
def c4session_row_factory(cursor, row):
    return C4Session(*row)