diff --git a/seahub/api2/endpoints/wiki_pages.py b/seahub/api2/endpoints/wiki_pages.py index 783961a0be..3b57239d6e 100644 --- a/seahub/api2/endpoints/wiki_pages.py +++ b/seahub/api2/endpoints/wiki_pages.py @@ -5,7 +5,8 @@ import urllib2 from rest_framework import status from rest_framework.authentication import SessionAuthentication -from rest_framework.permissions import IsAuthenticated +from rest_framework.permissions import ( + IsAuthenticated, IsAuthenticatedOrReadOnly) from rest_framework.response import Response from rest_framework.views import APIView from seaserv import seafile_api, get_file_id_by_path @@ -24,7 +25,7 @@ logger = logging.getLogger(__name__) class WikiPagesView(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) - permission_classes = (IsAuthenticated, ) + permission_classes = (IsAuthenticatedOrReadOnly, ) throttle_classes = (UserRateThrottle, ) def get(slef, request, slug): @@ -56,6 +57,10 @@ class WikiPagesView(APIView): wiki_page_object = get_wiki_page_object(wiki, page_name) wiki_pages_object.append(wiki_page_object) + # sort pages by name + wiki_pages_object.sort(lambda x, y: cmp(x['name'].lower(), + y['name'].lower())) + return Response({ "data": wiki_pages_object }) @@ -112,7 +117,7 @@ class WikiPagesView(APIView): class WikiPageView(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) - permission_classes = (IsAuthenticated, ) + permission_classes = (IsAuthenticatedOrReadOnly, ) throttle_classes = (UserRateThrottle, ) def get(self, request, slug, page_name="home"): diff --git a/seahub/wiki/models.py b/seahub/wiki/models.py index 8fc12f3181..a8ed6a9f75 100644 --- a/seahub/wiki/models.py +++ b/seahub/wiki/models.py @@ -108,6 +108,7 @@ class Wiki(models.Model): class Meta: unique_together = (('username', 'repo_id'),) + ordering = ["name"] @property def link(self): diff --git a/tests/api/endpoints/test_wiki_pages.py b/tests/api/endpoints/test_wiki_pages.py index 5d5496c5b0..5b34662132 100644 --- a/tests/api/endpoints/test_wiki_pages.py +++ b/tests/api/endpoints/test_wiki_pages.py @@ -3,25 +3,34 @@ import json from django.core.urlresolvers import reverse import seaserv from seaserv import seafile_api, ccnet_api - + from seahub.wiki.models import Wiki from seahub.test_utils import BaseTestCase class WikiPagesViewTest(BaseTestCase): def setUp(self): - self.login_as(self.user) - - wiki = Wiki.objects.add('test wiki pages', self.user.username, + self.wiki = Wiki.objects.add('test wiki pages', self.user.username, repo_id=self.repo.id) - self.url = reverse('api-v2.1-wiki-pages', args=[wiki.slug]) + self.url = reverse('api-v2.1-wiki-pages', args=[self.wiki.slug]) - def test_can_list_pages(self): - seaserv.post_empty_file(self.repo.id, "/", "789.md", - self.user.username) seaserv.post_empty_file(self.repo.id, "/", "987.md", self.user.username) + seaserv.post_empty_file(self.repo.id, "/", "789.md", + self.user.username) + + def test_can_list_pages(self): + self.login_as(self.user) + + resp = self.client.get(self.url) + json_resp = json.loads(resp.content) + self.assertEqual(200, resp.status_code) + assert len(json_resp['data']) == 2 + + def test_can_list_public_wiki_pages(self): + self.wiki.permission = 'public' + self.wiki.save() resp = self.client.get(self.url) json_resp = json.loads(resp.content) @@ -29,6 +38,8 @@ class WikiPagesViewTest(BaseTestCase): assert len(json_resp['data']) == 2 def test_can_add_page(self): + self.login_as(self.user) + resp = self.client.post(self.url, { 'name': '2018', }) @@ -37,27 +48,51 @@ class WikiPagesViewTest(BaseTestCase): self.assertEqual(200, resp.status_code) assert json_resp['name'] == '2018' + def test_list_pages_sorted_by_name(self): + self.login_as(self.user) + + seaserv.post_empty_file(self.repo.id, "/", "issues.md", + self.user.username) + seaserv.post_empty_file(self.repo.id, "/", "home.md", + self.user.username) + seaserv.post_empty_file(self.repo.id, "/", "img test.md", + self.user.username) + + resp = self.client.get(self.url) + json_resp = json.loads(resp.content) + names = [e['name'] for e in json_resp['data']] + assert names[0] == '789' + assert names[-1] == 'issues' class WikiPageViewTest(BaseTestCase): def setUp(self): - self.login_as(self.user) self.wiki = Wiki.objects.add('test wiki page', self.user.username, repo_id=self.repo.id) self.url = reverse('api-v2.1-wiki-page', args=[self.wiki.slug, "home"]) - - def test_can_get_page_content(self): self.create_file_with_content(file_name='home.md', parent_dir='/', content='2018', username=self.user.username) + def test_can_get_page_content(self): + self.login_as(self.user) + resp = self.client.get(self.url) json_resp = json.loads(resp.content) self.assertEqual(200, resp.status_code) assert json_resp['content'] == '2018' assert json_resp['meta']['name'] == 'home' + def test_can_get_public_page(self): + self.wiki.permission = 'public' + self.wiki.save() + + resp = self.client.get(self.url) + self.assertEqual(200, resp.status_code) + def test_can_delete_page(self): + self.login_as(self.user) + resp = self.client.delete(self.url) self.assertEqual(200, resp.status_code) diff --git a/tests/seahub/wiki/test_utils.py b/tests/seahub/wiki/test_utils.py new file mode 100644 index 0000000000..19e2e5b770 --- /dev/null +++ b/tests/seahub/wiki/test_utils.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from seaserv import seafile_api + +from seahub.wiki.models import Wiki +from seahub.wiki.utils import is_valid_wiki_name, get_wiki_page_object +from seahub.test_utils import BaseTestCase + + +class TestIsValidWikiName(BaseTestCase): + def test_valid_name(self): + assert is_valid_wiki_name('a -_123') is True + assert is_valid_wiki_name(u'维基 abc') is True + + def test_invalid_name(self): + assert is_valid_wiki_name('aa/.') is False + assert is_valid_wiki_name(' ') is False + + +class TestGetWikiPageObject(BaseTestCase): + def test_get(self): + wiki = Wiki.objects.add('new wiki', self.user.username) + assert wiki is not None + + seafile_api.post_empty_file(wiki.repo_id, '/', + 'home.md', self.user.username) + + p = get_wiki_page_object(wiki, 'home') + assert p['updated_at'] is not None + assert p['last_modifier_name'] is not None