def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) self.db = self.env.get_db_cnx() setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment(self.db) self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1'] cursor = self.db.cursor() # Populate table with initial test data, not synced with tickets yet. cursor.execute(""" INSERT INTO tags (tagspace, name, tag) VALUES ('ticket', '1', 'deleted')""") self.realm = 'ticket' self._create_ticket(self.tags) self.req = Mock() # Mock an anonymous request. self.req.perm = PermissionCache(self.env)
def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment() self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1', 'tag2'] # Populate tables with initial test data. self._create_ticket(self.tags) # Mock an anonymous request. self.anon_req = Mock() self.anon_req.perm = PermissionCache(self.env) self.req = Mock(authname='editor') self.req.authname = 'editor' self.req.perm = PermissionCache(self.env, username='******')
def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) self.db = self.env.get_db_cnx() setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment(self.db) self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1', 'tag2'] cursor = self.db.cursor() # Populate tables with initial test data. self._create_ticket(self.tags) # Mock an anonymous request. self.anon_req = Mock() self.anon_req.perm = PermissionCache(self.env) self.req = Mock(authname='editor') self.req.authname = 'editor' self.req.perm = PermissionCache(self.env, username='******')
def test_available_providers(self): # Standard implementations of DefaultTagProvider should be registered. seen = [] for provider in [TicketTagProvider(self.env), WikiTagProvider(self.env)]: self.failIf(provider not in self.tag_s.tag_providers) # Ensure unique provider references, a possible bug in Trac-0.11. self.failIf(provider in seen) seen.append(provider)
def upgrade_environment(self, db): """Each schema version should have its own upgrade module, named upgrades/dbN.py, where 'N' is the version number (int). """ db_mgr = DatabaseManager(self.env) schema_ver = self.get_schema_version(db) cursor = db.cursor() # Is this a new installation? if not schema_ver: # Perform a single-step install: Create plugin schema and # insert default data into the database. connector = db_mgr._get_connector()[0] for table in db_default.schema: for stmt in connector.to_sql(table): cursor.execute(stmt) for table, cols, vals in db_default.get_data(db): cursor.executemany( "INSERT INTO %s (%s) VALUES (%s)" % (table, ','.join(cols), ','.join(['%s' for c in cols])), vals) else: # Perform incremental upgrades. for i in range(schema_ver + 1, db_default.schema_version + 1): name = 'db%i' % i try: upgrades = __import__('tractags.upgrades', globals(), locals(), [name]) script = getattr(upgrades, name) except AttributeError: raise TracError( _( "No upgrade module for version %(num)i " "(%(version)s.py)", num=i, version=name)) script.do_upgrade(self.env, i, cursor) cursor.execute( """ UPDATE system SET value=%s WHERE name='tags_version' """, (db_default.schema_version, )) self.log.info("Upgraded TracTags db schema from version %d to %d" % (schema_ver, db_default.schema_version)) TicketTagProvider(self.env)._fetch_tkt_tags(db) self.log.info("Synchronized ticket attributes to tags table") db.commit()
class TicketTagProviderTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment() self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1', 'tag2'] # Populate tables with initial test data. self._create_ticket(self.tags) def tearDown(self): self.env.shutdown() shutil.rmtree(self.env.path) # Helpers def _create_ticket(self, tags, **kwargs): ticket = Ticket(self.env) ticket['keywords'] = u' '.join(sorted(map(to_unicode, tags))) ticket['summary'] = 'summary' ticket['reporter'] = 'admin' for name, value in kwargs.iteritems(): ticket[name] = value ticket.insert() return ticket def _revert_tractags_schema_init(self): with self.env.db_transaction as db: db("DROP TABLE IF EXISTS tags") db("DROP TABLE IF EXISTS tags_change") db("DELETE FROM system WHERE name='tags_version'") db("DELETE FROM permission WHERE action %s" % db.like(), ('TAGS_%', )) def _tags(self): tags = {} for name, tag in self.env.db_query(""" SELECT name,tag FROM tags """): if name in tags: tags[name].add(tag) else: tags[name] = set([tag]) return tags # Tests def test_get_tagged_resources(self): # No tags, no restrictions, all resources. req = MockRequest(self.env, authname='editor') self.assertEquals( [r for r in self.provider.get_tagged_resources(req, None)][0][1], set(self.tags)) # Force fine-grained perm-check check for all tags, not just the one # from query. self.provider.fast_permcheck = False self.assertEquals([ r for r in self.provider.get_tagged_resources( req, set(self.tags[:1])) ][0][1], set(self.tags)) def test_get_tags(self): req = MockRequest(self.env, authname='editor') resource = Resource('ticket', 2) self.assertRaises(ResourceNotFound, self.provider.get_resource_tags, req, resource) self._create_ticket(self.tags) self.assertEquals( [tag for tag in self.provider.get_resource_tags(req, resource)], self.tags) def test_set_tags(self): req = MockRequest(self.env, authname='editor') tags = ['tag3'] ticket = Ticket(self.env, 1) ticket['keywords'] = tags[0] # Tags get updated by TicketChangeListener method. ticket.save_changes(req.authname) self.assertEquals(self.tag_sys.get_all_tags(req).keys(), tags) def test_remove_tags(self): req = MockRequest(self.env, authname='editor') anon_req = MockRequest(self.env, authname='anonymous') resource = Resource('ticket', 1) # Anonymous lacks required permissions. self.assertRaises(PermissionError, self.provider.remove_resource_tags, anon_req, resource) # Shouldn't raise an error with appropriate permission. self.provider.remove_resource_tags(req, resource, 'comment') ticket = Ticket(self.env, 1) self.assertEquals(ticket['keywords'], '') def test_describe_tagged_resource(self): req = MockRequest(self.env, authname='editor') resource = Resource('ticket', 1) self.assertEquals( self.provider.describe_tagged_resource(req, resource), 'defect: summary') def test_create_ticket_by_anonymous(self): req = MockRequest(self.env, authname='editor') ticket = self._create_ticket(self.tags, reporter='anonymous') tags = self.provider.get_resource_tags(req, ticket.resource) self.assertEquals(tags, set(self.tags)) def test_update_ticket_by_anonymous(self): req = MockRequest(self.env, authname='editor') ticket = self._create_ticket([]) tags = self.provider.get_resource_tags(req, ticket.resource) self.assertEquals(tags, set([])) ticket['keywords'] = ', '.join(self.tags) ticket.save_changes('anonymous', comment='Adding keywords') tags = self.provider.get_resource_tags(req, ticket.resource) self.assertEquals(tags, set(self.tags))
class TicketTagProviderTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) self.db = self.env.get_db_cnx() setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment(self.db) self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1'] cursor = self.db.cursor() # Populate table with initial test data, not synced with tickets yet. cursor.execute(""" INSERT INTO tags (tagspace, name, tag) VALUES ('ticket', '1', 'deleted')""") self.realm = 'ticket' self._create_ticket(self.tags) self.req = Mock() # Mock an anonymous request. self.req.perm = PermissionCache(self.env) def tearDown(self): self.db.close() # Really close db connections. self.env.shutdown() shutil.rmtree(self.env.path) # Helpers def _create_ticket(self, tags): ticket = Ticket(self.env) ticket['keywords'] = u' '.join(sorted(map(to_unicode, tags))) ticket['summary'] = 'summary' ticket.insert() def _revert_tractags_schema_init(self): cursor = self.db.cursor() cursor.execute("DROP TABLE IF EXISTS tags") cursor.execute("DELETE FROM system WHERE name='tags_version'") cursor.execute( "DELETE FROM permission WHERE action %s" % self.db.like(), ('TAGS_%', )) def _tags(self): tags = {} cursor = self.db.cursor() cursor.execute("SELECT name,tag FROM tags") for name, tag in cursor.fetchall(): if name in tags: tags[name].add(tag) else: tags[name] = set([tag]) return tags # Tests def test_get_tags(self): resource = Resource('ticket', 2) self.assertRaises(ResourceNotFound, self.provider.get_resource_tags, self.req, resource) self._create_ticket(self.tags) self.assertEquals([ tag for tag in self.provider.get_resource_tags(self.req, resource) ], self.tags) #ignore_closed_tickets def test_set_tags(self): resource = Resource('ticket', 1) self.tags = ['tag2'] # Anonymous lacks required permissions. self.assertRaises(PermissionError, self.provider.set_resource_tags, self.req, resource, self.tags) self.req.authname = 'user' self.req.perm = PermissionCache(self.env, username='******') # Shouldn't raise an error with appropriate permission. self.provider.set_resource_tags(self.req, resource, self.tags) self.assertEquals( self.tag_sys.get_all_tags(self.req, '').keys(), self.tags) def test_remove_tags(self): resource = Resource('ticket', 1) # Anonymous lacks required permissions. self.assertRaises(PermissionError, self.provider.remove_resource_tags, self.req, resource) self.req.authname = 'user' self.req.perm = PermissionCache(self.env, username='******') # Shouldn't raise an error with appropriate permission. self.provider.remove_resource_tags(self.req, resource, 'comment') tkt = Ticket(self.env, 1) self.assertEquals(tkt['keywords'], '') def test_describe_tagged_resource(self): resource = Resource('ticket', 1) self.assertEquals( self.provider.describe_tagged_resource(self.req, resource), 'defect: summary')
class TicketTagProviderTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) self.db = self.env.get_db_cnx() setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment(self.db) self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1', 'tag2'] cursor = self.db.cursor() # Populate tables with initial test data. self._create_ticket(self.tags) # Mock an anonymous request. self.anon_req = Mock() self.anon_req.perm = PermissionCache(self.env) self.req = Mock(authname='editor') self.req.authname = 'editor' self.req.perm = PermissionCache(self.env, username='******') def tearDown(self): self.db.close() # Really close db connections. self.env.shutdown() shutil.rmtree(self.env.path) # Helpers def _create_ticket(self, tags, **kwargs): ticket = Ticket(self.env) ticket['keywords'] = u' '.join(sorted(map(to_unicode, tags))) ticket['summary'] = 'summary' ticket['reporter'] = 'admin' for name, value in kwargs.iteritems(): ticket[name] = value ticket.insert() return ticket def _revert_tractags_schema_init(self): cursor = self.db.cursor() cursor.execute("DROP TABLE IF EXISTS tags") cursor.execute("DROP TABLE IF EXISTS tags_change") cursor.execute("DELETE FROM system WHERE name='tags_version'") cursor.execute("DELETE FROM permission WHERE action %s" % self.db.like(), ('TAGS_%',)) def _tags(self): tags = {} cursor = self.db.cursor() cursor.execute("SELECT name,tag FROM tags") for name, tag in cursor: if name in tags: tags[name].add(tag) else: tags[name] = set([tag]) return tags # Tests def test_get_tagged_resources(self): # No tags, no restrictions, all resources. self.assertEquals( [r for r in self.provider.get_tagged_resources(self.req, None)][0][1], set(self.tags)) # Force fine-grained perm-check check for all tags, not just the one # from query. self.provider.fast_permcheck = False self.assertEquals( [r for r in self.provider.get_tagged_resources(self.req, set(self.tags[:1]))][0][1], set(self.tags)) def test_get_tags(self): resource = Resource('ticket', 2) self.assertRaises(ResourceNotFound, self.provider.get_resource_tags, self.req, resource) self._create_ticket(self.tags) self.assertEquals( [tag for tag in self.provider.get_resource_tags(self.req, resource)], self.tags) #ignore_closed_tickets def test_set_tags(self): tags = ['tag3'] ticket = Ticket(self.env, 1) ticket['keywords'] = tags[0] # Tags get updated by TicketChangeListener method. ticket.save_changes(self.req.authname) self.assertEquals(self.tag_sys.get_all_tags(self.req).keys(), tags) def test_remove_tags(self): resource = Resource('ticket', 1) # Anonymous lacks required permissions. self.assertRaises(PermissionError, self.provider.remove_resource_tags, self.anon_req, resource) # Shouldn't raise an error with appropriate permission. self.provider.remove_resource_tags(self.req, resource, 'comment') ticket = Ticket(self.env, 1) self.assertEquals(ticket['keywords'], '') def test_describe_tagged_resource(self): resource = Resource('ticket', 1) self.assertEquals( self.provider.describe_tagged_resource(self.req, resource), 'defect: summary') def test_create_ticket_by_anonymous(self): ticket = self._create_ticket(self.tags, reporter='anonymous') tags = self.provider.get_resource_tags(self.req, ticket.resource) self.assertEquals(tags, set(self.tags)) def test_update_ticket_by_anonymous(self): ticket = self._create_ticket([]) tags = self.provider.get_resource_tags(self.req, ticket.resource) self.assertEquals(tags, set([])) ticket['keywords'] = ', '.join(self.tags) ticket.save_changes('anonymous', comment='Adding keywords') tags = self.provider.get_resource_tags(self.req, ticket.resource) self.assertEquals(tags, set(self.tags))
class TicketTagProviderTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(default_data=True, enable=['trac.*', 'tractags.*']) self.env.path = tempfile.mkdtemp() self.perms = PermissionSystem(self.env) self.db = self.env.get_db_cnx() setup = TagSetup(self.env) # Current tractags schema is setup with enabled component anyway. # Revert these changes for getting default permissions inserted. self._revert_tractags_schema_init() setup.upgrade_environment(self.db) self.provider = TicketTagProvider(self.env) self.realm = 'ticket' self.tag_sys = TagSystem(self.env) self.tags = ['tag1'] cursor = self.db.cursor() # Populate table with initial test data, not synced with tickets yet. cursor.execute(""" INSERT INTO tags (tagspace, name, tag) VALUES ('ticket', '1', 'deleted')""") self.realm = 'ticket' self._create_ticket(self.tags) self.req = Mock() # Mock an anonymous request. self.req.perm = PermissionCache(self.env) def tearDown(self): self.db.close() # Really close db connections. self.env.shutdown() shutil.rmtree(self.env.path) # Helpers def _create_ticket(self, tags): ticket = Ticket(self.env) ticket['keywords'] = u' '.join(sorted(map(to_unicode, tags))) ticket['summary'] = 'summary' ticket.insert() def _revert_tractags_schema_init(self): cursor = self.db.cursor() cursor.execute("DROP TABLE IF EXISTS tags") cursor.execute("DELETE FROM system WHERE name='tags_version'") cursor.execute("DELETE FROM permission WHERE action %s" % self.db.like(), ('TAGS_%',)) def _tags(self): tags = {} cursor = self.db.cursor() cursor.execute("SELECT name,tag FROM tags") for name, tag in cursor.fetchall(): if name in tags: tags[name].add(tag) else: tags[name] = set([tag]) return tags # Tests def test_get_tags(self): resource = Resource('ticket', 2) self.assertRaises(ResourceNotFound, self.provider.get_resource_tags, self.req, resource) self._create_ticket(self.tags) self.assertEquals( [tag for tag in self.provider.get_resource_tags(self.req, resource)], self.tags) #ignore_closed_tickets def test_set_tags(self): resource = Resource('ticket', 1) self.tags = ['tag2'] # Anonymous lacks required permissions. self.assertRaises(PermissionError, self.provider.set_resource_tags, self.req, resource, self.tags) self.req.authname = 'user' self.req.perm = PermissionCache(self.env, username='******') # Shouldn't raise an error with appropriate permission. self.provider.set_resource_tags(self.req, resource, self.tags) self.assertEquals(self.tag_sys.get_all_tags(self.req, '').keys(), self.tags) def test_remove_tags(self): resource = Resource('ticket', 1) # Anonymous lacks required permissions. self.assertRaises(PermissionError, self.provider.remove_resource_tags, self.req, resource) self.req.authname = 'user' self.req.perm = PermissionCache(self.env, username='******') # Shouldn't raise an error with appropriate permission. self.provider.remove_resource_tags(self.req, resource, 'comment') tkt = Ticket(self.env, 1) self.assertEquals(tkt['keywords'], '') def test_describe_tagged_resource(self): resource = Resource('ticket', 1) self.assertEquals( self.provider.describe_tagged_resource(self.req, resource), 'defect: summary')