Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Flask~=0.11.1
pep8~=1.7.0
pytest~=3.0.3
pytest-json~=0.4.0
-e git+https://github.com/startup-systems/splinter.git@ba3afc8a0750dcfea096f8f89adb58f8f8d78276#egg=splinter[flask]
splinter[flask]~=0.7.5
4 changes: 2 additions & 2 deletions squawker/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-- TODO change this
DROP TABLE IF EXISTS mytable;
CREATE TABLE mytable (id integer);
DROP TABLE IF EXISTS squawks;
CREATE TABLE squawks (id INTEGER PRIMARY KEY AUTOINCREMENT, body TEXT);
38 changes: 33 additions & 5 deletions squawker/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, g
from flask import Flask, g, render_template, request, redirect, url_for
import sqlite3


Expand Down Expand Up @@ -36,12 +36,40 @@ def close_connection(exception):
db.close()
# ------------------------------

# Add posts
def addPost(data):
conn = get_db()
cur = conn.cursor()
cur.execute("INSERT INTO squawks (body) VALUES(?)", (data,))
conn.commit()

@app.route('/')
def root():
def getPosts():
conn = get_db()
# TODO change this
return "Hello World!"
cur = conn.cursor()
cur.execute("""SELECT id, body FROM
squawks
ORDER BY id DESC """)
temp = cur.fetchall()
data = []
idx = []
cur.close()
return temp

# Routes

# Index
@app.route('/')
def root():
squawks = getPosts()
return render_template('index.html', squawks=squawks)

@app.route('/add/', methods=['POST']) # Add
def add():
# Check if post is 140 characters
if (len(request.form["new_body"]) > 140):
return abort(400)
addPost(request.form["new_body"])
return redirect(url_for('root'))


if __name__ == '__main__':
Expand Down
35 changes: 35 additions & 0 deletions squawker/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">

<title>Squawker</title>

</head>

<body>
<div>
<h2>Squawker</h2>

{% for squawk in squawks %}
<div>
<h4>{{ squawk[0] }}</h4>
<div><p>{{ squawk[1] }}</p></div>
</div>
{% endfor %}

<div>
<div>
<form action="add/" method="post">

<textarea placeholder="Type post..." name="new_body" id="new_body" maxlength="140" ></textarea>

<input type="submit" value="Submit">

</form>
</div>
</div>
</div>
</body>
</html>
74 changes: 74 additions & 0 deletions tests/flaskclient_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# https://github.com/cobrateam/splinter/issues/515

from urllib import parse

from splinter.browser import _DRIVERS
from splinter.driver import flaskclient

__all__ = ['FlaskClient']


class FlaskClient(flaskclient.FlaskClient):
"""
A patched `FlaskClient` driver that implements more standard `302`/`303`
behaviour and that sets data for `GET` requests against the URL.
"""

driver_name = 'flask'

def _do_method(self, method, url, data=None):

# Set the initial URL and client/HTTP method
self._url = url
func_method = getattr(self._browser, method.lower())

# Continue to make requests until a non 30X response is recieved
while True:
self._last_urls.append(url)

# If we're making a GET request set the data against the URL as a
# query.
if method.lower() == 'get':

# Parse the existing URL and it's query
url_parts = parse.urlparse(url)
url_params = parse.parse_qs(url_parts.query)

# Update any existing query dictionary with the `data` argument
url_params.update(data or {})
query = parse.urlencode(url_params, doseq=True)
url_parts = url_parts._replace(query=query)

# Rebuild the URL
url = parse.urlunparse(url_parts)

# As the `data` argument will be passed as a keyword argument to
# the `func_method` we set it `None` to prevent it populating
# `flask.request.form` on `GET` requests.
data = None

# Call the flask client
self._response = func_method(
url,
headers=self._custom_headers,
data=data,
follow_redirects=False
)

# Implement more standard `302`/`303` behaviour
if self._response.status_code in (302, 303):
func_method = getattr(self._browser, 'get')

# If the response was not in the `30X` range we're done
if self._response.status_code not in (301, 302, 303, 305, 307):
break

# If the response was in the `30X` range get next URL to request
url = self._response.headers['Location']

self._url = self._last_urls[-1]
self._post_load()


# Patch the default `FlaskClient` driver
_DRIVERS['flask'] = FlaskClient
5 changes: 3 additions & 2 deletions tests/test_squawker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import string
import tempfile
import time
from . import flaskclient_fix


URL = '/'
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_server_side_validation(browser):
@pytest.mark.score(5)
@pytest.mark.xfail
def test_page_size_limit(browser):
bodies = create_squawks(browser, PAGE_SIZE + 1)
bodies = create_squawks(browser, PAGE_SIZE + 1, delay=1)

browser.visit(URL)
assert browser.is_text_not_present(bodies[0])
Expand Down Expand Up @@ -175,7 +176,7 @@ def test_next_not_present_on_last_page(browser):
@pytest.mark.score(5)
@pytest.mark.xfail
def test_pagination(browser):
bodies = create_squawks(browser, PAGE_SIZE + 1)
bodies = create_squawks(browser, PAGE_SIZE + 1, delay=1)

browser.visit(URL)
browser.find_by_xpath(NEXT_XPATH).first.click()
Expand Down