# 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
class TestDatastoresController(TestController):

    def setUp(self):
        meta.Session.query(DataStore).delete()

    def tearDown(self):
        meta.Session.rollback()
 
    def _insert_datastores(self):
        ds1 = DataStore('datastore1', 'postgis', 'pgsql://toto')
        ds2 = DataStore('datastore2', 'path', 'file://my/path')
        meta.Session.add_all([ds1, ds2])
        meta.Session.flush()
        return ds1.id, ds2.id

    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


# 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

class TestDatastoresController(TestController):

    def setUp(self):
        meta.Session.query(DataStore).delete()

    def tearDown(self):
        meta.Session.rollback()
 
    def _insert_datastores(self):
        ds1 = DataStore('datastore1', 'postgis', 'pgsql://toto')
        ds2 = DataStore('datastore2', 'path', 'file://my/path')
        meta.Session.add_all([ds1, ds2])
        meta.Session.flush()
        return ds1.id, ds2.id

    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):