From 900ccd4c4772f547638e1e5ac0814a43723e7aaa Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Fri, 31 Jan 2014 16:19:29 -0500 Subject: [PATCH] Start on unit tests for the API endpoint --- endpoints/api.py | 2 + test/test_api_usage.py | 85 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/test_api_usage.py diff --git a/endpoints/api.py b/endpoints/api.py index 2b31e4700..b4cec5ca4 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -314,6 +314,8 @@ def create_new_user(): @internal_api_call def signin_user(): signin_data = request.get_json() + if not signin_data: + abort(404) username = signin_data['username'] password = signin_data['password'] diff --git a/test/test_api_usage.py b/test/test_api_usage.py new file mode 100644 index 000000000..23c1c1d33 --- /dev/null +++ b/test/test_api_usage.py @@ -0,0 +1,85 @@ +import unittest +import json + +from flask import url_for +from endpoints.api import api +from app import app +from initdb import setup_database_for_testing, finished_database_for_testing +from specs import build_specs + +app.register_blueprint(api, url_prefix='/api') + +NO_ACCESS_USER = 'freshuser' +READ_ACCESS_USER = 'reader' +ADMIN_ACCESS_USER = 'devtable' +ORGANIZATION = 'buynlarge' + +class ApiTestCase(unittest.TestCase): + def setUp(self): + setup_database_for_testing(self) + self.app = app.test_client() + self.ctx = app.test_request_context() + self.ctx.__enter__() + + def tearDown(self): + finished_database_for_testing(self) + self.ctx.__exit__(True, None, None) + + def getJsonResponse(self, method_name): + rv = self.app.get(url_for(method_name)) + assert rv.status_code == 200 + data = rv.data + parsed = json.loads(data) + return parsed + + def login(self, username): + self.app.post(url_for('api.signin_user'), + data=json.dumps(dict(username=username, password='password')), + headers={"Content-Type": "application/json"}) + +class TestDiscovery(ApiTestCase): + def test_discovery(self): + """ Basic sanity check that discovery returns valid JSON in the expected format. """ + json = self.getJsonResponse('api.discovery') + found = set([]) + for method_info in json['endpoints']: + found.add(method_info['name']) + + assert 'discovery' in found + +class TestPlans(ApiTestCase): + def test_plans(self): + """ Basic sanity check that the plans are returned in the expected format. """ + json = self.getJsonResponse('api.list_plans') + found = set([]) + for method_info in json['plans']: + found.add(method_info['stripeId']) + + assert 'free' in found + +class TestLoggedInUser(ApiTestCase): + def test_guest(self): + json = self.getJsonResponse('api.get_logged_in_user') + assert json['anonymous'] == True + + def test_user(self): + self.login(READ_ACCESS_USER) + json = self.getJsonResponse('api.get_logged_in_user') + assert json['anonymous'] == False + assert json['username'] == READ_ACCESS_USER + +class TestGetUserPrivateCount(ApiTestCase): + def test_nonallowed(self): + self.login(READ_ACCESS_USER) + json = self.getJsonResponse('api.get_user_private_count') + assert json['privateCount'] == 0 + assert json['reposAllowed'] == 0 + + def test_allowed(self): + self.login(ADMIN_ACCESS_USER) + json = self.getJsonResponse('api.get_user_private_count') + assert json['privateCount'] == 6 + assert json['reposAllowed'] == 125 + +if __name__ == '__main__': + unittest.main()