Ejemplo n.º 1
0
    def test_syncdb_sqlite(self):
        "Test syncdb works properly wih sqlite"

        # creating a two level nested folder structure
        tmp = gettempdir()
        random_dir_1 = 'test_my_app-%s' % ''.join(map(str,random.sample(range(100),5)))
        random_dir_2 = 'test_my_app-%s' % ''.join(map(str,random.sample(range(100),5)))
        db_file_name = 'testapp.db'
        abs_path = os.path.join(tmp, random_dir_1, random_dir_2, db_file_name)

        # set the db path
        self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///%s' % abs_path

        # The path must not exists before the syncdb
        self.assertFalse(os.path.exists(abs_path))

        # launch the syncdb
        syncdb(self.app)

        # The path must exists after the syncdb
        self.assertTrue(os.path.exists(abs_path))

        # Trying with in-memory db
        self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
        syncdb(self.app)
Ejemplo n.º 2
0
    def test_user_no_bcrypt(self):
        """Test the user creation in the case of no bcrypt is passed
            to the create_flask_app"""

        from assentio import db, create_flask_app
        from assentio.manage import _syncdb as syncdb

        # Passing only the db param
        args = {"db": db}

        class TestingConfig(object):
            TESTING = True
            CSRF_ENABLED = False
            SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"

        # Creating a local app and create a user
        local_app = create_flask_app(config_object=TestingConfig, **args)
        syncdb(local_app)

        self.create_user("test_nobcrypt", "test", local_app)

        with local_app.app_context():
            user = User.query.filter_by(username="******").first()
            # Assert the user is created
            self.assertTrue(user)
            # Assert the password is stored as plaintext
            self.assertEqual(user.password, "test")
Ejemplo n.º 3
0
    def test_portlet(self):
        "Check portlets engine"
        
        db = self.app.extensions['sqlalchemy'].db 
        blog_app = self.app.extensions['blog']

        # defining a TestPortlet Type
        class TestPortlet(BasePortlet, db.Model):
            pass

        # Updating the portlets
        blog_app.update_portlets(TestPortlet)

        # Resync the db so the new table is created
        syncdb(self.app)

        # Tryng to instantiate a BasePortlet, it's not possible
        with self.assertRaises(RuntimeError):
            BasePortlet()

        # Let's try again
        test_portlet = TestPortlet() 
        test_portlet.title = 'Test Portlet'
        test_portlet.save(self.app)

        # Let's create a new TestPortlet instance
        test_portlet_2 = TestPortlet()
        test_portlet_2.title = 'Test Portlet 2'

        # check the type 
        self.assertEqual(test_portlet_2.type, 'testportlet')

        # and the string representation
        self.assertEqual(test_portlet_2.__repr__(), '<testportlet "Test Portlet 2">')

        # check the type is in the extension registry
        self.assertIn(test_portlet_2.type, self.app.extensions['blog_portlets'])

        # check get_template raise a NotImplementedError
        with self.assertRaises(NotImplementedError):
            test_portlet_2.get_template()
Ejemplo n.º 4
0
    def test_slots(self):
        "Check slots working"
        
        # Login as anonymous
        ctx = self._fake_user_context()

        db = self.app.extensions['sqlalchemy'].db 
        blog_app = self.app.extensions['blog']

        # Create a new kind of porlet
        class MyPortlet(BasePortlet, db.Model):
            pass

        # Updating the portlets
        blog_app.update_portlets(MyPortlet)

        syncdb(self.app)

        # Create a Slot an portlet related with it
        test_slot = PortletSlot()
        test_slot.name = "slot"

        portlet = MyPortlet()
        portlet.title = 'My Portlet'
        portlet.slot = test_slot
        portlet.order = 2

        portlet2 = MyPortlet()
        portlet2.title = 'My Portlet 2'
        portlet2.slot = test_slot
        portlet2.order = 1 

        db.session.add(test_slot)
        db.session.add(portlet)
        db.session.add(portlet2)
        db.session.commit()

        portlets = blog_app.get_portlets_by_slot('another_slot')
        
        # There are no porlets on that slot
        self.assertEqual(len(portlets), 0)

        # so, check for our slot
        portlets = blog_app.get_portlets_by_slot('slot')
        
        # Now there're 2 portlets, but we are anoymous so we expect 0
        self.assertEqual(len(portlets), 0)

        # so let's authenticate
        ctx.pop()
        ctx = self._fake_user_context(as_admin=True)
        
        portlets = blog_app.get_portlets_by_slot('slot', ordered=True)

        # and now there are 2
        self.assertEqual(len(portlets), 2)

        # Check they're ordered
        self.assertEqual(portlets[0].title, 'My Portlet 2')
        self.assertEqual(portlets[1].title, 'My Portlet')

        ctx.pop()