def test_webpy_page(self): cursor = database_f.get_cursor() origional_require = user.require user.require = emulate_require for page_name in web.page_dict.keys(): if page_name in skip_pages: continue # Reset the CGI form each time so we're not sending data gui_test_utils.new_cgi_form({}) the_page = web.import_page(page_name, handle_exception=False) if page_name in expect_exception: try: self.assertRaises(Exception, the_page.main, cursor) except Exception: print("") print("Page name: %s" % page_name) raise else: try: the_page.main(cursor) except Exception: print("") print(page_name) raise user.require = origional_require
def install(options): from profiteer import sync as sync_module """Installs the system""" # Setup database connection if config.get('db_username') == "username": if config.get('db_password') == "password": print(cli_f.shell_text("[y]No database login[/y]")) print(""" Access to the database has not yet been setup. Open config.json and fill in values for db_host, db_username, db_password and db_name. You can optionally also create values for test and mock databases too. These will allow you to run unit tests involving the database and to trial code against mock data. When ready, run this program again.""") return False # Test database connection try: cursor = database_f.get_cursor() except Exception: print(cli_f.shell_text("[r]Database not accessible[/r]")) print(""" The login details for the database are incorrect. Double check the db_host, db_username, db_password and db_name fields in config.json When ready, run this program again to retry the connection.""") return False print(cli_f.shell_text("[g]Connected to database[/g]")) # Install database o = sync_module.main(fix=True, show_fixes=False, print_output=False) # Insert admin query = """UPDATE users SET password = '******' WHERE id = 1;""".format(user.encode_password('password', 'K*WJgU&j8M) ZT?=J_T-TUfH9*lY#!>@')) try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) print(cli_f.shell_text("[g]Database installed[/g]")) return True
def test_one_field(self): cursor = database_f.get_cursor() # Test string field gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "username"), ("where", "id = 1"), ("p", False), )) self.assertEqual("admin", get_one_field.main(cursor)) # Test boolean field gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "root"), ("where", "id = 1"), ("p", False), )) self.assertEqual(True, get_one_field.main(cursor)) # Now test setting it gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "root"), ("where", "id = 1"), ("value", "False"), ("p", False), )) self.assertEqual('False', edit_one_field.main(cursor)) # Get it again gui_test_utils.new_cgi_form(( ("table", "users"), ("field", "root"), ("where", "id = 1"), ("p", False), )) self.assertEqual(False, get_one_field.main(cursor))
def main(): # Need this so it uses the correct path try: for k, v in page_dict.items(): v[1] = v[1].replace("gui/", "{}/gui/".format(sys.path[0])) except Exception as e: print(error.html_render(context=1)) raise import cgitb cgitb.enable(context=1) cgitb.html = error.html_render # Override the shell patterns to output HTML instead cli_f.shell_patterns = cli_f.html_patterns # Connect to DB cursor = database_f.get_cursor() # Default to listing players m = common_f.get_val("mode", pages.default_page) the_page = import_page(m) output = [] try: page_results = the_page.main(cursor) except Exception as e: print("Content-type: text/html; charset=utf-8") print("") print( "There was an error executing the main function of the_page: %s" % str(the_page).replace("<", "<").replace(">", ">") ) print("<br /><br />") print(error.log_error(cursor, e, context=0)) return # Is this an AJAX request? ajax = bool(common_f.get_val("ajax", False)) # Redirect if page_results == "" and the_page.page_data.get("Redirect", "") != "": print("Location: {0}".format(the_page.page_data["Redirect"])) print("") return # Serving content print(the_page.page_data.get("Content-type", "Content-type: text/html; charset=utf-8")) print(html_f.cookies) print("") # Import the header/footer try: find = imp.find_module(the_page.page_data["Template"], ["templates"]) the_template = imp.load_module("the_template", *find) find[0].close() except KeyError: print(error.html_render(context=1, headers=False)) exit() except Exception: try: find[0].close() except Exception: pass print(error.html_render(context=1, headers=False)) print("<br /><br />Found template: {}<br />".format(the_page.page_data["Template"])) exit() # Headers if the_page.page_data.get("Headers", True) and page_results != "" and ajax != True: output.append(the_template.headers(cursor, the_page.page_data)) # Core output output.append(page_results) the_page.page_data["time"] = time.time() - start_time # Footers if the_page.page_data.get("Headers", True) and page_results != "" and ajax != True: output.append(the_template.footers(cursor, the_page.page_data)) output = de_unicode("".join([str(i) for i in output])) # We now want to print it out, sometimes there can be errors # related to unicode try: print(output) except UnicodeEncodeError as e: ignore_uni_errror = common_f.get_val("iue", 0) from profiteer import config try: f = open("%sutf8_out.html" % config.get("cache_path"), "w", encoding="utf-8") f.write(output) f.close() except Exception: pass if ignore_uni_errror: _print_ignoring_error(output) o = output print( "Unicode error at character %d, Ignore errors by adding '&iue=1', <a href='http://localhost/profiteer/utf8_out.html'>alternately view raw</a><br />" % e.start ) print( "%s<strong style='color:red;'>*</strong>%s" % ( o[e.start - 300 : e.start].replace("<", "<").replace(">", ">"), o[e.start + 1 : e.start + 20].replace("<", "<").replace(">", ">"), ) ) print("<br />") print(e.start, "<br />") print(dir(e)) exit() except Exception as e: raise