1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-03 12:31:07 +00:00
seahub/tests/api/test_search.py

64 lines
2.1 KiB
Python
Raw Normal View History

import json
2018-02-09 06:24:03 +00:00
import pytest
from mock import patch
from django.core.urlresolvers import reverse
2018-02-09 06:24:03 +00:00
from seahub.test_utils import BaseTestCase, TRAVIS
2018-02-09 06:24:03 +00:00
from seaserv import seafile_api
2018-02-09 06:24:03 +00:00
class SearchTest(BaseTestCase):
def setUp(self):
self.file_path = self.file
self.repo_id = self.repo.id
self.url = reverse('api_search')
self.mock_total = 15
self.mock_results = [
{
"repo_owner_name": "name",
"repo_id": self.repo_id,
"name": "lian.png",
"repo_owner_contact_email": "freeplant@163.com",
"repo_owner_email": "freeplant@163.com",
"last_modified": 1469415777,
"content_highlight": "",
"fullpath": self.file_path,
"repo_name": "seafile-design",
"is_dir": False,
2018-02-09 06:24:03 +00:00
"size": 142758,
"repo": seafile_api.get_repo(self.repo_id)
},
]
@patch('seahub.api2.views.HAS_FILE_SEARCH', True)
2018-02-09 06:24:03 +00:00
@patch('seahub.api2.views.search_files')
@pytest.mark.skipif(TRAVIS, reason="")
def test_can_search_file(self, mock_search_files):
mock_search_files.return_value = self.mock_results, \
2018-02-08 09:17:14 +00:00
self.mock_total
self.login_as(self.user)
resp = self.client.get(self.url + '?q=lian')
2018-02-08 09:17:14 +00:00
self.assertEqual(200, resp.status_code)
json_resp = json.loads(resp.content)
2018-02-08 09:17:14 +00:00
assert json_resp['total'] == self.mock_total
assert json_resp['results'][0]['repo_id'] == self.mock_results[0]['repo_id']
@patch('seahub.api2.views.HAS_FILE_SEARCH', True)
2018-02-09 06:24:03 +00:00
def test_can_not_search_with_invalid_repo_permission(self):
self.login_as(self.admin)
resp = self.client.get(self.url + '?q=lian&search_repo=%s' %
self.repo_id)
self.assertEqual(403, resp.status_code)
@patch('seahub.api2.views.HAS_FILE_SEARCH', True)
2018-02-08 09:17:14 +00:00
def test_can_not_search_without_q_parameter(self):
self.login_as(self.user)
resp = self.client.get(self.url)
self.assertEqual(400, resp.status_code)