85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
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()
|