Start on unit tests for the API endpoint
This commit is contained in:
parent
fdb628a0b0
commit
900ccd4c47
2 changed files with 87 additions and 0 deletions
|
@ -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']
|
||||
|
|
85
test/test_api_usage.py
Normal file
85
test/test_api_usage.py
Normal file
|
@ -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()
|
Reference in a new issue