Move end to end auth tests for APIs into pytest

This commit is contained in:
Joseph Schorr 2018-07-17 16:28:03 -04:00
parent 496d94138c
commit fcb9fd3792
2 changed files with 63 additions and 69 deletions

View file

@ -0,0 +1,63 @@
import pytest
from mock import patch
from endpoints.api.search import EntitySearch, LinkExternalEntity
from endpoints.api.test.shared import conduct_api_call
from endpoints.test.shared import client_with_identity
from test.test_ldap import mock_ldap
from test.test_external_jwt_authn import fake_jwt
from test.test_keystone_auth import fake_keystone
from test.fixtures import *
@pytest.fixture(params=[
mock_ldap,
fake_jwt,
fake_keystone,
])
def auth_engine(request):
return request.param
@pytest.fixture(params=[
False,
True,
])
def requires_email(request):
return request.param
def test_entity_search(auth_engine, requires_email, client):
with auth_engine(requires_email=requires_email) as auth:
with patch('endpoints.api.search.authentication', auth):
# Try an unknown prefix.
response = conduct_api_call(client, EntitySearch, 'GET', params=dict(prefix='unknown'))
results = response.json['results']
assert len(results) == 0
# Try a known prefix.
response = conduct_api_call(client, EntitySearch, 'GET', params=dict(prefix='cool'))
results = response.json['results']
entity = results[0]
assert entity['name'] == 'cool.user'
assert entity['kind'] == 'external'
def test_link_external_entity(auth_engine, requires_email, client):
with auth_engine(requires_email=requires_email) as auth:
with patch('endpoints.api.search.authentication', auth):
with client_with_identity('devtable', client) as cl:
# Try an unknown user.
conduct_api_call(cl, LinkExternalEntity, 'POST', params=dict(username='unknownuser'),
expected_code=400)
# Try a known user.
response = conduct_api_call(cl, LinkExternalEntity, 'POST',
params=dict(username='cool.user'))
entity = response.json['entity']
assert entity['name'] == 'cool_user'
assert entity['kind'] == 'user'

View file

@ -1,69 +0,0 @@
import unittest
from mock import patch
from endpoints.api.search import EntitySearch, LinkExternalEntity
from test.test_api_usage import ApiTestCase, ADMIN_ACCESS_USER
from test.test_ldap import mock_ldap
from test.test_external_jwt_authn import fake_jwt
from test.test_keystone_auth import fake_keystone
class EndToEndAuthMixin:
def test_entity_search(self):
with self.get_authentication() as auth:
with patch('endpoints.api.search.authentication', auth):
# Try an unknown prefix.
json_data = self.getJsonResponse(EntitySearch, params=dict(prefix='unknown'))
results = json_data['results']
self.assertEquals(0, len(results))
# Try a known prefix.
json_data = self.getJsonResponse(EntitySearch, params=dict(prefix='cool'))
results = json_data['results']
self.assertEquals(1, len(results))
self.assertEquals('external', results[0]['kind'])
self.assertEquals('cool.user', results[0]['name'])
def test_link_external_entity(self):
with self.get_authentication() as auth:
with patch('endpoints.api.search.authentication', auth):
self.login(ADMIN_ACCESS_USER)
# Try an unknown user.
self.postResponse(LinkExternalEntity, params=dict(username='unknownuser'),
expected_code=400)
# Try a known user.
json_data = self.postJsonResponse(LinkExternalEntity, params=dict(username='cool.user'))
entity = json_data['entity']
self.assertEquals('cool_user', entity['name'])
self.assertEquals('user', entity['kind'])
class TestLDAPEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return mock_ldap()
class TestJWTEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return fake_jwt()
class TestKeystone3EndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return fake_keystone(3)
class TestLDAPNoEmailEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return mock_ldap(requires_email=False)
class TestJWTNoEmailEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return fake_jwt(requires_email=False)
class TestKeystone3NoEmailEndToEnd(ApiTestCase, EndToEndAuthMixin):
def get_authentication(self):
return fake_keystone(3, requires_email=False)
if __name__ == '__main__':
unittest.main()