Skip to content

Add model validations

Bishwa Hang Rai requested to merge 424085-add-namespace-validation into master

What does this MR do and why?

Add model validations

Extends existing namespace validation on AddOnPurchase model. It now further checks if namespace is root namespace and of kind Group, along with presence, when on Gitlab.com

There aren't any AddOnPurchase records on production yet, as the feature flag: purchase_code_suggestions is not enabled.

  1. https://gitlab.com/gitlab-org/customers-gitlab-com/-/issues/6656+
  2. https://gitlab.com/gitlab-org/gitlab/-/issues/415047+

How to set up and validate locally

  1. Checkout this branch
  2. Start gdk as SaaS, if not already: GITLAB_SIMULATE_SAAS=1 gdk restart
  3. Create a new root group namespace
  4. Open console GITLAB_SIMULATE_SAAS=1 gdk rails c
  add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "code_suggestions") {|e| e.description = "Test"}
  p = GitlabSubscriptions::AddOnPurchase.new(add_on: add_on,expires_on: 1.month.from_now, quantity: 5, purchase_xid: 'A-S0001')

  # check that gdk is run as SAAS
  ::Gitlab::CurrentSettings.should_check_namespace_plan? # true
  p.valid? # => false
  p.errors.details # {:namespace=>[{:error=>:invalid}]}

  p.namespace = Namespaces::UserNamespace.last # there is always one UserNamespace present
  p.valid? # => false
  p.errors.details # {:namespace=>[{:error=>:invalid}]}

  p.namespace = Namespaces::ProjectNamespace.last # create a Project locally, if not present
  p.valid? # => false
  p.errors.details # {:namespace=>[{:error=>:invalid}]}

  p.namespace = Group.last # Root Group Namespace created in step 2 above
  p.valid? # => true
  p.errors.details # {}

MR acceptance checklist

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

Related to #424085

Edited by Bishwa Hang Rai

Merge request reports