Sha256: 8af20c7eb7a363b761e17a1fde34bc22b2fe20f7446be5827c00b739ba079a2a

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

# Routine for creating an account, only for use when stubbing and not on production.

module OpenStax
  module Accounts
    module Dev
      class CreateAccount
        lev_routine

        protected

        def exec(inputs={})
          fatal_error(code: :cannot_create_account_in_production) if Rails.env.production?
          fatal_error(code: :can_only_create_account_when_stubbing) \
            unless OpenStax::Accounts.configuration.enable_stubbing?

          username = inputs[:username]
          if username.blank?
            while username.blank? || Account.where(username: username).exists? do
              username = SecureRandom.hex(3).to_s
            end
          else
            fatal_error(
              code: :account_already_exists,
              message: "One or more accounts with username \"#{username}\" already exist."
            ) if Account.where(username: username).exists?
          end

          outputs.account = OpenStax::Accounts::Account.create(
            openstax_uid: -SecureRandom.hex(4).to_i(16)/2,
            access_token: SecureRandom.hex.to_s,
            username: username,
            role: inputs[:role] || :unknown_role,
            uuid: SecureRandom.uuid,
            support_identifier: "cs_#{SecureRandom.hex(4)}",
            school_type: inputs[:school_type] || :unknown_school_type,
            school_location: inputs[:school_location] || :unknown_school_location,
            is_test: true
          )

          transfer_errors_from(outputs.account, {type: :verbatim}, true)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
openstax_accounts-9.9.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.8.1 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.8.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.7.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.6.1 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.6.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.5.1 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.5.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.4.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.3.0 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.0.5 app/routines/openstax/accounts/dev/create_account.rb
openstax_accounts-9.0.4 app/routines/openstax/accounts/dev/create_account.rb