Skip to content

Commit 1f06d1b

Browse files
arrestletyraziel
andauthored
[AAP-44277] License module now validates API responses for subscription IDs. (Moved from Tower) (#16096)
* resolve bug and add simple unit tests * Update awx_collection/plugins/modules/license.py Co-authored-by: Andrew Potozniak <[email protected]> --------- Co-authored-by: Andrew Potozniak <[email protected]>
1 parent 873f5c0 commit 1f06d1b

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

awx_collection/plugins/modules/license.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'''
6868

6969
import base64
70+
7071
from ..module_utils.controller_api import ControllerAPIModule
7172

7273

@@ -120,11 +121,17 @@ def main():
120121

121122
# Do the actual install, if we need to
122123
if perform_install:
123-
json_output['changed'] = True
124124
if module.params.get('manifest', None):
125-
module.post_endpoint('config', data={'manifest': manifest.decode()})
125+
response = module.post_endpoint('config', data={'manifest': manifest.decode()})
126126
else:
127-
module.post_endpoint('config/attach', data={'subscription_id': module.params.get('subscription_id')})
127+
response = module.post_endpoint('config/attach', data={'subscription_id': module.params.get('subscription_id')})
128+
129+
# Check API response for errors (AAP-44277 fix)
130+
if response and response.get('status_code') and response.get('status_code') != 200:
131+
error_msg = response.get('json', {}).get('error', 'License operation failed')
132+
module.fail_json(msg=error_msg)
133+
134+
json_output['changed'] = True
128135

129136
module.exit_json(**json_output)
130137

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
__metaclass__ = type
4+
5+
import pytest
6+
7+
8+
@pytest.mark.django_db
9+
def test_license_invalid_subscription_id_should_fail(run_module, admin_user):
10+
"""Test invalid subscription ID returns failure."""
11+
result = run_module('license', {'subscription_id': 'invalid-test-12345', 'state': 'present'}, admin_user)
12+
13+
assert result.get('failed', False)
14+
assert 'msg' in result
15+
assert 'subscription' in result['msg'].lower()
16+
17+
18+
@pytest.mark.django_db
19+
def test_license_invalid_manifest_should_fail(run_module, admin_user):
20+
"""Test invalid manifest returns failure."""
21+
result = run_module('license', {'manifest': '/nonexistent/test.zip', 'state': 'present'}, admin_user)
22+
23+
assert result.get('failed', False)
24+
assert 'msg' in result
25+
26+
27+
@pytest.mark.django_db
28+
def test_license_state_absent_works(run_module, admin_user):
29+
"""Test license removal works."""
30+
result = run_module('license', {'state': 'absent'}, admin_user)
31+
32+
assert not result.get('failed', False)

0 commit comments

Comments
 (0)