Skip to content

Commit

Permalink
wcaOnPrem: raise a 404 with feature_not_available for playbook ExpGen
Browse files Browse the repository at this point in the history
Raise an err 404 with `feature_not_available` code when the client tries
to access the ExpGen endpoint of a OnPrem server.
  • Loading branch information
goneri committed May 28, 2024
1 parent e2d8e12 commit 3008efe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ansible_wisdom/ai/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ class ServiceUnavailable(BaseWisdomAPIException):
default_detail = 'An error occurred attempting to complete the request.'


class FeatureNotAvailable(BaseWisdomAPIException):
status_code = 404
default_code = 'feature_not_available'
default_detail = 'The feature is not available.'


class InternalServerError(BaseWisdomAPIException):
status_code = 500
default_code = 'internal_server'
Expand Down
9 changes: 9 additions & 0 deletions ansible_wisdom/ai/api/model_client/wca_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from requests.auth import HTTPBasicAuth
from requests.exceptions import HTTPError

from ansible_ai_connect.ai.api.exceptions import FeatureNotAvailable
from ansible_ai_connect.ai.api.formatter import (
get_task_names_from_prompt,
strip_task_preamble_from_multi_task_prompt,
Expand Down Expand Up @@ -581,3 +582,11 @@ def self_test(self) -> HealthCheckSummary:
)

return summary

def generate_playbook(
self, request, text: str = "", create_outline: bool = False, outline: str = ""
) -> tuple[str, str]:
raise FeatureNotAvailable

def explain_playbook(self, request, content: str) -> str:
raise FeatureNotAvailable
31 changes: 31 additions & 0 deletions ansible_wisdom/ai/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@
DataSource,
)
from ansible_ai_connect.ai.api.utils import segment_analytics_telemetry
from ansible_ai_connect.main.tests.test_views import create_user_with_provider
from ansible_ai_connect.organizations.models import Organization
from ansible_ai_connect.test_utils import (
WisdomAppsBackendMocking,
WisdomLogAwareMixin,
WisdomServiceLogAwareTestCase,
)
from ansible_ai_connect.users.constants import USER_SOCIAL_AUTH_PROVIDER_AAP

DEFAULT_SUGGESTION_ID = uuid.uuid4()

Expand Down Expand Up @@ -2744,3 +2746,32 @@ def test_service_unavailable(self, invoke):
with self.assertRaises(Exception):
r = self.client.post(reverse('generations'), payload, format='json')
self.assertEqual(r.status_code, HTTPStatus.SERVICE_UNAVAILABLE)


@modify_settings()
@override_settings(WCA_SECRET_BACKEND_TYPE='dummy')
@override_settings(ANSIBLE_AI_MODEL_MESH_API_TYPE="wca-onprem")
@override_settings(ANSIBLE_WCA_USERNAME='bo')
@override_settings(ANSIBLE_AI_MODEL_MESH_API_KEY='my-secret-key')
class TestFeatureEnableForWcaOnprem(WisdomAppsBackendMocking):

def setUp(self):
super().setUp()
self.username = 'u' + "".join(random.choices(string.digits, k=5))
self.user = create_user_with_provider(USER_SOCIAL_AUTH_PROVIDER_AAP, rh_org_id=1981, social_auth_extra_data={"aap_licensed": True})
self.user.save()

def tearDown(self):
Organization.objects.filter(id=1981).delete()
self.user.delete()
super().tearDown()

@override_settings(ANSIBLE_AI_ENABLE_TECH_PREVIEW=False)
def test_feature_not_enabled_yet(self):
payload = {
"content": "Install Wordpress on a RHEL9",
"explanationId": str(uuid.uuid4()),
}
self.client.force_login(user=self.user)
r = self.client.post(reverse('explanations'), payload)
self.assertEqual(r.status_code, 404)
3 changes: 2 additions & 1 deletion ansible_wisdom/main/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
from ansible_ai_connect.users.tests.test_users import create_user


def create_user_with_provider(user_provider):
def create_user_with_provider(user_provider, **kwargs):
return create_user(
username='test_user_name',
password='test_passwords',
provider=user_provider,
external_username='anexternalusername',
**kwargs,
)


Expand Down

0 comments on commit 3008efe

Please sign in to comment.