def cleanup_user_by_email(email): users = fhqtest.admin_session.users({ "onpage": 10, "page": 0, "filter_text": email }) user = None for usr in users["data"]: if usr['email'] == email: user = usr if usr['created'] == '': fhqtest.log_err("Field created could not be empty for user #" + str(usr['id'])) exit(-1) if user != None: print("Removing '" + email + "' ...") user_delete = fhqtest.admin_session.user_delete({ "userid": user['id'], "password": fhqtest.ADMIN_PASSWORD }) fhqtest.check_response(user_delete, "User '" + email + "' succesfull removed")
def run_tests(): fhqtest.print_header(" > > > " + TEST_NAME + ": begin ") fhqtest.print_bold("Request main admin page... ") r = requests.get(fhqtest.TEST_WEB_SERVER + 'admin/') if r.status_code != 200: fhqtest.log_err("Wrong status code expected 200, but got " + str(r.status_code)) exit(-1) fhqtest.log_ok("admin page success got")
def run_tests(): leak_uuid1 = uuid.uuid1() leak1 = None #cleanup fhqtest.print_bold("Clean up test data... ") leaks = fhqtest.admin_session.leaks_list({"page": 0, "onpage": 10}) for leak in leaks["data"]: fhqtest.admin_session.leaks_delete({"id": leak["id"]}) print("Leak with id #" + str(leak["id"]) + " was succesfully deleted!") fhqtest.print_success("Cleaned") #leak creation test fhqtest.print_bold("Try create leak...") leak1 = fhqtest.admin_session.leaks_add({ "uuid": str(leak_uuid1), "game_uuid": fhqtest.GAME_UUID1, # "gameid": 0, "name": "test_leak1", "content": "some_secret_content", "score": 100 }) fhqtest.alert(leak1 == None, 'Could not get response') fhqtest.check_response(leak1, "Leak succesfull created") #leak update test leak_id = None leaks = fhqtest.admin_session.leaks_list({"page": 0, "onpage": 10}) for leak in leaks["data"]: if leak["id"] != '': leak_id = leak["id"] else: fhqtest.log_err("Id could not be empty") fhqtest.print_bold("Update leak... ") leak_updt = fhqtest.admin_session.leaks_update({ "id": leak_id, "name": "test_leak1_new", }) fhqtest.alert(leak_updt == None, 'Could not get response') if leak_updt['result'] == 'FAIL': fhqtest.log_err(leak_updt['error']) else: leaks = fhqtest.admin_session.leaks_list({"page": 0, "onpage": 10}) for leak in leaks["data"]: if leak["id"] == leak_id: fhqtest.print_bold("Check name after update... ") if leak["name"] != "test_leak1_new": fhqtest.log_err("Leak has wrong name") else: fhqtest.print_success("Leak has been successfully updated") else: fhqtest.log_err("This leak isn't the one we updated!") print("Id of this leak is #" + str(leak["id"])) #TODO leaks_list test #TODO leaks_buy test
libtestgames, libtestquests, libtestscoreboard, libtestpublicevents, libtestleaks, libtestknowledgebase, libtestserversettings, libteststats ] try: fhqtest.print_header(" > > > TESTS: begin ") libtestfhq.start_server() fhqtest.init_enviroment() for t in tests: fhqtest.print_header(" > > > " + t.test_name + ": begin ") try: t.run_tests() except Exception as e: fhqtest.deinit_enviroment() fhqtest.throw_err("Some tests wrong") exit(-1) fhqtest.print_header(" > > > " + t.test_name + ": end ") tests_passed = True finally: fhqtest.print_header(" > > > TESTS: end ") if tests_passed: fhqtest.print_success("All tests passed") else: fhqtest.log_err("Some tests failed") libtestfhq.stop_server()
def run_tests(): # scoreboard # users # user # user_change_password # user_create # user_delete # user_skills # user_update user2_session = None # Cleanup user1 = None user2 = None user3 = None fhqtest.print_bold("Clean up test data... ") cleanup_user_by_email("user1") cleanup_user_by_email("user2") cleanup_user_by_email("user3") cleanup_user_by_email("user3_test") fhqtest.print_success("Cleaning complete") # test create users fhqtest.print_bold("Try create " + user1_data['email'] + "...") user1 = fhqtest.admin_session.users_add(user1_data) fhqtest.check_response(user1, "User1 succesfull created") fhqtest.print_bold("Try create user2...") user2 = fhqtest.admin_session.users_add(user2_data) fhqtest.check_response(user2, "User2 succesfull created") fhqtest.print_bold("Try create user3...") user3 = fhqtest.admin_session.users_add(user3_data) fhqtest.check_response(user3, "User3 succesfull created") fhqtest.print_bold("Check unique user3 by email...") user3_again = fhqtest.admin_session.users_add(user3_data_again) fhqtest.alert(user3_again is None, 'Could not get response (users_add/user3 again)') if user3_again['result'] == "DONE": fhqtest.log_err("Wrong creating user email must be unique") else: fhqtest.print_success("User3 again - OK") fhqtest.print_bold("Check unique user by uuid...") user3_again2 = fhqtest.admin_session.users_add({"uuid": USER3_UUID, "email": "user3_test", "nick": "nick_user3_again", "password": "******", "role": "user"}) fhqtest.alert(user3_again2 is None, 'Could not get response (users_add/user3 again2)') if user3_again2['result'] == "DONE": fhqtest.log_err("Wrong creating user uuid must be unique") else: fhqtest.print_success("User3 again - OK") fhqtest.print_bold("Find user by nick...") users = fhqtest.admin_session.users({"onpage": 10, "page": 0, "filter_text": "nick_user2"}) # users_count = users["count"] user_found = None for usr in users["data"]: if usr['email'] == "user2": user_found = usr fhqtest.alert(user_found is None, 'Could not find user2') if user_found is not None: fhqtest.print_success("User2 found success - OK") fhqtest.print_bold("Update user country... ") fhqtest.alert(user_found["country"] != "", 'User country must be empty ') user2_update = fhqtest.admin_session.user_update({ "userid": user_found["id"], "country": "Some country", "university": "Some university", }) fhqtest.alert(user2_update is None, 'Could not find user2') fhqtest.check_response(user2_update, "User2 succesfull updated") user2_update = user2_update["data"] if "country" not in user2_update: fhqtest.throw_err("Not found field 'country' in response after user update") elif user2_update["country"] != "Some country": fhqtest.throw_err("Field 'country' expected value of 'Some country'") if "university" not in user2_update: fhqtest.throw_err("Not found field 'university' in response after user update") elif user2_update["university"] != "Some university": fhqtest.throw_err("Field 'university' expected value of 'Some university'") if "nick" not in user2_update: fhqtest.throw_err("Not found field 'nick' in response after user update") if "about" not in user2_update: fhqtest.throw_err("Not found field 'about' in response after user update") if "created" not in user2_update: fhqtest.throw_err("Not found field 'created' in response after user update") if "region" not in user2_update: fhqtest.throw_err("Not found field 'region' in response after user update") if "email" not in user2_update: fhqtest.throw_err("Not found field 'email' in response after user update") if "role" not in user2_update: fhqtest.throw_err("Not found field 'role' in response after user update") # TODO # if "updated" not in user2_update: # fhqtest.log_err("Not found field 'updated' in response after user update") # exit(-1) if "uuid" not in user2_update: fhqtest.log_err("Not found field 'uuid' in response after user update") exit(-1) test_login('user1') test_login('user2') test_login('user3') fhqtest.print_bold("Login by user2... ") user2_session = FreeHackQuestClient(fhqtest.TEST_SERVER) user2_login = user2_session.login({"email": "user2", "password": "******"}) fhqtest.alert(user2_login is None, 'Could not login as user2 (1)') fhqtest.alert(user2_login['result'] == 'FAIL', 'Could not login as user2 (2) (fail)') if user2_login['result'] == 'DONE': fhqtest.print_success("User2 login success - OK") fhqtest.print_bold("User tokens... ") user2_tokens = user2_session.users_tokens({}) user2_session.close() if user2_tokens['result'] != 'DONE': fhqtest.log_err("User2 tokens failed") exit(-1) else: fhqtest.print_success("User2 tokens success - OK") # deprecated method user2_found1 = fhqtest.admin_session.user({"userid": user_found["id"]}) fhqtest.alert(user2_found1 is None, 'Could not get user2') fhqtest.check_response(user2_found1, "User2 succesfull got (1)") user2_found1 = user2_found1['data'] if "email" not in user2_found1: fhqtest.log_err("Not found field 'email' in response user2_found1") exit(-1) elif user2_found1['email'] != 'user2': fhqtest.log_err("Expected field 'email' value 'user2', but got '" + user2_found1['email'] + "'") exit(-1) # TODO user2_found2 = fhqtest.admin_session.users_info({"uuid": user_found["id"]}) fhqtest.alert(user2_found2 is None, 'Could not get user2') fhqtest.check_response(user2_found2, "User2 succesfull got (2)") if "email" not in user2_found1: fhqtest.throw_err("Not found field 'email' in response user2_found1") elif user2_found1['email'] != 'user2': fhqtest.throw_err("Expected field 'email' value 'user2', but got '" + user2_found1['email'] + "'") sys.exit(-1) '''curr_user = fhqtest.admin_session.users_info({}) fhqtest.alert(curr_user == None, 'Could not get user2') fhqtest.check_response(curr_user, "User2 succesfull got (2)") if "email" not in curr_user: fhqtest.log_err("Not found field 'email' in response curr_user") exit(-1) elif curr_user['email'] != 'user2': fhqtest.log_err("Expected field 'email' value 'user2', but got '" + curr_user['email'] + "'") exit(-1)''' user2_skills = fhqtest.admin_session.user_skills({"userid": user_found["id"]}) fhqtest.alert(user2_skills is None, 'Could not get user skills') fhqtest.check_response(user2_skills, "User2 succesfull got skills") pprint(user2_skills) user3_remove = fhqtest.admin_session.users_delete({ "userid": user3['data']['userid'], "password": fhqtest.ADMIN_PASSWORD }) fhqtest.alert(user3_remove is None, 'Could not remove user') fhqtest.check_response(user3_remove, "User3 succesfull removed") user2_remove = fhqtest.admin_session.users_delete({ "userid": user_found["id"], "password": fhqtest.ADMIN_PASSWORD }) fhqtest.alert(user2_remove is None, 'Could not remove user') fhqtest.check_response(user2_remove, "User2 succesfull removed")
def run_tests(): # Cleanup fhqtest.print_bold("Clean up test data... ") game2 = fhqtest.admin_session.game_info({"uuid": fhqtest.GAME_UUID2}) fhqtest.alert(game2 == None, 'Could not get response') if game2['result'] == "DONE": game2_delete_r = fhqtest.admin_session.game_delete({ "uuid": fhqtest.GAME_UUID2, "admin_password": fhqtest.ADMIN_PASSWORD }) fhqtest.alert(game2_delete_r == None, 'Could not get response (game_delete)') if game2_delete_r['result'] == 'FAIL': fhqtest.log_err("Could not delete prevously game: " + str(game2_delete_r)) raise ValueError('Could not delete prevously game') fhqtest.print_success("Cleaned") game_name = fhqtest.generate_random(255) game_description = fhqtest.generate_random(300) game_teams = fhqtest.generate_random(255) game_state = "original" game_form = "online" game_type = "jeopardy" game_date_start = "2000-01-01 00:00:00" game_date_stop = "2001-01-01 00:00:00" game_date_restart = "2011-01-01 00:00:00" # test create game fhqtest.print_bold("Try create game...") game2 = fhqtest.admin_session.game_create({ "uuid": fhqtest.GAME_UUID2, "name": game_name, "description": game_description, "state": game_state, "form": game_form, "type": game_type, "date_start": game_date_start, "date_stop": game_date_stop, "date_restart": game_date_restart, "organizators": game_teams }) fhqtest.alert(game2 == None, 'Could not get response (2)') fhqtest.check_response(game2, "Game succesfull created") if game2['result'] == 'FAIL': raise ValueError('Could not create game') # test find game fhqtest.print_bold("Find game by uuid... ") game2 = fhqtest.admin_session.game_info({"uuid": fhqtest.GAME_UUID2}) fhqtest.alert(game2 == None, 'Could not get response') fhqtest.check_response(game2, "Game found") game2 = game2['data'] fhqtest.check_values("name of game", game2['name'], game_name) fhqtest.check_values("organizators of game", game2['organizators'], game_teams) fhqtest.check_values("form of game", game2['form'], game_form) fhqtest.check_values("state of game", game2['state'], game_state) fhqtest.check_values("type of game", game2['type'], game_type) fhqtest.check_values("description of game", game2['description'], game_description) fhqtest.check_values("maxscore of game", game2['maxscore'], 0) gameid = game2['local_id'] # print("gameid: " + str(gameid)) # list of games fhqtest.print_bold("Get list of games... ") games_list = fhqtest.admin_session.games({}) fhqtest.alert(game2 == None, 'Could not get response') fhqtest.check_response(games_list, "Games list got") bFound = False for game_ in games_list['data']: if game_['uuid'] == fhqtest.GAME_UUID2: bFound = True if bFound != True: fhqtest.log_err("Game not found in list") else: fhqtest.print_success("Game found in list") ''' Update game name ''' game_name = fhqtest.generate_random(255) fhqtest.print_bold("Update game... ") game_updt = fhqtest.admin_session.game_update({ "uuid": fhqtest.GAME_UUID2, "name": game_name, }) fhqtest.alert(game_updt == None, 'Could not get response') if game_updt['result'] == 'FAIL': fhqtest.log_err(game_updt['error']) else: game2_new = fhqtest.admin_session.game_info( {"uuid": fhqtest.GAME_UUID2}) fhqtest.print_bold("Check name after update... ") fhqtest.check_response(game2_new, "Games list got") game2_new = game2_new['data'] fhqtest.print_bold("Check the game updated name... ") if game2_new['name'] != game_name: fhqtest.log_err("Game updated has wrong name expected '" + game_name + "' got '" + game2_new['name'] + "'") else: fhqtest.print_success("Game the game updated name ok") fhqtest.print_bold("Check the game updated organizators... ") if game2_new['organizators'] != game_teams: fhqtest.log_err("Game updated has wrong organizators expected '" + game_teams + "' got '" + game2_new['organizators'] + "'") else: fhqtest.print_success("Game the game updated organizators ok") fhqtest.print_bold("Check the game updated description... ") if game2_new['description'] != game_description: fhqtest.log_err("Game updated has wrong description expected '" + game_description + "' got '" + game2_new['description'] + "'") else: fhqtest.print_success("Game the game updated description ok") fhqtest.print_bold("Check the game updated maxscore... ") if game2_new['maxscore'] != 0: fhqtest.log_err( "Game updated has wrong maxscore expected '0' got '" + str(game2_new['maxscore']) + "'") else: fhqtest.print_success("Game the game updated maxscore ok") # TODO upload log for test game fhqtest.print_bold("Game update logo test... ") f = open("files/game1.png", 'rb') image_png_base64 = f.read() f.close() img_len = len(image_png_base64) if img_len != 17045: fhqtest.log_err("Game Logo wrong size, current " + str(img_len) + ", but expected 17045") image_png_base64 = base64.b64encode(image_png_base64) image_png_base64 = image_png_base64.decode("utf-8") game_img = fhqtest.admin_session.game_update_logo({ "uuid": fhqtest.GAME_UUID2, "image_png_base64": image_png_base64 }) fhqtest.alert(game_img == None, 'Could not get response') fhqtest.check_response(game_img, "Game Logo updated") # cleanup export files fhqtest.print_bold("Game cleanup export test... ") tmp_dir_unpack_zip = fhqtest.TMP_DIR + '/game2' game_zip_path = fhqtest.TMP_DIR + '/game_' + fhqtest.GAME_UUID2 + '.zip' if os.path.isdir(tmp_dir_unpack_zip): shutil.rmtree(tmp_dir_unpack_zip) if os.path.exists(game_zip_path): os.remove(game_zip_path) # export fhqtest.print_bold("Game export test... ") game_exp = fhqtest.admin_session.game_export({"uuid": fhqtest.GAME_UUID2}) fhqtest.alert(game_exp == None, 'Could not get response') fhqtest.check_response(game_exp, "Game exported") if 'game_' + fhqtest.GAME_UUID2 + '.zip' != game_exp['data'][ 'zipfile_name']: fhqtest.log_err("zipfile_name for game is wrong ") game_zip = base64.b64decode(game_exp['data']['zipfile_base64']) f = open(game_zip_path, 'wb') f.write(game_zip) f.close() zip_ref = zipfile.ZipFile(game_zip_path, 'r') zip_ref.extractall(tmp_dir_unpack_zip) zip_ref.close() img_exported_path = tmp_dir_unpack_zip + '/' + fhqtest.GAME_UUID2 + '.png' json_exported_path = tmp_dir_unpack_zip + '/' + fhqtest.GAME_UUID2 + '.json' if not os.path.exists(img_exported_path): fhqtest.log_err("Not found image in zip " + img_exported_path) if not os.path.exists(json_exported_path): fhqtest.log_err("Not found json in zip " + json_exported_path) else: with open(json_exported_path) as f: game2_exported = json.load(f) # pprint(game2_exported) fhqtest.print_bold("Check the export game name... ") if game2_exported['name'] != game_name: fhqtest.log_err("Exported game has wrong name expected '" + game_name + "' got '" + game2_exported['name'] + "'") else: fhqtest.print_success("Exported game name ok") fhqtest.print_bold("Check the export game organizators... ") if game2_exported['organizators'] != game_teams: fhqtest.log_err("Exported game has wrong organizators expected '" + game_teams + "' got '" + game2_exported['organizators'] + "'") else: fhqtest.print_success("Exported game organizators ok") fhqtest.print_bold("Check the export game description... ") if game2_exported['description'] != game_description: fhqtest.log_err("Exported game has wrong description expected '" + game_description + "' got '" + game2_exported['description'] + "'") else: fhqtest.print_success("Exported game description ok") fhqtest.print_bold("Check the export game maxscore... ") if game2_exported['maxscore'] != 0: fhqtest.log_err( "Exported game has wrong maxscore expected '0' got '" + str(game2_exported['maxscore']) + "'") else: fhqtest.print_success("Exported game maxscore ok") gameid = game2_exported['local_id'] # print(game_exp) # remove game fhqtest.print_bold("Remove game by uuid... ") resp = fhqtest.admin_session.game_delete({ "uuid": fhqtest.GAME_UUID2, "admin_password": fhqtest.ADMIN_PASSWORD }) fhqtest.check_response(resp, "Game removed") fhqtest.check_response(resp, "TODO Game import") '''