1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-08 10:22:46 +00:00

clean up test code

This commit is contained in:
Chilledheart
2014-08-27 17:13:05 +08:00
committed by lins05
parent a882caec03
commit 3044d2de20
6 changed files with 25 additions and 50 deletions

View File

@@ -1,24 +1,5 @@
import os import os
if os.getenv('CI_BASE_URL'):
BASE_URL = os.getenv('CI_BASE_URL')
else:
BASE_URL = u'http://127.0.0.1:8000'
if os.getenv('CI_USERNAME'): BASE_URL = os.getenv('CI_BASE_URL', u'http://127.0.0.1:8000')
USERNAME = os.getenv('CI_USERNAME') USERNAME = os.getenv('CI_USERNAME', u'test@test.com')
else: PASSWORD = os.getenv('CI_PASSWORD', u'testtest')
USERNAME = u'test@test.com'
if os.getenv('CI_PASSWORD'):
PASSWORD = os.getenv('CI_PASSWORD')
else:
PASSWORD = u'testtest'
def getBaseUrl():
return BASE_URL
def getUserName():
return USERNAME
def getPassword():
return PASSWORD

View File

@@ -11,18 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from common import common from common.common import BASE_URL, USERNAME, PASSWORD
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
LOGIN_URL = common.getBaseUrl() + u'/accounts/login/' LOGIN_URL = BASE_URL + u'/accounts/login/'
HOME_URL = common.getBaseUrl() + u'/home/my/' HOME_URL = BASE_URL + u'/home/my/'
LOGOUT_URL = common.getBaseUrl() + u'/accounts/logout/' LOGOUT_URL = BASE_URL + u'/accounts/logout/'
USERNAME = common.getUserName() def get_logged_instance():
PASSWORD = common.getPassword()
def getLoggedInstance():
browser = webdriver.PhantomJS() browser = webdriver.PhantomJS()
browser.get(LOGIN_URL) browser.get(LOGIN_URL)
username_input = browser.find_element_by_name('username') username_input = browser.find_element_by_name('username')

View File

@@ -1,22 +1,22 @@
import integration as common from integration import HOME_URL, LOGOUT_URL, get_logged_instance
import unittest import unittest
class LoginTestCase(unittest.TestCase): class LoginTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.browser = common.getLoggedInstance() self.browser = get_logged_instance()
self.assertIsNotNone(self.browser) self.assertIsNotNone(self.browser)
self.addCleanup(self.browser.quit) self.addCleanup(self.browser.quit)
def testLogin(self): def testLogin(self):
self.assertRegexpMatches(self.browser.current_url, common.HOME_URL) self.assertRegexpMatches(self.browser.current_url, HOME_URL)
def testLogout(self): def testLogout(self):
myinfo_bar = self.browser.find_element_by_css_selector('#my-info') myinfo_bar = self.browser.find_element_by_css_selector('#my-info')
logout_input = self.browser.find_element_by_css_selector('a#logout') logout_input = self.browser.find_element_by_css_selector('a#logout')
myinfo_bar.click() myinfo_bar.click()
logout_input.click() logout_input.click()
self.assertRegexpMatches(self.browser.current_url, common.LOGOUT_URL) self.assertRegexpMatches(self.browser.current_url, LOGOUT_URL)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main(verbosity=2) unittest.main(verbosity=2)

View File

@@ -11,18 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from common import common from common.common import BASE_URL, USERNAME, PASSWORD
import requests, re import requests, re
PING_URL = common.getBaseUrl() + u'/api2/ping/' PING_URL = BASE_URL + u'/api2/ping/'
TOKEN_URL = common.getBaseUrl() + u'/api2/auth-token/' TOKEN_URL = BASE_URL + u'/api2/auth-token/'
AUTH_PING_URL = common.getBaseUrl() + u'/api2/auth/ping/' AUTH_PING_URL = BASE_URL + u'/api2/auth/ping/'
USERNAME = common.getUserName()
PASSWORD = common.getPassword()
META_AUTH = {'username': USERNAME, 'password': PASSWORD} META_AUTH = {'username': USERNAME, 'password': PASSWORD}
def getAuthToken(): def get_auth_token():
res = requests.post(TOKEN_URL, data=META_AUTH) res = requests.post(TOKEN_URL, data=META_AUTH)
if (res.status_code != 200): if (res.status_code != 200):
return None return None
@@ -31,8 +29,8 @@ def getAuthToken():
return None return None
return token return token
def getAuthedInstance(): def get_authed_instance():
token = getAuthToken() token = get_auth_token()
if (token == None): if (token == None):
return None return None
s = requests.Session() s = requests.Session()

View File

@@ -1,14 +1,14 @@
import integration_api as common from integration_api import AUTH_PING_URL, get_authed_instance
import unittest import unittest
class AuthPingApiTestCase(unittest.TestCase): class AuthPingApiTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.requests = common.getAuthedInstance() self.requests = get_authed_instance()
self.assertIsNotNone(self.requests) self.assertIsNotNone(self.requests)
def testAuthPingApi(self): def testAuthPingApi(self):
res = self.requests.get(common.AUTH_PING_URL) res = self.requests.get(AUTH_PING_URL)
self.assertEqual(res.status_code, 200) self.assertEqual(res.status_code, 200)
self.assertRegexpMatches(res.text, u'"pong"') self.assertRegexpMatches(res.text, u'"pong"')

View File

@@ -1,11 +1,10 @@
import integration_api as common from integration_api import PING_URL
import requests import requests, unittest
import unittest
class PingApiTestCase(unittest.TestCase): class PingApiTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.res = requests.get(common.PING_URL) self.res = requests.get(PING_URL)
def testPingApi(self): def testPingApi(self):
self.assertEqual(self.res.status_code, 200) self.assertEqual(self.res.status_code, 200)