tests: replace unmaintained pytest-capturelog by its maintained fork pytest-catchlog
This commit is contained in:
parent
34b7b3f6c6
commit
83388d396c
|
@ -38,7 +38,7 @@ def private_settings(request):
|
|||
@pytest.fixture
|
||||
def caplog(caplog):
|
||||
import py.io
|
||||
caplog.setLevel(logging.INFO)
|
||||
caplog.set_level(logging.INFO)
|
||||
caplog.handler.stream = py.io.TextIO()
|
||||
caplog.handler.records = []
|
||||
return caplog
|
||||
|
|
|
@ -89,11 +89,11 @@ def test_provision_user_attributes(settings, django_user_model, caplog):
|
|||
assert user.email == 'test@example.net'
|
||||
assert user.is_superuser is False
|
||||
assert user.is_staff is False
|
||||
assert len(caplog.records()) == 4
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'set field first_name' in caplog.text()
|
||||
assert 'set field last_name' in caplog.text()
|
||||
assert 'set field email' in caplog.text()
|
||||
assert len(caplog.records) == 4
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'set field first_name' in caplog.text
|
||||
assert 'set field last_name' in caplog.text
|
||||
assert 'set field email' in caplog.text
|
||||
|
||||
|
||||
def test_provision_user_groups(settings, django_user_model, caplog):
|
||||
|
@ -102,18 +102,18 @@ def test_provision_user_groups(settings, django_user_model, caplog):
|
|||
user = SAMLBackend().authenticate(saml_attributes=saml_attributes)
|
||||
assert user.groups.count() == 3
|
||||
assert set(user.groups.values_list('name', flat=True)) == set(saml_attributes['group'])
|
||||
assert len(caplog.records()) == 4
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'adding group GroupA' in caplog.text()
|
||||
assert 'adding group GroupB' in caplog.text()
|
||||
assert 'adding group GroupC' in caplog.text()
|
||||
assert len(caplog.records) == 4
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'adding group GroupA' in caplog.text
|
||||
assert 'adding group GroupB' in caplog.text
|
||||
assert 'adding group GroupC' in caplog.text
|
||||
saml_attributes2 = saml_attributes.copy()
|
||||
saml_attributes2['group'] = ['GroupB', 'GroupC']
|
||||
user = SAMLBackend().authenticate(saml_attributes=saml_attributes2)
|
||||
assert user.groups.count() == 2
|
||||
assert set(user.groups.values_list('name', flat=True)) == set(saml_attributes2['group'])
|
||||
assert len(caplog.records()) == 5
|
||||
assert 'removing group GroupA' in caplog.records()[-1].message
|
||||
assert len(caplog.records) == 5
|
||||
assert 'removing group GroupA' in caplog.records[-1].message
|
||||
|
||||
|
||||
def test_provision_is_superuser(settings, django_user_model, caplog):
|
||||
|
@ -124,11 +124,11 @@ def test_provision_is_superuser(settings, django_user_model, caplog):
|
|||
user = SAMLBackend().authenticate(saml_attributes=saml_attributes)
|
||||
assert user.is_superuser is True
|
||||
assert user.is_staff is True
|
||||
assert 'flag is_staff and is_superuser added' in caplog.text()
|
||||
assert 'flag is_staff and is_superuser added' in caplog.text
|
||||
user = SAMLBackend().authenticate(saml_attributes=saml_attributes)
|
||||
assert user.is_superuser is True
|
||||
assert user.is_staff is True
|
||||
assert not 'flag is_staff and is_superuser removed' in caplog.text()
|
||||
assert not 'flag is_staff and is_superuser removed' in caplog.text
|
||||
|
||||
|
||||
def test_provision_absent_attribute(settings, django_user_model, caplog):
|
||||
|
@ -142,11 +142,11 @@ def test_provision_absent_attribute(settings, django_user_model, caplog):
|
|||
del local_saml_attributes['email']
|
||||
user = SAMLBackend().authenticate(saml_attributes=local_saml_attributes)
|
||||
assert not user.email
|
||||
assert len(caplog.records()) == 4
|
||||
assert 'created new user' in caplog.text()
|
||||
assert re.search(r'invalid reference.*email', caplog.text())
|
||||
assert 'set field first_name' in caplog.text()
|
||||
assert 'set field last_name' in caplog.text()
|
||||
assert len(caplog.records) == 4
|
||||
assert 'created new user' in caplog.text
|
||||
assert re.search(r'invalid reference.*email', caplog.text)
|
||||
assert 'set field first_name' in caplog.text
|
||||
assert 'set field last_name' in caplog.text
|
||||
|
||||
|
||||
def test_provision_long_attribute(settings, django_user_model, caplog):
|
||||
|
@ -160,12 +160,12 @@ def test_provision_long_attribute(settings, django_user_model, caplog):
|
|||
local_saml_attributes['first_name'] = [('y' * 32)]
|
||||
user = SAMLBackend().authenticate(saml_attributes=local_saml_attributes)
|
||||
assert user.first_name == 'y' * 30
|
||||
assert len(caplog.records()) == 4
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'set field first_name' in caplog.text()
|
||||
assert 'to value %r ' % (u'y' * 30) in caplog.text()
|
||||
assert 'set field last_name' in caplog.text()
|
||||
assert 'set field email' in caplog.text()
|
||||
assert len(caplog.records) == 4
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'set field first_name' in caplog.text
|
||||
assert 'to value %r ' % (u'y' * 30) in caplog.text
|
||||
assert 'set field last_name' in caplog.text
|
||||
assert 'set field email' in caplog.text
|
||||
|
||||
|
||||
def test_lookup_user_transient_with_email(private_settings):
|
||||
|
|
|
@ -108,8 +108,8 @@ def test_sso_slo(db, app, idp, caplog, sp_settings):
|
|||
url, body = idp.process_authn_request_redirect(response['Location'])
|
||||
assert url.endswith(reverse('mellon_login'))
|
||||
response = app.post(reverse('mellon_login'), params={'SAMLResponse': body})
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'logged in using SAML' in caplog.text()
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'logged in using SAML' in caplog.text
|
||||
assert response['Location'].endswith(sp_settings.LOGIN_REDIRECT_URL)
|
||||
|
||||
|
||||
|
@ -118,8 +118,8 @@ def test_sso(db, app, idp, caplog, sp_settings):
|
|||
url, body = idp.process_authn_request_redirect(response['Location'])
|
||||
assert url.endswith(reverse('mellon_login'))
|
||||
response = app.post(reverse('mellon_login'), params={'SAMLResponse': body})
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'logged in using SAML' in caplog.text()
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'logged in using SAML' in caplog.text
|
||||
assert response['Location'].endswith(sp_settings.LOGIN_REDIRECT_URL)
|
||||
|
||||
|
||||
|
@ -129,7 +129,7 @@ def test_sso_request_denied(db, app, idp, caplog, sp_settings):
|
|||
assert url.endswith(reverse('mellon_login'))
|
||||
response = app.post(reverse('mellon_login'), params={'SAMLResponse': body})
|
||||
assert "status is not success codes: [u'urn:oasis:names:tc:SAML:2.0:status:Responder',\
|
||||
u'urn:oasis:names:tc:SAML:2.0:status:RequestDenied']" in caplog.text()
|
||||
u'urn:oasis:names:tc:SAML:2.0:status:RequestDenied']" in caplog.text
|
||||
|
||||
|
||||
def test_sso_artifact(db, app, caplog, sp_settings, idp_metadata, idp_private_key, rf):
|
||||
|
@ -145,29 +145,29 @@ def test_sso_artifact(db, app, caplog, sp_settings, idp_metadata, idp_private_ke
|
|||
acs_artifact_url = url.split('testserver', 1)[1]
|
||||
with HTTMock(idp.mock_artifact_resolver()):
|
||||
response = app.get(acs_artifact_url)
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'logged in using SAML' in caplog.text()
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'logged in using SAML' in caplog.text
|
||||
assert response['Location'].endswith(sp_settings.LOGIN_REDIRECT_URL)
|
||||
# force delog
|
||||
app.session.flush()
|
||||
assert 'dead artifact' not in caplog.text()
|
||||
assert 'dead artifact' not in caplog.text
|
||||
with HTTMock(idp.mock_artifact_resolver()):
|
||||
response = app.get(acs_artifact_url)
|
||||
# verify retry login was asked
|
||||
assert 'dead artifact' in caplog.text()
|
||||
assert 'dead artifact' in caplog.text
|
||||
assert response.status_code == 302
|
||||
assert reverse('mellon_login') in url
|
||||
response = response.follow()
|
||||
url, body = idp.process_authn_request_redirect(response['Location'])
|
||||
reset_caplog(caplog)
|
||||
# verify caplog has been cleaned
|
||||
assert 'created new user' not in caplog.text()
|
||||
assert 'created new user' not in caplog.text
|
||||
assert body is None
|
||||
assert reverse('mellon_login') in url
|
||||
assert 'SAMLart' in url
|
||||
acs_artifact_url = url.split('testserver', 1)[1]
|
||||
with HTTMock(idp.mock_artifact_resolver()):
|
||||
response = app.get(acs_artifact_url)
|
||||
assert 'created new user' in caplog.text()
|
||||
assert 'logged in using SAML' in caplog.text()
|
||||
assert 'created new user' in caplog.text
|
||||
assert 'logged in using SAML' in caplog.text
|
||||
assert response['Location'].endswith(sp_settings.LOGIN_REDIRECT_URL)
|
||||
|
|
|
@ -23,7 +23,7 @@ def test_create_server_connection_error(mocker, rf, private_settings, caplog):
|
|||
]
|
||||
request = rf.get('/')
|
||||
create_server(request)
|
||||
assert 'connection error' in caplog.text()
|
||||
assert 'connection error' in caplog.text
|
||||
|
||||
|
||||
def test_create_server_internal_server_error(mocker, rf, private_settings, caplog):
|
||||
|
@ -33,10 +33,10 @@ def test_create_server_internal_server_error(mocker, rf, private_settings, caplo
|
|||
}
|
||||
]
|
||||
request = rf.get('/')
|
||||
assert not 'failed with error' in caplog.text()
|
||||
assert not 'failed with error' in caplog.text
|
||||
with HTTMock(error_500):
|
||||
create_server(request)
|
||||
assert 'failed with error' in caplog.text()
|
||||
assert 'failed with error' in caplog.text
|
||||
|
||||
|
||||
def test_create_server_invalid_metadata(mocker, rf, private_settings, caplog):
|
||||
|
@ -46,11 +46,11 @@ def test_create_server_invalid_metadata(mocker, rf, private_settings, caplog):
|
|||
}
|
||||
]
|
||||
request = rf.get('/')
|
||||
assert not 'failed with error' in caplog.text()
|
||||
assert not 'failed with error' in caplog.text
|
||||
with HTTMock(error_500):
|
||||
create_server(request)
|
||||
assert len(caplog.records()) == 1
|
||||
assert re.search('METADATA.*is invalid', caplog.text())
|
||||
assert len(caplog.records) == 1
|
||||
assert re.search('METADATA.*is invalid', caplog.text)
|
||||
|
||||
|
||||
def test_create_server_invalid_metadata_file(mocker, rf, private_settings, caplog):
|
||||
|
@ -60,7 +60,7 @@ def test_create_server_invalid_metadata_file(mocker, rf, private_settings, caplo
|
|||
}
|
||||
]
|
||||
request = rf.get('/')
|
||||
assert not 'failed with error' in caplog.text()
|
||||
assert not 'failed with error' in caplog.text
|
||||
with mock.patch('mellon.adapters.file', mock.mock_open(read_data='yyy'), create=True):
|
||||
with HTTMock(error_500):
|
||||
server = create_server(request)
|
||||
|
@ -78,7 +78,7 @@ def test_create_server_good_metadata_file(mocker, rf, private_settings, caplog):
|
|||
'mellon.adapters.file', mock.mock_open(read_data=file('tests/metadata.xml').read()),
|
||||
create=True):
|
||||
server = create_server(request)
|
||||
assert 'ERROR' not in caplog.text()
|
||||
assert 'ERROR' not in caplog.text
|
||||
assert len(server.providers) == 1
|
||||
|
||||
|
||||
|
@ -89,9 +89,9 @@ def test_create_server_good_metadata(mocker, rf, private_settings, caplog):
|
|||
}
|
||||
]
|
||||
request = rf.get('/')
|
||||
assert not 'failed with error' in caplog.text()
|
||||
assert not 'failed with error' in caplog.text
|
||||
server = create_server(request)
|
||||
assert 'ERROR' not in caplog.text()
|
||||
assert 'ERROR' not in caplog.text
|
||||
assert len(server.providers) == 1
|
||||
|
||||
|
||||
|
@ -101,9 +101,9 @@ def test_create_server_invalid_idp_dict(mocker, rf, private_settings, caplog):
|
|||
}
|
||||
]
|
||||
request = rf.get('/')
|
||||
assert not 'failed with error' in caplog.text()
|
||||
assert not 'failed with error' in caplog.text
|
||||
create_server(request)
|
||||
assert 'missing METADATA' in caplog.text()
|
||||
assert 'missing METADATA' in caplog.text
|
||||
|
||||
|
||||
def test_create_server_good_metadata_url(mocker, rf, private_settings, caplog):
|
||||
|
@ -114,10 +114,10 @@ def test_create_server_good_metadata_url(mocker, rf, private_settings, caplog):
|
|||
]
|
||||
|
||||
request = rf.get('/')
|
||||
assert not 'failed with error' in caplog.text()
|
||||
assert not 'failed with error' in caplog.text
|
||||
with HTTMock(metadata_response):
|
||||
server = create_server(request)
|
||||
assert 'ERROR' not in caplog.text()
|
||||
assert 'ERROR' not in caplog.text
|
||||
assert len(server.providers) == 1
|
||||
|
||||
|
||||
|
|
|
@ -220,7 +220,7 @@ def test_malfortmed_artifact(private_settings, client, caplog):
|
|||
}]
|
||||
response = client.get('/login/?SAMLart=xxx', status=400)
|
||||
assert 'artifact is malformed' in response.content
|
||||
assert 'artifact is malformed' in caplog.text()
|
||||
assert 'artifact is malformed' in caplog.text
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -236,7 +236,7 @@ def test_error_500_on_artifact_resolve(private_settings, client, caplog, artifac
|
|||
}]
|
||||
with HTTMock(error_500):
|
||||
client.get('/login/?SAMLart=%s' % artifact)
|
||||
assert 'IdP returned 500' in caplog.text()
|
||||
assert 'IdP returned 500' in caplog.text
|
||||
|
||||
|
||||
def test_invalid_msg_on_artifact_resolve(private_settings, client, caplog, artifact):
|
||||
|
@ -245,4 +245,4 @@ def test_invalid_msg_on_artifact_resolve(private_settings, client, caplog, artif
|
|||
}]
|
||||
with HTTMock(html_response):
|
||||
client.get('/login/?SAMLart=%s' % artifact)
|
||||
assert 'ArtifactResolveResponse is malformed' in caplog.text()
|
||||
assert 'ArtifactResolveResponse is malformed' in caplog.text
|
||||
|
|
Loading…
Reference in New Issue