mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-13 05:39:59 +00:00
test with a normal user instead of an admin user
This commit is contained in:
@@ -5,12 +5,15 @@ import requests
|
|||||||
import unittest
|
import unittest
|
||||||
from nose.tools import assert_equal, assert_in # pylint: disable=E0611
|
from nose.tools import assert_equal, assert_in # pylint: disable=E0611
|
||||||
|
|
||||||
from tests.common.common import USERNAME, PASSWORD, IS_PRO
|
from tests.common.common import USERNAME, PASSWORD, IS_PRO, \
|
||||||
|
ADMIN_USERNAME, ADMIN_PASSWORD
|
||||||
|
|
||||||
from tests.common.utils import apiurl, urljoin, randstring
|
from tests.common.utils import apiurl, urljoin, randstring
|
||||||
from tests.api.urls import TOKEN_URL, GROUPS_URL, ACCOUNTS_URL, REPOS_URL
|
from tests.api.urls import TOKEN_URL, GROUPS_URL, ACCOUNTS_URL, REPOS_URL
|
||||||
|
|
||||||
class ApiTestBase(unittest.TestCase):
|
class ApiTestBase(unittest.TestCase):
|
||||||
_token = None
|
_token = None
|
||||||
|
_admin_token = None
|
||||||
|
|
||||||
use_test_user = False
|
use_test_user = False
|
||||||
use_test_group = False
|
use_test_group = False
|
||||||
@@ -98,14 +101,40 @@ class ApiTestBase(unittest.TestCase):
|
|||||||
return cls._req('DELETE', *args, **kwargs)
|
return cls._req('DELETE', *args, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _req(cls, method, *args, **kwargs):
|
def admin_get(cls, *args, **kwargs):
|
||||||
auth = kwargs.pop('auth', True)
|
kwargs['admin'] = True
|
||||||
if auth:
|
return cls.get(*args, **kwargs)
|
||||||
if cls._token is None:
|
|
||||||
cls._token = get_auth_token()
|
|
||||||
|
|
||||||
headers = kwargs.pop('headers', {})
|
@classmethod
|
||||||
headers.setdefault('Authorization', 'Token ' + cls._token)
|
def admin_post(cls, *args, **kwargs):
|
||||||
|
kwargs['admin'] = True
|
||||||
|
return cls.post(*args, **kwargs)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def admin_put(cls, *args, **kwargs):
|
||||||
|
kwargs['admin'] = True
|
||||||
|
return cls.put(*args, **kwargs)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def admin_delete(cls, *args, **kwargs):
|
||||||
|
kwargs['admin'] = True
|
||||||
|
return cls.delete(*args, **kwargs)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _req(cls, method, *args, **kwargs):
|
||||||
|
admin = kwargs.pop('admin', False)
|
||||||
|
if admin:
|
||||||
|
if cls._admin_token is None:
|
||||||
|
cls._admin_token = get_auth_token(ADMIN_USERNAME,
|
||||||
|
ADMIN_PASSWORD)
|
||||||
|
token = cls._admin_token
|
||||||
|
else:
|
||||||
|
if cls._token is None:
|
||||||
|
cls._token = get_auth_token(USERNAME, PASSWORD)
|
||||||
|
token = cls._token
|
||||||
|
|
||||||
|
headers = kwargs.get('headers', {})
|
||||||
|
headers.setdefault('Authorization', 'Token ' + token)
|
||||||
kwargs['headers'] = headers
|
kwargs['headers'] = headers
|
||||||
|
|
||||||
expected = kwargs.pop('expected', 200)
|
expected = kwargs.pop('expected', 200)
|
||||||
@@ -136,9 +165,9 @@ class ApiTestBase(unittest.TestCase):
|
|||||||
msg = 'Expected not empty, but it is'
|
msg = 'Expected not empty, but it is'
|
||||||
self.assertGreater(len(lst), 0, msg)
|
self.assertGreater(len(lst), 0, msg)
|
||||||
|
|
||||||
def get_auth_token():
|
def get_auth_token(username, password):
|
||||||
res = requests.post(TOKEN_URL,
|
res = requests.post(TOKEN_URL,
|
||||||
data=dict(username=USERNAME, password=PASSWORD))
|
data=dict(username=username, password=password))
|
||||||
assert_equal(res.status_code, 200)
|
assert_equal(res.status_code, 200)
|
||||||
token = res.json()['token']
|
token = res.json()['token']
|
||||||
assert_equal(len(token), 40)
|
assert_equal(len(token), 40)
|
||||||
|
@@ -1,18 +1,17 @@
|
|||||||
|
import requests
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from tests.common.utils import apiurl, urljoin
|
from tests.common.utils import apiurl, urljoin, randstring
|
||||||
from tests.api.apitestbase import USERNAME, ApiTestBase
|
from tests.api.apitestbase import USERNAME, ApiTestBase
|
||||||
from tests.api.urls import ACCOUNTS_URL, ACCOUNT_INFO_URL, PING_URL, \
|
from tests.api.urls import ACCOUNTS_URL, ACCOUNT_INFO_URL, PING_URL, \
|
||||||
AUTH_PING_URL
|
AUTH_PING_URL
|
||||||
|
|
||||||
test_account_username = u'test_tmp@test.com'
|
test_account_username = 'test_%s@test.com' % randstring(10)
|
||||||
test_account_password = r'test_test'
|
test_account_password = randstring(20)
|
||||||
test_account_password2 = r'test_test2'
|
test_account_password2 = randstring(20)
|
||||||
test_account_url = urljoin(ACCOUNTS_URL, test_account_username)
|
test_account_url = urljoin(ACCOUNTS_URL, test_account_username)
|
||||||
|
|
||||||
class AccountsApiTest(ApiTestBase):
|
class AccountsApiTest(ApiTestBase):
|
||||||
use_test_uesr = True
|
|
||||||
|
|
||||||
def test_check_account_info(self):
|
def test_check_account_info(self):
|
||||||
info = self.get(ACCOUNT_INFO_URL).json()
|
info = self.get(ACCOUNT_INFO_URL).json()
|
||||||
self.assertIsNotNone(info)
|
self.assertIsNotNone(info)
|
||||||
@@ -21,47 +20,34 @@ class AccountsApiTest(ApiTestBase):
|
|||||||
self.assertIsNotNone(info['usage'])
|
self.assertIsNotNone(info['usage'])
|
||||||
|
|
||||||
def test_list_accounts(self):
|
def test_list_accounts(self):
|
||||||
accounts = self.get(ACCOUNTS_URL).json()
|
# Normal user can not list accounts
|
||||||
found = False
|
self.get(ACCOUNTS_URL, expected=403)
|
||||||
for account in accounts:
|
accounts = self.admin_get(ACCOUNTS_URL).json()
|
||||||
if account['email'] == USERNAME:
|
self.assertGreaterEqual(accounts, 2)
|
||||||
found = True
|
# TODO: check returned json, test start/limit param
|
||||||
self.assertTrue(found)
|
|
||||||
|
|
||||||
def test_create_account(self):
|
def test_create_delete_account(self):
|
||||||
data = {'password': test_account_password}
|
data = {'password': test_account_password}
|
||||||
res = self.put(test_account_url, data=data, expected=201)
|
# non-admin user can not create new user
|
||||||
self.assertEqual(res.text, u'"success"')
|
self.put(test_account_url, data=data, expected=403)
|
||||||
self.delete(test_account_url)
|
|
||||||
|
|
||||||
def test_update_account(self):
|
res = self.admin_put(test_account_url, data=data, expected=201)
|
||||||
data = {'password': test_account_password}
|
|
||||||
self.put(test_account_url, data=data, expected=201)
|
|
||||||
data = {
|
|
||||||
'password': test_account_password2,
|
|
||||||
'is_staff': 1,
|
|
||||||
'is_active': 1,
|
|
||||||
}
|
|
||||||
res = self.put(test_account_url, data=data)
|
|
||||||
self.assertEqual(res.text, u'"success"')
|
self.assertEqual(res.text, u'"success"')
|
||||||
self.delete(test_account_url)
|
|
||||||
|
|
||||||
def test_delete_account(self):
|
# non-admin user can not delete a user
|
||||||
data = {'password': test_account_password}
|
self.delete(test_account_url, expected=403)
|
||||||
self.put(test_account_url, data=data, expected=201)
|
|
||||||
res = self.delete(test_account_url)
|
self.admin_delete(test_account_url)
|
||||||
self.assertEqual(res.text, u'"success"')
|
# check the user is really deleted
|
||||||
accounts = self.get(ACCOUNTS_URL).json()
|
self.admin_get(test_account_url, expected=404)
|
||||||
found = False
|
|
||||||
for account in accounts:
|
|
||||||
if account['email'] == test_account_username:
|
|
||||||
found = True
|
|
||||||
self.assertFalse(found)
|
|
||||||
|
|
||||||
def test_auth_ping(self):
|
def test_auth_ping(self):
|
||||||
res = self.get(AUTH_PING_URL)
|
res = self.get(AUTH_PING_URL)
|
||||||
self.assertRegexpMatches(res.text, u'"pong"')
|
self.assertRegexpMatches(res.text, u'"pong"')
|
||||||
|
res = requests.get(AUTH_PING_URL)
|
||||||
|
self.assertEqual(res.status_code, 403)
|
||||||
|
|
||||||
def test_ping(self):
|
def test_ping(self):
|
||||||
res = self.get(PING_URL, auth=False)
|
res = requests.get(PING_URL)
|
||||||
self.assertRegexpMatches(res.text, u'"pong"')
|
self.assertRegexpMatches(res.text, u'"pong"')
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
@@ -1,13 +1,16 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
BASE_URL = os.getenv('CI_BASE_URL', u'http://127.0.0.1:8000')
|
BASE_URL = os.getenv('TEST_BASE_URL', u'http://127.0.0.1:8000')
|
||||||
USERNAME = os.getenv('CI_USERNAME', u'test@test.com')
|
USERNAME = os.getenv('TEST_USERNAME', u'test@seahubtest.com')
|
||||||
PASSWORD = os.getenv('CI_PASSWORD', u'testtest')
|
PASSWORD = os.getenv('TEST_PASSWORD', u'testtest')
|
||||||
|
|
||||||
|
ADMIN_USERNAME = os.getenv('TEST_ADMIN_USERNAME', u'admin@seahubtest.com')
|
||||||
|
ADMIN_PASSWORD = os.getenv('TEST_ADMIN_PASSWORD', u'adminadmin')
|
||||||
|
|
||||||
if BASE_URL[-1] != '/':
|
if BASE_URL[-1] != '/':
|
||||||
BASE_URL += '/'
|
BASE_URL += '/'
|
||||||
|
|
||||||
if os.getenv('CI_IS_PRO', u'') == u'':
|
if os.getenv('TEST_IS_PRO', u'') == u'':
|
||||||
IS_PRO = False
|
IS_PRO = False
|
||||||
else:
|
else:
|
||||||
IS_PRO = True
|
S_PRO = True
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
: ${PYTHON=python}
|
: ${PYTHON=python}
|
||||||
# Change these if you run on local machine
|
export TEST_USERNAME="test@seahubtest.com"
|
||||||
export CI_USERNAME="test@test.com"
|
export TEST_PASSWORD="testtest"
|
||||||
export CI_PASSWORD="testtest"
|
export TEST_ADMIN_USERNAME="admin@seahubtest.com"
|
||||||
|
export TEST_ADMIN_PASSWORD="adminadmin"
|
||||||
|
|
||||||
# If you run this script on your local machine, you must set CCNET_CONF_DIR
|
# If you run this script on your local machine, you must set CCNET_CONF_DIR
|
||||||
# and SEAFILE_CONF_DIR like this:
|
# and SEAFILE_CONF_DIR like this:
|
||||||
@@ -25,10 +26,14 @@ cd "$SEAHUB_SRCDIR"
|
|||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
###############################
|
###############################
|
||||||
# create database and a new user
|
# create database and two new users: an admin, and a normal user
|
||||||
###############################
|
###############################
|
||||||
$PYTHON ./manage.py syncdb
|
$PYTHON ./manage.py syncdb
|
||||||
$PYTHON -c "import ccnet; pool = ccnet.ClientPool('${CCNET_CONF_DIR}'); ccnet_threaded_rpc = ccnet.CcnetThreadedRpcClient(pool, req_pool=True); ccnet_threaded_rpc.add_emailuser('${CI_USERNAME}', '${CI_PASSWORD}', 1, 1);"
|
|
||||||
|
# create normal user
|
||||||
|
$PYTHON -c "import ccnet; pool = ccnet.ClientPool('${CCNET_CONF_DIR}'); ccnet_threaded_rpc = ccnet.CcnetThreadedRpcClient(pool, req_pool=True); ccnet_threaded_rpc.add_emailuser('${TEST_USERNAME}', '${TEST_PASSWORD}', 0, 1);"
|
||||||
|
# create admin
|
||||||
|
$PYTHON -c "import ccnet; pool = ccnet.ClientPool('${CCNET_CONF_DIR}'); ccnet_threaded_rpc = ccnet.CcnetThreadedRpcClient(pool, req_pool=True); ccnet_threaded_rpc.add_emailuser('${TEST_ADMIN_USERNAME}', '${TEST_ADMIN_PASSWORD}', 1, 1);"
|
||||||
}
|
}
|
||||||
|
|
||||||
function start_seahub() {
|
function start_seahub() {
|
||||||
|
Reference in New Issue
Block a user