|
1 | 1 | from django.contrib.auth.models import AnonymousUser |
2 | 2 | from django.test import RequestFactory, TestCase |
3 | 3 |
|
| 4 | +from kitsune.products.managers import ProductSupportConfigManager |
4 | 5 | from kitsune.products.models import ProductSupportConfig |
5 | 6 | from kitsune.products.tests import ( |
6 | 7 | ProductFactory, |
@@ -299,3 +300,89 @@ def test_no_support_channels_constraint(self): |
299 | 300 | ) |
300 | 301 |
|
301 | 302 | self.assertIn("at_least_one_support_channel", str(context.exception)) |
| 303 | + |
| 304 | + |
| 305 | +class SubscriptionRoutingTests(TestCase): |
| 306 | + """Tests for subscription_only routing in route_support_request().""" |
| 307 | + |
| 308 | + def setUp(self): |
| 309 | + self.factory = RequestFactory() |
| 310 | + self.product = ProductFactory(slug="mozilla-vpn") |
| 311 | + self.zendesk_config = ZendeskConfigFactory() |
| 312 | + self.user = UserFactory() |
| 313 | + self.config = ProductSupportConfigFactory( |
| 314 | + product=self.product, |
| 315 | + forum_config=None, |
| 316 | + zendesk_config=self.zendesk_config, |
| 317 | + is_active=True, |
| 318 | + subscription_only=True, |
| 319 | + ) |
| 320 | + |
| 321 | + def test_subscriber_routes_to_zendesk(self): |
| 322 | + """Subscribed user on a subscription_only product is routed to ZD.""" |
| 323 | + self.user.profile.products.add(self.product) |
| 324 | + |
| 325 | + request = self.factory.get("/") |
| 326 | + request.user = self.user |
| 327 | + |
| 328 | + support_type, can_switch = ProductSupportConfig.objects.route_support_request( |
| 329 | + request, self.product |
| 330 | + ) |
| 331 | + |
| 332 | + self.assertEqual(support_type, ProductSupportConfig.SUPPORT_TYPE_ZENDESK) |
| 333 | + self.assertFalse(can_switch) |
| 334 | + |
| 335 | + def test_non_subscriber_with_redirect_returns_redirect_sentinel(self): |
| 336 | + """Non-subscribed user is returned SUPPORT_TYPE_REDIRECT when redirect product is set.""" |
| 337 | + redirect_product = ProductFactory(slug="firefox") |
| 338 | + self.config.unsubscribed_redirect_product = redirect_product |
| 339 | + self.config.save() |
| 340 | + |
| 341 | + request = self.factory.get("/") |
| 342 | + request.user = self.user # has no subscription |
| 343 | + |
| 344 | + support_type, can_switch = ProductSupportConfig.objects.route_support_request( |
| 345 | + request, self.product |
| 346 | + ) |
| 347 | + |
| 348 | + self.assertEqual(support_type, ProductSupportConfigManager.SUPPORT_TYPE_REDIRECT) |
| 349 | + self.assertFalse(can_switch) |
| 350 | + |
| 351 | + def test_non_subscriber_without_redirect_returns_hide_sentinel(self): |
| 352 | + """Non-subscribed user is returned SUPPORT_TYPE_HIDE when no redirect product is set.""" |
| 353 | + request = self.factory.get("/") |
| 354 | + request.user = self.user # has no subscription, no redirect product configured |
| 355 | + |
| 356 | + support_type, can_switch = ProductSupportConfig.objects.route_support_request( |
| 357 | + request, self.product |
| 358 | + ) |
| 359 | + |
| 360 | + self.assertEqual(support_type, ProductSupportConfigManager.SUPPORT_TYPE_HIDE) |
| 361 | + self.assertFalse(can_switch) |
| 362 | + |
| 363 | + def test_anonymous_user_returns_hide_sentinel(self): |
| 364 | + """Anonymous users on a subscription_only product are treated as non-subscribers.""" |
| 365 | + request = self.factory.get("/") |
| 366 | + request.user = AnonymousUser() |
| 367 | + |
| 368 | + support_type, can_switch = ProductSupportConfig.objects.route_support_request( |
| 369 | + request, self.product |
| 370 | + ) |
| 371 | + |
| 372 | + self.assertEqual(support_type, ProductSupportConfigManager.SUPPORT_TYPE_HIDE) |
| 373 | + self.assertFalse(can_switch) |
| 374 | + |
| 375 | + def test_subscription_only_false_bypasses_check(self): |
| 376 | + """When subscription_only is False, all users pass through to normal routing.""" |
| 377 | + self.config.subscription_only = False |
| 378 | + self.config.save() |
| 379 | + |
| 380 | + request = self.factory.get("/") |
| 381 | + request.user = self.user # no subscription, but subscription_only is off |
| 382 | + |
| 383 | + support_type, can_switch = ProductSupportConfig.objects.route_support_request( |
| 384 | + request, self.product |
| 385 | + ) |
| 386 | + |
| 387 | + self.assertEqual(support_type, ProductSupportConfig.SUPPORT_TYPE_ZENDESK) |
| 388 | + self.assertFalse(can_switch) |
0 commit comments