class CommentAPITest(AbstractTest): tables = ['user', 'category', 'thread', 'comment'] test_tables = ['user', 'category', 'thread', 'comment'] client = init_server().test_client() def setUp(self): super(CommentAPITest, self).setUp() self.load_fixtures() user = User.get_user_secret(user_id=1) token = generate_token(user_id=user.get('user_id'), email=user.get('email')) self.headers = {'Authorization': 'Bearer %s' % token} # TODO テストにおけるdatetimeの扱いをどうにかする #2 def test_post_comment(self): '''POST /comment 201 CREATE ''' data = { 'thread_id': 1, 'text': 'particle', } response = self.client.post('/comment', data=json.dumps(data), content_type='application/json', headers=self.headers) self.assertEqual(201, response.status_code)
class SortAPITest(AbstractTest): tables = ['user', 'sort'] test_tables = ['user', 'sort'] client = init_server().test_client() def setUp(self): super(SortAPITest, self).setUp() self.load_fixtures() user = User.get_user_secret(user_id=1) token = generate_token( user_id=user.get('user_id'), email=user.get('email') ) self.headers = {'Authorization': 'Bearer %s' % token} def test_get(self): '''GET /sorts 200 OK ''' response = self.client.get( '/sorts', headers=self.headers ) actual = json.loads(response.data.decode()) expect = self.test_data.get('sort') self.assertListEqual(expect, actual)
class CategoryAPITest(AbstractTest): tables = ['user', 'category'] client = init_server().test_client() def setUp(self): super().setUp() self.load_fixtures() def test_get(self): '''GET /category 200 OK ''' user_id = 1 email = User.get_email(user_id=user_id) token = generate_token(user_id=user_id, email=email) response = self.client.get( '/category', headers={'Authorization': 'Bearer %s' % token}, ) actual = json.loads(response.data.decode()) expect = [ { 'category_id': 1, 'name': '雑談' }, { 'category_id': 2, 'name': '恋愛' }, { 'category_id': 3, 'name': '学業' }, ] self.assertEqual(expect, actual)
class AuthAPITest(AbstractTest): tables = ['user'] test_tables = ['user'] client = init_server().test_client() def setUp(self): super(AuthAPITest, self).setUp() self.load_fixtures() def test_login(self): '''GET /login 200 OK ''' user = self.filter_test_data(table='user', field='user_id', target=1)[0] data = { 'email': user.get('email'), 'password': user.get('password'), } response = self.client.get('/auth/login', data=json.dumps(data), content_type='application/json') self.assertEqual(200, response.status_code) # response.dataにweb_tokenがあるかチェック actual = json.loads(response.data.decode()) exist_web_token = True if actual.get('web_token') else False self.assertEqual(True, exist_web_token) def test_login_failed(self): '''GET /login 400 FAILED LOGIN ''' user = self.filter_test_data(table='user', field='user_id', target=1)[0] data = { 'email': user.get('email'), 'password': '******', } response = self.client.get('/auth/login', data=json.dumps(data), content_type='application/json') self.assertEqual(400, response.status_code) def test_register(self): '''POST /register 201 CREATE /auth/prog_userからlogin_tokenを擬似的に取得する ''' email = 'iris@test_gmail.com' # login_tokenを擬似的に取得 login_token = ProvisionalUser.post(email=email) data = { 'email': email, 'login_token': login_token, 'password': '******', 'nick_name': 'iris', } response = self.client.post('/auth/register', data=json.dumps(data), content_type='application/json') self.assertEqual(201, response.status_code) actual = User.get_by_email(email=email) expect = { 'nick_name': 'iris', 'profile': None, 'twitter_name': None, } self.assertDictEqual(expect, actual) def test_register_error_duplicate_email(self): '''POST /register 400 既に登録してあるemailの場合 ''' email = 'iris@test_gmail.com' # login_tokenを擬似的に取得 login_token = ProvisionalUser.post(email=email) data = { 'email': email, 'login_token': login_token, 'password': '******', 'nick_name': 'iris', } response = self.client.post('/auth/register', data=json.dumps(data), content_type='application/json') self.assertEqual(201, response.status_code) response = self.client.post('/auth/register', data=json.dumps(data), content_type='application/json') self.assertEqual(400, response.status_code) def test_register_error_duplicate_nick_name(self): '''POST /register 400 既に登録してあるemailの場合 ''' email = 'iris@test_gmail.com' # login_tokenを擬似的に取得 login_token = ProvisionalUser.post(email=email) data = { 'email': email, 'login_token': login_token, 'password': '******', 'nick_name': 'iris', } response = self.client.post('/auth/register', data=json.dumps(data), content_type='application/json') self.assertEqual(201, response.status_code) # nick_name重複登録 email = 'iris2@test_gmail.com' # login_tokenを擬似的に取得 login_token = ProvisionalUser.post(email=email) data = { 'email': email, 'login_token': login_token, 'password': '******', 'nick_name': 'iris', } response = self.client.post('/auth/register', data=json.dumps(data), content_type='application/json') self.assertEqual(400, response.status_code)
class ThreadAPITest(AbstractTest): tables = ['user', 'category', 'thread'] test_tables = ['user', 'category', 'thread'] client = init_server().test_client() def setUp(self): super(ThreadAPITest, self).setUp() self.load_fixtures() user = User.get_user_secret(user_id=1) token = generate_token(user_id=user.get('user_id'), email=user.get('email')) self.headers = {'Authorization': 'Bearer %s' % token} # TODO テストにおけるdatetimeの扱いをどうにかする #2 def test_get_all_by_c_id(self): '''GET /threads 200 OK ''' data = { 'category_id': 1, 'sort_id': 1, 'paging': 1, } response = self.client.get('/threads', data=json.dumps(data), content_type='application/json', headers=self.headers) self.assertEqual(200, response.status_code) def test_get_thread(self): '''GET /thread 200 OK ''' thread_id = 1 response = self.client.get('/thread/%d' % thread_id, headers=self.headers) actual = json.loads(response.data.decode()) actual.get('thread').update({ 'create_at': datetime.strptime( actual.get('thread').get('create_at'), "%a, %d %b %Y %H:%M:%S GMT"), 'update_at': datetime.strptime( actual.get('thread').get('update_at'), "%a, %d %b %Y %H:%M:%S GMT"), }) thread = self.filter_test_data(table='thread', field='thread_id', target=1)[0] expect = {'thread': thread, 'comments': []} self.assertDictEqual(expect, actual) # TODO テストにおけるdatetimeの扱いをどうにかする #2 def test_post(self): '''POST /thread 200 OK ''' data = { 'title': 'title24', 'category_id': 1, 'comment_text': 'hello', } response = self.client.post('/thread', data=json.dumps(data), content_type='application/json', headers=self.headers) self.assertEqual(200, response.status_code) def test_delete_thread(self): '''DELETE /thread 200 OK ''' thread_id = 1 response = self.client.delete('/thread/%d' % thread_id, headers=self.headers) self.assertEqual(200, response.status_code) response = self.client.get('/thread/%d' % thread_id, headers=self.headers) self.assertEqual(400, response.status_code) def test_delete_no_thread(self): '''DELETE /thread 400 NOT FOUND ''' thread_id = 100 response = self.client.delete('/thread/%d' % thread_id, headers=self.headers) self.assertEqual(400, response.status_code)
from app import init_server from app.settings import loop if __name__ == "__main__": loop.run_until_complete(init_server(loop)) loop.run_forever()
class UserAPITest(AbstractTest): tables = ['user'] test_tables = ['user'] client = init_server().test_client() def setUp(self): super(UserAPITest, self).setUp() self.load_fixtures() user = User.get_user_secret(user_id=1) token = generate_token( user_id=user.get('user_id'), email=user.get('email') ) self.headers = {'Authorization': 'Bearer %s' % token} def test_get_user(self): '''GET /user 200 OK ''' response = self.client.get( '/user', headers=self.headers ) user = self.filter_test_data( table='user', field='user_id', target=1 )[0] actual = json.loads(response.data.decode()) expect = { 'nick_name': user.get('nick_name'), 'profile': user.get('profile'), 'twitter_name': user.get('twitter_name'), } self.assertEqual(expect, actual) def test_put(self): '''PUT /user 200 OK ''' data = { 'nick_name': 'kiku', 'profile': None, 'twitter_name': 'matsu', } response = self.client.put( '/user', data=json.dumps(data), content_type='application/json', headers=self.headers ) self.assertEqual(200, response.status_code) response = self.client.get( '/user', headers=self.headers ) actual = json.loads(response.data.decode()) expect = data self.assertDictEqual(expect, actual) def test_put_password(self): '''PUT /user/password 200 OK ''' new_password = '******' user = self.filter_test_data( table='user', field='user_id', target=1 )[0] data = { 'password': user.get('password'), 'new_password': new_password, } response = self.client.put( '/user/password', data=json.dumps(data), content_type='application/json', headers=self.headers ) self.assertEqual(200, response.status_code) actual = User.get_user_secret(1) expect = user expect.update({ 'password': new_password }) self.assertDictEqual(expect, actual) def test_put_password_400(self): '''PUT /user/password 400 invalid password ''' new_password = '******' data = { 'password': '******', 'new_password': new_password, } response = self.client.put( '/user/password', data=json.dumps(data), content_type='application/json', headers=self.headers ) self.assertEqual(400, response.status_code)