Exemplo n.º 1
0
 def test_change_context_uninitialized(self):
     """ Change the context on a session not initialized with a context """
     session = ConnectorSession(self.cr, self.uid)
     test_key = 'test_key'
     with session.change_context({test_key: 'value'}):
         self.assertEqual(session.context.get('test_key'), 'value')
     self.assertNotIn(test_key, session.context)
 def test_change_context_uninitialized(self):
     """ Change the context on a session not initialized with a context """
     session = ConnectorSession(self.cr, self.uid)
     test_key = "test_key"
     with session.change_context({test_key: "value"}):
         self.assertEqual(session.context.get("test_key"), "value")
     self.assertNotIn(test_key, session.context)
 def test_change_context_uninitialized(self):
     """ Change the context on a session not initialized with a context """
     session = ConnectorSession(self.cr, self.uid)
     test_key = 'test_key'
     with session.change_context({test_key: 'value'}):
         self.assertEqual(session.context.get('test_key'), 'value')
     self.assertNotIn(test_key, session.context)
Exemplo n.º 4
0
class test_connector_session(common.TransactionCase):
    """ Test ConnectorSession """

    def setUp(self):
        super(test_connector_session, self).setUp()
        self.context = {'lang': 'fr_FR'}
        self.session = ConnectorSession(self.cr,
                                        self.uid,
                                        context=self.context)

    def test_change_user(self):
        """
        Change the user and check if it is reverted correctly at the end
        """
        original_uid = self.session.uid
        new_uid = 2
        with self.session.change_user(new_uid):
            self.assertEqual(self.session.uid, new_uid)
        self.assertEqual(self.session.uid, original_uid)

    def test_model_with_transaction(self):
        """
        Create a session with a model name, we should be able to access
        the model from a transaction
        """
        res_users = self.registry('res.users').search_count(self.cr,
                                                            self.uid,
                                                            [])
        sess_res_users_obj = self.session.pool.get('res.users')
        sess_res_users = sess_res_users_obj.search_count(self.cr,
                                                         self.uid,
                                                         [])
        self.assertEqual(sess_res_users, res_users)

    def test_change_context(self):
        """
        Change the context and check if it is reverted correctly at the end
        """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: 'value'}):
            self.assertIn(test_key, self.session.context)
        self.assertNotIn(test_key, self.session.context)

        # change the context on a session not initialized with a context
        session = ConnectorSession(self.cr, self.uid)
        with session.change_context({test_key: 'value'}):
            self.assertIn(test_key, session.context)
        self.assertNotIn(test_key, session.context)
    def test_change_context(self):
        """
        Change the context and check if it is reverted correctly at the end
        """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: 'value'}):
            self.assertIn(test_key, self.session.context)
        self.assertNotIn(test_key, self.session.context)

        # change the context on a session not initialized with a context
        session = ConnectorSession(self.cr, self.uid)
        with session.change_context({test_key: 'value'}):
            self.assertIn(test_key, session.context)
        self.assertNotIn(test_key, session.context)
Exemplo n.º 6
0
    def test_change_context(self):
        """
        Change the context and check if it is reverted correctly at the end
        """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: 'value'}):
            self.assertIn(test_key, self.session.context)
        self.assertNotIn(test_key, self.session.context)

        # change the context on a session not initialized with a context
        session = ConnectorSession(self.cr, self.uid)
        with session.change_context({test_key: 'value'}):
            self.assertIn(test_key, session.context)
        self.assertNotIn(test_key, session.context)
Exemplo n.º 7
0
class test_connector_session(common.TransactionCase):
    """ Test ConnectorSession """
    def setUp(self):
        super(test_connector_session, self).setUp()
        self.context = {'lang': 'fr_FR'}
        self.session = ConnectorSession(self.cr,
                                        self.uid,
                                        context=self.context)

    def test_change_user(self):
        """
        Change the user and check if it is reverted correctly at the end
        """
        original_uid = self.session.uid
        new_uid = 2
        with self.session.change_user(new_uid):
            self.assertEqual(self.session.uid, new_uid)
        self.assertEqual(self.session.uid, original_uid)

    def test_model_with_transaction(self):
        """
        Create a session with a model name, we should be able to access
        the model from a transaction
        """
        res_users = self.registry('res.users').search_count(
            self.cr, self.uid, [])
        sess_res_users_obj = self.session.pool.get('res.users')
        sess_res_users = sess_res_users_obj.search_count(self.cr, self.uid, [])
        self.assertEqual(sess_res_users, res_users)

    def test_change_context(self):
        """
        Change the context and check if it is reverted correctly at the end
        """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: 'value'}):
            self.assertIn(test_key, self.session.context)
        self.assertNotIn(test_key, self.session.context)

        # change the context on a session not initialized with a context
        session = ConnectorSession(self.cr, self.uid)
        with session.change_context({test_key: 'value'}):
            self.assertIn(test_key, session.context)
        self.assertNotIn(test_key, session.context)
Exemplo n.º 8
0
class TestJobStorageMultiCompany(common.TransactionCase):
    """ Test storage of jobs """

    def setUp(self):
        super(TestJobStorageMultiCompany, self).setUp()
        self.session = ConnectorSession(self.cr, self.uid, context={})
        self.queue_job = self.env['queue.job']
        grp_connector_manager = self.ref("connector.group_connector_manager")
        User = self.env['res.users']
        Company = self.env['res.company']
        Partner = self.env['res.partner']
        self.other_partner_a = Partner.create(
            {"name": "My Company a",
             "is_company": True,
             "email": "*****@*****.**",
             })
        self.other_company_a = Company.create(
            {"name": "My Company a",
             "partner_id": self.other_partner_a.id,
             "rml_header1": "My Company Tagline",
             "currency_id": self.ref("base.EUR")
             })
        self.other_user_a = User.create(
            {"partner_id": self.other_partner_a.id,
             "company_id": self.other_company_a.id,
             "company_ids": [(4, self.other_company_a.id)],
             "login": "******",
             "name": "my user",
             "groups_id": [(4, grp_connector_manager)]
             })
        self.other_partner_b = Partner.create(
            {"name": "My Company b",
             "is_company": True,
             "email": "*****@*****.**",
             })
        self.other_company_b = Company.create(
            {"name": "My Company b",
             "partner_id": self.other_partner_b.id,
             "rml_header1": "My Company Tagline",
             "currency_id": self.ref("base.EUR")
             })
        self.other_user_b = User.create(
            {"partner_id": self.other_partner_b.id,
             "company_id": self.other_company_b.id,
             "company_ids": [(4, self.other_company_b.id)],
             "login": "******",
             "name": "my user 1",
             "groups_id": [(4, grp_connector_manager)]
             })

    def _create_job(self):
        self.cr.execute('delete from queue_job')
        job(task_a)
        task_a.delay(self.session, 'res.users')
        stored = self.queue_job.search([])
        self.assertEqual(len(stored), 1)
        return stored

    def test_job_default_company_id(self):
        """the default company is the one from the current user_id"""
        stored = self._create_job()
        self.assertEqual(stored.company_id.id,
                         self.ref("base.main_company"),
                         'Incorrect default company_id')
        with self.session.change_user(self.other_user_b.id):
            stored = self._create_job()
            self.assertEqual(stored.company_id.id,
                             self.other_company_b.id,
                             'Incorrect default company_id')

    def test_job_no_company_id(self):
        """ if we put an empty company_id in the context
         jobs are created without company_id"""
        with self.session.change_context({'company_id': None}):
            stored = self._create_job()
            self.assertFalse(stored.company_id,
                             ' Company_id should be empty')

    def test_job_specific_company_id(self):
        """If a company_id specified in the context
        it's used by default for the job creation"""
        s = self.session
        with s.change_context({'company_id': self.other_company_a.id}):
            stored = self._create_job()
            self.assertEqual(stored.company_id.id,
                             self.other_company_a.id,
                             'Incorrect company_id')

    def test_job_subscription(self):
        # if the job is created without company_id, all members of
        # connector.group_connector_manager must be followers
        User = self.env['res.users']
        with self.session.change_context(company_id=None):
            stored = self._create_job()
        stored._subscribe_users()
        users = User.search(
            [('groups_id', '=', self.ref('connector.group_connector_manager'))]
        )
        self.assertEqual(len(stored.message_follower_ids), len(users))
        expected_partners = [u.partner_id for u in users]
        self.assertSetEqual(
            set(stored.message_follower_ids.mapped('partner_id')),
            set(expected_partners))
        followers_id = stored.message_follower_ids.mapped('partner_id.id')
        self.assertIn(self.other_partner_a.id, followers_id)
        self.assertIn(self.other_partner_b.id, followers_id)
        # jobs created for a specific company_id are followed only by
        # company's members
        s = self.session
        with s.change_context(company_id=self.other_company_a.id):
            stored = self._create_job()
        stored.sudo(self.other_user_a.id)._subscribe_users()
        # 2 because admin + self.other_partner_a
        self.assertEqual(len(stored.message_follower_ids), 2)
        users = User.browse([SUPERUSER_ID, self.other_user_a.id])
        expected_partners = [u.partner_id for u in users]
        self.assertSetEqual(
            set(stored.message_follower_ids.mapped('partner_id')),
            set(expected_partners))
        followers_id = stored.message_follower_ids.mapped('partner_id.id')
        self.assertIn(self.other_partner_a.id, followers_id)
        self.assertNotIn(self.other_partner_b.id, followers_id)
class test_connector_session(common.TransactionCase):
    """ Test ConnectorSession """

    def setUp(self):
        super(test_connector_session, self).setUp()
        self.context = {"lang": "fr_FR"}
        self.session = ConnectorSession(self.cr, self.uid, context=self.context)

    def test_env(self):
        """ Check the session properties """
        session = self.session
        self.assertEqual(session.cr, session.env.cr)
        self.assertEqual(session.uid, session.env.uid)
        self.assertEqual(session.context, session.env.context)
        self.assertEqual(session.pool, session.env.registry)

    def test_from_env(self):
        """ ConnectorSession.from_env(env) """
        session = ConnectorSession.from_env(self.env)
        self.assertEqual(session.cr, self.env.cr)
        self.assertEqual(session.uid, self.env.uid)
        self.assertEqual(session.context, self.env.context)
        self.assertEqual(session.pool, self.env.registry)

    def test_change_user(self):
        """
        Change the user and check if it is reverted correctly at the end
        """
        original_uid = self.session.uid
        original_env = self.session.env
        new_uid = self.env.ref("base.user_demo").id
        with self.session.change_user(new_uid):
            # a new openerp.api.Environment is generated with the user
            self.assertNotEqual(self.session.env, original_env)
            self.assertEqual(self.session.uid, new_uid)
        self.assertEqual(self.session.env, original_env)
        self.assertEqual(self.session.uid, original_uid)

    def test_model_with_transaction(self):
        """ Use a method on a model from the pool """
        res_users = self.registry("res.users").search_count(self.cr, self.uid, [])
        sess_res_users_obj = self.session.pool.get("res.users")
        sess_res_users = sess_res_users_obj.search_count(self.cr, self.uid, [])
        self.assertEqual(sess_res_users, res_users)

    def test_new_model_with_transaction(self):
        """ Use a method on a model from the new api """
        res_users = self.env["res.users"].search_count([])
        sess_res_users_model = self.session.env["res.users"]
        sess_res_users = sess_res_users_model.search_count([])
        self.assertEqual(sess_res_users, res_users)

    def test_change_context(self):
        """ Change the context, it is reverted at the end """
        test_key = "test_key"
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: "value"}):
            self.assertEqual(self.session.context.get("test_key"), "value")
        self.assertNotIn(test_key, self.session.context)

    def test_change_context_keyword(self):
        """ Change the context by keyword, it is reverted at the end """
        test_key = "test_key"
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context(test_key="value"):
            self.assertEqual(self.session.context.get("test_key"), "value")
        self.assertNotIn(test_key, self.session.context)

    def test_change_context_uninitialized(self):
        """ Change the context on a session not initialized with a context """
        session = ConnectorSession(self.cr, self.uid)
        test_key = "test_key"
        with session.change_context({test_key: "value"}):
            self.assertEqual(session.context.get("test_key"), "value")
        self.assertNotIn(test_key, session.context)

    def test_is_module_installed(self):
        """ Test on an installed module """
        self.assertTrue(self.session.is_module_installed("connector"))

    def test_is_module_uninstalled(self):
        """ Test on an installed module """
        self.assertFalse(self.session.is_module_installed("lambda"))

    def test_is_module_installed_cache_not_propagated(self):
        """ Test if the cache is well different for the different modules """
        self.assertTrue(self.session.is_module_installed("connector"))
        self.assertFalse(self.session.is_module_installed("#dummy#"))

    def test_is_module_installed_cache_invalidation(self):
        """ Test on an invalidation of cache about installed modules """
        module = self.env["ir.module.module"]
        domain = [("name", "=", "base")]
        self.assertTrue(self.session.is_module_installed("base"))
        # only to check that the cache works, the in validation is done only
        # if the field state is modified by write method, UGLY but no other
        # solution
        self.env.cr.execute("UPDATE ir_module_module " "SET state='uninstalled' " "WHERE name='base'")
        self.assertTrue(self.session.is_module_installed("base"))
        module.search(domain).state = "uninstalled"
        self.assertFalse(self.session.is_module_installed("base"))
        module.search(domain).state = "installed"
        self.assertTrue(self.session.is_module_installed("base"))
Exemplo n.º 10
0
class TestJobStorageMultiCompany(common.TransactionCase):
    """ Test storage of jobs """

    def setUp(self):
        super(TestJobStorageMultiCompany, self).setUp()
        self.session = ConnectorSession(self.cr, self.uid, context={})
        self.queue_job = self.env['queue.job']
        grp_connector_manager = self.ref("connector.group_connector_manager")
        User = self.env['res.users']
        Company = self.env['res.company']
        Partner = self.env['res.partner']
        self.other_partner_a = Partner.create(
            {"name": "My Company a",
             "is_company": True,
             "email": "*****@*****.**",
             })
        self.other_company_a = Company.create(
            {"name": "My Company a",
             "partner_id": self.other_partner_a.id,
             "rml_header1": "My Company Tagline",
             "currency_id": self.ref("base.EUR")
             })
        self.other_user_a = User.create(
            {"partner_id": self.other_partner_a.id,
             "company_id": self.other_company_a.id,
             "company_ids": [(4, self.other_company_a.id)],
             "login": "******",
             "name": "my user",
             "groups_id": [(4, grp_connector_manager)]
             })
        self.other_partner_b = Partner.create(
            {"name": "My Company b",
             "is_company": True,
             "email": "*****@*****.**",
             })
        self.other_company_b = Company.create(
            {"name": "My Company b",
             "partner_id": self.other_partner_b.id,
             "rml_header1": "My Company Tagline",
             "currency_id": self.ref("base.EUR")
             })
        self.other_user_b = User.create(
            {"partner_id": self.other_partner_b.id,
             "company_id": self.other_company_b.id,
             "company_ids": [(4, self.other_company_b.id)],
             "login": "******",
             "name": "my user 1",
             "groups_id": [(4, grp_connector_manager)]
             })

    def _create_job(self):
        self.cr.execute('delete from queue_job')
        job(task_a)
        task_a.delay(self.session, 'res.users')
        stored = self.queue_job.search([])
        self.assertEqual(len(stored), 1)
        return stored

    def test_job_default_company_id(self):
        """the default company is the one from the current user_id"""
        stored = self._create_job()
        self.assertEqual(stored.company_id.id,
                         self.ref("base.main_company"),
                         'Incorrect default company_id')
        with self.session.change_user(self.other_user_b.id):
            stored = self._create_job()
            self.assertEqual(stored.company_id.id,
                             self.other_company_b.id,
                             'Incorrect default company_id')

    def test_job_no_company_id(self):
        """ if we put an empty company_id in the context
         jobs are created without company_id"""
        with self.session.change_context({'company_id': None}):
            stored = self._create_job()
            self.assertFalse(stored.company_id,
                             ' Company_id should be empty')

    def test_job_specific_company_id(self):
        """If a company_id specified in the context
        it's used by default for the job creation"""
        s = self.session
        with s.change_context({'company_id': self.other_company_a.id}):
            stored = self._create_job()
            self.assertEqual(stored.company_id.id,
                             self.other_company_a.id,
                             'Incorrect company_id')

    def test_job_subscription(self):
        # if the job is created without company_id, all members of
        # connector.group_connector_manager must be followers
        User = self.env['res.users']
        with self.session.change_context(company_id=None):
            stored = self._create_job()
        stored._subscribe_users()
        users = User.search(
            [('groups_id', '=', self.ref('connector.group_connector_manager'))]
        )
        self.assertEqual(len(stored.message_follower_ids), len(users))
        expected_partners = [u.partner_id for u in users]
        self.assertSetEqual(set(stored.message_follower_ids),
                            set(expected_partners))
        followers_id = [f.id for f in stored.message_follower_ids]
        self.assertIn(self.other_partner_a.id, followers_id)
        self.assertIn(self.other_partner_b.id, followers_id)
        # jobs created for a specific company_id are followed only by
        # company's members
        s = self.session
        with s.change_context(company_id=self.other_company_a.id):
            stored = self._create_job()
        stored.sudo(self.other_user_a.id)._subscribe_users()
        # 2 because admin + self.other_partner_a
        self.assertEqual(len(stored.message_follower_ids), 2)
        users = User.browse([SUPERUSER_ID, self.other_user_a.id])
        expected_partners = [u.partner_id for u in users]
        self.assertSetEqual(set(stored.message_follower_ids),
                            set(expected_partners))
        followers_id = [f.id for f in stored.message_follower_ids]
        self.assertIn(self.other_partner_a.id, followers_id)
        self.assertNotIn(self.other_partner_b.id, followers_id)
Exemplo n.º 11
0
class test_connector_session(common.TransactionCase):
    """ Test ConnectorSession """
    def setUp(self):
        super(test_connector_session, self).setUp()
        self.context = {'lang': 'fr_FR'}
        self.session = ConnectorSession(self.cr,
                                        self.uid,
                                        context=self.context)

    def test_env(self):
        """ Check the session properties """
        session = self.session
        self.assertEqual(session.cr, session.env.cr)
        self.assertEqual(session.uid, session.env.uid)
        self.assertEqual(session.context, session.env.context)
        self.assertEqual(session.pool, session.env.registry)

    def test_from_env(self):
        """ ConnectorSession.from_env(env) """
        session = ConnectorSession.from_env(self.env)
        self.assertEqual(session.cr, self.env.cr)
        self.assertEqual(session.uid, self.env.uid)
        self.assertEqual(session.context, self.env.context)
        self.assertEqual(session.pool, self.env.registry)

    def test_change_user(self):
        """
        Change the user and check if it is reverted correctly at the end
        """
        original_uid = self.session.uid
        original_env = self.session.env
        new_uid = self.env.ref('base.user_demo').id
        with self.session.change_user(new_uid):
            # a new openerp.api.Environment is generated with the user
            self.assertNotEqual(self.session.env, original_env)
            self.assertEqual(self.session.uid, new_uid)
        self.assertEqual(self.session.env, original_env)
        self.assertEqual(self.session.uid, original_uid)

    def test_model_with_transaction(self):
        """ Use a method on a model from the pool """
        res_users = self.registry('res.users').search_count(
            self.cr, self.uid, [])
        sess_res_users_obj = self.session.pool.get('res.users')
        sess_res_users = sess_res_users_obj.search_count(self.cr, self.uid, [])
        self.assertEqual(sess_res_users, res_users)

    def test_new_model_with_transaction(self):
        """ Use a method on a model from the new api """
        res_users = self.env['res.users'].search_count([])
        sess_res_users_model = self.session.env['res.users']
        sess_res_users = sess_res_users_model.search_count([])
        self.assertEqual(sess_res_users, res_users)

    def test_change_context(self):
        """ Change the context, it is reverted at the end """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: 'value'}):
            self.assertEqual(self.session.context.get('test_key'), 'value')
        self.assertNotIn(test_key, self.session.context)

    def test_change_context_keyword(self):
        """ Change the context by keyword, it is reverted at the end """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context(test_key='value'):
            self.assertEqual(self.session.context.get('test_key'), 'value')
        self.assertNotIn(test_key, self.session.context)

    def test_change_context_uninitialized(self):
        """ Change the context on a session not initialized with a context """
        session = ConnectorSession(self.cr, self.uid)
        test_key = 'test_key'
        with session.change_context({test_key: 'value'}):
            self.assertEqual(session.context.get('test_key'), 'value')
        self.assertNotIn(test_key, session.context)

    def test_is_module_installed(self):
        """ Test on an installed module """
        self.assertTrue(self.session.is_module_installed('connector'))

    def test_is_module_uninstalled(self):
        """ Test on an installed module """
        self.assertFalse(self.session.is_module_installed('lambda'))

    def test_is_module_installed_cache_not_propagated(self):
        """ Test if the cache is well different for the different modules """
        self.assertTrue(self.session.is_module_installed('connector'))
        self.assertFalse(self.session.is_module_installed('#dummy#'))
class test_connector_session(common.TransactionCase):
    """ Test ConnectorSession """

    def setUp(self):
        super(test_connector_session, self).setUp()
        self.context = {'lang': 'fr_FR'}
        self.session = ConnectorSession(self.cr,
                                        self.uid,
                                        context=self.context)

    def test_env(self):
        """ Check the session properties """
        session = self.session
        self.assertEqual(session.cr, session.env.cr)
        self.assertEqual(session.uid, session.env.uid)
        self.assertEqual(session.context, session.env.context)
        self.assertEqual(session.pool, session.env.registry)

    def test_from_env(self):
        """ ConnectorSession.from_env(env) """
        session = ConnectorSession.from_env(self.env)
        self.assertEqual(session.cr, self.env.cr)
        self.assertEqual(session.uid, self.env.uid)
        self.assertEqual(session.context, self.env.context)
        self.assertEqual(session.pool, self.env.registry)

    def test_change_user(self):
        """
        Change the user and check if it is reverted correctly at the end
        """
        original_uid = self.session.uid
        original_env = self.session.env
        new_uid = self.env.ref('base.user_demo').id
        with self.session.change_user(new_uid):
            # a new openerp.api.Environment is generated with the user
            self.assertNotEqual(self.session.env, original_env)
            self.assertEqual(self.session.uid, new_uid)
        self.assertEqual(self.session.env, original_env)
        self.assertEqual(self.session.uid, original_uid)

    def test_model_with_transaction(self):
        """ Use a method on a model from the pool """
        res_users = self.registry('res.users').search_count(self.cr,
                                                            self.uid,
                                                            [])
        sess_res_users_obj = self.session.pool.get('res.users')
        sess_res_users = sess_res_users_obj.search_count(self.cr,
                                                         self.uid,
                                                         [])
        self.assertEqual(sess_res_users, res_users)

    def test_new_model_with_transaction(self):
        """ Use a method on a model from the new api """
        res_users = self.env['res.users'].search_count([])
        sess_res_users_model = self.session.env['res.users']
        sess_res_users = sess_res_users_model.search_count([])
        self.assertEqual(sess_res_users, res_users)

    def test_change_context(self):
        """ Change the context, it is reverted at the end """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context({test_key: 'value'}):
            self.assertEqual(self.session.context.get('test_key'), 'value')
        self.assertNotIn(test_key, self.session.context)

    def test_change_context_keyword(self):
        """ Change the context by keyword, it is reverted at the end """
        test_key = 'test_key'
        self.assertNotIn(test_key, self.session.context)
        with self.session.change_context(test_key='value'):
            self.assertEqual(self.session.context.get('test_key'), 'value')
        self.assertNotIn(test_key, self.session.context)

    def test_change_context_uninitialized(self):
        """ Change the context on a session not initialized with a context """
        session = ConnectorSession(self.cr, self.uid)
        test_key = 'test_key'
        with session.change_context({test_key: 'value'}):
            self.assertEqual(session.context.get('test_key'), 'value')
        self.assertNotIn(test_key, session.context)

    def test_is_module_installed(self):
        """ Test on an installed module """
        self.assertTrue(self.session.is_module_installed('connector'))

    def test_is_module_uninstalled(self):
        """ Test on an installed module """
        self.assertFalse(self.session.is_module_installed('lambda'))

    def test_is_module_installed_cache_not_propagated(self):
        """ Test if the cache is well different for the different modules """
        self.assertTrue(self.session.is_module_installed('connector'))
        self.assertFalse(self.session.is_module_installed('#dummy#'))
Exemplo n.º 13
0
class test_job_storage_multi_company(common.TransactionCase):
    """ Test storage of jobs """
    def setUp(self):
        super(test_job_storage_multi_company, self).setUp()
        self.pool = openerp.modules.registry.RegistryManager.get(common.DB)
        self.session = ConnectorSession(self.cr, self.uid, context={})
        self.queue_job = self.registry('queue.job')
        grp_connector_manager = self.ref("connector.group_connector_manager")
        User = self.registry('res.users')
        Company = self.registry('res.company')
        Partner = self.registry('res.partner')
        self.other_partner_id_a = Partner.create(
            self.cr, self.uid, {
                "name": "My Company a",
                "is_company": True,
                "email": "*****@*****.**",
            })
        self.other_company_id_a = Company.create(
            self.cr, self.uid, {
                "name": "My Company a",
                "partner_id": self.other_partner_id_a,
                "rml_header1": "My Company Tagline",
                "currency_id": self.ref("base.EUR")
            })
        self.other_user_id_a = User.create(
            self.cr, self.uid, {
                "partner_id": self.other_partner_id_a,
                "company_id": self.other_company_id_a,
                "company_ids": [(4, self.other_company_id_a)],
                "login": "******",
                "name": "my user",
                "groups_id": [(4, grp_connector_manager)]
            })
        self.other_partner_id_b = Partner.create(
            self.cr, self.uid, {
                "name": "My Company b",
                "is_company": True,
                "email": "*****@*****.**",
            })
        self.other_company_id_b = Company.create(
            self.cr, self.uid, {
                "name": "My Company b",
                "partner_id": self.other_partner_id_b,
                "rml_header1": "My Company Tagline",
                "currency_id": self.ref("base.EUR")
            })
        self.other_user_id_b = User.create(
            self.cr, self.uid, {
                "partner_id": self.other_partner_id_b,
                "company_id": self.other_company_id_b,
                "company_ids": [(4, self.other_company_id_b)],
                "login": "******",
                "name": "my user 1",
                "groups_id": [(4, grp_connector_manager)]
            })

    def _create_job(self):
        self.cr.execute('delete from queue_job')
        job(task_a)
        task_a.delay(self.session, 'res.users')
        stored = self.queue_job.search(self.cr, self.uid, [])
        self.assertEqual(len(stored), 1)
        return self.queue_job.browse(self.cr, self.uid, stored[0])

    def test_job_default_company_id(self):
        """the default company is the one from the current user_id"""
        stored_brw = self._create_job()
        self.assertEqual(stored_brw.company_id.id,
                         self.ref("base.main_company"),
                         'Incorrect default company_id')
        with self.session.change_user(self.other_user_id_b):
            stored_brw = self._create_job()
            self.assertEqual(stored_brw.company_id.id, self.other_company_id_b,
                             'Incorrect default company_id')

    def test_job_no_company_id(self):
        """ if we put an empty company_id in the context
         jobs are created without company_id"""
        with self.session.change_context({'company_id': None}):
            stored_brw = self._create_job()
            self.assertFalse(stored_brw.company_id,
                             ' Company_id should be empty')

    def test_job_specific_company_id(self):
        """If a company_id specified in the context
        it's used by default for the job creation"""
        s = self.session
        with s.change_context({'company_id': self.other_company_id_a}):
            stored_brw = self._create_job()
            self.assertEqual(stored_brw.company_id.id, self.other_company_id_a,
                             'Incorrect company_id')

    def test_job_subscription(self):
        # if the job is created without company_id, all members of
        # connector.group_connector_manager must be followers
        User = self.registry("res.users")
        with self.session.change_context({'company_id': None}):
            stored_brw = self._create_job()
        self.queue_job._subscribe_users(self.cr, self.uid, [stored_brw.id])
        stored_brw.refresh()
        user_ids = User.search(self.cr, self.uid, [
            ('groups_id', '=', self.ref('connector.group_connector_manager'))
        ])
        self.assertEqual(len(stored_brw.message_follower_ids), len(user_ids))
        users = self.registry("res.users").browse(self.cr, self.uid, user_ids)
        expected_partners = [u.partner_id for u in users]
        self.assertSetEqual(set(stored_brw.message_follower_ids),
                            set(expected_partners))
        followers_id = [f.id for f in stored_brw.message_follower_ids]
        self.assertIn(self.other_partner_id_a, followers_id)
        self.assertIn(self.other_partner_id_b, followers_id)
        # jobs created for a specific company_id are followed only by
        # company's members
        s = self.session
        with s.change_context({'company_id': self.other_company_id_a}):
            stored_brw = self._create_job()
        self.queue_job._subscribe_users(self.cr, self.other_user_id_a,
                                        [stored_brw.id])
        stored_brw.refresh()
        # 2 because admin + self.other_partner_id_a
        self.assertEqual(len(stored_brw.message_follower_ids), 2)
        users = User.browse(self.cr, self.uid,
                            [SUPERUSER_ID, self.other_user_id_a])
        expected_partners = [u.partner_id for u in users]
        self.assertSetEqual(set(stored_brw.message_follower_ids),
                            set(expected_partners))
        followers_id = [f.id for f in stored_brw.message_follower_ids]
        self.assertIn(self.other_partner_id_a, followers_id)
        self.assertNotIn(self.other_partner_id_b, followers_id)
class test_job_storage_multi_company(common.TransactionCase):
    """ Test storage of jobs """

    def setUp(self):
        super(test_job_storage_multi_company, self).setUp()
        self.pool = openerp.modules.registry.RegistryManager.get(common.DB)
        self.session = ConnectorSession(self.cr, self.uid, context={})
        self.queue_job = self.registry('queue.job')
        self.other_partner_id_a = self.registry('res.partner').create(self.cr, self.uid, {
                            "name": "My Company a",
                            "is_company": True,
                            "email": "*****@*****.**",
                            })
        self.other_company_id_a = self.registry('res.company').create(self.cr, self.uid, {
                            "name": "My Company a",
                            "partner_id": self.other_partner_id_a,
                            "rml_header1": "My Company Tagline",
                            "currency_id": self.ref("base.EUR")
                            })
        self.other_user_id_a = self.registry('res.users').create(self.cr, self.uid, {
                            "partner_id": self.other_partner_id_a,
                            "company_id": self.other_company_id_a,
                            "company_ids": [(4, self.other_company_id_a)],
                            "login": "******",
                            "name": "my user",
                            "groups_id": [
                                                  (4, self.ref("connector.group_connector_manager"))]
                            })
        self.other_partner_id_b = self.registry('res.partner').create(self.cr, self.uid, {
                            "name": "My Company b",
                            "is_company": True,
                            "email": "*****@*****.**",
                            })
        self.other_company_id_b = self.registry('res.company').create(self.cr, self.uid, {
                            "name": "My Company b",
                            "partner_id": self.other_partner_id_b,
                            "rml_header1": "My Company Tagline",
                            "currency_id": self.ref("base.EUR")
                            })
        self.other_user_id_b = self.registry('res.users').create(self.cr, self.uid, {
                            "partner_id": self.other_partner_id_b,
                            "company_id": self.other_company_id_b,
                            "company_ids": [(4, self.other_company_id_b)],
                            "login": "******",
                            "name": "my user 1",
                            "groups_id": [
                                                  (4, self.ref("connector.group_connector_manager"))]
                            })

    def _create_job(self):
        self.cr.execute('delete from queue_job')
        job(task_a)
        task_a.delay(self.session, 'res.users')
        stored = self.queue_job.search(self.cr, self.uid, [])
        self.assertEqual(len(stored), 1)
        return self.queue_job.browse(self.cr, self.uid, stored[0])

    def test_job_default_company_id(self):
        """the default company is the one from the current user_id"""
        stored_brw = self._create_job()
        self.assertEqual(
                         stored_brw.company_id.id,
                         self.ref("base.main_company"),
                         'Incorrect default company_id')
        with self.session.change_user(self.other_user_id_b):
            stored_brw = self._create_job()
            self.assertEqual(
                             stored_brw.company_id.id,
                             self.other_company_id_b,
                             'Incorrect default company_id')

    def test_job_no_company_id(self):
        """ if we put an empty company_id in the context
         jobs are created without company_id"""
        with self.session.change_context({'company_id': None}):
            stored_brw = self._create_job()
            self.assertFalse(
                             stored_brw.company_id,
                             ' Company_id should be empty')

    def test_job_specific_company_id(self):
        """If a company_id specified in the context
        it's used by default for the job creation"""
        with self.session.change_context({'company_id': self.other_company_id_a}):
            stored_brw = self._create_job()
            self.assertEqual(
                             stored_brw.company_id.id,
                             self.other_company_id_a,
                             'Incorrect company_id')

    def test_job_subscription(self):
        # if the job is created without company_id, all members of
        # connector.group_connector_manager must be followers
        with self.session.change_context({'company_id': None}):
            stored_brw = self._create_job()
        self.queue_job. _subscribe_users(self.cr, self.uid, [stored_brw.id])
        stored_brw.refresh()
        user_ids = self.registry('res.users').search(
                self.cr, self.uid, [('groups_id', '=', self.ref('connector.group_connector_manager'))])
        self.assertEqual(len(stored_brw.message_follower_ids), len(user_ids))
        expected_partners = [u.partner_id for u in self.registry("res.users").browse(self.cr, self.uid, user_ids)]
        self.assertSetEqual(set(stored_brw.message_follower_ids), set(expected_partners))
        followers_id = [f.id for f in stored_brw.message_follower_ids]
        self.assertIn(self.other_partner_id_a, followers_id)
        self.assertIn(self.other_partner_id_b, followers_id)
        # jobs created for a specific company_id are followed only by company's members
        with self.session.change_context({'company_id': self.other_company_id_a}):
            stored_brw = self._create_job()
        self.queue_job. _subscribe_users(self.cr, self.other_user_id_a, [stored_brw.id])
        stored_brw.refresh()
        self.assertEqual(len(stored_brw.message_follower_ids), 2)  # 2 because admin + self.other_partner_id_a
        expected_partners = [u.partner_id for u in self.registry("res.users").browse(self.cr, self.uid, [SUPERUSER_ID, self.other_user_id_a])]
        self.assertSetEqual(set(stored_brw.message_follower_ids), set(expected_partners))
        followers_id = [f.id for f in stored_brw.message_follower_ids]
        self.assertIn(self.other_partner_id_a, followers_id)
        self.assertNotIn(self.other_partner_id_b, followers_id)