Skip to content

Check for root namespace before creating add-on purchase

What does this MR do and why?

Part of https://gitlab.com/gitlab-org/gitlab/-/issues/414605+

This change checks for the namespace to be a root namespace before creating a subscription add-on purchase. It aligns with the restrictions that only root namespaces can purchase a SaaS subscription.

How to set up and validate locally

  1. Create the code suggestions add-on in the rails console (note: This record will be created via the API endpoints in !123382 (merged) if it's missing):

    add_on = GitlabSubscriptions::AddOn.create(name: :code_suggestions, description: 'Add-on for code suggestions')
  2. Load a sub group and user to use:

    namespace = Group.where.not(parent_id: nil).first
    user = User.find(<USER_ID>)
  3. Test the create service with the sub group:

    params = {
      quantity: 1,
      expires_on: Date.current + 1.month,
      purchase_xid: '1a2b3c'
    }
    
    GitlabSubscriptions::AddOnPurchases::CreateService.new(
      user,
      namespace,
      add_on,
      params
    ).execute

    Expected result: Error with the message that the namespace is not a root namespace.

  4. Load a root namespace:

    namespace = Group.where(parent_id: nil).first
  5. Test the create service with the root namespace:

     params = {
       quantity: 1,
       expires_on: Date.current + 1.month,
       purchase_xid: '1a2b3c'
     }
    
     GitlabSubscriptions::AddOnPurchases::CreateService.new(
       user,
       namespace,
       add_on,
       params
     ).execute

    Expected result: Success.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading