def test_user_customization_acl(self): customization_mock = mock.Mock() customization_mock.user = self.user1 css_path = css_upload_path(customization_mock, "anything") js_path = js_upload_path(customization_mock, "anything") UserCustomization.objects.create( user=self.user1, custom_css=css_path, custom_js=js_path ) # First user should be able to get his resources. assert self.client.login(username="******", password="******") with mock.patch("sendfile.sendfile") as sendfile: sendfile.return_value = HttpResponse() response = self.client.get(reverse("custom_resource", args=(self.user1.id, "css"))) self.assertEqual(response.status_code, 200) response = self.client.get(reverse("custom_resource", args=(self.user1.id, "js"))) self.assertEqual(response.status_code, 200) # Second user should not be able to get first user's resources. assert self.client.login(username="******", password="******") with mock.patch("sendfile.sendfile") as sendfile: sendfile.return_value = HttpResponse() response = self.client.get(reverse("custom_resource", args=(self.user1.id, "css"))) self.assertEqual(response.status_code, 403) response = self.client.get(reverse("custom_resource", args=(self.user1.id, "js"))) self.assertEqual(response.status_code, 403)
def test_user_customization_delete(self): assert self.client.login(username="******", password="******") customization_mock = mock.Mock() customization_mock.user = self.user1 css_path = css_upload_path(customization_mock, "anything") js_path = js_upload_path(customization_mock, "anything") customization = UserCustomization.objects.create( user=self.user1, custom_css=css_path, custom_js=js_path ) css_full_path = customization.custom_css.path js_full_path = customization.custom_js.path data = { 'custom_css': "", 'custom_js': "" } with mock.patch("django.core.files.storage.FileSystemStorage.delete") as delete: self.client.post(reverse("user_customization"), data) self.assertEqual(delete.call_count, 2) self.assertEqual(delete.call_args_list, [mock.call(css_full_path), mock.call(js_full_path)])
def test_user_customization_get(self): assert self.client.login(username="******", password="******") customization_mock = mock.Mock() customization_mock.user = self.user1 css_path = css_upload_path(customization_mock, "anything") js_path = js_upload_path(customization_mock, "anything") UserCustomization.objects.create( user=self.user1, custom_css=css_path, custom_js=js_path ) with mock.patch("sendfile.sendfile") as sendfile: sendfile.return_value = HttpResponse() response = self.client.get(reverse("custom_resource", args=(self.user1.id, "js"))) self.assertEqual(response.status_code, 200, response.content) sendfile.assert_called_once_with(response.wsgi_request, os.path.join(settings.SENDFILE_ROOT, js_path)) with mock.patch("sendfile.sendfile") as sendfile: sendfile.return_value = HttpResponse() response = self.client.get(reverse("custom_resource", args=(self.user1.id, "css"))) self.assertEqual(response.status_code, 200, response.content) sendfile.assert_called_once_with(response.wsgi_request, os.path.join(settings.SENDFILE_ROOT, css_path))