Exemplo n.º 1
0
def get_csrf():
    if not current_app.config['TESTING']:
        rand = base64.urlsafe_b64encode(os.urandom(30))
        ip = get_ip()
        CSRF.update_csrf(ip, rand)
        db.session.commit()
        return rand
    else:
        return 'test'
Exemplo n.º 2
0
    def test_update(self):
        self.assertEqual(CSRF.query.count(), 0)

        ip = '127.0.0.1'

        # Adding first row that will be removed later
        csrf_1 = CSRF(ip=ip, csrf='random', timestamp=datetime.today() - timedelta(hours=2))
        db.session.add(csrf_1)
        db.session.flush()
        self.assertEqual(CSRF.query.count(), 1)

        CSRF.update_csrf(ip, 'another_random')

        # csrf_1 should be replaced with the new row.
        rows = CSRF.query.all()
        self.assertEqual(len(rows), 1)
        self.assertEqual(rows[0].ip, ip)
        self.assertEqual(rows[0].csrf, 'another_random')

        # If we update one more time within 1 hour, both rows should be present.
        CSRF.update_csrf(ip, 'one_more_random')
        self.assertEqual(CSRF.query.count(), 2)
Exemplo n.º 3
0
def get_csrf():
    ip = get_ip()
    rand = base64.urlsafe_b64encode(os.urandom(30))
    CSRF.update_csrf(ip, rand)
    db.session.commit()
    return rand