From c5df37777bf4b0d6562bb457b50a9b96f32eb2f7 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Tue, 22 Sep 2020 15:22:35 -0400 Subject: [PATCH] Addig tests and updating minor module bugs --- .../plugins/modules/tower_application.py | 32 ++++---- awx_collection/test/awx/test_application.py | 28 +++++++ .../targets/tower_application/tasks/main.yml | 79 +++++++++++++++++++ 3 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 awx_collection/test/awx/test_application.py create mode 100644 awx_collection/tests/integration/targets/tower_application/tasks/main.yml diff --git a/awx_collection/plugins/modules/tower_application.py b/awx_collection/plugins/modules/tower_application.py index 9efbd1d72d..acadc8e66e 100644 --- a/awx_collection/plugins/modules/tower_application.py +++ b/awx_collection/plugins/modules/tower_application.py @@ -37,13 +37,13 @@ options: default: "password" choices: ["password", "authorization-code"] type: str - required: True + required: False client_type: description: - Set to public or confidential depending on how secure the client device is. choices: ["public", "confidential"] type: str - required: True + required: False organization: description: - Name of organization for application. @@ -74,7 +74,7 @@ EXAMPLES = ''' description: "Foo bar application" organization: "test" state: present - authorization-grant-type: password + authorization_grant_type: password client-type: public - name: Add Foo application @@ -83,14 +83,15 @@ EXAMPLES = ''' description: "Foo bar application" organization: "test" state: present - authorization-grant-type: authorization-code + authorization_grant_type: authorization-code client-type: confidential - redirect_uris: http://tower.com/api/v2/ + redirect_uris: + - http://tower.com/api/v2/ ''' import time -from ..module_utils.tower_api import TowerModule +from ..module_utils.tower_api import TowerAPIModule def main(): @@ -98,8 +99,8 @@ def main(): argument_spec = dict( name=dict(required=True), description=dict(), - authorization_grant_type=dict(required=True), - client_type=dict(required=True, choices=['public', 'confidential']), + authorization_grant_type=dict(choices=["password", "authorization-code"]), + client_type=dict(choices=['public', 'confidential']), organization=dict(required=True), redirect_uris=dict(type="list", elements='str'), state=dict(choices=['present', 'absent'], default='present'), @@ -107,7 +108,7 @@ def main(): ) # Create a module for ourselves - module = TowerModule(argument_spec=argument_spec) + module = TowerAPIModule(argument_spec=argument_spec) # Extract our parameters name = module.params.get('name') @@ -115,16 +116,15 @@ def main(): authorization_grant_type = module.params.get('authorization_grant_type') client_type = module.params.get('client_type') organization = module.params.get('organization') - redirect_uris = ' '.join(module.params.get('redirect_uris')) + redirect_uris = module.params.get('redirect_uris') state = module.params.get('state') # Attempt to look up the related items the user specified (these will fail the module if not found) org_id = module.resolve_name_to_id('organizations', organization) # Attempt to look up application based on the provided name and org ID - application = module.get_one('applications', **{ + application = module.get_one('applications', name_or_id=name, **{ 'data': { - 'name': name, 'organization': org_id } }) @@ -139,14 +139,16 @@ def main(): # Create the data that gets sent for create and update application_fields = { 'name': name, - 'authorization_grant_type': authorization_grant_type, - 'client_type': client_type, 'organization': org_id, } + if authorization_grant_type is not None: + application_fields['authorization_grant_type'] = authorization_grant_type + if client_type is not None: + application_fields['client_type'] = client_type if description is not None: application_fields['description'] = description if redirect_uris is not None: - application_fields['redirect_uris'] = redirect_uris + application_fields['redirect_uris'] = ' '.join(redirect_uris) # If the state was present and we can let the module build or update the existing application, this will return on its own module.create_or_update_if_needed( diff --git a/awx_collection/test/awx/test_application.py b/awx_collection/test/awx/test_application.py new file mode 100644 index 0000000000..1ccb78c7d2 --- /dev/null +++ b/awx_collection/test/awx/test_application.py @@ -0,0 +1,28 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import pytest + +from awx.main.models import Organization, Application + + +@pytest.mark.django_db +def test_create_application(run_module, admin_user): + org = Organization.objects.create(name='foo') + + module_args = { + 'name': 'foo_app', + 'description': 'barfoo', + 'state': 'present', + 'authorization_grant_type': 'password', + 'client_type': 'public', + 'organization': 'foo', + } + + result = run_module('tower_application', module_args, admin_user) + assert result.get('changed'), result + + application = Application.objects.get(name='foo_app') + assert application.description == 'barfoo' + assert application.organization_id == org.id + diff --git a/awx_collection/tests/integration/targets/tower_application/tasks/main.yml b/awx_collection/tests/integration/targets/tower_application/tasks/main.yml new file mode 100644 index 0000000000..212aec884b --- /dev/null +++ b/awx_collection/tests/integration/targets/tower_application/tasks/main.yml @@ -0,0 +1,79 @@ +--- +- name: Generate a test id + set_fact: + test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" + +- name: Generate names + set_fact: + app1_name: "AWX-Collection-tests-tower_application-app1-{{ test_id }}" + app2_name: "AWX-Collection-tests-tower_application-app2-{{ test_id }}" + app3_name: "AWX-Collection-tests-tower_application-app3-{{ test_id }}" + +- block: + - name: Create an application + tower_application: + name: "{{ app1_name }}" + authorization_grant_type: "password" + client_type: "public" + organization: "Default" + state: present + register: result + + - assert: + that: + - "result is changed" + + - name: Delete our application + tower_application: + name: "{{ app1_name }}" + organization: "Default" + state: absent + register: result + + - assert: + that: + - "result is changed" + + - name: Create a second application + tower_application: + name: "{{ app2_name }}" + authorization_grant_type: "authorization-code" + client_type: "confidential" + organization: "Default" + description: "Another application" + redirect_uris: + - http://tower.com/api/v2/ + - http://tower.com/api/v2/teams + state: present + register: result + + - assert: + that: + - "result is changed" + + - name: Create an all trusting application + tower_application: + name: "{{ app3_name }}" + organization: "Default" + description: "All Trusting Application" + skip_authorization: True + authorization_grant_type: "password" + client_type: "confidential" + state: present + register: result + + - assert: + that: + - "result is changed" + + always: + - name: Delete our application + tower_application: + name: "{{ item }}" + organization: "Default" + state: absent + register: result + loop: + - "{{ app1_name }}" + - "{{ app2_name }}" + - "{{ app3_name }}"