# Path: studio/tests/lib/helpers.py
# def log_in(app, login, password):
#     response = app.get(url('/login'),
#             params={
#                 'login': login,
#                 'password': password,
#                 })
#     assert response.status.startswith('302 ')
# 
# def log_out(app):
#     response = app.get(url('/logout'))
#     assert response.status.startswith('302 ')


import simplejson 
from studio.tests import *
from studio.model import *
from studio.tests.lib.helpers import log_in, log_out
    def test_index(self):
        self._insert_datastores()
        log_in(self.app, 'admin', 'password')
        response = self.app.get(url('datastores'))
        assert response.response.content_type == 'application/json'
        response = simplejson.loads(response.response._body)
        assert isinstance(response, dict)
        r = response['datastores']
        assert isinstance(r, list)
        assert len(r) == 2
        log_out(self.app)
        # test anonymous user
        response = self.app.get(url('datastores'), status=302)
        assert response.location.find('signin') >= 0

    def test_create(self):
        params = {'name': 'datastore3',
                  'type': 'postgis',
                  'ogrstring': 'pgsql://titi'}
        log_in(self.app, 'admin', 'password')
        response = self.app.post(url('datastores'),
                                 params = simplejson.dumps(params),
                                 content_type='application/json')
        results = meta.Session.query(DataStore).all()
        assert len(results) == 1
        assert results[0].name == 'datastore3'
        assert results[0].type == 'postgis'
        assert results[0].ogrstring == 'pgsql://titi'
        log_out(self.app)
        # test forbidden user

# Path: studio/tests/lib/helpers.py
# def log_in(app, login, password):
#     response = app.get(url('/login'),
#             params={
#                 'login': login,
#                 'password': password,
#                 })
#     assert response.status.startswith('302 ')
# 
# def log_out(app):
#     response = app.get(url('/logout'))
#     assert response.status.startswith('302 ')


# Path: studio/tests/functional/test_datastores.py
import simplejson 
from studio.tests import *
from studio.model import *
from studio.tests.lib.helpers import log_in, log_out

    def test_index(self):
        self._insert_datastores()
        log_in(self.app, 'admin', 'password')
        response = self.app.get(url('datastores'))
        assert response.response.content_type == 'application/json'
        response = simplejson.loads(response.response._body)
        assert isinstance(response, dict)
        r = response['datastores']
        assert isinstance(r, list)
        assert len(r) == 2
        log_out(self.app)
        # test anonymous user
        response = self.app.get(url('datastores'), status=302)
        assert response.location.find('signin') >= 0

    def test_create(self):
        params = {'name': 'datastore3',
                  'type': 'postgis',
                  'ogrstring': 'pgsql://titi'}
        log_in(self.app, 'admin', 'password')
        response = self.app.post(url('datastores'),
                                 params = simplejson.dumps(params),
                                 content_type='application/json')
        results = meta.Session.query(DataStore).all()
        assert len(results) == 1
        assert results[0].name == 'datastore3'
        assert results[0].type == 'postgis'
        assert results[0].ogrstring == 'pgsql://titi'
        log_out(self.app)
        # test forbidden user

        log_in(self.app, 'enduser', 'password')