First half of pylint fixes for the unit tests.

This commit is contained in:
yackob03 2014-02-06 14:40:36 -05:00
parent 2a89c2bb35
commit ec8f599a11

View file

@ -1,11 +1,10 @@
import unittest
import json
import json as py_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
from data import model
app.register_blueprint(api, url_prefix='/api')
@ -25,6 +24,7 @@ NEW_USER_DETAILS = {
'email': 'bobby@tables.com',
}
class ApiTestCase(unittest.TestCase):
def setUp(self):
setup_database_for_testing(self)
@ -40,12 +40,13 @@ class ApiTestCase(unittest.TestCase):
rv = self.app.get(url_for(method_name, **params))
self.assertEquals(200, rv.status_code)
data = rv.data
parsed = json.loads(data)
parsed = py_json.loads(data)
return parsed
def postResponse(self, method_name, params={}, data={}, expected_code=200):
rv = self.app.post(url_for(method_name, **params), data=json.dumps(data),
headers={"Content-Type": "application/json"})
rv = self.app.post(url_for(method_name, **params),
data=py_json.dumps(data),
headers={"Content-Type": "application/json"})
self.assertEquals(rv.status_code, expected_code)
return rv.data
@ -59,36 +60,43 @@ class ApiTestCase(unittest.TestCase):
self.assertEquals(rv.status_code, expected_code)
return rv.data
def postJsonResponse(self, method_name, params={}, data={}, expected_code=200):
rv = self.app.post(url_for(method_name, **params), data=json.dumps(data),
headers={"Content-Type": "application/json"})
def postJsonResponse(self, method_name, params={}, data={},
expected_code=200):
rv = self.app.post(url_for(method_name, **params),
data=py_json.dumps(data),
headers={"Content-Type": "application/json"})
if rv.status_code != expected_code:
print 'Mismatch data for method %s: %s' % (method_name, rv.data)
self.assertEquals(rv.status_code, expected_code)
data = rv.data
parsed = json.loads(data)
parsed = py_json.loads(data)
return parsed
def putJsonResponse(self, method_name, params={}, data={}, expected_code=200):
rv = self.app.put(url_for(method_name, **params), data=json.dumps(data),
headers={"Content-Type": "application/json"})
def putJsonResponse(self, method_name, params={}, data={},
expected_code=200):
rv = self.app.put(url_for(method_name, **params), data=py_json.dumps(data),
headers={"Content-Type": "application/json"})
if rv.status_code != expected_code:
print 'Mismatch data for method %s: %s' % (method_name, rv.data)
self.assertEquals(rv.status_code, expected_code)
data = rv.data
parsed = json.loads(data)
parsed = py_json.loads(data)
return parsed
def login(self, username, password='password'):
return self.postJsonResponse('api.signin_user', data=dict(username=username, password=password))
return self.postJsonResponse('api.signin_user',
data=dict(username=username,
password=password))
class TestDiscovery(ApiTestCase):
def test_discovery(self):
""" Basic sanity check that discovery returns valid JSON in the expected format. """
""" 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']:
@ -96,9 +104,11 @@ class TestDiscovery(ApiTestCase):
assert 'discovery' in found
class TestPlans(ApiTestCase):
def test_plans(self):
""" Basic sanity check that the plans are returned in the expected format. """
""" Basic sanity check that the plans are returned in the expectedformat.
"""
json = self.getJsonResponse('api.list_plans')
found = set([])
for method_info in json['plans']:
@ -106,6 +116,7 @@ class TestPlans(ApiTestCase):
assert 'free' in found
class TestLoggedInUser(ApiTestCase):
def test_guest(self):
json = self.getJsonResponse('api.get_logged_in_user')
@ -117,6 +128,7 @@ class TestLoggedInUser(ApiTestCase):
assert json['anonymous'] == False
assert json['username'] == READ_ACCESS_USER
class TestGetUserPrivateCount(ApiTestCase):
def test_nonallowed(self):
self.login(READ_ACCESS_USER)
@ -130,11 +142,13 @@ class TestGetUserPrivateCount(ApiTestCase):
assert json['privateCount'] == 6
assert json['reposAllowed'] > 0
class TestConvertToOrganization(ApiTestCase):
def test_sameadminuser(self):
self.login(READ_ACCESS_USER)
json = self.postJsonResponse('api.convert_user_to_organization',
data={'adminUser': READ_ACCESS_USER, 'adminPassword': 'password'},
data={'adminUser': READ_ACCESS_USER,
'adminPassword': 'password'},
expected_code=400)
self.assertEqual('The admin user is not valid', json['message'])
@ -142,18 +156,22 @@ class TestConvertToOrganization(ApiTestCase):
def test_invalidadminuser(self):
self.login(READ_ACCESS_USER)
json = self.postJsonResponse('api.convert_user_to_organization',
data={'adminUser': 'unknownuser', 'adminPassword': 'password'},
data={'adminUser': 'unknownuser',
'adminPassword': 'password'},
expected_code=400)
self.assertEqual('The admin user credentials are not valid', json['message'])
self.assertEqual('The admin user credentials are not valid',
json['message'])
def test_invalidadminpassword(self):
self.login(READ_ACCESS_USER)
json = self.postJsonResponse('api.convert_user_to_organization',
data={'adminUser': ADMIN_ACCESS_USER, 'adminPassword': 'invalidpass'},
data={'adminUser': ADMIN_ACCESS_USER,
'adminPassword': 'invalidpass'},
expected_code=400)
self.assertEqual('The admin user credentials are not valid', json['message'])
self.assertEqual('The admin user credentials are not valid',
json['message'])
def test_convert(self):
self.login(READ_ACCESS_USER)
@ -170,7 +188,8 @@ class TestConvertToOrganization(ApiTestCase):
# Verify the admin user is the org's admin.
self.login(ADMIN_ACCESS_USER)
json = self.getJsonResponse('api.get_organization', params=dict(orgname=READ_ACCESS_USER))
json = self.getJsonResponse('api.get_organization',
params=dict(orgname=READ_ACCESS_USER))
self.assertEquals(READ_ACCESS_USER, json['name'])
self.assertEquals(True, json['is_admin'])
@ -179,16 +198,19 @@ class TestConvertToOrganization(ApiTestCase):
class TestChangeUserDetails(ApiTestCase):
def test_changepassword(self):
self.login(READ_ACCESS_USER)
self.putJsonResponse('api.change_user_details', data=dict(password='newpasswordiscool'))
self.putJsonResponse('api.change_user_details',
data=dict(password='newpasswordiscool'))
self.login(READ_ACCESS_USER, password='newpasswordiscool')
def test_changeinvoiceemail(self):
self.login(READ_ACCESS_USER)
json = self.putJsonResponse('api.change_user_details', data=dict(invoice_email=True))
json = self.putJsonResponse('api.change_user_details',
data=dict(invoice_email=True))
self.assertEquals(True, json['invoice_email'])
json = self.putJsonResponse('api.change_user_details', data=dict(invoice_email=False))
json = self.putJsonResponse('api.change_user_details',
data=dict(invoice_email=False))
self.assertEquals(False, json['invoice_email'])
@ -227,7 +249,8 @@ class TestGetMatchingEntities(ApiTestCase):
self.login(NO_ACCESS_USER)
json = self.getJsonResponse('api.get_matching_entities',
params=dict(prefix='o', namespace=ORGANIZATION, includeTeams=True))
params=dict(prefix='o', namespace=ORGANIZATION,
includeTeams=True))
names = set([r['name'] for r in json['results']])
assert 'outsideorg' in names
@ -237,7 +260,8 @@ class TestGetMatchingEntities(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.getJsonResponse('api.get_matching_entities',
params=dict(prefix='o', namespace=ORGANIZATION, includeTeams=True))
params=dict(prefix='o', namespace=ORGANIZATION,
includeTeams=True))
names = set([r['name'] for r in json['results']])
assert 'outsideorg' in names
@ -248,24 +272,29 @@ class TestCreateOrganization(ApiTestCase):
def test_existinguser(self):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse('api.create_organization', data=dict(name=ADMIN_ACCESS_USER),
json = self.postJsonResponse('api.create_organization',
data=dict(name=ADMIN_ACCESS_USER),
expected_code=400)
self.assertEquals('A user or organization with this name already exists', json['message'])
self.assertEquals('A user or organization with this name already exists',
json['message'])
def test_existingorg(self):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse('api.create_organization', data=dict(name=ORGANIZATION),
json = self.postJsonResponse('api.create_organization',
data=dict(name=ORGANIZATION),
expected_code=400)
self.assertEquals('A user or organization with this name already exists', json['message'])
self.assertEquals('A user or organization with this name already exists',
json['message'])
def test_createorg(self):
self.login(ADMIN_ACCESS_USER)
data = self.postResponse('api.create_organization',
data=dict(name='neworg', email='test@example.com'),
data=dict(name='neworg',
email='test@example.com'),
expected_code=201)
self.assertEquals('Created', data)
@ -275,7 +304,8 @@ class TestCreateOrganization(ApiTestCase):
assert organization is not None
# Verify the admin user is the org's admin.
json = self.getJsonResponse('api.get_organization', params=dict(orgname='neworg'))
json = self.getJsonResponse('api.get_organization',
params=dict(orgname='neworg'))
self.assertEquals('neworg', json['name'])
self.assertEquals(True, json['is_admin'])
@ -293,14 +323,16 @@ class TestGetOrganization(ApiTestCase):
def test_getorganization(self):
self.login(READ_ACCESS_USER)
json = self.getJsonResponse('api.get_organization', params=dict(orgname=ORGANIZATION))
json = self.getJsonResponse('api.get_organization',
params=dict(orgname=ORGANIZATION))
self.assertEquals(ORGANIZATION, json['name'])
self.assertEquals(False, json['is_admin'])
def test_getorganization_asadmin(self):
self.login(ADMIN_ACCESS_USER)
json = self.getJsonResponse('api.get_organization', params=dict(orgname=ORGANIZATION))
json = self.getJsonResponse('api.get_organization',
params=dict(orgname=ORGANIZATION))
self.assertEquals(ORGANIZATION, json['name'])
self.assertEquals(True, json['is_admin'])
@ -370,7 +402,9 @@ class TestCreateOrganizationPrototypes(ApiTestCase):
json = self.postJsonResponse('api.create_organization_prototype_permission',
params=dict(orgname=ORGANIZATION),
data=dict(role='read', delegate={'kind': 'team', 'name': 'readers'}))
data=dict(role='read',
delegate={'kind': 'team',
'name': 'readers'}))
self.assertEquals('read', json['role'])
pid = json['id']
@ -419,13 +453,12 @@ class TestUpdateOrganizationPrototypes(ApiTestCase):
# Update a prototype.
json = self.putJsonResponse('api.delete_organization_prototype_permission',
params=dict(orgname=ORGANIZATION, prototypeid=pid),
data=dict(role='admin'))
params=dict(orgname=ORGANIZATION,
prototypeid=pid), data=dict(role='admin'))
self.assertEquals('admin', json['role'])
class TestGetOrganiaztionMembers(ApiTestCase):
def test_getmembers(self):
self.login(ADMIN_ACCESS_USER)
@ -441,7 +474,8 @@ class TestGetOrganiaztionMembers(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.getJsonResponse('api.get_organization_member',
params=dict(orgname=ORGANIZATION, membername=ADMIN_ACCESS_USER))
params=dict(orgname=ORGANIZATION,
membername=ADMIN_ACCESS_USER))
self.assertEquals(ADMIN_ACCESS_USER, json['member']['name'])
self.assertEquals('user', json['member']['kind'])
@ -464,7 +498,8 @@ class TestGetOrganizationPrivateAllowed(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
data = self.postResponse('api.create_organization',
data=dict(name='neworg', email='test@example.com'),
data=dict(name='neworg',
email='test@example.com'),
expected_code=201)
json = self.getJsonResponse('api.get_organization_private_allowed',
@ -478,8 +513,10 @@ class TestUpdateOrganizationTeam(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
data = self.postJsonResponse('api.update_organization_team',
params=dict(orgname=ORGANIZATION, teamname='readers'),
data=dict(description = 'My cool team', role = 'creator'))
params=dict(orgname=ORGANIZATION,
teamname='readers'),
data=dict(description='My cool team',
role='creator'))
self.assertEquals('My cool team', data['description'])
self.assertEquals('creator', data['role'])
@ -496,15 +533,18 @@ class TestUpdateOrganizationTeam(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
data = self.putJsonResponse('api.update_organization_team',
params=dict(orgname=ORGANIZATION, teamname='newteam'),
data=dict(description = 'My cool team', role = 'member'),
params=dict(orgname=ORGANIZATION,
teamname='newteam'),
data=dict(description='My cool team',
role='member'),
expected_code=201)
self.assertEquals('My cool team', data['description'])
self.assertEquals('member', data['role'])
# Verify the team was created.
json = self.getJsonResponse('api.get_organization', params=dict(orgname=ORGANIZATION))
json = self.getJsonResponse('api.get_organization',
params=dict(orgname=ORGANIZATION))
assert 'newteam' in json['teams']
@ -516,7 +556,8 @@ class TestDeleteOrganizationTeam(ApiTestCase):
params=dict(orgname=ORGANIZATION, teamname='readers'))
# Make sure the team was deleted
json = self.getJsonResponse('api.get_organization', params=dict(orgname=ORGANIZATION))
json = self.getJsonResponse('api.get_organization',
params=dict(orgname=ORGANIZATION))
assert not 'readers' in json['teams']
def test_attemptdeleteowners(self):
@ -539,7 +580,8 @@ class TestGetOrganizationTeamMembers(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.getJsonResponse('api.get_organization_team_members',
params=dict(orgname=ORGANIZATION, teamname='readers'))
params=dict(orgname=ORGANIZATION,
teamname='readers'))
assert READ_ACCESS_USER in json['members']
@ -549,12 +591,14 @@ class TestUpdateOrganizationTeamMember(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
self.postJsonResponse('api.update_organization_team_member',
params=dict(orgname=ORGANIZATION, teamname='readers', membername=NO_ACCESS_USER))
params=dict(orgname=ORGANIZATION, teamname='readers',
membername=NO_ACCESS_USER))
# Verify the user was added to the team.
json = self.getJsonResponse('api.get_organization_team_members',
params=dict(orgname=ORGANIZATION, teamname='readers'))
params=dict(orgname=ORGANIZATION,
teamname='readers'))
assert NO_ACCESS_USER in json['members']
@ -564,12 +608,14 @@ class TestDeleteOrganizationTeamMember(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
self.deleteResponse('api.delete_organization_team_member',
params=dict(orgname=ORGANIZATION, teamname='readers', membername=READ_ACCESS_USER))
params=dict(orgname=ORGANIZATION, teamname='readers',
membername=READ_ACCESS_USER))
# Verify the user was removed from the team.
json = self.getJsonResponse('api.get_organization_team_members',
params=dict(orgname=ORGANIZATION, teamname='readers'))
params=dict(orgname=ORGANIZATION,
teamname='readers'))
assert not READ_ACCESS_USER in json['members']
@ -579,7 +625,8 @@ class TestCreateRepo(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse('api.create_repo',
data=dict(repository='simple', visibility='public'),
data=dict(repository='simple',
visibility='public'),
expected_code=400)
self.assertEquals('Repository already exists', json['message'])
@ -589,7 +636,9 @@ class TestCreateRepo(ApiTestCase):
self.login(ADMIN_ACCESS_USER)
json = self.postJsonResponse('api.create_repo',
data=dict(repository='newrepo', visibility='public', description=''))
data=dict(repository='newrepo',
visibility='public',
description=''))
self.assertEquals(ADMIN_ACCESS_USER, json['namespace'])
@ -645,7 +694,9 @@ class TestListRepos(ApiTestCase):
def test_listrepos_filter(self):
self.login(READ_ACCESS_USER)
json = self.getJsonResponse('api.list_repos', params=dict(namespace=ORGANIZATION, public=False))
json = self.getJsonResponse('api.list_repos',
params=dict(namespace=ORGANIZATION,
public=False))
for repo in json['repositories']:
self.assertEquals(ORGANIZATION, repo['namespace'])
@ -658,63 +709,68 @@ class TestListRepos(ApiTestCase):
class TestUpdateRepo(ApiTestCase):
def test_updatedescription(self):
SIMPLE_REPO = ADMIN_ACCESS_USER + '/simple'
def test_updatedescription(self):
self.login(ADMIN_ACCESS_USER)
self.putJsonResponse('api.update_repo',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
params=dict(repository=self.SIMPLE_REPO),
data=dict(description='Some cool repo'))
# Verify the repo description was updated.
json = self.getJsonResponse('api.get_repo',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
params=dict(repository=self.SIMPLE_REPO))
self.assertEquals('Some cool repo', json['description'])
class TestChangeRepoVisibility(ApiTestCase):
def test_changevisibility(self):
SIMPLE_REPO = ADMIN_ACCESS_USER + '/simple'
def test_changevisibility(self):
self.login(ADMIN_ACCESS_USER)
# Make public.
self.postJsonResponse('api.change_repo_visibility',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
params=dict(repository=self.SIMPLE_REPO),
data=dict(visibility='public'))
# Verify the visibility.
json = self.getJsonResponse('api.get_repo',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
params=dict(repository=self.SIMPLE_REPO))
self.assertEquals(True, json['is_public'])
# Make private.
self.postJsonResponse('api.change_repo_visibility',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
params=dict(repository=self.SIMPLE_REPO),
data=dict(visibility='private'))
# Verify the visibility.
json = self.getJsonResponse('api.get_repo',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
params=dict(repository=self.SIMPLE_REPO))
self.assertEquals(False, json['is_public'])
class TestDeleteRepository(ApiTestCase):
def test_deleterepo(self):
SIMPLE_REPO = ADMIN_ACCESS_USER + '/simple'
def test_deleterepo(self):
self.login(ADMIN_ACCESS_USER)
self.deleteResponse('api.delete_repository',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'))
params=dict(repository=self.SIMPLE_REPO))
# Verify the repo was deleted.
self.getResponse('api.get_repo',
params=dict(repository=ADMIN_ACCESS_USER + '/simple'),
params=dict(repository=self.SIMPLE_REPO),
expected_code=404)
class TestGetRepository(ApiTestCase):
PUBLIC_REPO = PUBLIC_USER + '/publicrepo'
def test_getrepo_public_asguest(self):
json = self.getJsonResponse('api.get_repo',
params=dict(repository=PUBLIC_USER + '/publicrepo'))
params=dict(repository=self.PUBLIC_REPO))
self.assertEquals(PUBLIC_USER, json['namespace'])
self.assertEquals('publicrepo', json['name'])
@ -732,7 +788,7 @@ class TestGetRepository(ApiTestCase):
self.login(PUBLIC_USER)
json = self.getJsonResponse('api.get_repo',
params=dict(repository=PUBLIC_USER + '/publicrepo'))
params=dict(repository=self.PUBLIC_REPO))
self.assertEquals(False, json['is_organization'])
self.assertEquals(True, json['can_write'])
@ -887,6 +943,7 @@ class TestListAndGetImage(ApiTestCase):
self.assertEquals(image['id'], ijson['id'])
class TestGetImageChanges(ApiTestCase):
def test_getimagechanges(self):
self.login(ADMIN_ACCESS_USER)
@ -1067,6 +1124,7 @@ class TestRepoPermissions(ApiTestCase):
self.assertEquals(1, len(permissions))
assert not 'owners' in permissions
class TestApiTokens(ApiTestCase):
def listTokens(self):
return self.getJsonResponse('api.list_repo_tokens',