def parse_sentence(sent, doc_offset, tokenizer): """ Perform tokenization on sent :param string sent: :param tuple doc_offset: (start, end) :param tokenizer: :return: models.Sentence object """ sent_obj = models.Sentence(content=sent, doc_offset=doc_offset) sent_obj.tokens = [] tokens = tokenizer.tokenize(sent) current_pos = 0 for tok in tokens: start_offset = sent.find(tok, current_pos) end_offset = start_offset + len(tok) token_obj = models.Token( content=tok, sent_offset=(start_offset, end_offset), doc_offset=(doc_offset[0] + start_offset, doc_offset[0] + end_offset), # metadata={'pos_tag': pos} ) current_pos = end_offset sent_obj.tokens.append(token_obj) return sent_obj
def test_validate_token_ok_02(self) -> None: """ Test the function that validates a token, with a success case for an REFRESH token. """ # The expected result expected_result = True, 123 # Prepare the mocks configuration.REFRESH_TOKEN_VALIDITY_DAYS = 5 token = authentication.generate_token(123, authentication.TokenType.REFRESH, unittest.mock.MagicMock()) db_calls_mock.get_token.return_value = models.Token( token, datetime.date.today() + datetime.timedelta(days=configuration.REFRESH_TOKEN_VALIDITY_DAYS)) # Call the function actual_result = authentication.validate_token( token.encode(), authentication.TokenType.REFRESH, unittest.mock.MagicMock()) # Verify the result self.assertEqual(expected_result, actual_result) db_calls_mock.get_token.assert_called()
def round_tokens(round_id): token_form = models.TokenForm(request.form) if token == '': # no token, return to page return redirect(request.referrer) #get the round by id try: round = models.Round.objects.get(id=round_id) except: # error, return to where you came from return redirect(request.referrer) # create tokens token = models.Token() token.token = request.form.get('token') # append question to token list tokens.append(token) # save it token.save() return redirect('/rounds/%s' % round.slug)
def test_generate_token_ok_01(self) -> None: """ Test the function that generates a token, with a success case for REFRESH. """ # The expected result expected_expiration_date = datetime.datetime.today().replace( microsecond=0) + datetime.timedelta(days=5) expected_type = 'REFRESH' # Prepare the mocks token_payload = {'key': 'value'} token = jwt.encode(token_payload, 'secret key', algorithm='HS256') configuration.REFRESH_TOKEN_VALIDITY_DAYS = 5 db_calls_mock.register_token.return_value = models.Token( token, expected_expiration_date.date()) # Call the function actual_result = authentication.generate_token( 1, authentication.TokenType.REFRESH, unittest.mock.MagicMock()) # Verify the result self.assertIsNotNone(actual_result) db_calls_mock.register_token.assert_called() # Verify the date actual_expiration_date = datetime.datetime.fromtimestamp( authentication.get_token_field(actual_result, 'exp')) self.assertEqual(expected_expiration_date, actual_expiration_date) # Verify the other fields exist self.assertEqual(expected_type, authentication.get_token_field(actual_result, 'type')) self.assertEqual(1, authentication.get_token_field(actual_result, 'user'))
def __parse_sentence(sent, doc_offset, tokenizer): """ Perform tokenization on sent :param string sent: :param tuple doc_offset: (start, end) :param tokenizer: :return: Sentence object """ sent_obj = models.Sentence(content=sent, doc_offset=doc_offset) sent_obj.tokens = [] tokens = tokenizer.parse(sent) current_pos = 0 for tok in tokens: t = tok.string.strip() start_offset = sent.find(t, current_pos) end_offset = start_offset + len(t) token_obj = models.Token(content=t, sent_offset=( start_offset, end_offset), doc_offset=( doc_offset[0] + start_offset, doc_offset[0] + end_offset), metadata={'POS': tok.tag_}) current_pos = end_offset sent_obj.tokens.append(token_obj) return sent_obj
def create_resetting_pass_token(db: Session, form): email = form.email if not db.query(models.User).filter(models.User.email == email).first(): return False generated_token = hashlib.sha256( (email + secret + datetime.datetime.utcnow()).encode('utf-8')).hexdigest() new_token = models.Token( email=email, token=generated_token, ) db.add(new_token) db.commit() content = generated_token msg = MIMEText(content, text_subtype) msg['Subject'] = "Password reset" msg['From'] = sender_email conn = SMTP(smtp_server) conn.set_debuglevel(False) conn.login(sender_email, password) try: conn.sendmail(sender_email, email, msg.as_string()) finally: conn.quit() return True
def test_betanode_perform_join_tests(self): rete = models.Rete().save() # Represents the LHS of one production. conditions = [] conditions.append(('?','?x','on','?y')) conditions.append(('?','?y','left-of','?z')) conditions.append(('?','?z','color','red')) facts = [] facts.append(T('block1', 'on', 'block2')) facts.append(T('block2', 'left-of', 'block3')) facts.append(T('block3', 'color', 'red')) # Arbitrary alphanode, needed for right side of betanode. an = models.AlphaNode(rete=rete, field=models.ID_IDX, operation=models.EQ_IDX, value=None).save() bn = models.BetaJoinNode(alphanode=an, _alphanode=an).save() # Note, dummy token, not shown here, always has index == 0. t0 = models.Token(wme=facts[0], parent=None, index=1).save() self.assertEqual(t0.index, 1) t1 = models.Token(wme=facts[1], parent=t0, index=t0.index+1).save() self.assertEqual(t1.index, 2) t2 = models.Token(wme=facts[2], parent=t1, index=t1.index+1).save() self.assertEqual(t2.index, 3) # Confirm indexing order by tokens according to Token.get_wme(index) self.assertEqual(t2.get_wme(index=1), facts[0]) self.assertEqual(t2.get_wme(index=2), facts[1]) self.assertEqual(t2.get_wme(index=3), facts[2]) test = models.TestAtJoinNode(parent=bn, field_of_arg1=models.S_IDX, condition_number_of_arg2=2, field_of_arg2=models.O_IDX).save() self.assertEqual(bn.tests.all().count(), 1) #TODO:why facts[2]? self.assertEqual(bn.perform_join_tests(token=t2, wme=facts[2]), True)
def get_token(): ''' Generate and return an token in json format after adding it to the database ''' token_string = ''.join([ random.choice(string.ascii_uppercase + string.digits) for _ in range(32) ]) token = models.Token(token=token_string) models.get_db().add(token) models.get_db().commit() return jsonify({"token": token_string})
def auth(mail, password): user = models.User.query.filter_by(mail=mail).first() if (user != None): #if(user.password == sha512(password)): # NOTE: no time for security, no hashing if (user.password == password): token = user.token[0] if len(user.token) == 1 else None if (token != None): token.token = generate_token() token.updateExpiration() else: token = models.Token(uid=user.uid, token=generate_token()) db.session.add(token) db.session.commit() return user session.clear() return False
def post(self): req = loginParser.parse_args() login_creds = models.User(name=req["name"], password=req["password"], email=None) try: if models.User.verify_password(login_creds): new_token = models.Token( token=create_access_token( identity=models.User.findId(login_creds.name)), user_id=models.User.findId(login_creds.name), ) return {"token": models.Token.save_token_todb(new_token)}, 200 elif models.User.verify_password(login_creds) is False: return {"message": "Incorrect password"}, 403 else: return {"message": "Incorrect username"}, 403 except Exception as e: return e, 500
def test_generate_access_token_ok(self) -> None: """ Test the function that generates an access token, with a success case. """ # The expected result expected_expiration_date = datetime.datetime.today().replace( microsecond=0) + datetime.timedelta(hours=10) expected_type = 'ACCESS' # Prepare the mocks configuration.REFRESH_TOKEN_VALIDITY_DAYS = 5 configuration.ACCESS_TOKEN_VALIDITY_HOURS = 10 refresh_token = authentication.generate_token( 123, authentication.TokenType.REFRESH, unittest.mock.MagicMock()) db_calls_mock.register_token.return_value = models.Token( refresh_token, expected_expiration_date.date()) # Call the function actual_result, actual_token = authentication.generate_access_token( unittest.mock.MagicMock(), refresh_token) # Verify the result self.assertTrue(actual_result) self.assertIsNotNone(actual_token) # Verify the date actual_expiration_date = datetime.datetime.fromtimestamp( authentication.get_token_field(actual_token, 'exp')) self.assertEqual(expected_expiration_date, actual_expiration_date) # Verify the other fields exist self.assertEqual(expected_type, authentication.get_token_field(actual_token, 'type')) self.assertEqual(123, authentication.get_token_field(actual_token, 'user'))
def test_wme_removal_structures(self): rete = models.Rete().save() # Represents the LHS of one production. conditions = [] conditions.append(('?','?x','on','?y')) conditions.append(('?','?y','left-of','?z')) conditions.append(('?','?z','color','red')) facts = [] facts.append(T('block1', 'on', 'block2')) facts.append(T('block2', 'left-of', 'block3')) facts.append(T('block3', 'color', 'red')) for i,condition in enumerate(conditions): p = models.Production.get('p%i'%i,[]) condition = models.Condition.get(p, *condition) rete.build_or_share_alpha_memory(condition) for fact in facts: rete.add_wme(fact) # Per page 29, find all alphanode memories containing this WME. wme = facts[0] self.assertEqual(wme.alphanodes.all().count(), 2) # Per page 29, find all tokens with WME as the last element. # Create sample tokens. wme = facts[2] t0 = models.Token(wme=facts[0], parent=None).save()#1 t1 = models.Token(wme=facts[1], parent=t0).save() t2 = models.Token(wme=facts[2], parent=t1).save()#3 t0 = models.Token(wme=facts[2], parent=None).save() t1 = models.Token(wme=facts[0], parent=t0).save() t2 = models.Token(wme=facts[1], parent=t1).save()#6 q = models.Token.objects.filter(wme=wme).exclude(id__in=models.Token.objects.filter(parent__isnull=False).values_list('parent')) self.assertEqual(q.count(), 1) self.assertEqual(q[0].id, 3)
def add_token(user_id, target, value): session.add(models.Token(user_id=user_id, target=target, value=value)) session.commit()
name='Dominik Boehi', email='*****@*****.**', nethzid='dboehi') s.add(u2) d = models.Device(name='Der Testgeraet', pubkey='alfonso', creation_date=datetime.date.today(), is_online=False, is_enabled=True) s.add(d) data = '2015-12-01'.split('-') date = datetime.date(int(data[0]), int(data[1]), int(data[2])) t = models.Token(name='Testtoken Yatekii', value='migros', description='Bla', owner=u1, flashed=False, expiry_date=date, creation_date=datetime.date.today()) d.tokens.append(t) s.add(t) t = models.Token(name='Testtoken Tiwalun', value='random', description='Blo', owner=u2, flashed=False, expiry_date=date, creation_date=datetime.date.today()) d.tokens.append(t) s.add(t) s.commit()