Skip to content

Handle multiple duo add-ons during user add-on assignment cleanup

What does this MR do and why?

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

Prior to this change, Duo Pro and Duo Enterprise add-ons were mutually exclusive and a namespace couldn't have both. With the Duo Enterprise trial, a namespace can now have a paid Duo Pro purchase while also trialing Duo Enterprise.

This change ensures that both add-ons' user assignments are cleaned up after a user is destroyed.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

  1. Create a new root group namespace
  2. Setup some seed records
namespace = Namespace.last
duo_pro_add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "code_suggestions") { |e| e.description = "Test" }
duo_pro_add_on_purchase = GitlabSubscriptions::AddOnPurchase.create!(
  add_on: duo_pro_add_on, namespace: namespace, expires_on: 1.month.from_now, quantity: 5, purchase_xid: 'A-S0001'
)
duo_enterprise_add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "duo_enterprise") { |e| e.description = "Test" }
duo_enterprise_add_on_purchase = GitlabSubscriptions::AddOnPurchase.create!(
  add_on: duo_enterprise_add_on, namespace: namespace, expires_on: 1.month.from_now, quantity: 5, purchase_xid: 'A-S0001', trial: true
)
user = User.find 70 # or another user

# add user as guest
namespace.add_guest(user)

# assign seat to the user
duo_pro_add_on_purchase.assigned_users.create(user: user)
duo_enterprise_add_on_purchase.assigned_users.create(user: user)

# does not remove seat assignment as the user still has membership to namespace
GitlabSubscriptions::AddOnPurchases::CleanupUserAddOnAssignmentWorker.new.perform(namespace.id, user.id) # returns nil
duo_pro_add_on_purchase.assigned_users.count # 1
duo_enterprise_add_on_purchase.assigned_users.count # 1

# remove the existing memberships
namespace.members.where(user: user).destroy_all

# removes the seat assignment as there aren't any memberships to namespace
GitlabSubscriptions::AddOnPurchases::CleanupUserAddOnAssignmentWorker.new.perform(namespace.id, user.id)
duo_pro_add_on_purchase.assigned_users.count # 0
duo_enterprise_add_on_purchase.assigned_users.count # 0
Edited by Corinna Gogolok

Merge request reports

Loading