authentic/tests/idp_oidc/test_manager.py

114 lines
4.1 KiB
Python

# authentic2 - versatile identity manager
# Copyright (C) 2010-2019 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from authentic2_idp_oidc import app_settings as oidc_app_settings
from authentic2_idp_oidc.models import OIDCClaim, OIDCClient
from tests.utils import login
@pytest.fixture
def app(app, admin):
login(app, admin)
return app
def test_add_oidc_service(app):
resp = app.get('/manage/services/')
assert 'Add OIDC service' in resp.text
assert OIDCClient.objects.count() == 0
assert OIDCClaim.objects.count() == 0
resp = resp.click('Add OIDC service')
form = resp.form
form['name'] = 'Test'
form['redirect_uris'] = 'http://example.com'
resp = form.submit()
assert OIDCClient.objects.count() == 1
assert OIDCClaim.objects.count() == len(oidc_app_settings.DEFAULT_MAPPINGS)
oidc_client = OIDCClient.objects.get()
assert resp.location == f'/manage/services/{oidc_client.pk}/'
resp = resp.follow()
assert "Settings" in resp.text
assert "Delete" in resp.text
class TestEdit:
@pytest.fixture(autouse=True)
def oidc_client(self, db):
return OIDCClient.objects.create(name='Test', slug='test', redirect_uris='http://example.com')
def test_edit(self, app):
resp = app.get('/manage/services/')
resp = resp.click('Test')
resp = resp.click('Settings')
resp = resp.click('Edit')
form = resp.form
form['name'] = 'New Test'
form['colour'] = '#ff00ff'
resp = form.submit()
assert resp.location == '..'
resp = resp.follow()
assert "New Test" in resp.text
assert "#ff00ff" in resp.text
def test_delete(self, app):
resp = app.get('/manage/services/')
resp = resp.click('Test')
resp = resp.click('Delete')
resp = resp.form.submit().follow()
assert OIDCClient.objects.count() == 0
def test_add_claim(self, app, oidc_client):
resp = app.get(f'/manage/services/{oidc_client.pk}/settings/')
resp = resp.click('Add claim')
form = resp.form
form['name'] = 'claim'
form['value'] = 'value'
form['scopes'] = 'profile'
resp = form.submit()
assert resp.location == f'/manage/services/{oidc_client.pk}/'
assert OIDCClaim.objects.filter(
client=oidc_client, name='claim', value='value', scopes='profile'
).exists()
class TestEditClaim:
@pytest.fixture(autouse=True)
def claim(self, oidc_client):
return OIDCClaim.objects.create(client=oidc_client, name='claim', value='value', scopes='profile')
def test_edit_claim(self, app, oidc_client, claim):
resp = app.get(f'/manage/services/{oidc_client.pk}/settings/')
assert "claim" in resp.text
resp = resp.click('Edit', index=1)
form = resp.form
form['value'] = 'new value'
resp = form.submit()
assert resp.location == f'/manage/services/{oidc_client.pk}/'
claim.refresh_from_db()
assert claim.value == 'new value'
def test_delete_claim(self, app, oidc_client):
resp = app.get(f'/manage/services/{oidc_client.pk}/settings/')
assert "claim" in resp.text
resp = resp.click('Delete')
form = resp.form
resp = form.submit()
assert resp.location == f'/manage/services/{oidc_client.pk}/'
assert OIDCClaim.objects.filter(client=oidc_client).count() == 0