Skip to content

Commit

Permalink
Add test for analytic ship
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealHaoLiu committed Aug 30, 2024
1 parent 4e32860 commit 65e15a5
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion awx/main/tests/functional/analytics/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import json
import os
import tarfile
import tempfile
from unittest import mock
import pytest

from django.conf import settings
from awx.main.analytics import gather, register
from django.test.utils import override_settings
from awx.main.analytics import gather, register, ship


@register('example', '1.0')
Expand Down Expand Up @@ -57,3 +59,83 @@ def test_gather(mock_valid_license):
os.remove(tgz)
except Exception:
pass


@pytest.fixture
def temp_analytic_tar():
# Create a temporary file and yield its path
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(b"data")
temp_file_path = temp_file.name
yield temp_file_path
# Clean up the temporary file after the test
os.remove(temp_file_path)


@pytest.fixture
def mock_analytic_post():
# Patch the Session.post method to return a mock response with status_code 200
with mock.patch('awx.main.analytics.core.requests.Session.post', return_value=mock.Mock(status_code=200)) as mock_post:
yield mock_post


@pytest.mark.parametrize(
"setting_map, expected_result, expected_auth",
[
# Test case 1: Valid Red Hat credentials
(
{
'REDHAT_USERNAME': 'redhat_user',
'REDHAT_PASSWORD': 'redhat_pass', # NOSONAR
'SUBSCRIPTION_USERNAME': None,
'SUBSCRIPTION_PASSWORD': None,
},
True,
('redhat_user', 'redhat_pass'),
),
# Test case 2: Valid Subscription credentials
(
{
'REDHAT_USERNAME': None,
'REDHAT_PASSWORD': None,
'SUBSCRIPTION_USERNAME': 'subs_user',
'SUBSCRIPTION_PASSWORD': 'subs_pass', # NOSONAR
},
True,
('subs_user', 'subs_pass'),
),
# Test case 3: No credentials
(
{
'REDHAT_USERNAME': None,
'REDHAT_PASSWORD': None,
'SUBSCRIPTION_USERNAME': None,
'SUBSCRIPTION_PASSWORD': None,
},
False,
None, # No request should be made
),
# Test case 4: Mixed credentials
(
{
'REDHAT_USERNAME': None,
'REDHAT_PASSWORD': 'redhat_pass', # NOSONAR
'SUBSCRIPTION_USERNAME': 'subs_user',
'SUBSCRIPTION_PASSWORD': None,
},
False,
None, # Invalid, no request should be made
),
],
)
@pytest.mark.django_db
def test_ship_credential(setting_map, expected_result, expected_auth, temp_analytic_tar, mock_analytic_post):
with override_settings(**setting_map):
result = ship(temp_analytic_tar)

assert result == expected_result
if expected_auth:
mock_analytic_post.assert_called_once()
assert mock_analytic_post.call_args[1]['auth'] == expected_auth
else:
mock_analytic_post.assert_not_called()

0 comments on commit 65e15a5

Please sign in to comment.