From 3b60529488be0725407197af2a3b364d182ae0c0 Mon Sep 17 00:00:00 2001 From: John Westcott IV Date: Mon, 8 Jun 2020 06:42:33 -0400 Subject: [PATCH] Fixes in testing and parameter processing --- awx_collection/plugins/modules/tower_token.py | 13 ++++++++++--- awx_collection/test/awx/test_token.py | 5 +++-- .../targets/tower_token/tasks/main.yml | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/awx_collection/plugins/modules/tower_token.py b/awx_collection/plugins/modules/tower_token.py index 3ee2e2b504..3be5cebde5 100644 --- a/awx_collection/plugins/modules/tower_token.py +++ b/awx_collection/plugins/modules/tower_token.py @@ -143,7 +143,16 @@ def main(): ) # Create a module for ourselves - module = TowerModule(argument_spec=argument_spec) + module = TowerModule( + argument_spec=argument_spec, + mutually_exclusive=[ + ('existing_token', 'existing_token_id'), + ], + # If we are state absent make sure one of existing_token or existing_token_id are present + required_if=[ + [ 'state', 'absent', ('existing_token', 'existing_token_id'), True,], + ], + ) # Extract our parameters description = module.params.get('description') @@ -155,8 +164,6 @@ def main(): if state == 'absent': if not existing_token: - if not existing_token_id: - module.fail_json(msg='When deleting a token you specify either the parameter existing_token or existing_token_id') existing_token = module.get_one('tokens', **{ 'data': { 'id': existing_token_id, diff --git a/awx_collection/test/awx/test_token.py b/awx_collection/test/awx/test_token.py index 107a833af6..e3f1d2a8c1 100644 --- a/awx_collection/test/awx/test_token.py +++ b/awx_collection/test/awx/test_token.py @@ -22,7 +22,8 @@ def test_create_token(run_module, admin_user): } result = run_module('tower_token', module_args, admin_user) - assert result, result.get('changed') + assert result.get('changed'), result tokens = OAuth2AccessToken.objects.filter(description='barfoo') - assert len(tokens) == 1, tokens[0].description == 'barfoo' + assert len(tokens) == 1, 'Rokens with description of barfoo != 0: {0}'.format(len(tokens)) + assert tokens[0].scope == 'read', 'Token was not given read access' diff --git a/awx_collection/tests/integration/targets/tower_token/tasks/main.yml b/awx_collection/tests/integration/targets/tower_token/tasks/main.yml index 0c4d1031c1..2854d4e6ca 100644 --- a/awx_collection/tests/integration/targets/tower_token/tasks/main.yml +++ b/awx_collection/tests/integration/targets/tower_token/tasks/main.yml @@ -36,7 +36,22 @@ - assert: that: - results is failed - - '"When deleting a token you specify either the parameter existing_token or existing_token_id" == results.msg' + # We don't assert a message here because it handled by ansible + +- name: Try to delete a token with both existing_token or existing_token_id + tower_token: + existing_token: + id: 1234 + existing_token_id: 1234 + state: absent + register: results + ignore_errors: True + +- assert: + that: + - results is failed + # We don't assert a message here because it handled by ansible + - block: - name: Create a Token