from flask import Flask, g
import unittest
import json
from simplekv.fs import FilesystemStore
from flaskext.kvsession import KVSessionExtension
from datetime import datetime, timedelta

from api import app, db
from common.database import Database

class APITest(unittest.TestCase):

    def setUp(self):
        global db
        store = FilesystemStore('session')
        KVSessionExtension(store, app)
        
        # Load the debug config
        app.config.from_pyfile('../config.defaults.py')
        app.config.from_pyfile('../config_debug.py')
        app.secret_key = app.config['SECRET_KEY']
        db = Database(app.config)

        self._setup_database()
        app.testing = True
        self.app = app.test_client(use_cookies=True)


    """Setup the database
        by clearing it and loading the schema"""
    def _setup_database(self):
        con = db.get_connection()
        cur = con.cursor()

        cur.execute(open('schema.sql', 'r').read())
        con.commit()
        
        db.put_away_connection(con)

    # Make the csrf_token test and make it expire in a min
    def _setup_csrf(self):
        with self.app.session_transaction() as sess:
            sess['csrf'] = 'test'
            sess['csrf_expire'] = datetime.now() + timedelta(minutes=1) 

    def test_1_api_base(self):
        rv = self.app.get('/api/')
        data = json.loads(rv.data)
        assert data['status']['code'] is 0
        assert data['csrf_token']

    def test_2_api_create_user(self):
        data = json.dumps(dict(
            csrf_token='test',
            email='test@example.com',
            password='test'))
        
        self._setup_csrf()
        
        rv = self.app.post('/api/signup/', data=data,
            content_type='application/json')

        data = json.loads(rv.data)
        assert data['status']['code'] is 0
        assert data['csrf_token']

if __name__ == '__main__':
    unittest.main()

@@ -204,16 +204,56 @@
 app, db%0A
+from api.functions import register_user%0A
 from com
@@ -281,16 +281,16 @@
 atabase%0A
-
 %0Aclass A
@@ -1320,16 +1320,29 @@
 es=1) %0A%0A
+    # Test /%0A
     def 
@@ -1512,16 +1512,50 @@
 oken'%5D%0A%0A
+    # Test registering a new user%0A
     def 
@@ -1560,16 +1560,17 @@
 f test_2
+a
 _api_cre
@@ -1931,32 +1931,32 @@
 '%5D%5B'code'%5D is 0%0A
-
         assert d
@@ -1974,16 +1974,561 @@
 oken'%5D%0A%0A
+    # Test that we can't register a user twice%0A    def test_2b_api_create_user(self):%0A        register_user('test@example.com', 'test')%0A        data = json.dumps(dict(%0A            csrf_token='test',%0A            email='test@example.com',%0A            password='test'))%0A        %0A        self._setup_csrf()%0A        %0A        rv = self.app.post('/api/signup/', data=data,%0A            content_type='application/json')%0A%0A        data = json.loads(rv.data)%0A        assert data%5B'status'%5D%5B'code'%5D is 5%0A        assert data%5B'csrf_token'%5D%0A        assert True%0A%0A
 if __nam

Add register user twice check