def test_redirect_create_full_params(self): request = apigw_post( "redirects", body={ "uses_left": 7, "can_rickroll": True, "url": "https://google.com", "password": "******", }, ) res = app.lambda_handler(request, "") assert res.get("statusCode") == 200 redirect_id = json.loads(res.get("body", "{}")).get("redirect_id", None) self.redirect_ids.append(redirect_id) assert redirect_id is not None redirect_entry = Redirect(app.REDIRECTS_TABLE, redirect_id) assert redirect_entry.exists() assert redirect_entry.item.get("uses_left") == 7 assert redirect_entry.item.get("can_rickroll") assert redirect_entry.item.get("url") == "https://google.com" assert redirect_entry.password == "SneakyPass"
def Access(self, opts): """ [file] Show status of a file.""" string = self.check_opts(opts, 0) if string == False: print "too little argument!" return False findout = [] from redirect import Redirect r = Redirect() self.ftpconn.dir() lines = r.getitems() for line in lines: try: indexs = line.index(string) findout.append(line) except: pass if len(findout) == 0: print "%s is not found." % string else: for line in findout: print line return False
def test_spend_a_use(self): redirect = Redirect(REDIRECTS_TABLE, self.redirect_id) redirect.spend_a_use() assert redirect.item.get("uses_left") == 9 # Check Dynamo updated assert Redirect(REDIRECTS_TABLE, self.redirect_id).item.get("uses_left") == 9
def test_create_redirect(self): try: redirect_id = Redirect.create_redirect(REDIRECTS_TABLE, "https://google.com") self.redirect_id = redirect_id assert len(redirect_id) == REDIRECT_ID_LEN assert Redirect(REDIRECTS_TABLE, redirect_id).exists() except: assert False, "Create redirect without erroring"
def validation(self, user, email, password): if user[0][1] == email: print('User exists') if user[0][2] == password: redir.redir_main_page(self) return [user] else: message.flash_message(self, 'wrong pass') else: print('error')
def test_redirect_create(self): request = apigw_post("redirects", body={"url": "https://google.com"}) res = app.lambda_handler(request, "") assert res.get("statusCode") == 200 redirect_id = json.loads(res.get("body", "{}")).get("redirect_id", None) self.redirect_ids.append(redirect_id) assert redirect_id is not None redirect_entry = Redirect(app.REDIRECTS_TABLE, redirect_id) assert redirect_entry.exists()
def register(self, email, password): self.email = email self.password = password user_id = db.get_user_by_email(email, password) if bool(user_id) is False: db.add_user(email, password) message.flash_message(self, key='success_reg') redir.redir_log_page(self) elif bool(user_id) is True: message.flash_message(self, key='exists') else: print('Error')
def redirects(self): if self.redirect: redirect_location = self.internal.getResponseHeaderValue("Location") redirect_url = htmlunit.URL(self.reqresp.request.webrequest.getUrl(), redirect_location).toString() if redirect_url == self.reqresp.request.webrequest.getUrl().toString(): redirect_url = self.initial_url return [Redirect(redirect_url, self.reqresp)] else: return []
def __init__(self, parent = None): QTextEdit.__init__(self, parent) self.setUndoRedoEnabled(False) self.setCursorWidth(5) self.setTextColor(QColor("white")) p = self.palette() p.setColor(QPalette.Base, QColor("#000000")) self.setPalette(p) self.fixedPosition = 0 self.redirect = Redirect() self.redirect.OnChildStdOutWrite.connect(self.OnChildStdOutWrite) self.redirect.OnChildStarted.connect(self.OnChildStarted) self.redirect.OnChildStdErrWrite.connect(self.OnChildStdErrWrite) self.redirect.OnChildTerminate.connect(self.OnChildTerminate)
def teardown(self): if self.redirect_id: Redirect(app.REDIRECTS_TABLE, self.redirect_id).delete()
def setup(self): self.redirect_id = Redirect.create_redirect(app.REDIRECTS_TABLE, "https://google.com")
def teardown(self): for rid in self.redirect_ids: Redirect(app.REDIRECTS_TABLE, rid).delete()
def test_delete_redirect(self): try: Redirect(REDIRECTS_TABLE, self.redirect_id).delete() assert not Redirect(REDIRECTS_TABLE, self.redirect_id).exists() except: assert False, "Delete redirect without erroring"
class QConsoleWidget(QTextEdit): def __init__(self, parent=None): QTextEdit.__init__(self, parent) self.setUndoRedoEnabled(False) self.setCursorWidth(5) self.setTextColor(QColor("white")) p = self.palette() p.setColor(QPalette.Base, QColor("#000000")) self.setPalette(p) self.fixedPosition = 0 self.redirect = Redirect() self.redirect.OnChildStdOutWrite.connect(self.OnChildStdOutWrite) self.redirect.OnChildStarted.connect(self.OnChildStarted) self.redirect.OnChildStdErrWrite.connect(self.OnChildStdErrWrite) self.redirect.OnChildTerminate.connect(self.OnChildTerminate) self.redirect.StartChildProcess(False) def OnChildStarted(self): pass def OnChildStdOutWrite(self, szOutput): self.insertPlainText(szOutput) self.fixedPosition = self.textCursor().position() def OnChildStdErrWrite(self, szOutput): self.append(szOutput) self.fixedPosition = self.textCursor().position() def OnChildTerminate(self): self.redirect.TerminateChildProcess() def keyPressEvent(self, event): accept = False key = event.key() if key == Qt.Key_Backspace or key == Qt.Key_Left: accept = self.textCursor().position() > self.fixedPosition elif key == Qt.Key_Return: accept = False count = self.toPlainText().count() - self.fixedPosition cmd = self.toPlainText().right(count) self.redirect.WriteChildStdIn(cmd + "\n") elif key == Qt.Key_Up: accept = False else: accept = self.textCursor().position() >= self.fixedPosition if accept: QTextEdit.keyPressEvent(self, event) def cursorPositionChanged(self): if self.textCursor().position() < self.fixedPosition: self.textCursor().setPosition(self.fixedPosition) # fix the #bug1 when the main process exits, it occurs # QProcess: Destroyed while process is still running. def closeEvent(self, event): self.redirect.TerminateChildProcess() event.accept()
def lambda_handler(event, context): """Sample pure Lambda function Parameters ---------- event: dict, required API Gateway Lambda Proxy Input Format Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format context: object, required Lambda Context runtime methods and attributes Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html Returns ------ API Gateway Lambda Proxy Output Format: dict Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html """ path = event["pathParameters"]["proxy"] body = json.loads(event.get("body") if event.get("body") else "{}") query_params = event.get("queryStringParameters") # https://github.com/awslabs/aws-sam-cli/issues/1860 event["headers"].update({name.lower(): value for name, value in event["headers"].items()}) headers = event.get("headers") # GET /redirects/<Alphanumeric> regex = rf"^redirects/([a-zA-Z0-9]{{{REDIRECT_ID_LEN},}})$" if re.match(regex, path) and event["httpMethod"] == "GET": redirect_id = re.match(regex, path).group(1) requested_redirect = Redirect(REDIRECTS_TABLE, redirect_id) if not requested_redirect.exists(): return NOT_FOUND if requested_redirect.is_password_protected(): if query_params.get("pass", None) != requested_redirect.password: return { "statusCode": 401, "body": json.dumps({"message": "This direct requires a pass query parameter"}), } if query_params.get("use") == "true": requested_redirect.spend_a_use() response = {"redirect": requested_redirect.item} # Seed off combination of redirectid and user token so user gets same result every time if headers.get("user-token") and query_params.get("use") == "true": random.seed(f"{redirect_id}{headers.get('user-token')}") response["rickrolled"] = random.randint(1, 100) < RICKROLL_CHANCE return { "statusCode": 200, "body": simplejson.dumps(response), } # POST /redirects if re.match(r"^redirects$", path) and event["httpMethod"] == "POST": url = body.get("url") can_rickroll = body.get("can_rickroll") uses_left = body.get("uses_left", 10) password = body.get("password") if is_url(url): redirect_id = Redirect.create_redirect( REDIRECTS_TABLE, url, uses_left=uses_left, can_rickroll=can_rickroll, password=password, ) return { "statusCode": 200, "body": json.dumps({"redirect_id": redirect_id}), } else: return bad_request(["url"]) return NOT_FOUND
import sys from string import ascii_uppercase from redirect import Redirect if __name__ == '__main__': path = 'test.txt' letter = iter(ascii_uppercase) print(next(letter), '[out]') print(next(letter), '[err]', file=sys.stderr) with Redirect(path, 'o'): print(next(letter), '[out]', 'in Redirect') print(next(letter), '[err]', 'in Redirect', file=sys.stderr) print(next(letter), '[out]') print(next(letter), '[err]', file=sys.stderr) with Redirect(path, 'e'): print(next(letter), '[out]', 'in Redirect') print(next(letter), '[err]', 'in Redirect', file=sys.stderr) print(next(letter), '[out]') print(next(letter), '[err]', file=sys.stderr) with Redirect(path, 'oe'): print(next(letter), '[out]', 'in Redirect') print(next(letter), '[err]', 'in Redirect', file=sys.stderr) print(next(letter), '[out]') print(next(letter), '[err]', file=sys.stderr)