class URIMapper: def __init__(self): self.dict = Dictionary() self.authen = Authentication() self.course = Course() self.error = Error() def execute(self, environ): path = environ['PATH_INFO'] #----- dictionary group ----- if path.startswith('/lookup:'): return self.dict.lookup(environ) #----- authentication group ----- elif path.startswith('/register:'): return self.authen.register(environ) elif path.startswith('/login:'******'/logout:'): return self.authen.logout(environ) #----- learning group ----- elif path.startswith('/levels:'): return self.course.levels(environ) elif path.startswith('/lessons:'): return self.course.lessons(environ) elif path.startswith('/listen:'): return self.course.listen(environ) else: return self.error.bad_request()
class SampleUsers(): def __init__(self): self.a = Authentication() def load_sample(self): self.a._register('tuan anh', 'pta', '841673450799') self.a._register('*****@*****.**', md5('3.').hexdigest(), '01673450799')
class CourseTest(unittest.TestCase): def setUp(self): self.c = Course() self.a = Authentication() self.root = 'http://localhost/' self.api_key = 'c74df88f0b1db2e4f48fdc4903851d41' def _levels(self, lang='en'): request = 'levels:lang="%s"&session_id="%s"&api_key="%s"' \ % (quote(lang), self.a._login('Tuan Anh', 'pta'), self.api_key) uri = self.root + request output = urlopen(uri).read() return get_status_code(output) def _lessons(self, lang='en'): try: request = 'levels:lang="%s"&session_id="%s"&api_key="%s"' \ % (quote(lang), self.a._login('Tuan Anh', 'pta'), self.api_key) uri = self.root + request output = urlopen(uri).read() if get_status_code(output) is not None: first_level = get_property(output, 'name') request = 'lessons:lang="%s"&level="%s"&session_id="%s"&api_key="%s"' \ % (quote(lang), quote(first_level), self.a._login('Tuan Anh', 'pta'), self.api_key) uri = self.root + request output = urlopen(uri).read() return get_status_code(output) else: return None except: return -1 def _listen(self, username, password): pass def test_levels(self): self.assertEqual(self._levels('en'), None) self.assertEqual(self._levels('vi'), None) self.assertEqual(self._levels('asdf'), 406) self.assertEqual(self._levels(''), 406) def test_lessons(self): self.assertEqual(self._lessons('en'), None) self.assertEqual(self._lessons('vi'), None) self.assertEqual(self._lessons('enz'), -1)
class DictionaryTest(unittest.TestCase): def setUp(self): self.d = Dictionary() self.a = Authentication() self.root = 'http://localhost/' self.api_key = 'c74df88f0b1db2e4f48fdc4903851d41' def _lookup(self, username, password, keyword): request = 'lookup:keyword="%s"&session_id="%s"&api_key="%s"' \ % (quote(keyword), self.a._login(username, password), self.api_key) uri = self.root + request data = urlopen(uri).read() return get_status_code(data) def test_lookup(self): self.assertEqual(self._lookup("Tuan Anh", "pta", 'hello'), 200) self.assertEqual(self._lookup("Tuan Anh", "", 'hello'), 401) self.assertEqual(self._lookup("Tuan Anh", "pta", ''), 404) self.assertEqual(self._lookup("Tuan Anh", "pta", 'helloasdfz'), 404)
def setUp(self): self.a = Authentication() self.root = 'http://localhost/' self.api_key = 'c74df88f0b1db2e4f48fdc4903851d41'
class AuthenTest(unittest.TestCase): def setUp(self): self.a = Authentication() self.root = 'http://localhost/' self.api_key = 'c74df88f0b1db2e4f48fdc4903851d41' def tearDown(self): self.a._remove('') self.a._remove('AloneRoad') def _register(self, username, password, phone_number): request = 'register:username="******"&password="******"&phone_number="%s"&api_key="%s"' \ % (quote(username), quote(password), quote(phone_number), self.api_key) uri = self.root + request status_code = get_status_code(urlopen(uri).read()) return status_code def _login(self, username, password): request = 'login:username="******"&password="******"&api_key="%s"' \ % (quote(username), quote(password), self.api_key) uri = self.root + request status_code = get_status_code(urlopen(uri).read()) return status_code def _get_session_id(self, username, password): request = 'login:username="******"&password="******"&api_key="%s"' \ % (quote(username), quote(password), self.api_key) uri = self.root + request data = urlopen(uri).read() if get_status_code(data) == 202: return get_property(data, 'session_id') return None def _logout(self, session_id): request = 'logout:session_id="%s"&api_key="%s"' \ % (session_id, self.api_key) uri = self.root + request status_code = get_status_code(urlopen(uri).read()) return status_code def test_register(self): self.assertEqual(self._register("", "asd", "01673450799"), 406) self.assertEqual(self._register("Tuan Anh", "", "01673450799"), 406) self.assertEqual(self._register("Tuan Anh", "asd", "673450799"), 406) self.assertEqual(self._register("Tuan Anh", "asd", "a1673450799"), 406) self.assertEqual(self._register("AloneRoad", "asd", "01673450799"), 406) self.a._remove('AloneRoad') md5_password = md5("3.").hexdigest() self.assertEqual(self._register("AloneRoad", md5_password, "01673450799"), 201) def test_login(self): self.assertEqual(self._login('Tuan Anh', 'pta'), 202) self.assertEqual(self._login('tuan anh', 'pta'), 202) self.assertEqual(self._login('tuananh', 'pta'), 401) self.assertEqual(self._login('Tuan Anh', 'PTA'), 401) self.assertEqual(self._login('Tuan Anh', 'ta'), 401) self.assertEqual(self._login('Tuan Anh', ''), 401) self.assertEqual(self._login('', 'pta'), 401) def test_logout(self): session_id = self._get_session_id("*****@*****.**", md5('3.').hexdigest()) self.assertEqual(self._logout(session_id), 200) self.assertEqual(self._logout('asdf'), 401)
def __init__(self): self.dict = Dictionary() self.authen = Authentication() self.course = Course() self.error = Error()
def new_func(*args, **kwargs): auth = Authentication(request) if not auth.authenticate(): return jsonify(auth.error) return func(*args, **kwargs)
def __init__(self): self.a = Authentication()