Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

renames & match's unittest

  • Loading branch information...
commit 3efe8f62e3c14fbc968afbb40b7834b871c3f1d6 1 parent e786c58
@jaramir authored
View
12 houserule/__init__.py
@@ -22,14 +22,10 @@
def load_user( id ):
return User.query.filter_by( id=id ).first()
-if os.environ.get( "DEBUG", "0" ) == "1":
- app.config["DEBUG"] = True
-
-app.config["SQLALCHEMY_DATABASE_URI"] = \
- os.environ.get( "HEROKU_SHARED_POSTGRESQL_ORANGE_URL", "sqlite://" )
+app.config["SQLALCHEMY_DATABASE_URI"] = os.environ["HEROKU_SHARED_POSTGRESQL_ORANGE_URL"]
db = SQLAlchemy( app )
-from model import *
-from view import *
-from form import *
+from models import *
+from views import *
+from forms import *
View
5 houserule/form.py → houserule/forms.py
@@ -5,7 +5,7 @@
from flaskext.wtf import TextField, PasswordField, SubmitField, BooleanField
from flaskext.wtf.html5 import EmailField
-from model import User
+from models import User
from houserule import bcrypt
class Unique( object ):
@@ -78,3 +78,6 @@ class LoginForm( Form ):
class BGGTestForm( Form ):
username = TextField( "Nome utente BGG" )
submit = SubmitField( "Elenca giochi" )
+
+class MatchForm( Form ):
+ submit = SubmitField( "Salva" )
View
15 houserule/model.py → houserule/models.py
@@ -18,3 +18,18 @@ def __init__( self, username, password, email ):
self.username = username
self.password = bcrypt.generate_password_hash( password )
self.email = email
+
+class Game( db.Model ):
+ __tablename__ = "games"
+
+ id = db.Column( db.Integer, primary_key=True )
+ bgg_id = db.Column( db.Integer )
+ name = db.Column( db.String( 150 ) )
+ thumbnail_url = db.Column( db.String( 150 ) )
+
+class Match( db.Model ):
+ __tablename__ = "matches"
+
+ id = db.Column( db.Integer, primary_key=True )
+ organizer_id = db.Column( db.Integer, db.ForeignKey( "users.id" ) )
+ game_id = db.Column( db.Integer, db.ForeignKey( "games.id" ) )
View
24 houserule/view.py → houserule/views.py
@@ -3,10 +3,11 @@
from houserule import app, db
from flask import render_template, request, redirect, url_for, flash, send_from_directory
-from form import RegistrationForm, LoginForm, BGGTestForm
-from model import User
from flaskext.login import login_user, logout_user, login_required, current_user
+
import pyBGG
+import forms
+import models
@app.route( "/" )
def splash():
@@ -18,9 +19,9 @@ def index():
@app.route( "/register", methods=( "GET", "POST" ) )
def register():
- form = RegistrationForm()
+ form = forms.RegistrationForm()
if form.validate_on_submit():
- user = User( form.username.data, form.password.data, form.email.data )
+ user = models.User( form.username.data, form.password.data, form.email.data )
db.session.add( user )
db.session.commit()
flash( "Grazie per esserti registrato!" )
@@ -29,9 +30,9 @@ def register():
@app.route( "/login", methods=( "GET", "POST" ) )
def login():
- form = LoginForm()
+ form = forms.LoginForm()
if form.validate_on_submit():
- user = User.query.filter_by( username=form.username.data ).first()
+ user = models.User.query.filter_by( username=form.username.data ).first()
login_user( user, remember=form.remember.data )
flash( "Bentornato %s!" % user.username )
return redirect( request.args.get( "next" ) or url_for( "index" ) )
@@ -47,8 +48,17 @@ def logout():
@app.route( "/bggtest", methods=( "GET", "POST" ) )
@login_required
def bggtest():
- form = BGGTestForm()
+ form = forms.BGGTestForm()
collection = []
if form.validate_on_submit():
collection = pyBGG.collection( form.username.data, own=True, prefetch=True )
return render_template( "bggtest.html", form=form, collection=collection )
+
+@app.route( "/match", methods=( "GET", "POST" ) )
+@login_required
+def match():
+ form = forms.MatchForm()
+ if form.validate_on_submit():
+ flash( "Grazie per aver proposto una nuova partita!" )
+ return redirect( url_for( "index" ) )
+ return render_template( "match.html", form=form )
View
18 run.py
@@ -1,18 +0,0 @@
-#!/usr/bin/python
-# coding: utf-8
-
-from houserule import app, db
-import argparse
-import os
-
-if __name__ == "__main__":
- parser = argparse.ArgumentParser( description = "HouseRule Development Server" )
- parser.add_argument( "--initdb", action="store_true", help="Create initial database" )
- args = parser.parse_args()
-
- if args.initdb:
- db.create_all()
-
- port = int( os.environ.get( "PORT", "5000" ) )
- app.run( "0.0.0.0", port )
-
View
22 run_local_server.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+# coding: utf-8
+
+import argparse
+import os
+
+os.environ["HEROKU_SHARED_POSTGRESQL_ORANGE_URL"] = "sqlite:///../db.sqlite"
+os.environ["SECRET_KEY"] = "development server not so secret key"
+
+port = int( os.environ.get( "PORT", "5000" ) )
+
+parser = argparse.ArgumentParser( description = "HouseRule Development Server" )
+parser.add_argument( "--initdb", action="store_true", help="Create initial database" )
+parser.add_argument( "--debug", action="store_true", help="Start with debugging and reloading" )
+args = parser.parse_args()
+
+from houserule import app, db
+
+if args.initdb:
+ db.create_all()
+
+app.run( "0.0.0.0", port, debug=args.debug )
View
3  templates/index.html
@@ -7,6 +7,9 @@
<a href="{{ url_for( "bggtest" ) }}">BoardGameGeek Test</a>
</li>
<li>
+ <a href="{{ url_for( "match" ) }}">Organizza una partita</a>
+ </li>
+ <li>
<a href="{{ url_for( "logout" ) }}">Esci</a>
</li>
{% else %}
View
10 templates/match.html
@@ -0,0 +1,10 @@
+{% extends "layout.html" %}
+{% from "_formhelpers.html" import render_field %}
+
+{% block body %}
+ <form method="POST" action="{{ url_for( "bggtest" ) }}">
+ {{ form.hidden_tag() }}
+ {{ form.submit }}
+ </form>
+
+{% endblock %}
View
58 test.py
@@ -1,15 +1,25 @@
#!/usr/bin/python
# coding: utf-8
+import os
+os.environ["SECRET_KEY"] = "unittest"
+
import unittest
import houserule
+houserule.app.config["TESTING"] = True
+houserule.app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
+houserule.app.config["CSRF_ENABLED"] = False
+
class TestApp( unittest.TestCase ):
def setUp( self ):
- houserule.app.config["TESTING"] = True
+ houserule.db.create_all()
self.app = houserule.app.test_client()
+ def tearDown( self ):
+ houserule.db.drop_all()
+
def test_home_page_works( self ):
r = self.app.get( '/' )
self.assertTrue( r.data )
@@ -19,8 +29,50 @@ def test_404_page( self ):
r = self.app.get( '/i-am-not-found/' )
self.assertEquals( r.status_code, 404 )
- def test_user_loader( self ):
- self.assertEqual( self.app.load_user( "nonesiste" ), None )
+ def test_register( self ):
+ data = {
+ "username": "ciccio",
+ "email": "ciccio@slack.it",
+ "password": "cicciociccio",
+ "verify": "cicciociccio",
+ }
+ r = self.app.post( "/register", data=data, follow_redirects=True )
+ self.assertIn( "Grazie", r.data )
+
+ def test_register_unique_username( self ):
+ self.test_register()
+ self.assertRaises( self.failureException, self.test_register )
+
+ def test_login( self ):
+ self.test_register()
+ data = {
+ "username": "ciccio",
+ "password": "cicciociccio",
+ }
+ r = self.app.post( "/login", data=data, follow_redirects=True )
+ self.assertIn( "Bentornato ciccio", r.data )
+
+ def test_ciccio_is_the_one( self ):
+ self.test_register()
+ users = houserule.models.User.query.all()
+ self.assertEqual( len( users ), 1 )
+ self.assertEqual( users[0].id, 1 )
+ self.assertEqual( users[0].username, "ciccio" )
+
+ def test_match_requires_login( self ):
+ r = self.app.post( "/match", data={} )
+ self.assertEqual( "401 UNAUTHORIZED", r.status )
+
+ def test_create_match( self ):
+ self.test_login()
+ data = {
+ "bgg_id": "421",
+ "name": "1830: Railways & Robber Barons",
+ "thumbnail": "http://cf.geekdo-images.com/images/pic882119_t.jpg",
+ "user_id": "1",
+ }
+ r = self.app.post( "/match", data=data, follow_redirects=True )
+ self.assertIn( "Grazie", r.data )
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.