Skip to content

Refactor UserAddOnAssignments and the GQL API

Mohamed Hamda requested to merge 432212/mhamda/assign_sm_users into master

What does this MR do and why?

This MR is a component of the MVC epic, aimed at enabling UserAddOnAssignments for Code Suggestions seat management on a Self-Managed instance.

Here, we are allowing the admin to assign all users who are human, excluding bots, blocked, banned, and pending_approval users.

Related to #432212

In this MR, we: Refactor UserAddOnAssignments and the GQL API for self-managed use

  • Refactor UserAddOnAssignments to be a base service
  • Introduce new service architecture
  • Introduce UserAddOnAssignments for self-managed
  • Introduce eligible_for_self_managed_code_suggestions
  • Cover changes with specs

Screenshots or screen recordings

Screenshot_2023-11-29_at_10.56.48

How to set up and validate locally

Note: Access is limited to admins

Note: Make sure you are running a self-managed installation: gdk stop rails-web && GITLAB_RAILS_RACK_TIMEOUT_ENABLE_LOGGING=false PUMA_SINGLE_MODE=true GITLAB_SIMULATE_SAAS=0 gdk rails s

  1. Pick or create a group and add guests, developers, owners, bots, and banned, and blocked users.

  2. Visit localhost:3000/graphql-explorer and add the specified query below.

  3. Setup AddOnPurchase

add_on = GitlabSubscriptions::AddOn.find_or_create_by!(name: "code_suggestions") {|e| e.description = "Test"}
add_on_purchase = GitlabSubscriptions::AddOnPurchase.create!(
  add_on: add_on, expires_on: 1.month.from_now, quantity: 5, purchase_xid: 'A-S0001'
)
add_on_purchase.to_global_id.to_s # "gid://gitlab/GitlabSubscriptions::AddOnPurchase/4"

Feature.enable(:hamilton_seat_management) # enable the feature flag
  1. Go to GraphQL explorer (logged in as admin): http://gdk.test:3000/-/graphql-explorer, and use the following query:
mutation {
  userAddOnAssignmentCreate(
    input: {
      userId: "gid://gitlab/User/ID",
      addOnPurchaseId: "gid://gitlab/GitlabSubscriptions::AddOnPurchase/ID"
    }) {
    addOnPurchase {
      id
      name
      purchasedQuantity
      assignedQuantity
    }
    errors
  }
}
  1. Check that the response has no errors errors: []
  2. Sending the same request again for the assigned user will return the user and an empty error.
  3. Update the quantity so no seats are available: add_on_purchase.update!(quantity: 1)
  4. Adding new users should respond: {errors: ['NO_SEATS_AVAILABLE']}
  5. Sending user_id with the guest_user role in mutation results should be successful and raise no errors.
  6. Sending user_id with blocked/banned/bot/inactive, etc.. user role in mutation results: {errors: ['INVALID_USER_MEMBERSHIP']}

To test the denied add test scenarios:

  1. Setup the seed record again if needed
  2. Login with a user who is not an admin.
  3. Try to assign any user. It will return errors.

Verify SaaS previously merged work is not broken:

  • gdk stop rails-web && GITLAB_RAILS_RACK_TIMEOUT_ENABLE_LOGGING=false PUMA_SINGLE_MODE=true GITLAB_SIMULATE_SAAS=1 gdk rails s
  • Follow the docs for SaaS setup/test !126964 (merged)

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 #432212

Edited by Mohamed Hamda

Merge request reports