##// END OF EJS Templates
audit-logger: unify calls to repo.delete and also store source of call, api/web.
marcink -
r1752:9e047971 default
parent child Browse files
Show More
@@ -30,43 +30,45 b' from rhodecode.api.tests.utils import ('
30 30 class TestApiDeleteRepo(object):
31 31 def test_api_delete_repo(self, backend):
32 32 repo = backend.create_repo()
33
33 repo_name = repo.repo_name
34 34 id_, params = build_data(
35 35 self.apikey, 'delete_repo', repoid=repo.repo_name, )
36 36 response = api_call(self.app, params)
37 37
38 38 expected = {
39 'msg': 'Deleted repository `%s`' % (repo.repo_name,),
39 'msg': 'Deleted repository `%s`' % (repo_name,),
40 40 'success': True
41 41 }
42 42 assert_ok(id_, expected, given=response.body)
43 43
44 44 def test_api_delete_repo_by_non_admin(self, backend, user_regular):
45 45 repo = backend.create_repo(cur_user=user_regular.username)
46 repo_name = repo.repo_name
46 47 id_, params = build_data(
47 48 user_regular.api_key, 'delete_repo', repoid=repo.repo_name, )
48 49 response = api_call(self.app, params)
49 50
50 51 expected = {
51 'msg': 'Deleted repository `%s`' % (repo.repo_name,),
52 'msg': 'Deleted repository `%s`' % (repo_name,),
52 53 'success': True
53 54 }
54 55 assert_ok(id_, expected, given=response.body)
55 56
56 57 def test_api_delete_repo_by_non_admin_no_permission(self, backend):
57 58 repo = backend.create_repo()
59 repo_name = repo.repo_name
58 60 id_, params = build_data(
59 61 self.apikey_regular, 'delete_repo', repoid=repo.repo_name, )
60 62 response = api_call(self.app, params)
61 expected = 'repository `%s` does not exist' % (repo.repo_name)
63 expected = 'repository `%s` does not exist' % (repo_name)
62 64 assert_error(id_, expected, given=response.body)
63 65
64 66 def test_api_delete_repo_exception_occurred(self, backend):
65 67 repo = backend.create_repo()
68 repo_name = repo.repo_name
66 69 id_, params = build_data(
67 70 self.apikey, 'delete_repo', repoid=repo.repo_name, )
68 71 with mock.patch.object(RepoModel, 'delete', crash):
69 72 response = api_call(self.app, params)
70 expected = 'failed to delete repository `%s`' % (
71 repo.repo_name,)
73 expected = 'failed to delete repository `%s`' % (repo_name,)
72 74 assert_error(id_, expected, given=response.body)
@@ -29,6 +29,7 b' from rhodecode.api.utils import ('
29 29 get_user_group_or_error, get_user_or_error, validate_repo_permissions,
30 30 get_perm_or_error, parse_args, get_origin, build_commit_data,
31 31 validate_set_owner_permissions)
32 from rhodecode.lib import audit_logger
32 33 from rhodecode.lib import repo_maintenance
33 34 from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi
34 35 from rhodecode.lib.utils2 import str2bool, time_to_datetime
@@ -1154,6 +1155,7 b' def delete_repo(request, apiuser, repoid'
1154 1155 """
1155 1156
1156 1157 repo = get_repo_or_error(repoid)
1158 repo_name = repo.repo_name
1157 1159 if not has_superadmin_permission(apiuser):
1158 1160 _perms = ('repository.admin',)
1159 1161 validate_repo_permissions(apiuser, repoid, repo, _perms)
@@ -1171,18 +1173,27 b' def delete_repo(request, apiuser, repoid'
1171 1173 'Cannot delete `%s` it still contains attached forks' %
1172 1174 (repo.repo_name,)
1173 1175 )
1176 repo_data = repo.get_api_data()
1177 RepoModel().delete(repo, forks=forks)
1174 1178
1175 RepoModel().delete(repo, forks=forks)
1179 repo = audit_logger.RepoWrap(repo_id=None,
1180 repo_name=repo.repo_name)
1181
1182 audit_logger.store(
1183 action='repo.delete',
1184 action_data={'repo_data': repo_data, 'source': 'api_call'},
1185 user=apiuser, repo=repo, commit=False)
1186
1187 ScmModel().mark_for_invalidation(repo_name, delete=True)
1176 1188 Session().commit()
1177 1189 return {
1178 'msg': 'Deleted repository `%s`%s' % (
1179 repo.repo_name, _forks_msg),
1190 'msg': 'Deleted repository `%s`%s' % (repo_name, _forks_msg),
1180 1191 'success': True
1181 1192 }
1182 1193 except Exception:
1183 1194 log.exception("Exception occurred while trying to delete repo")
1184 1195 raise JSONRPCError(
1185 'failed to delete repository `%s`' % (repo.repo_name,)
1196 'failed to delete repository `%s`' % (repo_name,)
1186 1197 )
1187 1198
1188 1199
@@ -97,13 +97,14 b' class RepoSettingsView(RepoAppView):'
97 97 repo_data = self.db_repo.get_api_data()
98 98 RepoModel().delete(self.db_repo, forks=handle_forks)
99 99
100 repo = audit_logger.RepoWrap(repo_id=self.db_repo.repo_id,
100 repo = audit_logger.RepoWrap(repo_id=None,
101 101 repo_name=self.db_repo.repo_name)
102 102 audit_logger.store(
103 action='repo.delete', action_data={'repo_data': repo_data},
103 action='repo.delete',
104 action_data={'repo_data': repo_data, 'source': 'web_action'},
104 105 user=self._rhodecode_user, repo=repo, commit=False)
105 106
106 ScmModel().mark_for_invalidation(self.db_repo_name)
107 ScmModel().mark_for_invalidation(self.db_repo_name, delete=True)
107 108 h.flash(
108 109 _('Deleted repository `%s`') % self.db_repo_name,
109 110 category='success')
@@ -12,7 +12,6 b''
12 12 ******************************************************************************/
13 13 function registerRCRoutes() {
14 14 // routes registration
15 pyroutes.register('home', '/', []);
16 15 pyroutes.register('new_repo', '/_admin/create_repository', []);
17 16 pyroutes.register('edit_user', '/_admin/users/%(user_id)s/edit', ['user_id']);
18 17 pyroutes.register('edit_user_group_members', '/_admin/user_groups/%(user_group_id)s/edit/members', ['user_group_id']);
@@ -92,6 +91,7 b' function registerRCRoutes() {'
92 91 pyroutes.register('register', '/_admin/register', []);
93 92 pyroutes.register('reset_password', '/_admin/password_reset', []);
94 93 pyroutes.register('reset_password_confirmation', '/_admin/password_reset_confirmation', []);
94 pyroutes.register('home', '/', []);
95 95 pyroutes.register('user_autocomplete_data', '/_users', []);
96 96 pyroutes.register('user_group_autocomplete_data', '/_user_groups', []);
97 97 pyroutes.register('repo_list_data', '/_repos', []);
General Comments 0
You need to be logged in to leave comments. Login now