def test_get_user_info(self): db_url = os.path.join('sqlite:///' + os.getcwd(), 'game.db') api = ChessAPI(db_url=db_url) api.sign_up({"username": "******", "password": "******"}) ret = api.get_user_info("admin") self.assertEqual(ret["valid"], True) self.assertEqual(ret["username"], "admin")
def test_unique_session(self): db_url = os.path.join('sqlite:///' + os.getcwd(), 'game.db') api = ChessAPI(db_url=db_url) session_set = set() for i in range(1000): session_id = api.generate_unique_session_id() session_set.add(session_id) api.sessions[session_id] = None self.assertEqual(len(session_set), 1000) ChessAPITestCase.remove_db_file()
def test_replay_a_session(self): db_url = os.path.join('sqlite:///' + os.getcwd(), 'game.db') api = ChessAPI(db_url=db_url) session_id = api.create_game("hu4396", "pvp")["session_id"] api.update_game({"session_id": session_id, "src": Utility.encode(1, 0), "tar": Utility.encode(3, 0), "role": "Pawn", "user": "******"}) ret = api.replay("hu4396", session_id, 1) self.assertEqual(ret["history"], {'src': 'a2', 'tar': 'a4'})
def test_refuses_to_execute_invalid_request(self): db_url = os.path.join('sqlite:///' + os.getcwd(), 'game.db') api = ChessAPI(db_url=db_url) session_id = api.create_game("hu4396", "pvp")["session_id"] ret = api.undo("admin", session_id) self.assertEqual(ret["valid"], False) ret = api.login({"username": "******", "password": "******"}) self.assertEqual(ret["valid"], False) ret = api.update_game({"session_id": session_id, "user": "******"}) self.assertEqual(ret["valid"], False)
def test_get_rankings(self): db_url = os.path.join('sqlite:///' + os.getcwd(), 'game.db') api = ChessAPI(db_url=db_url) ret = api.get_rankings() self.assertEqual(ret["rankings"], [])
def create_app(db_url="init"): app = Flask(__name__) driver = ChessAPI(db_url) @app.route('/chess/info', methods=['POST']) def fen(): request_data = json.loads(request.data) ret = driver.get_info(request_data) return ret @app.route('/chess/new', methods=['POST']) def new(): request_data = json.loads(request.data) mode = request_data["mode"] user = request_data["user"] ret = driver.create_game(user, mode) return ret @app.route('/chess/update', methods=['POST']) def result(): request_data = json.loads(request.data) return driver.update_game(request_data) @app.route('/chess/<session_id>/<coordinate>', methods=['POST']) def show(session_id, coordinate): session_id = int(session_id) user = json.loads(request.data)["user"] ret = driver.get_checked_moves({"session_id": session_id, "coordinate": coordinate}, user) return ret @app.route('/resume', methods=['POST']) def resume(): request_data = json.loads(request.data) return driver.resume_game(request_data["user"]) @app.route('/replays', methods=['POST']) def replays(): request_data = json.loads(request.data) return driver.replay_game(request_data["user"]) @app.route('/undo', methods=['POST']) def undo(): request_data = json.loads(request.data) return driver.undo(request_data["user"], request_data["session_id"]) @app.route('/replay', methods=['POST']) def replay(): request_data = json.loads(request.data) user = request_data["user"] session_id = request_data["session_id"] step = request_data["step"] return driver.replay(user, session_id, step) # for user account @app.route('/signup', methods=['POST']) def signup(): request_data = json.loads(request.data) return driver.sign_up(request_data) @app.route('/login', methods=['POST']) def sign_in(): request_data = json.loads(request.data) return driver.login(request_data) @app.route('/user', methods=['POST']) def user_info(): request_data = json.loads(request.data) return driver.get_user_info(request_data["user"]) @app.route('/rankings', methods=['GET']) def get_rankings(): return driver.get_rankings() return app