|
| 1 | +# Copyright 2023 Red Hat, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Experimental LDAP Integration https://issues.redhat.com/browse/AAP-16938 |
| 16 | +# All of this file includes minimal sanity tests for experimental LDAP |
| 17 | +# integration changes using django-ansible-base which will be removed later |
| 18 | + |
| 19 | +import pytest |
| 20 | +from rest_framework import status |
| 21 | +from rest_framework.test import APIClient |
| 22 | + |
| 23 | +from tests.integration.constants import api_url_v1 |
| 24 | + |
| 25 | +TEST_LDAP_AUTHENTICATOR = { |
| 26 | + "name": "Dev LDAP Container", |
| 27 | + "enabled": True, |
| 28 | + "configuration": { |
| 29 | + "BIND_DN": "cn=admin,dc=example,dc=org", |
| 30 | + "BIND_PASSWORD": "admin", |
| 31 | + "CONNECTION_OPTIONS": {"OPT_REFERRALS": 0, "OPT_NETWORK_TIMEOUT": 30}, |
| 32 | + "GROUP_SEARCH": [ |
| 33 | + "ou=groups,dc=example,dc=org", |
| 34 | + "SCOPE_SUBTREE", |
| 35 | + "(objectClass=groupOfNames)", |
| 36 | + ], |
| 37 | + "GROUP_TYPE": "MemberDNGroupType", |
| 38 | + "GROUP_TYPE_PARAMS": {"name_attr": "cn", "member_attr": "member"}, |
| 39 | + "SERVER_URI": ["ldap://host.containers.internal:389"], |
| 40 | + "START_TLS": True, |
| 41 | + "USER_ATTR_MAP": { |
| 42 | + "email": "mail", |
| 43 | + "last_name": "sn", |
| 44 | + "first_name": "givenName", |
| 45 | + }, |
| 46 | + "USER_DN_TEMPLATE": "cn=%(user)s,ou=users,dc=example,dc=org", |
| 47 | + "USER_SEARCH": [ |
| 48 | + "ou=users,dc=example,dc=org", |
| 49 | + "SCOPE_SUBTREE", |
| 50 | + "(cn=%(user)s)", |
| 51 | + ], |
| 52 | + }, |
| 53 | + "type": "aap_eda.core.authenticator_plugins.ldap", |
| 54 | +} |
| 55 | + |
| 56 | +TEST_AUTHENTICATOR_MAP = { |
| 57 | + "name": "Admin", |
| 58 | + "order": 1, |
| 59 | + "organization": "Admin", |
| 60 | + "revoke": True, |
| 61 | + "team": None, |
| 62 | + "triggers": { |
| 63 | + "groups": {"has_or": ["cn=admins,ou=groups,dc=example,dc=org"]} |
| 64 | + }, |
| 65 | + "map_type": "is_superuser", |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.django_db |
| 70 | +def test_create_authenticators(client: APIClient): |
| 71 | + response = client.post( |
| 72 | + f"{api_url_v1}/authenticators/", |
| 73 | + data=TEST_LDAP_AUTHENTICATOR, |
| 74 | + ) |
| 75 | + |
| 76 | + assert response.status_code == status.HTTP_201_CREATED |
| 77 | + data = response.data |
| 78 | + assert data["name"] == TEST_LDAP_AUTHENTICATOR["name"] |
| 79 | + assert data["enabled"] == TEST_LDAP_AUTHENTICATOR["enabled"] |
| 80 | + assert ( |
| 81 | + data["configuration"]["SERVER_URI"] |
| 82 | + == TEST_LDAP_AUTHENTICATOR["configuration"]["SERVER_URI"] |
| 83 | + ) |
| 84 | + assert data["type"] == TEST_LDAP_AUTHENTICATOR["type"] |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.django_db |
| 88 | +def test_list_authenticators(client: APIClient): |
| 89 | + response = client.get(f"{api_url_v1}/authenticators/") |
| 90 | + assert response.status_code == status.HTTP_200_OK |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.django_db |
| 94 | +def test_create_authenticator_maps(client: APIClient): |
| 95 | + auth_response = client.post( |
| 96 | + f"{api_url_v1}/authenticators/", |
| 97 | + data=TEST_LDAP_AUTHENTICATOR, |
| 98 | + ) |
| 99 | + |
| 100 | + response = client.post( |
| 101 | + f"{api_url_v1}/authenticator_maps/", |
| 102 | + data={ |
| 103 | + "authenticator": auth_response.data["id"], |
| 104 | + **TEST_AUTHENTICATOR_MAP, |
| 105 | + }, |
| 106 | + ) |
| 107 | + assert response.status_code == status.HTTP_201_CREATED |
| 108 | + data = response.data |
| 109 | + assert data["name"] == TEST_AUTHENTICATOR_MAP["name"] |
| 110 | + assert data["organization"] == TEST_AUTHENTICATOR_MAP["organization"] |
| 111 | + assert data["map_type"] == TEST_AUTHENTICATOR_MAP["map_type"] |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.django_db |
| 115 | +def test_list_authenticator_maps(client: APIClient): |
| 116 | + response = client.get(f"{api_url_v1}/authenticator_maps/") |
| 117 | + assert response.status_code == status.HTTP_200_OK |
0 commit comments