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'
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
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)
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'])
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)
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
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/'
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
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
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
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
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'] )
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)
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)
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'