def test_save_history_with_user(self): """Make sure the reference to user is correctly saved in history""" # create empy path for data delete_data_path(self.tmp_path) create_empty_data_path(self.tmp_path) # load info without history valid_info = self.borgia_info_content data = {'files': self.files, 'info': json.dumps(valid_info)} with self.client: self.login() # create the game through API result = self.client.post('/api/create', data=data, content_type='multipart/form-data') self.assertEqual(result.status_code, 201) game_path = result.json["path"] # get the game content game_info = read_content(game_path) # check history event content self.assertEqual(len(game_info["history"]), 1) event = game_info["history"][0] self.assertEqual(event["type"], "create") self.assertEqual(event["user"], self.user_email) # make some changes to the data new_info = valid_info.copy() new_info["title"] = "bla bla bla" # post the update new_data = { 'info': json.dumps(new_info), 'slug': json.dumps(new_info["slug"]) } result = self.client.post('/api/update', data=new_data, content_type='multipart/form-data') self.assertEqual(result.status_code, 201) # read updated game game_path = result.json["path"] new_game_info = read_content(game_path) new_game_info["title"] = "bla bla bla" # check history event content self.assertEqual(len(new_game_info["history"]), 2) event = new_game_info["history"][1] self.assertEqual(event["type"], "update") self.assertEqual(event["user"], self.user_email)
def games(**kwargs): data_dir = os.path.join(os.getcwd(), 'data') errors_count = 0 app=create_app() with app.app_context(): for game_folder_name in os.listdir(data_dir) : errors = [] path = os.path.join(data_dir,game_folder_name) # get only folders if os.path.isdir(path): info_file = os.path.join(path, "info.json") if os.path.exists(info_file): info = read_content(path) errors = validate_content(info, get_all_errors=True) if len(errors): print(info["title"] + bcolors.FAIL + " %s"%len(errors) + bcolors.ENDC) errors_count = errors_count + len(errors) else : print(bcolors.OKGREEN + info["title"] + " " + u"\u2713" + bcolors.ENDC) print() print("Done.") if errors_count : red("%s JSON formatting errors."%errors_count) else : green("No errors detected.") print()
def test_create_slug_once_and_for_all(self): tmp = "/tmp" slug = "game-bla-bla-bla-fr" # clean delete_data_path(os.path.join(tmp, slug)) info = self.borgia_info_content.copy() info["title"] = "bla bla bla" info.pop('slug', None) # remove slug self.assertRaises(KeyError, lambda: info["slug"]) game_path = create_content(info, None, tmp) game_info = read_content(game_path) self.assertEqual(game_info["slug"], "game-bla-bla-bla-fr") new_game_info = info.copy() new_game_info["title"] = "bla bla bla 2" updated_content = update_content_info(game_path, new_game_info) self.assertEqual(slug, updated_content["slug"]) self.assertEqual(game_info["slug"], updated_content["slug"])
def test_read_content(self): """Make sure an info file is read properly""" info = read_content(self.game_path) self.assertTrue("title" in info.keys()) self.assertTrue("files" in info.keys()) self.assertEquals(len(info["files"]), 1) self.assertEquals(info["slug"], "borgia-le-jeu-malsain-fr")
def test_validate_content_multiple_errors(self): info = read_content(self.game_path) info_wrong = info.copy() info_wrong["description"] = 72 info_wrong["title"] = 12 info_wrong["credentials"]["license"] = ["hahaah"] errors = validate_content(info_wrong, get_all_errors=True) self.assertEquals(len(errors), 4)
def test_validate_content(self): """Make sure an info file is parsed properly""" info = read_content(self.game_path) self.assertEquals(validate_content(info), None) # make sure error is raised with a basic mistake info_wrong = info.copy() info_wrong["description"] = 72 self.assertRaises(ValidationError, lambda: validate_content(info_wrong))
def test_update_content_state(self): """State should be updated when asked to""" info = self.borgia_info_content new_game_path = os.path.join(self.tmp_path, info["slug"]) new_game = update_content_state(new_game_path, "validated") self.assertEqual(new_game["state"], "validated") info_stored_file = read_content(new_game_path) self.assertEqual(info_stored_file["state"], "validated")
def test_update_content_info(self): tmp = "/tmp" info = self.borgia_info_content game_path = create_content(info, None, tmp) game_info = read_content(game_path) new_game_info = game_info.copy() new_game_info["title"] = "bla bla bla" updated_content = update_content_info(game_path, new_game_info) self.assertEqual(len(updated_content["history"]), 2) event = updated_content["history"][1] self.assertEqual(event["type"], "update")
def api_delete_game(path): game_path = os.path.join(current_app.config["DATA_DIR"], path) content = read_content(game_path) author_email = content["history"][0]["user"] is_author = author_email == current_user.email if is_author or current_user.has_role("superuser") : game_path = os.path.join(current_app.config["DATA_DIR"], path) content = delete_content(game_path) return jsonify({ "message" : "Success : file %s deleted"%game_path }), 203 abort(405)
def test_read_content_validation(self): """Wrong game should raises validation error""" wrong_content = self.borgia_info_content.copy() wrong_content["title"] = 345 # create a bad record if needed wrong_game_path = os.path.join(TEST_DATA_DIR, "wrong-game") os.makedirs(wrong_game_path) write_info_json(wrong_content, wrong_game_path) with_errors = read_content(wrong_game_path) self.assertIs(type(with_errors["errors"]), list) self.assertIs(len(with_errors["errors"]), 1) self.assertIs(type(with_errors["errors"][0]), dict) self.assertIn("title", with_errors["errors"][0]["path"]) self.assertIn("345", with_errors["errors"][0]["message"])
def update_content_state(resource_path, new_state): # get current state info = read_content(resource_path) old_state = info["state"] # validate state change if not is_valid_state_change(old_state, new_state): raise LudoboxError( "<Wrong State> Can not update state content to the same state.") # update info new_info = info.copy() new_info["state"] = new_state new_content = update_content_info(resource_path, new_info, state=new_state) return new_content
def validate(game, **kwargs): if os.path.exists(game): app=create_app() with app.app_context(): info = read_content(game) errors = validate_content(info, get_all_errors=True) if len(errors): for i, err in enumerate(errors): print( "Error %s"%i , "--"*50) print(err["path"]) red(err["message"]) print() print() print(info["title"]) red("%s ERRORS"%len(errors)) print() else : print(bcolors.OKGREEN + info["title"] + ". No errors " + u"\u2713" + bcolors.ENDC) else : print('ERROR : %s does not exist. Please specify a valid game path'%game)
def test_create_content_without_attachements(self): """ Make sure that content is written properly""" tmp = "/tmp" info = self.borgia_info_content new_game = create_content(info, None, tmp) # dir and files are written properly self.assertTrue(os.path.isdir(new_game)) json_path = os.path.join(new_game, "info.json") self.assertTrue(os.path.exists(json_path)) new_game_info = read_content(new_game) # basic check for simlarity self.assertTrue(info["title"], new_game_info["title"]) # make sure history has been written self.assertIn("history", new_game_info.keys()) self.assertTrue(len(new_game_info["history"]), 1) event = new_game_info["history"][0] self.assertTrue(event["type"], "create")
return True elif confirm == 'n': return False needs_update = [] with app.app_context(): games = get_content_index() # read all json file and check for errors for game in games: game_path = os.path.join(config["data_dir"], game['slug']) info = read_content(game_path) try: flagged = False history = info["history"] for event in history: try: if not is_valid_event(event): flagged = True except AssertionError: flagged = True if flagged: needs_update.append(game_path) except KeyError: # files with no history needs_update.append(game_path)
def test_borgia_game_data(self): """Make sure the borgias are okay with the model""" borgia_game_path = os.path.join(self.tmp_path, 'game-borgia-le-jeu-malsain-fr') borgia_info = read_content(borgia_game_path) self.assertEquals(validate_content(borgia_info), None)
def api_single_game(path): game_path = os.path.join(current_app.config["DATA_DIR"], path) if not os.path.exists(game_path): abort(404) content = read_content(game_path) return jsonify(content)