class TracAdminTestCase(TracAdminTestCaseBase): def setUp(self): self.env = EnvironmentStub(default_data=True) self.admin = TracAdmin() self.admin.env_set('', self.env) def tearDown(self): self.env.reset_db() self.env = None def test_permission_list_ok(self): """Tests the 'permission list' command in trac-admin.""" rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_list_includes_undefined_actions(self): """Undefined actions are included in the User Action table, but not in the Available Actions list. """ self.env.disable_component(trac.search.web_ui.SearchModule) rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_add_one_action_ok(self): """ Tests the 'permission add' command in trac-admin. This particular test passes valid arguments to add one permission and checks for success. """ self.execute('permission add test_user WIKI_VIEW') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_add_multiple_actions_ok(self): """ Tests the 'permission add' command in trac-admin. This particular test passes valid arguments to add multiple permissions and checks for success. """ self.execute('permission add test_user LOG_VIEW FILE_VIEW') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_add_already_exists(self): """ Tests the 'permission add' command in trac-admin. This particular test passes a permission that already exists and checks for the message. Other permissions passed are added. """ rv, output = self.execute('permission add anonymous WIKI_CREATE ' 'WIKI_VIEW WIKI_MODIFY') self.assertEqual(0, rv, output) rv, output2 = self.execute('permission list') self.assertEqual(0, rv, output2) self.assertExpectedResult(output + output2) def test_permission_add_subject_already_in_group(self): """ Tests the 'permission add' command in trac-admin. This particular test passes a group that the subject is already a member of and checks for the message. Other permissions passed are added. """ rv, output1 = self.execute('permission add user1 group2') self.assertEqual(0, rv, output1) rv, output2 = self.execute('permission add user1 group1 group2 ' 'group3') self.assertEqual(0, rv, output2) rv, output3 = self.execute('permission list') self.assertEqual(0, rv, output3) self.assertExpectedResult(output2 + output3) def test_permission_add_differs_from_action_by_casing(self): """ Tests the 'permission add' command in trac-admin. This particular test passes a permission that differs from an action by casing and checks for the message. None of the permissions in the list are granted. """ rv, output = self.execute('permission add joe WIKI_CREATE ' 'Trac_Admin WIKI_MODIFY') self.assertEqual(2, rv, output) rv, output2 = self.execute('permission list') self.assertEqual(0, rv, output2) self.assertExpectedResult(output + output2) def test_permission_add_unknown_action(self): """ Tests the 'permission add' command in trac-admin. This particular test tries granting NOT_A_PERM to a user. NOT_A_PERM does not exist in the system. None of the permissions in the list are granted. """ rv, output = self.execute('permission add joe WIKI_CREATE ' 'NOT_A_PERM WIKI_MODIFY') self.assertEqual(2, rv, output) rv, output2 = self.execute('permission list') self.assertEqual(0, rv, output2) self.assertExpectedResult(output + output2) def test_permission_remove_one_action_ok(self): """ Tests the 'permission remove' command in trac-admin. This particular test passes valid arguments to remove one permission and checks for success. """ self.execute('permission remove anonymous TICKET_MODIFY') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_multiple_actions_ok(self): """ Tests the 'permission remove' command in trac-admin. This particular test passes valid arguments to remove multiple permission and checks for success. """ self.execute('permission remove anonymous WIKI_CREATE WIKI_MODIFY') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_all_actions_for_user(self): """ Tests the 'permission remove' command in trac-admin. This particular test removes all permissions for anonymous. """ self.execute('permission remove anonymous *') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_action_for_all_users(self): """ Tests the 'permission remove' command in trac-admin. This particular test removes the TICKET_CREATE permission from all users. """ self.execute('permission add anonymous TICKET_CREATE') self.execute('permission remove * TICKET_CREATE') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_unknown_user(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing a permission from an unknown user. """ rv, output = self.execute('permission remove joe TICKET_VIEW') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_action_not_granted(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing TICKET_CREATE from user anonymous, who doesn't have that permission. """ rv, output = self.execute('permission remove anonymous TICKET_CREATE') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_action_granted_through_meta_permission(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing WIKI_VIEW from a user. WIKI_VIEW has been granted through user anonymous.""" self.execute('permission add joe TICKET_VIEW') rv, output = self.execute('permission remove joe WIKI_VIEW') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_unknown_action(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing NOT_A_PERM from a user. NOT_A_PERM does not exist in the system.""" rv, output = self.execute('permission remove joe NOT_A_PERM') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_unknown_action_granted(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing NOT_A_PERM from a user. NOT_A_PERM does not exist in the system, but the user possesses the permission.""" self.env.db_transaction( """ INSERT INTO permission VALUES (%s, %s) """, ('joe', 'NOT_A_PERM')) rv, output = self.execute('permission remove joe NOT_A_PERM') self.assertEqual(0, rv, output) rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_export_ok(self): """ Tests the 'permission export' command in trac-admin. This particular test exports the default permissions to stdout. """ rv, output = self.execute('permission export') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_import_ok(self): """ Tests the 'permission import' command in trac-admin. This particular test exports additional permissions, removes them and imports them back. """ user = u'test_user\u0250' self.execute('permission add ' + user + ' WIKI_VIEW') self.execute('permission add ' + user + ' TICKET_VIEW') rv, output = self.execute('permission export') self.execute('permission remove ' + user + ' *') rv, output = self.execute('permission import', input=output) self.assertEqual(0, rv, output) self.assertEqual('', output) rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output)
class TracadminTestCase(TracAdminTestCaseBase): """ Tests the output of trac-admin and is meant to be used with .../trac/tests.py. """ def setUp(self): self.env = EnvironmentStub(default_data=True, enable=('trac.*', ), disable=('trac.tests.*', )) self.admin = TracAdmin() self.admin.env_set('', self.env) def tearDown(self): self.env = None @property def datetime_format_hint(self): return get_datetime_format_hint(get_console_locale(self.env)) def test_python_with_optimizations_returns_error(self): """Error is returned when a command is executed in interpreter with optimizations enabled. """ with Popen((sys.executable, '-O', '-m', 'trac.admin.console', 'help'), stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=close_fds) as proc: stdout, stderr = proc.communicate(input='') self.assertEqual(2, proc.returncode) self.assertEqual("Python with optimizations is not supported.", stderr.strip()) # Help test def test_help_ok(self): """ Tests the 'help' command in trac-admin. Since the 'help' command has no command arguments, it is hard to call it incorrectly. As a result, there is only this one test. """ rv, output = self.execute('help') self.assertEqual(0, rv, output) self.assertExpectedResult( output, { 'version': self.env.trac_version, 'date_format_hint': get_date_format_hint() }) self.assertTrue(all(len(line) < 80 for line in output.split('\n')), "Lines should be less than 80 characters in length.") # Locale test def _test_get_console_locale_with_babel(self): from babel.core import Locale, UnknownLocaleError locales = get_available_locales() en_US = Locale.parse('en_US') de = Locale.parse('de') def unset_locale_envs(): for name in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): if name in os.environ: del os.environ[name] if 'de' in locales: unset_locale_envs() self.assertIsNone(get_console_locale(None, None)) self.assertEqual(de, get_console_locale(None, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') self.assertEqual(de, get_console_locale(self.env, None)) self.assertEqual(de, get_console_locale(self.env, 'C')) self.env.config.set('trac', 'default_language', 'en_US') self.assertEqual(en_US, get_console_locale(self.env, None)) self.assertEqual(en_US, get_console_locale(self.env, 'C')) self.assertEqual(de, get_console_locale(self.env, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') os.environ['LANG'] = 'POSIX' # unavailable locale in Trac self.assertIsNone(get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LANG'] = '****' # invalid locale self.assertIsNone(get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LANG'] = 'en_US.utf-8' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) os.environ['LC_MESSAGES'] = 'de_DE.utf-8' self.assertEqual(de, get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) os.environ['LC_ALL'] = 'en_US.utf-8' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) os.environ['LANGUAGE'] = 'de_DE:en_US:en' self.assertEqual(de, get_console_locale()) self.assertEqual(de, get_console_locale(self.env)) if not locales: # compiled catalog is missing unset_locale_envs() self.assertIsNone(get_console_locale(None, 'de_DE.UTF8')) self.env.config.set('trac', 'default_language', 'de') self.assertIsNone(get_console_locale(self.env, None)) self.assertIsNone(get_console_locale(self.env, 'C')) self.assertIsNone(get_console_locale(self.env, 'de_DE.UTF8')) os.environ['LANG'] = 'en_US.utf-8' os.environ['LC_MESSAGES'] = 'de_DE.utf-8' os.environ['LC_ALL'] = 'en_US.utf-8' os.environ['LANGUAGE'] = 'de_DE:en_US' self.assertEqual(en_US, get_console_locale()) self.assertEqual(en_US, get_console_locale(self.env)) def _test_get_console_locale_without_babel(self): os.environ['LANG'] = 'en_US.utf-8' os.environ['LC_MESSAGES'] = 'de_DE.utf-8' os.environ['LC_ALL'] = 'en_US.utf-8' os.environ['LANGUAGE'] = 'de_DE:en_US' self.assertIsNone(get_console_locale(None, 'en_US.UTF8')) self.env.config.set('trac', 'default_language', '') self.assertIsNone(get_console_locale(self.env, 'en_US.UTF8')) self.assertIsNone(get_console_locale(self.env)) self.env.config.set('trac', 'default_language', 'en_US') self.assertIsNone(get_console_locale(self.env, 'en_US.UTF8')) self.assertIsNone(get_console_locale(self.env)) if has_babel: test_get_console_locale = _test_get_console_locale_with_babel else: test_get_console_locale = _test_get_console_locale_without_babel # Attachment tests def test_attachment_list_empty(self): """ Tests the 'attachment list' command in trac-admin, on a wiki page that doesn't have any attachments. """ # FIXME: Additional tests should be written for the other 'attachment' # commands. This requires being able to control the current # time, which in turn would require centralizing the time # provider, for example in the environment object. rv, output = self.execute('attachment list wiki:WikiStart') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_attachment_add_nonexistent_resource(self): """Tests the 'attachment add' command in trac-admin, on a non-existent resource.""" rv, output = self.execute('attachment add wiki:NonExistentPage "%s"' % __file__) self.assertEqual(2, rv, output) self.assertExpectedResult(output) # Config tests def test_config_get(self): """ Tests the 'config get' command in trac-admin. This particular test gets the project name from the config. """ self.env.config.set('project', 'name', 'Test project') rv, output = self.execute('config get project name') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_config_set(self): """ Tests the 'config set' command in trac-admin. This particular test sets the project name using an option value containing a space. """ rv, output = self.execute('config set project name "Test project"') self.assertEqual(0, rv, output) self.assertExpectedResult(output) self.assertEqual('Test project', self.env.config.get('project', 'name')) def test_config_remove(self): """ Tests the 'config remove' command in trac-admin. This particular test removes the project name from the config, therefore reverting the option to the default value. """ self.env.config.set('project', 'name', 'Test project') rv, output = self.execute('config remove project name') self.assertEqual(0, rv, output) self.assertExpectedResult(output) self.assertEqual('My Project', self.env.config.get('project', 'name')) # Permission tests def test_permission_list_ok(self): """Tests the 'permission list' command in trac-admin.""" rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_list_includes_undefined_actions(self): """Undefined actions are included in the User Action table, but not in the Available Actions list. """ self.env.disable_component(trac.search.web_ui.SearchModule) rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_add_one_action_ok(self): """ Tests the 'permission add' command in trac-admin. This particular test passes valid arguments to add one permission and checks for success. """ self.execute('permission add test_user WIKI_VIEW') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_add_multiple_actions_ok(self): """ Tests the 'permission add' command in trac-admin. This particular test passes valid arguments to add multiple permissions and checks for success. """ self.execute('permission add test_user LOG_VIEW FILE_VIEW') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_add_already_exists(self): """ Tests the 'permission add' command in trac-admin. This particular test passes a permission that already exists and checks for the message. Other permissions passed are added. """ rv, output = self.execute('permission add anonymous WIKI_CREATE ' 'WIKI_VIEW WIKI_MODIFY') self.assertEqual(0, rv, output) rv, output2 = self.execute('permission list') self.assertEqual(0, rv, output2) self.assertExpectedResult(output + output2) def test_permission_add_subject_already_in_group(self): """ Tests the 'permission add' command in trac-admin. This particular test passes a group that the subject is already a member of and checks for the message. Other permissions passed are added. """ rv, output1 = self.execute('permission add user1 group2') self.assertEqual(0, rv, output1) rv, output2 = self.execute('permission add user1 group1 group2 ' 'group3') self.assertEqual(0, rv, output2) rv, output3 = self.execute('permission list') self.assertEqual(0, rv, output3) self.assertExpectedResult(output2 + output3) def test_permission_add_differs_from_action_by_casing(self): """ Tests the 'permission add' command in trac-admin. This particular test passes a permission that differs from an action by casing and checks for the message. None of the permissions in the list are granted. """ rv, output = self.execute('permission add joe WIKI_CREATE ' 'Trac_Admin WIKI_MODIFY') self.assertEqual(2, rv, output) rv, output2 = self.execute('permission list') self.assertEqual(0, rv, output2) self.assertExpectedResult(output + output2) def test_permission_add_unknown_action(self): """ Tests the 'permission add' command in trac-admin. This particular test tries granting NOT_A_PERM to a user. NOT_A_PERM does not exist in the system. None of the permissions in the list are granted. """ rv, output = self.execute('permission add joe WIKI_CREATE ' 'NOT_A_PERM WIKI_MODIFY') self.assertEqual(2, rv, output) rv, output2 = self.execute('permission list') self.assertEqual(0, rv, output2) self.assertExpectedResult(output + output2) def test_permission_remove_one_action_ok(self): """ Tests the 'permission remove' command in trac-admin. This particular test passes valid arguments to remove one permission and checks for success. """ self.execute('permission remove anonymous TICKET_MODIFY') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_multiple_actions_ok(self): """ Tests the 'permission remove' command in trac-admin. This particular test passes valid arguments to remove multiple permission and checks for success. """ self.execute('permission remove anonymous WIKI_CREATE WIKI_MODIFY') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_all_actions_for_user(self): """ Tests the 'permission remove' command in trac-admin. This particular test removes all permissions for anonymous. """ self.execute('permission remove anonymous *') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_action_for_all_users(self): """ Tests the 'permission remove' command in trac-admin. This particular test removes the TICKET_CREATE permission from all users. """ self.execute('permission add anonymous TICKET_CREATE') self.execute('permission remove * TICKET_CREATE') rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_remove_unknown_user(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing a permission from an unknown user. """ rv, output = self.execute('permission remove joe TICKET_VIEW') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_action_not_granted(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing TICKET_CREATE from user anonymous, who doesn't have that permission. """ rv, output = self.execute('permission remove anonymous TICKET_CREATE') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_action_granted_through_meta_permission(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing WIKI_VIEW from a user. WIKI_VIEW has been granted through user anonymous.""" self.execute('permission add joe TICKET_VIEW') rv, output = self.execute('permission remove joe WIKI_VIEW') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_unknown_action(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing NOT_A_PERM from a user. NOT_A_PERM does not exist in the system.""" rv, output = self.execute('permission remove joe NOT_A_PERM') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_permission_remove_unknown_action_granted(self): """ Tests the 'permission remove' command in trac-admin. This particular test tries removing NOT_A_PERM from a user. NOT_A_PERM does not exist in the system, but the user possesses the permission.""" self.env.db_transaction( """ INSERT INTO permission VALUES (%s, %s) """, ('joe', 'NOT_A_PERM')) rv, output = self.execute('permission remove joe NOT_A_PERM') self.assertEqual(0, rv, output) rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_export_ok(self): """ Tests the 'permission export' command in trac-admin. This particular test exports the default permissions to stdout. """ rv, output = self.execute('permission export') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_permission_import_ok(self): """ Tests the 'permission import' command in trac-admin. This particular test exports additional permissions, removes them and imports them back. """ user = u'test_user\u0250' self.execute('permission add ' + user + ' WIKI_VIEW') self.execute('permission add ' + user + ' TICKET_VIEW') rv, output = self.execute('permission export') self.execute('permission remove ' + user + ' *') rv, output = self.execute('permission import', input=output) self.assertEqual(0, rv, output) self.assertEqual('', output) rv, output = self.execute('permission list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_backslash_use_ok(self): if self.admin.interactive: self.execute('version add \\') else: self.execute(r"version add '\'") rv, output = self.execute('version list') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_no_sessions(self): rv, output = self.execute('session list authenticated') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_authenticated(self): _prep_session_table(self.env) rv, output = self.execute('session list authenticated') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_anonymous(self): _prep_session_table(self.env) rv, output = self.execute('session list anonymous') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_all(self): _prep_session_table(self.env) if self.admin.interactive: rv, output = self.execute("session list *") else: rv, output = self.execute("session list '*'") self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_authenticated_sid(self): _prep_session_table(self.env) rv, output = self.execute('session list name00') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_anonymous_sid(self): _prep_session_table(self.env) rv, output = self.execute('session list name10:0') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_list_missing_sid(self): _prep_session_table(self.env) rv, output = self.execute('session list thisdoesntexist') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_add_missing_sid(self): rv, output = self.execute('session add') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_add_duplicate_sid(self): _prep_session_table(self.env) rv, output = self.execute('session add name00') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_add_sid_all(self): rv, output = self.execute('session add john John [email protected]') self.assertEqual(0, rv, output) rv, output = self.execute('session list john') self.assertExpectedResult( output, {'today': format_date(None, console_date_format)}) def test_session_add_sid(self): rv, output = self.execute('session add john') self.assertEqual(0, rv, output) rv, output = self.execute('session list john') self.assertExpectedResult( output, {'today': format_date(None, console_date_format)}) def test_session_add_sid_name(self): rv, output = self.execute('session add john John') self.assertEqual(0, rv, output) rv, output = self.execute('session list john') self.assertExpectedResult( output, {'today': format_date(None, console_date_format)}) def test_session_set_attr_name(self): _prep_session_table(self.env) rv, output = self.execute('session set name name00 JOHN') self.assertEqual(0, rv, output) rv, output = self.execute('session list name00') self.assertExpectedResult(output) def test_session_set_attr_email(self): _prep_session_table(self.env) rv, output = self.execute('session set email name00 [email protected]') self.assertEqual(0, rv, output) rv, output = self.execute('session list name00') self.assertExpectedResult(output) def test_session_set_attr_default_handler(self): _prep_session_table(self.env) rv, output = \ self.execute('session set default_handler name00 SearchModule') self.assertEqual(0, rv, output) rv, output = self.execute('session list name00') self.assertExpectedResult(output) def test_session_set_attr_default_handler_invalid(self): _prep_session_table(self.env) rv, output = \ self.execute('session set default_handler name00 InvalidModule') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_set_attr_missing_attr(self): rv, output = self.execute('session set') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_set_attr_missing_value(self): rv, output = self.execute('session set name john') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_set_attr_missing_sid(self): rv, output = self.execute('session set name') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_set_attr_nonexistent_sid(self): rv, output = self.execute('session set name john foo') self.assertEqual(2, rv, output) self.assertExpectedResult(output) def test_session_delete_sid(self): _prep_session_table(self.env) rv, output = self.execute('session delete name00') self.assertEqual(0, rv, output) rv, output = self.execute('session list nam00') self.assertExpectedResult(output) def test_session_delete_missing_params(self): rv, output = self.execute('session delete') self.assertEqual(0, rv, output) self.assertExpectedResult(output) def test_session_delete_anonymous(self): _prep_session_table(self.env) rv, output = self.execute('session delete anonymous') self.assertEqual(0, rv, output) rv, output = self.execute('session list *') self.assertExpectedResult(output) def test_session_delete_multiple_sids(self): _prep_session_table(self.env) rv, output = self.execute('session delete name00 name01 name02 ' 'name03') self.assertEqual(0, rv, output) rv, output = self.execute('session list *') self.assertExpectedResult(output) def test_session_purge_age(self): _prep_session_table(self.env, spread_visits=True) rv, output = self.execute('session purge 20100112') self.assertEqual(0, rv, output) rv, output = self.execute('session list *') self.assertExpectedResult(output) def test_session_purge_invalid_date(self): rv, output = self.execute('session purge <purge>') self.assertEqual(2, rv, output) self.assertExpectedResult( output, { 'hint': self.datetime_format_hint, 'isohint': get_datetime_format_hint('iso8601') }) def test_help_session_purge(self): doc = self.get_command_help('session', 'purge') self.assertIn(u'"YYYY-MM-DDThh:mm:ss±hh:mm"', doc)
class PermissionSystemTestCase(BaseTestCase): def setUp(self): self.env = EnvironmentStub(enable=[ perm.PermissionSystem, perm.DefaultPermissionGroupProvider, perm.DefaultPermissionStore ] + self.permission_requestors) self.perm = perm.PermissionSystem(self.env) def tearDown(self): self.env.reset_db() def test_get_actions(self): tpr_perms = ['TEST_ADMIN', 'TEST_CREATE', 'TEST_DELETE', 'TEST_MODIFY'] all_perms = tpr_perms + ['TRAC_ADMIN'] self.assertEqual(all_perms, self.perm.get_actions()) self.assertEqual(tpr_perms, self.perm.get_actions(skip=self.perm)) def test_get_actions_dict(self): self.assertEqual( { 'TEST_ADMIN': ['TEST_CREATE', 'TEST_DELETE', 'TEST_MODIFY'], 'TEST_CREATE': [], 'TEST_DELETE': [], 'TEST_MODIFY': [], 'TRAC_ADMIN': ['TEST_ADMIN', 'TEST_CREATE', 'TEST_DELETE', 'TEST_MODIFY'], }, self.perm.get_actions_dict()) self.assertEqual( { 'TEST_ADMIN': ['TEST_CREATE', 'TEST_DELETE', 'TEST_MODIFY'], 'TEST_CREATE': [], 'TEST_DELETE': [], 'TEST_MODIFY': [], }, self.perm.get_actions_dict(skip=self.perm)) def test_all_permissions(self): self.assertEqual( { 'TRAC_ADMIN': True, 'TEST_CREATE': True, 'TEST_DELETE': True, 'TEST_MODIFY': True, 'TEST_ADMIN': True }, self.perm.get_user_permissions()) def test_simple_permissions(self): self.perm.grant_permission('bob', 'TEST_CREATE') self.perm.grant_permission('jane', 'TEST_DELETE') self.perm.grant_permission('jane', 'TEST_MODIFY') self.assertEqual({'TEST_CREATE': True}, self.perm.get_user_permissions('bob')) self.assertEqual({ 'TEST_DELETE': True, 'TEST_MODIFY': True }, self.perm.get_user_permissions('jane')) def test_meta_permissions(self): self.perm.grant_permission('bob', 'TEST_CREATE') self.perm.grant_permission('jane', 'TEST_ADMIN') self.assertEqual({'TEST_CREATE': True}, self.perm.get_user_permissions('bob')) self.assertEqual( { 'TEST_CREATE': True, 'TEST_DELETE': True, 'TEST_MODIFY': True, 'TEST_ADMIN': True }, self.perm.get_user_permissions('jane')) def test_undefined_permissions(self): """Only defined actions are returned in the dictionary.""" self.perm.grant_permission('bob', 'TEST_CREATE') self.perm.grant_permission('jane', 'TEST_DELETE') self.perm.grant_permission('jane', 'TEST_MODIFY') self.env.disable_component(self.permission_requestors[0]) self.assertEqual({}, self.perm.get_user_permissions('bob')) self.assertEqual({}, self.perm.get_user_permissions('jane')) def test_grant_permission_differs_from_action_by_casing(self): """`TracError` is raised when granting a permission that differs from an action by casing. """ self.assertRaises(TracError, self.perm.grant_permission, 'user1', 'Test_Create') def test_grant_permission_already_granted(self): """`PermissionExistsError` is raised when granting a permission that has already been granted. """ self.perm.grant_permission('user1', 'TEST_CREATE') self.assertRaises(perm.PermissionExistsError, self.perm.grant_permission, 'user1', 'TEST_CREATE') def test_grant_permission_already_in_group(self): """`PermissionExistsError` is raised when adding a user to a group of which they are already a member. """ self.perm.grant_permission('user1', 'group1') self.assertRaises(perm.PermissionExistsError, self.perm.grant_permission, 'user1', 'group1') def test_get_all_permissions(self): self.perm.grant_permission('bob', 'TEST_CREATE') self.perm.grant_permission('jane', 'TEST_ADMIN') expected = [('bob', 'TEST_CREATE'), ('jane', 'TEST_ADMIN')] for res in self.perm.get_all_permissions(): self.assertIn(res, expected) def test_get_groups_dict(self): permissions = [('user2', 'group1'), ('user1', 'group1'), ('user3', 'group1'), ('user3', 'group2')] for perm_ in permissions: self.perm.grant_permission(*perm_) groups = self.perm.get_groups_dict() self.assertEqual(2, len(groups)) self.assertEqual(['user1', 'user2', 'user3'], groups['group1']) self.assertEqual(['user3'], groups['group2']) def test_get_users_dict(self): permissions = [('user2', 'TEST_CREATE'), ('user1', 'TEST_DELETE'), ('user1', 'TEST_ADMIN'), ('user1', 'TEST_CREATE')] for perm_ in permissions: self.perm.grant_permission(*perm_) users = self.perm.get_users_dict() self.assertEqual(2, len(users)) self.assertEqual(['TEST_ADMIN', 'TEST_CREATE', 'TEST_DELETE'], users['user1']) self.assertEqual(['TEST_CREATE'], users['user2']) def test_get_permission_groups(self): permissions = [ ('user1', 'group1'), ('group1', 'group2'), ('group2', 'group3'), ('user2', 'group4'), ('user1', 'group5'), ('group6', 'group7'), ('user3', 'group8'), # test recursion ('group8', 'group9'), ('group9', 'group8'), ('user3', 'group11'), ('group11', 'group10'), # test recursion ('group10', 'group11'), ('group10', 'group10'), ] for perm_ in permissions: self.perm.grant_permission(*perm_) self.assertEqual([ 'anonymous', 'authenticated', 'group1', 'group2', 'group3', 'group5' ], self.perm.get_permission_groups('user1')) self.assertEqual(['anonymous', 'authenticated', 'group4'], self.perm.get_permission_groups('user2')) self.assertEqual([ 'anonymous', 'authenticated', 'group10', 'group11', 'group8', 'group9' ], self.perm.get_permission_groups('user3')) def test_expand_actions_iter_7467(self): # Check that expand_actions works with iterators (#7467) perms = [ 'TEST_ADMIN', 'TEST_CREATE', 'TEST_DELETE', 'TEST_MODIFY', 'TRAC_ADMIN' ] self.assertEqual(perms, self.perm.expand_actions(['TRAC_ADMIN'])) self.assertEqual(perms, self.perm.expand_actions(iter(['TRAC_ADMIN'])))
class UpgradeTestCase(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(path=mkdtemp()) self.env.config.filename = os.path.join(self.env.path, 'trac.ini') self.env.config.set('trac', 'repository_sync_per_request', 'repos1, repos3, repos5') def tearDown(self): self.env.reset_db_and_disk() def test_saves_backup(self): """Backup file is saved during upgrade.""" config = self.env.config db32.do_upgrade(self.env, VERSION, None) self.assertTrue(os.path.exists(config.filename + '.db32.bak')) def test_repository_sync_per_request_default_value(self): """The default repository sync_per_request attribute is set to true when repository_sync_per_request is not set in trac.ini. """ self.env.config.remove('trac', 'repository_sync_per_request') repositories = self.env.config['repositories'] repositories.set('.dir', '/var/svn') repositories.set('.type', 'svn') repositories.set('git.dir', '/var/git') repositories.set('git.type', 'git') db32.do_upgrade(self.env, VERSION, None) repositories = self.env.config['repositories'] self.assertIn('.sync_per_request', repositories) self.assertTrue(repositories.getbool('.sync_per_request')) self.assertNotIn('git.sync_per_request', repositories) self.assertFalse(repositories.getbool('git.sync_per_request')) def test_repository_sync_per_request_default_value_with_db(self): """The default repository sync_per_request attribute is set to true when repository_sync_per_request is not set in trac.ini. """ self.env.config.remove('trac', 'repository_sync_per_request') # directly insert repository records instead of DbRepositoryProvider # to avoid a TracError "The repository type 'svn' is not supported" with self.env.db_transaction as db: db.executemany( """INSERT INTO repository (id,name,value) VALUES (%s,%s,%s)""", [(1, 'name', ''), (1, 'dir', '/var/svn'), (1, 'type', 'svn'), (2, 'name', 'git'), (2, 'dir', '/var/git'), (2, 'type', 'git')]) db32.do_upgrade(self.env, VERSION, None) repos = RepositoryManager(self.env).get_all_repositories() self.assertIn('', repos) self.assertTrue(repos['']['sync_per_request']) self.assertEqual( '1', self.env.db_query(""" SELECT value FROM repository WHERE id=1 AND name='sync_per_request'""")[0][0]) self.assertIn('git', repos) self.assertFalse(repos['git']['sync_per_request']) self.assertIsNone( self.env.db_query(""" SELECT value FROM repository WHERE id=2 AND name='sync_per_request'""")[0][0]) def test_gitweb_configuration_moved(self): """The Gitweb configuration is moved from the [git] section to the [gitweb-repositories] section. """ projects_list = os.path.join(self.env.path, 'projects_list') projects_base = os.path.dirname(projects_list) projects_url = 'http://localhost/%s' with open(projects_list, 'w') as f: f.write(""" repos1 user1+<*****@*****.**> repos2 """) config = self.env.config['git'] config.set('projects_list', projects_list) config.set('projects_base', projects_base) config.set('projects_url', projects_url) repos1_dir = os.path.join(projects_base, 'repos1') repos2_dir = os.path.join(projects_base, 'repos2') db32.do_upgrade(self.env, VERSION, None) repos = RepositoryManager(self.env).get_all_repositories() self.assertIn('repos1', repos) self.assertTrue(repos['repos1']['sync_per_request']) self.assertEqual(repos1_dir, repos['repos1']['dir']) self.assertEqual('http://localhost/repos1', repos['repos1']['url']) self.assertIn('repos2', repos) self.assertFalse(repos['repos2']['sync_per_request']) self.assertEqual(repos2_dir, repos['repos2']['dir']) self.assertEqual('http://localhost/repos2', repos['repos2']['url']) config = self.env.config['gitweb-repositories'] self.assertNotIn('projects_list', self.env.config) self.assertNotIn('projects_base', self.env.config) self.assertNotIn('projects_url', self.env.config) self.assertNotIn('repository_sync_per_request', self.env.config) self.assertEqual(projects_list, config.get('projects_list')) self.assertEqual(projects_base, config.get('projects_base')) self.assertEqual(projects_url, config.get('projects_url')) self.assertEqual('repos1', config.get('sync_per_request')) def test_repository_providers_disabled(self): """Repository configuration is rewritten when repository providers are disabled. """ projects_list = os.path.join(self.env.path, 'projects_list') projects_base = os.path.dirname(projects_list) projects_url = 'http://localhost/%s' with open(projects_list, 'w') as f: f.write(""" repos1 user1+<*****@*****.**> repos2 """) config = self.env.config['git'] config.set('projects_list', projects_list) config.set('projects_base', projects_base) config.set('projects_url', projects_url) db_provider = DbRepositoryProvider(self.env) db_provider.add_repository('repos3', '/var/git/repos3', 'git') db_provider.add_repository('repos4', '/var/git/repos4', 'git') config = self.env.config['repositories'] config.set('repos5.dir', '/var/svn/repos4') config.set('repos5.type', 'svn') config.set('repos6.dir', '/var/svn/repos5') config.set('repos6.type', 'svn') self.env.disable_component(GitwebProjectsRepositoryProvider) self.env.disable_component(DbRepositoryProvider) self.env.disable_component(RepositoryManager) db32.do_upgrade(self.env, VERSION, None) self.env.enable_component(GitwebProjectsRepositoryProvider) self.env.enable_component(DbRepositoryProvider) self.env.enable_component(RepositoryManager) repos = RepositoryManager(self.env).get_all_repositories() config = self.env.config['gitweb-repositories'] self.assertEqual(projects_list, config.get('projects_list')) self.assertEqual(projects_base, config.get('projects_base')) self.assertEqual(projects_url, config.get('projects_url')) self.assertEqual('repos1', config.get('sync_per_request')) self.assertIn('repos1', repos) self.assertTrue(repos['repos1']['sync_per_request']) self.assertIn('repos2', repos) self.assertFalse(repos['repos2']['sync_per_request']) self.assertIn('repos3', repos) self.assertTrue(repos['repos3']['sync_per_request']) self.assertIn('repos4', repos) self.assertFalse(repos['repos4']['sync_per_request']) self.assertIn('repos5', repos) self.assertTrue(repos['repos5']['sync_per_request']) self.assertIn('repos6', repos) self.assertFalse(repos['repos6']['sync_per_request'])