Skip to content

Commit

Permalink
client create enc library with disable enable_enc_library test
Browse files Browse the repository at this point in the history
  • Loading branch information
kklein33 committed Sep 27, 2017
1 parent 829ee2d commit 0f97c18
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
6 changes: 3 additions & 3 deletions seahub/api2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def _create_repo(self, request, repo_name, repo_desc, username, org_id):
passwd = None

if (passwd is not None) and (not config.ENABLE_ENCRYPTED_LIBRARY):
return api_error(status.HTTP_403_FORBIDDEN,
return None, api_error(status.HTTP_403_FORBIDDEN,
'NOT allow to create encrypted library.')

if org_id > 0:
Expand All @@ -714,9 +714,9 @@ def _create_repo(self, request, repo_name, repo_desc, username, org_id):

def _create_enc_repo(self, request, repo_id, repo_name, repo_desc, username, org_id):
if not config.ENABLE_ENCRYPTED_LIBRARY:
return api_error(status.HTTP_403, 'NOT allow to create encrypted library.')
return None, api_error(status.HTTP_403_FORBIDDEN, 'NOT allow to create encrypted library.')
if not _REPO_ID_PATTERN.match(repo_id):
return api_error(status.HTTP_400_BAD_REQUEST, 'Repo id must be a valid uuid')
return None, api_error(status.HTTP_400_BAD_REQUEST, 'Repo id must be a valid uuid')
magic = request.data.get('magic', '')
random_key = request.data.get('random_key', '')
try:
Expand Down
66 changes: 62 additions & 4 deletions tests/api/test_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import pytest
import uuid
from constance import config

from django.core.urlresolvers import reverse
from seaserv import seafile_api
Expand Down Expand Up @@ -156,7 +157,14 @@ def _get_repo_info(self, sync_token, repo_id, **kwargs):
'repo/%s/permission-check/?op=upload' % repo_id)
self.get(url, use_token=False, headers=headers, **kwargs)


class NewReposApiTest(BaseTestCase):
# @pytest.mark.xfail
def setUp(self):
self.clear_cache()
self.login_as(self.admin)
config.ENABLE_ENCRYPTED_LIBRARY = True

def test_create_encrypted_repo(self):
"""Test create an encrypted repo with the secure keys generated on client
side.
Expand All @@ -172,19 +180,69 @@ def test_create_encrypted_repo(self):
'magic': enc_info.magic,
'random_key': enc_info.random_key,
}
res = self.post(REPOS_URL, data=data)
repo = res.json()
repo = self.client.post(REPOS_URL, data=data)
repo = json.loads(repo.content)
assert repo['repo_id'] == repo_id
assert repo['encrypted']
assert repo['magic'] == enc_info.magic
assert repo['random_key'] == enc_info.random_key

# validate the password on server
set_password_url = apiurl('/api2/repos/{}/'.format(repo['repo_id']))
self.post(set_password_url, data={'password': password})
self.client.post(set_password_url, data={'password': password})

# do some file operation
self.create_file(repo_id=repo['repo_id'], parent_dir='/', filename='repo-test', username=self.admin.username)

def test_create_encrypted_repo_with_disable_create_enc_library(self):
"""Test create an encrypted repo with the secure keys generated on client
side.
"""
config.ENABLE_ENCRYPTED_LIBRARY = False
repo_id = str(uuid.uuid4())
password = randstring(16)
enc_version = 2
enc_info = seafile_api.generate_magic_and_random_key(enc_version, repo_id, password)
data = {
'name': 'enc-test-with-disable-encry',
'repo_id': repo_id,
'enc_version': enc_version,
'magic': enc_info.magic,
'random_key': enc_info.random_key,
}
res = self.client.post(REPOS_URL, data=data)
assert res.status_code == 403

def test_create_encrypted_repo_with_invalid_repoid(self):
"""Test create an encrypted repo with the secure keys generated on client
side.
"""
repo_id = 'qwer'
password = randstring(16)
enc_version = 2
enc_info = seafile_api.generate_magic_and_random_key(enc_version, repo_id, password)
data = {
'name': 'enc-test-with-disable-encry',
'repo_id': repo_id,
'enc_version': enc_version,
'magic': enc_info.magic,
'random_key': enc_info.random_key,
}
res = self.client.post(REPOS_URL, data=data)
print res
assert res.status_code == 400

# repo = res.json()
# assert repo['repo_id'] == repo_id
# assert repo['encrypted']
# assert repo['magic'] == enc_info.magic
# assert repo['random_key'] == enc_info.random_key
#
# # validate the password on server
# set_password_url = apiurl('/api2/repos/{}/'.format(repo['repo_id']))
# self.post(set_password_url, data={'password': password})
#
# do some file operation
self.create_file(repo['repo_id'])


# Uncomment following to test api performance.
Expand Down

0 comments on commit 0f97c18

Please sign in to comment.