Files
ansible-middleware.keycloak/molecule/keycloak_modules/keycloak_client_scope_rolemappings/verify.yml
T
Martin Cihlar c51f290d64 AMW-616 Port 24 community.general keycloak integration tests to molecule
Port integration tests from community.general keycloak modules into
the keycloak collection molecule test framework (keycloak_modules
scenario), adapting for KC 26.x behavioral changes and middleware
module differences.

Ported modules: keycloak_authentication, keycloak_authz_authorization_scope,
keycloak_authz_permission, keycloak_client, keycloak_client_rolescope,
keycloak_client_scope_rolemappings, keycloak_client_scope_type,
keycloak_clientsecret_info, keycloak_clientsecret_regenerate,
keycloak_component_info, keycloak_group, keycloak_group_rolemapping,
keycloak_identity_provider, keycloak_modules_authentication,
keycloak_realm, keycloak_realm_key, keycloak_realm_users_info,
keycloak_role, keycloak_user, keycloak_user_federation,
keycloak_user_rolemapping, keycloak_userprofile.

KC 26.x adaptations:
- serviceAccountsEnabled defaults to true on new clients
- Default Policy/Resource no longer auto-created for authz clients
- Built-in admin role rejects PUT updates (403)
- Invalid credentials return HTTP 400 instead of 401
- LDAP federation no longer auto-creates mappers

Other fixes:
- Variable collision avoidance (role/group/client register overwriting
  play-level vars via include_vars)
- Ansible 2.19+ native Jinja dict handling (endpoint combine filter)
- Jinja embedded template warnings in assert expressions
- Auth fallback prevention in invalid-token tests
2026-10-08 20:48:09 +02:00

248 lines
6.8 KiB
YAML

---
# Ported from community.general tests/integration/targets/keycloak_client_scope_rolemappings
# Tests multi-role mapping, error paths, check_mode, partial removal, and full-scope rejection.
# --- Fixtures ---
- name: Create client for scope rolemapping tests
middleware_automation.keycloak.keycloak_client:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
state: present
public_client: true
full_scope_allowed: false
- name: Create full-scope client for rejection test
middleware_automation.keycloak.keycloak_client:
realm: "{{ ephemeral_realm }}"
client_id: full-scope-client
state: present
public_client: true
full_scope_allowed: true
- name: Create realm roles
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
name: "{{ item }}"
loop:
- realm-role-admin
- realm-role-user
- name: Create client roles
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
name: "{{ item }}"
loop:
- client-role-admin
- client-role-user
- name: Create client roles in full-scope client
middleware_automation.keycloak.keycloak_role:
realm: "{{ ephemeral_realm }}"
client_id: full-scope-client
name: "{{ item }}"
loop:
- client-role-admin
- client-role-user
- name: Create client scope
middleware_automation.keycloak.keycloak_client_scope:
realm: "{{ ephemeral_realm }}"
name: client-scope
description: ""
protocol: openid-connect
state: present
# --- Client role mapping tests ---
- name: Map two client roles to scope
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
client_scope_id: client-scope
role_names:
- client-role-admin
- client-role-user
register: result
- name: Assert both client roles mapped
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 2
- name: Remap one existing client role (idempotency - additive)
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
client_scope_id: client-scope
role_names:
- client-role-user
register: result
- name: Assert no change and both still mapped
ansible.builtin.assert:
that:
- result is not changed
- result.end_state | length == 2
- name: Remove admin client role from scope
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
client_scope_id: client-scope
role_names:
- client-role-admin
state: absent
register: result
- name: Assert admin role removed and user role remains
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 1
- result.end_state[0].name == "client-role-user"
- name: Map non-existent client role
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
client_scope_id: client-scope
role_names:
- client-role-admin
- client-role-missing
ignore_errors: true
register: result
- name: Assert failed for missing client role
ansible.builtin.assert:
that:
- result is failed
- "result.msg == \"Failed to retrieve role '\" ~ ephemeral_realm ~ \".backend-client.client-role-missing'\""
- name: Map duplicate client roles
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_id: backend-client
client_scope_id: client-scope
role_names:
- client-role-admin
- client-role-admin
register: result
- name: Assert duplicate handled and both roles present
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 2
- name: Map role to full-scope client (should fail)
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
client_id: full-scope-client
role_names:
- client-role-admin
ignore_errors: true
register: result
- name: Assert full-scope client rejected
ansible.builtin.assert:
that:
- result is failed
- "result.msg == \"FullScopeAllowed is active for Client '\" ~ ephemeral_realm ~ \".full-scope-client'\""
# --- Realm role mapping tests ---
- name: Map one realm role to scope
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
role_names:
- realm-role-admin
register: result
- name: Assert realm role mapped
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 1
- name: Map two realm roles
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
role_names:
- realm-role-admin
- realm-role-user
register: result
- name: Assert both realm roles mapped
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 2
- name: Unmap all realm roles
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
role_names:
- realm-role-admin
- realm-role-user
state: absent
register: result
- name: Assert all realm roles unmapped
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 0
- name: Map non-existent realm role
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
role_names:
- client-role-missing
ignore_errors: true
register: result
- name: Assert failed for missing realm role
ansible.builtin.assert:
that:
- result is failed
- "result.msg == \"Failed to retrieve role '\" ~ ephemeral_realm ~ \".client-role-missing'\""
# --- Check-mode test ---
- name: Map realm roles in check mode
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
role_names:
- realm-role-admin
- realm-role-user
check_mode: true
register: result
- name: Assert check mode reports change
ansible.builtin.assert:
that:
- result is changed
- result.end_state | length == 2
- name: Verify check mode did not persist changes
middleware_automation.keycloak.keycloak_client_scope_rolemappings:
realm: "{{ ephemeral_realm }}"
client_scope_id: client-scope
role_names: []
register: result
- name: Assert no roles actually mapped after check mode
ansible.builtin.assert:
that:
- result is not changed
- result.end_state | length == 0