import os
import unittest
from flask import (abort, session,
                   redirect, request, g)

from taoblog import application as app
from taoblog.models import (ModelError, Base,
                            Session, bind_engine)
from taoblog.models.user import User
from taoblog.views.helpers import save_account_to_session, get_next_url


bind_engine('sqlite:///:memory:', echo=False)
app.config['ADMIN_EMAIL'] = ['author@taoblog.com',
                             'admin@taoblog.com']


class TaoblogTestCase(unittest.TestCase):
    def db_setup(self):
        Base.metadata.create_all()
        self.session = Session()

    def db_teardown(self):
        self.session.close()
        Base.metadata.drop_all()

    @property
    def app(self):
        if not hasattr(self, '_app') or self._app is None:
            self._app = app.test_client()
        return self._app

    def login(self, **data):
        data.setdefault('name', 'User')
        data.setdefault('provider', 'openid')
        data.setdefault('secret', 'a secret')
        data.setdefault('email', 'user@taoblog.com')
        data.setdefault('sid', 'sid')
        return self.app.post('/login/testing', data=data)

    def login_as_admin(self):
        return self.login(email=app.config['ADMIN_EMAIL'][0])

    def logout(self):
        return self.app.post('/logout/testing',
                             data={'sid': 'sid'})


def get_tests_root():
    return os.path.dirname(__file__)


@app.route('/login/testing', methods=['POST'])
def login_testing():
    account = None
    try:
        account = User(name=request.form.get('name'),
                       email=request.form.get('email'),
                       provider=request.form.get('provider'),
                       identity=request.form.get('identity'))
        account.id = 1
    except ModelError:
        abort(400)
    save_account_to_session(account, sid=request.form.get('sid'))
    return redirect(get_next_url())


@app.route('/logout/testing', methods=['GET', 'POST'])
def logout_testing():
    if g.is_login and request.values.get('sid') == session.get('sid'):
        session.clear()
    session.pop('token', None)
    session.pop('provider', None)
    return redirect(get_next_url())
@@ -425,23 +425,21 @@
 'author@
-taoblog
+email
 .com',%0A 
@@ -477,21 +477,36 @@
 min@
-taoblog.com'%5D
+email.com'%5D%0Aapp.debug = True
 %0A%0A%0Ac
@@ -951,17 +951,17 @@
 name', '
-U
+u
 ser')%0A  
@@ -999,14 +999,21 @@
 ', '
-openid
+user_provider
 ')%0A 
@@ -1040,26 +1040,33 @@
 lt('
-secret', 'a secret
+identity', 'user_identity
 ')%0A 
@@ -1103,23 +1103,21 @@
 , 'user@
-taoblog
+email
 .com')%0A 
@@ -1302,16 +1302,69 @@
 AIL'%5D%5B0%5D
+,%0A                          identity='admin_identity'
 )%0A%0A    d
@@ -1430,58 +1430,8 @@
 ing'
-,%0A                             data=%7B'sid': 'sid'%7D
 )%0A%0A%0A
@@ -1495,102 +1495,446 @@
 )%0A%0A%0A
-@app.route('/login/testing', methods=%5B'POST'%5D)%0Adef login_testing():%0A    account = None%0A    try
+from taoblog.models.user import UserOperator%0Auser_op = UserOperator(Session())%0Afrom flask import jsonify%0A%0A%0A@app.route('/login/testing', methods=%5B'POST'%5D)%0Adef login_testing():%0A    provider = request.form.get('provider')%0A    identity = request.form.get('identity')%0A    sid = request.form.get('sid')%0A    user = user_op.get_user_by_identity(provider=provider,%0A                                        identity=identity)%0A    if user is not None
 :%0A  
@@ -1939,23 +1939,100 @@
         
+save_
 account
+_to_session(user, sid=sid)%0A        return jsonify(request.form)%0A    user
  = User(
@@ -2078,23 +2078,16 @@
         
- 
-      
 email=re
@@ -2107,31 +2107,24 @@
 t('email'),%0A
-       
             
@@ -2174,39 +2174,32 @@
                 
- 
-      
 identity=request
@@ -2229,133 +2229,75 @@
     
-    account.id = 1%0A    except ModelError:%0A        abort(400)%0A    save_account_to_session(account, sid=request.form.get('
+user_op.create_user(user)%0A    save_account_to_session(user, sid=
 sid
-')
 )%0A  
@@ -2305,39 +2305,36 @@
  return 
-redirect(get_next_url()
+jsonify(request.form
 )%0A%0A%0A@app
@@ -2414,195 +2414,54 @@
     
-if g.is_login and request.values.get('sid') == session.get('sid'):%0A        session.clear()%0A    session.pop('token', None)%0A    session.pop('provider', None)%0A    return redirect(get_next_url()
+session.clear()%0A    return jsonify(%7B'stat': 'ok'%7D
 )

Store user when login for testing and return json