Skip to content

Commit 54e1ca4

Browse files
authored
Open /chatbot view to users whose organization has an AAP sub (#1711)
1 parent 6360ecd commit 54e1ca4

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

ansible_ai_connect/ai/api/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
BlockUserWithSeatButWCANotReady,
115115
BlockWCANotReadyButTrialAvailable,
116116
IsAAPLicensed,
117+
IsOrganisationLightspeedSubscriber,
117118
)
118119
from .serializers import (
119120
ChatFeedback,
@@ -1051,7 +1052,7 @@ class ChatEndpointThrottle(EndpointRateThrottle):
10511052
permission_classes = [
10521053
permissions.IsAuthenticated,
10531054
IsAuthenticatedOrTokenHasScope,
1054-
IsRHInternalUser | IsTestUser | IsAAPUser,
1055+
IsRHInternalUser | IsTestUser | IsAAPUser | IsOrganisationLightspeedSubscriber,
10551056
]
10561057
required_scopes = ["read", "write"]
10571058
schema1_event = schema1.ChatBotOperationalEvent
@@ -1149,7 +1150,7 @@ class StreamingChatEndpointThrottle(EndpointRateThrottle):
11491150
permission_classes = [
11501151
permissions.IsAuthenticated,
11511152
IsAuthenticatedOrTokenHasScope,
1152-
IsRHInternalUser | IsTestUser | IsAAPUser,
1153+
IsRHInternalUser | IsTestUser | IsAAPUser | IsOrganisationLightspeedSubscriber,
11531154
]
11541155
required_scopes = ["read", "write"]
11551156
schema1_event = schema1.StreamingChatBotOperationalEvent

ansible_ai_connect/main/tests/test_views.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
)
3434
from ansible_ai_connect.main.settings.base import SOCIAL_AUTH_OIDC_KEY
3535
from ansible_ai_connect.main.views import LoginView
36+
from ansible_ai_connect.organizations.models import Organization
3637
from ansible_ai_connect.test_utils import (
3738
APIVersionTestCaseBase,
3839
create_user_with_provider,
@@ -249,6 +250,7 @@ def test_get_view_expired_trial(self):
249250
@override_settings(CHATBOT_DEFAULT_PROVIDER="wisdom")
250251
@override_settings(ANSIBLE_AI_CHATBOT_NAME="Awesome Chatbot")
251252
@override_settings(CHATBOT_DEBUG_UI=False)
253+
@override_settings(AUTHZ_DUMMY_ORGS_WITH_SUBSCRIPTION="12345")
252254
class TestChatbotView(TestCase):
253255
CHATBOT_PAGE_TITLE = "<title>Awesome Chatbot</title>"
254256
DOCUMENT_URL = (
@@ -279,19 +281,36 @@ def setUp(self):
279281
rh_internal=False,
280282
)
281283
self.non_rh_test_user.groups.add(self.test_group)
284+
self.non_rh_user_with_subscription = create_user_with_provider(
285+
provider=USER_SOCIAL_AUTH_PROVIDER_OIDC,
286+
username="non-rh-user-with-subscription",
287+
password="non-rh-password",
288+
289+
rh_internal=False,
290+
rh_org_id=12345,
291+
)
282292

283293
def tearDown(self):
284294
self.non_rh_user.delete()
285295
self.rh_user.delete()
286296
self.non_rh_test_user.delete()
287297
self.test_group.delete()
298+
self.non_rh_user_with_subscription.delete()
299+
Organization.objects.filter(id=12345).delete()
288300

289301
def test_chatbot_link_with_anonymous_user(self):
290302
r = self.client.get(reverse("home"))
291303
self.assertEqual(r.status_code, HTTPStatus.OK)
292304
self.assertContains(r, TestChatbotView.DOCUMENT_URL)
293305
self.assertNotContains(r, "Chatbot")
294306

307+
def test_chatbot_link_with_non_rh_user_and_subscription(self):
308+
self.client.force_login(user=self.non_rh_user_with_subscription)
309+
r = self.client.get(reverse("home"))
310+
self.assertEqual(r.status_code, HTTPStatus.OK)
311+
self.assertContains(r, TestChatbotView.DOCUMENT_URL)
312+
self.assertContains(r, "Chatbot")
313+
295314
def test_chatbot_link_with_non_rh_user(self):
296315
self.client.force_login(user=self.non_rh_user)
297316
r = self.client.get(reverse("home"))
@@ -331,6 +350,11 @@ def test_chatbot_view_with_non_rh_user(self):
331350
r = self.client.get(reverse("chatbot"))
332351
self.assertEqual(r.status_code, HTTPStatus.FORBIDDEN)
333352

353+
def test_chatbot_view_with_non_rh_user_and_subscription(self):
354+
self.client.force_login(user=self.non_rh_user_with_subscription)
355+
r = self.client.get(reverse("chatbot"))
356+
self.assertEqual(r.status_code, HTTPStatus.OK)
357+
334358
@override_settings(CHATBOT_DEFAULT_PROVIDER="")
335359
def test_chatbot_view_with_rh_user_but_chatbot_disabled(self):
336360
self.client.force_login(user=self.rh_user)

ansible_ai_connect/main/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ class ChatbotView(ProtectedTemplateView):
125125
template_name = "chatbot/index.html"
126126

127127
permission_classes = [
128-
IsRHInternalUser | IsTestUser | IsAAPUser,
128+
IsAuthenticated,
129+
IsRHInternalUser | IsTestUser | IsAAPUser | IsOrganisationLightspeedSubscriber,
129130
]
130131

131132
chatbot_enabled: bool

ansible_ai_connect/test_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def create_user(
4545
provider: str = None,
4646
social_auth_extra_data: any = {},
4747
rh_user_is_org_admin: Optional[bool] = None,
48+
rh_org_has_subscription: Optional[bool] = None,
4849
rh_user_id: str = None,
4950
rh_org_id: int = 1234567,
5051
org_opt_out: bool = False,
@@ -65,6 +66,8 @@ def create_user(
6566
social_auth.set_extra_data(social_auth_extra_data)
6667
if rh_user_is_org_admin:
6768
user.rh_user_is_org_admin = rh_user_is_org_admin
69+
if rh_org_has_subscription:
70+
user.rh_org_has_subscription = rh_org_has_subscription
6871
user.save()
6972
return user
7073

ansible_ai_connect/users/templates/users/home.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ <h1 class="pf-c-title pf-m-lg">{{ project_name }}</h1>
131131
{% if deployment_mode == 'saas' and user.is_authenticated and user.rh_user_is_org_admin %}
132132
<a class="pf-l-level__item" href="/console"><span class="fas fa-solid fa-cog"></span> Admin Portal</a>
133133
{% endif %}
134-
{% if chatbot_enabled and deployment_mode == 'saas' and user.is_authenticated and rh_internal_user_or_test_user %}
134+
{% if chatbot_enabled and deployment_mode == 'saas' and user.is_authenticated and can_access_chatbot %}
135135
<a class="pf-l-level__item" href="/chatbot"><span class="fas fa-solid fa-comments"></span> Chatbot</a>
136136
{% endif %}
137137
</div>

ansible_ai_connect/users/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ def get_context_data(self, **kwargs):
113113
context["documentation_url"] = settings.COMMERCIAL_DOCUMENTATION_URL
114114

115115
user = self.request.user
116-
context["rh_internal_user_or_test_user"] = user.is_authenticated and (
117-
user.rh_internal or user.groups.filter(name="test").exists()
116+
context["can_access_chatbot"] = user.is_authenticated and (
117+
user.rh_internal
118+
or user.groups.filter(name="test").exists()
119+
or user.rh_org_has_subscription
118120
)
119121

120122
# Show chatbot link when the chatbot service is configured.

0 commit comments

Comments
 (0)