def test_getConfig(): config = configManager.getConfig() assert isinstance(config, ConfigParser.RawConfigParser) commandManager.runRmCommand(sad._CONFIG_FILE_PATH) with pytest.raises(SystemExit) as pytest_wrapped_e: config = configManager.getConfig() assert pytest_wrapped_e.type == SystemExit commandManager.runRmCommand(sad._CONFIG_DIR_NAME_) with pytest.raises(SystemExit) as pytest_wrapped_e: config = configManager.getConfig() assert pytest_wrapped_e.type == SystemExit
def deploy(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.deploy() elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.deploy()
def configure(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.initConfiguration() signal.signal(signal.SIGINT, herokuManager.sigintHandler) elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.initConfiguration() signal.signal(signal.SIGINT, gaeManager.sigintHandler)
def test_printVerbose(capsys): logManager.printVerbose("TEST") captured = capsys.readouterr() assert captured.out == "TEST\n" config = configManager.getConfig() configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_VERBOSE_OPTION_, "no") logManager.printVerbose("TEST") captured = capsys.readouterr() assert captured.out == ""
def generateBot(outputBotFile): config = configManager.getConfig() TOKEN = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION) if deployUrl is None or deployUrl == sad._INIT_CONFIG_DEPLOY_URL: gaeBotErrorHandler.addError("Deploy Url is emprty", sad._CRITICAL_ERROR_) webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION) outputBotFile.write("from telegram import Update\n") outputBotFile.write("from flask import Flask, request\n\n") outputBotFile.write("app = Flask(__name__)\n") outputBotFile.write("global updater\n") outputBotFile.write("global TOKEN\n") outputBotFile.write("TOKEN = '" + TOKEN + "'\n") outputBotFile.write("updater = Updater(TOKEN)\n\n") if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: outputBotFile.write("@app.route('/' + TOKEN, methods=['POST'])\n") else: outputBotFile.write("@app.route('/" + webhookPath + "', methods=['POST'])\n") outputBotFile.write("def webhook_handler():\n") outputBotFile.write("\tif request.method == 'POST':\n") outputBotFile.write("\t\tlogging.info(request.get_json(force=True))\n") outputBotFile.write( "\t\tupdate = Update.de_json(request.get_json(force=True), updater.bot)\n" ) outputBotFile.write("\t\tupdater.dispatcher.process_update(update)\n") outputBotFile.write("\treturn 'ok'\n\n\n") outputBotFile.write( "@app.route('/set_webhook', methods=['GET', 'POST'])\n") outputBotFile.write("def set_webhook():\n") outputBotFile.write("\tdispatcher = updater.dispatcher\n") outputBotFile.write( "\tfunctionManager.functionManager.generateHandlers(dispatcher)\n") if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: outputBotFile.write("\ts = updater.bot.setWebhook('" + deployUrl + "' + TOKEN)\n") else: outputBotFile.write("\ts = updater.bot.setWebhook('" + deployUrl + webhookPath + "')\n") outputBotFile.write("\tif s:\n") outputBotFile.write("\t\treturn 'webhook setup ok'\n") outputBotFile.write("\telse:\n") outputBotFile.write("\t\treturn 'webhook setup failed'\n\n\n") outputBotFile.write("@app.route('/')\n") outputBotFile.write("def index():\n") outputBotFile.write("\treturn 'Hello World'\n") outputBotFile.close() gaeBotErrorHandler.handle()
def test_verifyConfig(): config = configManager.getConfig() configManager.verifyConfig(config) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_, "TEST") with pytest.raises(SystemExit) as pytest_wrapped_e: configManager.verifyConfig(config) assert pytest_wrapped_e.type == SystemExit configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_, sad._DEPLOY_HEROKU_OPTION) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_, "") with pytest.raises(SystemExit) as pytest_wrapped_e: configManager.verifyConfig(config) assert pytest_wrapped_e.type == SystemExit commandManager.runRmCommand(sad._CONFIG_FILE_PATH) commandManager.runTouchCommand(sad._CONFIG_FILE_PATH) with pytest.raises(SystemExit) as pytest_wrapped_e: config = configManager.getConfig() assert pytest_wrapped_e.type == SystemExit
def deleteCloudApp(): config = configManager.getConfig() projectName = configManager.get(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: commandManager.runGAEDeleteProject(projectName) configManager.set(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_, "") configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, "") configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, "")
def _initConfiguration(projectNameFlag=True, initProjectFlag=True, gitInitFlag=True, gitHerokuFlag=-1): config = configManager.getConfig() if gitInitFlag: logManager.printVerbose("Creating git...") commandManager.runGitInitCommand() gitHerokuFlag = -1 if projectNameFlag: logManager.printVerbose("Project name doesn't found") _getNewHerokuName(config) initProjectFlag = True if initProjectFlag: logManager.printVerbose("Project hasn't been created in heroku") erroFlag = False while True: if erroFlag: logManager.printVerbose( "The project can't created in heroku. Read the errors above and chose a new heroku project name" ) _getNewHerokuName(config) projectName = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._HEROKU_URL gitUrl = sad._HTTPS_ + sad._HEORKU_GIT_URL + projectName + sad._GIT_EXTENTION commandManager.runHerokuCreateCommand(projectName) if _verifyProject(projectName): configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) configManager.set(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_, gitUrl) gitHerokuFlag = _verifyRemoteHeroku(gitUrl) break else: erroFlag = True if gitHerokuFlag == -1: logManager.printVerbose("Adding git remote heroku...") gitUrl = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_) commandManager.runGitAddRemoteCommand(sad._DEPLOY_HEROKU_OPTION, gitUrl) if gitHerokuFlag == 0: logManager.printVerbose("Setting git remote heroku...") gitUrl = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_) commandManager.runGitSetRemoteUrlCommand(sad._DEPLOY_HEROKU_OPTION, gitUrl)
def initConfiguration(): logManager.printVerbose("Verifying configuration...") logManager.printVerbose("Verifying Google Cloud installation...") _verifyGAEInstallation() logManager.printVerbose("Verifying Curl installation...") _verifyCurlInstallation() logManager.printVerbose("Verifying Google Cloud login...") while True: if not _verifyGAELogin(): logManager.printVerbose("Can't find google account") logManager.printVerbose("Google login...") _GAELogin() else: break config = configManager.getConfig() projectName = configManager.get(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) logManager.printVerbose("Verifiying project name...") if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: logManager.printVerbose("Project name found: " + projectName) else: projectName = _getNewGAEName(config) if _verifyGAEProject(projectName): logManager.printVerbose( "The project has already been created in Google Cloud") token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._GAE_URL_ configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) else: logManager.printVerbose("Project hasn't been created in Google Cloud") while True: _GAECreate(projectName) if _verifyGAEProject(projectName): token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._GAE_URL_ configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) break logManager.printVerbose( "The project can't created in Google Cloud. Read the errors above and chose a new Google Cloud project name" ) projectName = _getNewGAEName(config) _GAEsetProject(projectName) logManager.printVerbose("All Configurations... OK")
def destroy(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) logManager.printVerbose("WARNING: This action delete the bot in the cloud") token = inputManager.getInput("For security, paste here the bot token: ") if token == configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_): logManager.printVerbose("Deleting bot in the cloud...") if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.deleteCloudApp() elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.deleteCloudApp() else: logManager.printVerbose("Ufff, the tokens don't match")
def test_log(): assert logManager._log() config = configManager.getConfig() configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_LOG_OPTION_, "no") assert not logManager._log() commandManager.runRmDirCommand(sad._CONFIG_DIR_NAME_) assert not logManager._log() commandManager.runMkdirCommand(sad._CONFIG_DIR_NAME_) assert not logManager._log() commandManager.runTouchCommand(sad._CONFIG_FILE_PATH) assert logManager._log() configFile = open(sad._CONFIG_FILE_PATH, 'w') configFile.write("[RaveGen]\n") configFile.close() assert logManager._log()
def deploy(): config = configManager.getConfig() deployUrl = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION) if deployUrl is None or deployUrl == sad._INIT_CONFIG_DEPLOY_URL: gaeErrorHandler.addError("Deploy Url is emprty", sad._CRITICAL_ERROR_) webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION) if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) gaeErrorHandler.handle() _createSkeleton() commandManager.runGAEDeploy() _deleteSkeleton() commandManager.runCurlCommand(deployUrl + "set_webhook") logManager.printVerbose("\nDeploying bot in Google Cloud...OK")
def test_printLog(): config = configManager.getConfig() configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_LOG_OPTION_, "no") logManager.printLog("TEST") assert os.path.exists(sad._LOG_FILE_PATH_) logFile = open(sad._LOG_FILE_PATH_, 'r') line = logFile.readline() logFile.close() assert not line configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_LOG_OPTION_, "yes") logManager.printLog("TEST") logFile = open(sad._LOG_FILE_PATH_, 'r') line = logFile.readline() logFile.close() line = line.split(":") assert line[3] == " TEST\n"
def test_generateBot(testFlag, hosting): projectManager.createInitProject(createBasicModules=True, hostingOption=hosting) if not testFlag: with pytest.raises(SystemExit) as pytest_wrapped_e: botManager.generateBot(testFlag=testFlag) assert pytest_wrapped_e.type == SystemExit config = configManager.getConfig() configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, "www.test.com") botManager.generateBot(testFlag=testFlag) assert os.path.exists(sad._OUTPUT_BOT_DIR_) assert os.path.exists(sad.OUTPUT_BOT_PATH) headers = utils._getHeaders() if testFlag: assert headers[sad._HEADER_TOKEN_FLAG] == sad._STR_TRUE_ else: assert headers[sad._HEADER_TOKEN_FLAG] == sad._STR_FALSE_
def initConfiguration(): logManager.printVerbose("Verifying configuration...") logManager.printVerbose("Verifying Heroku installation...") _verifyHerokuInstallation() logManager.printVerbose("Verifying Heroku login...") while True: if not _verifyHerokuLogIn(): logManager.printVerbose("Can't find heroku token") logManager.printVerbose("Heorku login...") _herokuLogIn() else: break config = configManager.getConfig() projectNameFlag = True initProjectFlag = True gitInitFlag = True gitHerokuFlag = True projectName = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) logManager.printVerbose("Verifiying project name...") if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: logManager.printVerbose("Project name found: " + projectName) projectNameFlag = False logManager.printVerbose("Verifiying project in heroku...") if _verifyProject(projectName): logManager.printVerbose( "The project has already been created in heroku") initProjectFlag = False if utils.file_Or_Directory_Exists(sad._ACTUAL_PATH, sad._GIT_DIR_): logManager.printVerbose("Git has already been created") gitInitFlag = False gitHerokuFlag = _verifyRemoteHeroku( configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_)) if gitHerokuFlag > 0: logManager.printVerbose("Git has already been configured") _initConfiguration(projectNameFlag, initProjectFlag, gitInitFlag, gitHerokuFlag) logManager.printVerbose("All Configurations... OK")
def test_set(section, option, value): config = configManager.getConfig() configManager.set(config, section, option, value) assert configManager.get(config, section, option) == value
def test_get(section, option, expected): config = configManager.getConfig() assert configManager.get(config, section, option) == expected
def test_getBoolean(value, expected): config = configManager.getConfig() configManager.set(config, "TEST-SEC", "TEST-OP", value) assert configManager.getboolean(config, "TEST-SEC", "TEST-OP") == expected assert configManager.getboolean(config, "TEST", "TEST-OP") is None
def test_setSection(section): config = configManager.getConfig() configManager.setSection(config, section)