33"""
44from unittest import mock
55
6+ import ddt
67import requests
8+ from django .conf import settings
79from django .test import TestCase
810from rest_framework import status
911
@@ -382,6 +384,7 @@ def test_create_new_customer_agreement(self, mock_license_manager_client):
382384 )
383385
384386
387+ @ddt .ddt
385388class TestGetOrCreateSubscriptionPlan (TestCase ):
386389 """
387390 Tests for the get_or_create_subscription_plan() function.
@@ -393,57 +396,99 @@ def test_get_existing_subscription_plan(self, mock_license_manager_client):
393396 'uuid' : 'sub-uuid' ,
394397 'title' : 'Test Plan' ,
395398 'salesforce_opportunity_line_item' : 'opp-line-item' ,
399+ 'product' : 1 ,
396400 },
397401 ]
398402
399403 result = provisioning_api .get_or_create_subscription_plan (
400- customer_agreement_uuid = None ,
404+ customer_agreement_uuid = 'customer-agreement-uuid' ,
401405 existing_subscription_list = existing_subscriptions ,
402406 plan_title = 'Test Plan' ,
403407 catalog_uuid = 'catalog-uuid' ,
404408 opp_line_item = 'opp-line-item' ,
405409 start_date = '2025-05-01' ,
406410 expiration_date = '2026-05-01' ,
407411 desired_num_licenses = 100 ,
408- product_id = None ,
412+ product_id = 1 ,
409413 )
410414
411415 self .assertEqual (result , existing_subscriptions [0 ])
412416 mock_license_manager_client .assert_not_called ()
413417
418+ @ddt .data (
419+ # Same prodcut_id, different opp_line_item.
420+ {
421+ 'existing_opp_line_item' : 'opp-line-item-1' ,
422+ 'existing_product_id' : 1 ,
423+ 'requesting_opp_line_item' : 'opp-line-item-2' ,
424+ 'requesting_product_id' : 1 ,
425+ },
426+ # Same opp_line_item, different prodcut_id.
427+ {
428+ 'existing_opp_line_item' : 'opp-line-item-1' ,
429+ 'existing_product_id' : 1 ,
430+ 'requesting_opp_line_item' : 'opp-line-item-1' ,
431+ 'requesting_product_id' : 2 ,
432+ },
433+ # Make sure requesting product_id=None falls back to a default which does not conflict.
434+ {
435+ 'existing_opp_line_item' : 'opp-line-item-1' ,
436+ 'existing_product_id' : settings .PROVISIONING_DEFAULTS ['subscription' ]['product_id' ] + 1 ,
437+ 'requesting_opp_line_item' : 'opp-line-item-1' ,
438+ 'requesting_product_id' : None , # fallback to settings.PROVISIONING_DEFAULTS['subscription']['product_id']
439+ },
440+ )
441+ @ddt .unpack
414442 @mock .patch .object (provisioning_api , 'LicenseManagerApiClient' , autospec = True )
415- def test_create_new_subscription_plan (self , mock_license_manager_client ):
443+ def test_create_new_subscription_plan (
444+ self ,
445+ mock_license_manager_client ,
446+ existing_opp_line_item ,
447+ existing_product_id ,
448+ requesting_opp_line_item ,
449+ requesting_product_id ,
450+ ):
451+ existing_subscriptions = [
452+ {
453+ 'uuid' : 'sub-uuid' ,
454+ 'title' : 'Test Plan' ,
455+ 'salesforce_opportunity_line_item' : existing_opp_line_item ,
456+ 'product' : existing_product_id ,
457+ },
458+ ]
416459 created_subscription = {
417460 'uuid' : 'new-sub-uuid' ,
418- 'salesforce_opportunity_line_item' : 'opp-line-item' ,
461+ 'salesforce_opportunity_line_item' : requesting_opp_line_item ,
419462 'title' : 'New Plan' ,
463+ # Simulate the fallback logic within LicenseManagerApiClient.create_subscription_plan().
464+ 'product' : requesting_product_id or settings .PROVISIONING_DEFAULTS ['subscription' ]['product_id' ],
420465 }
421466 mock_client = mock_license_manager_client .return_value
422467 mock_client .create_subscription_plan .return_value = created_subscription
423468
424469 result = provisioning_api .get_or_create_subscription_plan (
425470 customer_agreement_uuid = 'agreement-uuid' ,
426- existing_subscription_list = [] ,
471+ existing_subscription_list = existing_subscriptions ,
427472 plan_title = 'New Plan' ,
428473 catalog_uuid = 'catalog-uuid' ,
429- opp_line_item = 'opp-line-item' ,
474+ opp_line_item = requesting_opp_line_item ,
430475 start_date = '2025-05-01' ,
431476 expiration_date = '2026-05-01' ,
432477 desired_num_licenses = 50 ,
433478 extra_field = 'extra-value' ,
434- product_id = 'the-product' ,
479+ product_id = requesting_product_id ,
435480 )
436481
437482 self .assertEqual (result , created_subscription )
438483 mock_client .create_subscription_plan .assert_called_once_with (
439484 customer_agreement_uuid = 'agreement-uuid' ,
440485 enterprise_catalog_uuid = 'catalog-uuid' ,
441486 title = 'New Plan' ,
442- salesforce_opportunity_line_item = 'opp-line-item' ,
487+ salesforce_opportunity_line_item = requesting_opp_line_item ,
443488 start_date = '2025-05-01' ,
444489 expiration_date = '2026-05-01' ,
445490 desired_num_licenses = 50 ,
446- product_id = 'the-product' ,
491+ product_id = requesting_product_id ,
447492 extra_field = 'extra-value'
448493 )
449494
0 commit comments