Example #1
0
    def test_password_protection(self):
        Paste.new('Test', password='******')

        r = self.client.get('/p/1/')

        # 401 = unauthorised
        assert r.status_code == 401
        assert r.mimetype == 'text/html'
Example #2
0
    def test_password_protection(self):
        Paste.new('Test', password='******')

        r = self.client.get('/p/1/')

        # 401 = unauthorised
        assert r.status_code == 401
        assert r.mimetype == 'text/html'
Example #3
0
    def test_paste_creation(self):
        p = Paste.new("Look, we're testing!", password='******')

        # Pasting succeeded
        assert p is not None
        assert p['id'] == 1

        # Check passwords are being hashed
        # bcrypt outputs 60 bytes
        assert p['password'] != 'hunter2'
        assert len(p['password']) == 60

        # Now check paste creation using the web
        r = self.client.post('/', data=dict(
            paste='test',
            title='',
            password='',
            language='text',
            unlisted=None
        ))

        # Grab the newly made paste
        p = Paste.by_id(2)

        assert p['text'] == 'test'
        assert p['password'] is None
        assert r.status_code == 302
Example #4
0
def index():
    form = NewPaste()
    if form.validate_on_submit():
        # WTForms passes '' for empty text values,
        # this lambda switches them to None
        f = lambda s: s if s != '' else None
        vals = {
            'text': form.paste.data,
            'title': f(form.title.data),
            'language': f(form.language.data),
            'password': f(form.password.data),
            'unlisted': f(form.unlisted.data)
        }
        paste = Paste.new(**vals)
        if paste is None:
            return redirect(url_for('pastes.index'))
        else:
            authorise_viewing(paste['hash'])
            if paste['unlisted']:
                url = url_for('pastes.unlisted', paste_hash=paste['hash'])
            else:
                url = url_for('pastes.public', paste_id=paste['id'])
            return redirect(url)
    elif request.method == 'POST':
        # Form submitted but failed validation
        for field, error in form.errors.items():
            errormsg = '{0}: {1}'.format(field, error[0])
            flash(errormsg, 'error')

    return render_template('index.html', form=form)
Example #5
0
def add():
    form = request.form
    errors = []

    if form.get('unlisted', type=int) in (0, 1):
        unlisted = bool(form.get('unlisted', type=int))
    else:
        unlisted = False

    paste = {
        'text': form.get('contents'),
        'title': form.get('title'),
        'password': form.get('password'),
        'unlisted': unlisted,
        'language': form.get('language', 'text')
    }

    if paste['text'] is None:
        errors.append('No contents specified')
    if paste['unlisted'] not in (True, False):
        errors.append("Invalid value: (unlisted: '{0}')".format(
            paste['unlisted']))

    if errors:
        return jsonify(success=False, url=None, password=None, error=errors)

    p = Paste.new(**paste)
    if p is None:
        return jsonify(success=False, url=None, password=None, error=errors)

    return jsonify(success=True,
                   url=create_paste_url(p),
                   password=paste['password'])
Example #6
0
def index():
    form = NewPaste()
    if form.validate_on_submit():
        # WTForms passes '' for empty text values,
        # this lamba switches them to None
        f = lambda s: s if s != "" else None
        vals = {
            "text": form.paste.data,
            "title": f(form.title.data),
            "language": f(form.language.data),
            "password": f(form.password.data),
            "unlisted": f(form.unlisted.data),
        }
        paste = Paste.new(**vals)
        if paste is None:
            return redirect(url_for("pastes.index"))
        else:
            authorise_viewing(paste["hash"])
            if paste["unlisted"]:
                url = url_for("pastes.unlisted", paste_hash=paste["hash"])
            else:
                url = url_for("pastes.public", paste_id=paste["id"])
            return redirect(url)
    elif request.method == "POST":
        # Form submitted but failed validation
        for field, error in form.errors.items():
            errormsg = "{0}: {1}".format(field, error[0])
            flash(errormsg, "error")

    return render_template("index.html", form=form)
Example #7
0
    def test_paste_creation(self):
        p = Paste.new("Look, we're testing!", password='******')

        # Pasting succeeded
        assert p is not None
        assert p['id'] == 1

        # Check passwords are being hashed
        # bcrypt outputs 60 bytes
        assert p['password'] != 'hunter2'
        assert len(p['password']) == 60

        # Now check paste creation using the web
        r = self.client.post('/',
                             data=dict(paste='test',
                                       title='',
                                       password='',
                                       language='text',
                                       unlisted=None))

        # Grab the newly made paste
        p = Paste.by_id(2)

        assert p['text'] == 'test'
        assert p['password'] is None
        assert r.status_code == 302
Example #8
0
    def test_url(self):
        with app.test_request_context():
            p = Paste.new(text='test')
            url = utils.create_paste_url(p)
            assert url == 'http://localhost/p/1/'

            url = utils.create_paste_url(p, relative=True)
            assert url == '/p/1/'
Example #9
0
    def test_url(self):
        with app.test_request_context():
            p = Paste.new(text='test')
            url = utils.create_paste_url(p)
            assert url == 'http://localhost/p/1/'

            url = utils.create_paste_url(p, relative=True)
            assert url == '/p/1/'
Example #10
0
    def test_paste_deletion(self):
        self.add_account()
        p = Paste.new(text='test')

        self.client.post('/a/in',
                         data=dict(username='******', password='******'))

        self.client.post('/a/del/' + p['hash'],
                         data=dict(paste_hash=p['hash']))

        paste = Paste.by_hash(p['hash'])
        assert paste is None
Example #11
0
    def test_unlisted_paste(self):
        p = Paste.new('Test', unlisted=True)

        id = p['id']
        hash = p['hash']

        # Unlisted pastes should only be
        # accessed via /u/:hash
        r = self.client.get('/p/{0}/'.format(id))
        assert r.status_code == 404

        r = self.client.get('/u/{0}/'.format(hash))
        assert r.status_code == 200
Example #12
0
    def test_unlisted_paste(self):
        p = Paste.new('Test', unlisted=True)

        id = p['id']
        hash = p['hash']

        # Unlisted pastes should only be
        # accessed via /u/:hash
        r = self.client.get('/p/{0}/'.format(id))
        assert r.status_code == 404

        r = self.client.get('/u/{0}/'.format(hash))
        assert r.status_code == 200
Example #13
0
    def test_password_authentication(self):
        p = Paste.new('Test', password='******')

        with self.client as c:
            r = c.post('/p/authorise', data=dict(
                paste_hash=p['hash'],
                password='******',
                redirect='http://localhost/p/1/',
            ))

            # Check we've got the correct cookie
            # and are being redirected
            assert p['hash'] in session.get('authorised_pastes')
            assert r.status_code == 302
Example #14
0
    def test_paste_deletion(self):
        self.add_account()
        p = Paste.new(text='test')

        self.client.post('/a/in', data=dict(
            username='******',
            password='******'
        ))

        self.client.post('/a/del/' + p['hash'], data=dict(
            paste_hash=p['hash']
        ))

        paste = Paste.by_hash(p['hash'])
        assert paste is None
Example #15
0
    def test_password_authentication(self):
        p = Paste.new('Test', password='******')

        with self.client as c:
            r = c.post('/p/authorise',
                       data=dict(
                           paste_hash=p['hash'],
                           password='******',
                           redirect='http://localhost/p/1/',
                       ))

            # Check we've got the correct cookie
            # and are being redirected
            assert p['hash'] in session.get('authorised_pastes')
            assert r.status_code == 302
Example #16
0
def add():
    form = request.form
    errors = []

    if form.get('unlisted', type=int) in (0, 1):
        unlisted = bool(form.get('unlisted', type=int))
    else:
        unlisted = False

    paste = {
        'text': form.get('contents'),
        'title': form.get('title'),
        'password': form.get('password'),
        'unlisted': unlisted,
        'language': form.get('language', 'text')
    }

    if paste['text'] is None:
        errors.append('No contents specified')
    if paste['unlisted'] not in (True, False):
        errors.append(
            "Invalid value: (unlisted: '{0}')".format(paste['unlisted'])
        )

    if errors:
        return jsonify(
            success=False,
            url=None,
            password=None,
            error=errors
        )

    p = Paste.new(**paste)
    if p is None:
        return jsonify(
            success=False,
            url=None,
            password=None,
            error=errors
        )

    return jsonify(
        success=True,
        url=create_paste_url(p),
        password=paste['password']
    )
Example #17
0
def new():
    """
    Endpoint for creating a new paste.
    """
    form = request.form

    text = form.get("text")
    if text is None:
        return jsonify(error="required value missing: text"), 400

    unlisted = form.get("unlisted", "f")
    if unlisted.lower() in ("1", "true", "t", "y"):
        unlisted = True
    else:
        unlisted = False

    paste = {
        "text": text,
        "title": form.get("title"),
        "language": form.get("lang", "text"),
        "password": form.get("password"),
        "unlisted": unlisted,
    }

    p = Paste.new(**paste)
    if not Paste:
        return internal_server_error()

    response = {
        "url": create_paste_url(p),
        "shorturl": p["shortlink"],
        "paste": create_paste_dict(p),
        "password": paste["password"],
    }

    return jsonify(response)
Example #18
0
def new():
    """
    Endpoint for creating a new paste.
    """
    form = request.form

    text = form.get('text')
    if text is None:
        return jsonify(error='required value missing: text'), 400

    unlisted = form.get('unlisted', 'f')
    if unlisted.lower() in ('1', 'true', 't', 'y'):
        unlisted = True
    else:
        unlisted = False

    paste = {
        'text': text,
        'title': form.get('title'),
        'language': form.get('lang', 'text'),
        'password': form.get('password'),
        'unlisted': unlisted,
    }

    p = Paste.new(**paste)
    if not Paste:
        return internal_server_error()

    response = {
        'url': create_paste_url(p),
        'shorturl': p['shortlink'],
        'paste': create_paste_dict(p),
        'password': paste['password'],
    }

    return jsonify(response)
Example #19
0
 def test_raw_paste(self):
     Paste.new('Hello World!')
     r = self.client.get('/p/1/raw/')
     assert r.status_code == 200
     assert r.mimetype == 'text/plain'
Example #20
0
 def test_raw_paste(self):
     Paste.new('Hello World!')
     r = self.client.get('/p/1/raw/')
     assert r.status_code == 200
     assert r.mimetype == 'text/plain'