From 5d685b27c5454e29809fe6039e0cf8945cbbb52f Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Tue, 6 Mar 2018 20:05:44 +0100 Subject: API for tags (#34) * Help page and misc improvements Committing so I can go home >:| * [WIP] - API improvements for the tag features. Not completed. * renaming tag.py to tags.py and refactoring the nomenclature of docs to tags * fixed error message in tags, cleaning up app_test.py * tests for the tags feature * ignoring jsonify returns cause coverall can't handle them * Catch-all error view for the API blueprint * cleaning up APIErrorView a little * bringing coverage for tags.py to 100% * how did this get in here? * how did this get in here? ROUND 2 * Removing the 503 database error handling. It's not in use and we should probably rethink that whole custom error handling system anyway. * Converting the tags file to use the @api_params decorator instead of validating manually. Tested with bot staging. --- app_test.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 19 deletions(-) (limited to 'app_test.py') diff --git a/app_test.py b/app_test.py index eb94179e..2176fe08 100644 --- a/app_test.py +++ b/app_test.py @@ -13,21 +13,25 @@ app = manager.app class SiteTest(TestCase): - """ extend TestCase with flask app instantiation """ + """ Extend TestCase with flask app instantiation """ + def create_app(self): - """ add flask app configuration settings """ + """ Add flask app configuration settings """ server_name = 'pytest.local' + app.config['TESTING'] = True app.config['LIVESERVER_TIMEOUT'] = 10 app.config['SERVER_NAME'] = server_name app.config['API_SUBDOMAIN'] = f'http://api.{server_name}' app.config['STAFF_SUBDOMAIN'] = f'http://staff.{server_name}' app.allow_subdomain_redirects = True + return app -class BaseEndpoints(SiteTest): - """ test cases for the base endpoints """ +class RootEndpoint(SiteTest): + """ Test cases for the root endpoint and error handling """ + def test_index(self): """ Check the root path reponds with 200 OK """ response = self.client.get('/', 'http://pytest.local') @@ -104,35 +108,74 @@ class ApiEndpoints(SiteTest): self.assertEqual(response.json, {'status': 'ok'}) self.assertEqual(response.status_code, 200) - def test_api_tag(self): - """ Check tag api """ + def test_api_tags(self): + """ Check tag API """ os.environ['BOT_API_KEY'] = 'abcdefg' headers = {'X-API-Key': 'abcdefg', 'Content-Type': 'application/json'} - good_data = json.dumps({ + + post_data = json.dumps({ 'tag_name': 'testing', - 'tag_content': 'testing', - 'tag_category': 'testing'}) + 'tag_content': 'testing' + }) + + get_data = json.dumps({ + 'tag_name': 'testing' + }) bad_data = json.dumps({ - 'not_a_valid_key': 'testing', - 'tag_content': 'testing', - 'tag_category': 'testing'}) + 'not_a_valid_key': 'gross_faceman' + }) + + # POST method - no headers + response = self.client.post('/tags', app.config['API_SUBDOMAIN']) + self.assertEqual(response.status_code, 401) + + # POST method - no data + response = self.client.post('/tags', app.config['API_SUBDOMAIN'], headers=headers) + self.assertEqual(response.status_code, 400) + + # POST method - bad data + response = self.client.post('/tags', app.config['API_SUBDOMAIN'], headers=headers, data=bad_data) + self.assertEqual(response.status_code, 400) - response = self.client.get('/tag', app.config['API_SUBDOMAIN']) + # POST method - save tag + response = self.client.post('/tags', app.config['API_SUBDOMAIN'], headers=headers, data=post_data) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json, {"success": True}) + + # GET method - no headers + response = self.client.get('/tags', app.config['API_SUBDOMAIN']) self.assertEqual(response.status_code, 401) - response = self.client.get('/tag', app.config['API_SUBDOMAIN'], headers=headers) + # GET method - get all tags + response = self.client.get('/tags', app.config['API_SUBDOMAIN'], headers=headers) self.assertEqual(response.status_code, 200) + self.assertEqual(type(response.json), list) - response = self.client.post('/tag', app.config['API_SUBDOMAIN'], headers=headers, data=bad_data) + # GET method - get specific tag + response = self.client.get('/tags?tag_name=testing', app.config['API_SUBDOMAIN'], headers=headers) + self.assertEqual(response.json, { + 'tag_content': 'testing', + 'tag_name': 'testing' + }) + self.assertEqual(response.status_code, 200) + + # DELETE method - no headers + response = self.client.delete('/tags', app.config['API_SUBDOMAIN']) + self.assertEqual(response.status_code, 401) + + # DELETE method - no data + response = self.client.delete('/tags', app.config['API_SUBDOMAIN'], headers=headers) self.assertEqual(response.status_code, 400) - response = self.client.post('/tag', app.config['API_SUBDOMAIN'], headers=headers, data=good_data) - self.assertEqual(response.json, {'success': True}) + # DELETE method - bad data + response = self.client.delete('/tags', app.config['API_SUBDOMAIN'], headers=headers, data=bad_data) + self.assertEqual(response.status_code, 400) - response = self.client.get('/tag', app.config['API_SUBDOMAIN'], headers=headers, data=good_data) - self.assertEqual(response.json, [{'tag_name': 'testing'}]) + # DELETE method - delete the testing tag + response = self.client.delete('/tags', app.config['API_SUBDOMAIN'], headers=headers, data=get_data) self.assertEqual(response.status_code, 200) + self.assertEqual(response.json, {"success": True}) def test_api_user(self): """ Check insert user """ -- cgit v1.2.3