Last active
June 29, 2021 12:35
-
-
Save markhallen/3d6d27e4c9e5bbbd7d97b909e612e178 to your computer and use it in GitHub Desktop.
Add multiple accounts for testing based on a single email address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add multiple accounts for testing based on a single email address | |
class TestAccountsService | |
MAX_TRIES = 10 | |
def self.call(emails) | |
email_addresses = [] | |
Array(emails).each do |email| | |
email.downcase! | |
name = name_from_email(email) | |
create_support_user(email, name) | |
create_supplier_user(email, name) | |
school_that_orders = find_school_that_orders | |
create_body_user(school_that_orders, email, name) unless school_that_orders.nil? | |
rb_that_orders = find_rb_that_orders | |
create_body_user(rb_that_orders, email, name) unless rb_that_orders.nil? | |
email_addresses << find_related_accounts(email).pluck(:email_address) | |
end | |
email_addresses | |
end | |
def self.find_school_that_orders | |
MAX_TRIES.times do | |
school = LocalAuthority.where(vcap_feature_flag: false).sample.schools.gias_status_open.can_order.sample | |
return school unless school.nil? | |
end | |
puts "ERROR: Didn't find a school that orders and can_order!" | |
end | |
def self.find_rb_that_orders | |
if LocalAuthority.where(vcap_feature_flag: true).size.positive? | |
MAX_TRIES.times do | |
body = LocalAuthority.where(vcap_feature_flag: true).sample | |
return body unless body.schools.gias_status_open.can_order.size.positive? | |
end | |
end | |
puts "ERROR: Didn't find a responsible body that orders and has a school that can_order!" | |
end | |
def self.email_variation(email, variation) | |
email.sub('@', "+#{variation}@") | |
end | |
def self.create_body_user(body, email, name) | |
return unless User.find_by(email: email).nil? | |
create_test_user(body, email_variation(email, body_postfix(body)), name) | |
end | |
def self.create_test_user(body, email, name) | |
rb_level_access = body.is_a?(ResponsibleBody) ? true : false | |
body.users.find_or_create_by!(email_address: email) do |u| | |
u.full_name = "#{name} #{body_postfix(body).titlecase} User" | |
u.orders_devices = true | |
u.rb_level_access = rb_level_access | |
end | |
end | |
def self.create_support_user(email, name) | |
User.find_or_create_by!(email_address: email_variation(email, 'support')) do |u| | |
u.full_name = "#{name} Support User" | |
u.is_support = true | |
end | |
end | |
def self.create_supplier_user(email, name) | |
User.find_or_create_by!(email_address: email_variation(email, 'supplier')) do |u| | |
u.full_name = "#{name} Supplier User" | |
u.is_computacenter = true | |
end | |
end | |
def self.name_from_email(email) | |
email.split('@').first.sub('.', ' ').titlecase | |
end | |
def self.body_postfix(body) | |
return 'school' if body.is_a? School | |
return 'rb' if body.is_a? ResponsibleBody | |
body.class.to_s.underscore | |
end | |
def self.find_related_accounts(email) | |
email_name, email_domain = email.split('@') | |
User.where('email_address like ?', "#{email_name}+%@#{email_domain}") | |
end | |
def self.create_rb_that_orders | |
# Assumption: At least one ResponsibleBody with a computacenter_reference already exists | |
# Assumption: At least one ResponsibleBody with a gias_id already exists | |
# After running, the enviroment can no longer be consider clean | |
county = Faker::Address.county | |
responsible_body = LocalAuthority.find_or_create_by!(name: "#{county} County Council") do |rb| | |
rb.organisation_type = 'county' | |
rb.who_will_order_devices = 'responsible_body' | |
rb.gias_id = ResponsibleBody.maximum(:gias_id).to_i + 1 | |
rb.computacenter_reference = ResponsibleBody.maximum(:computacenter_reference).to_i + 1 | |
rb.address_1 = Faker::Address.street_address | |
rb.town = Faker::Address.city | |
rb.county = county | |
rb.postcode = Faker::Address.postcode | |
end | |
responsible_body.update!(vcap_feature_flag: true) | |
create_schools_in_rb(responsible_body) | |
add_school_to_vcap_pool(responsible_body.schools) | |
responsible_body.calculate_virtual_caps! | |
responsible_body | |
end | |
def self.create_schools_in_rb(responsible_body, quantity = 5) | |
quantity.times do | |
school = responsible_body.schools.create! do |s| | |
s.name = Faker::Educator.secondary_school | |
s.urn = (School.maximum(:urn) || 99_999) + 1 | |
s.address_1 = Faker::Address.street_address | |
s.town = Faker::Address.city | |
s.county = Faker::Address.county | |
s.postcode = Faker::Address.postcode | |
end | |
school.create_preorder_information!(who_will_order_devices: 'responsible_body') | |
std_device_allocation_value = Faker::Number.between(from: 20, to: 100) | |
school.device_allocations.std_device.create!(allocation: std_device_allocation_value) | |
coms_device_allocation_value = Faker::Number.between(from: 5, to: 30) | |
school.device_allocations.coms_device.create!(allocation: coms_device_allocation_value) | |
school.can_order! | |
school.std_device_allocation.update!(cap: std_device_allocation_value, devices_ordered: Faker::Number.between(from: 1, to: std_device_allocation_value)) | |
school.coms_device_allocation.update!(cap: coms_device_allocation_value, devices_ordered: Faker::Number.between(from: 1, to: coms_device_allocation_value)) | |
end | |
end | |
def self.add_school_to_vcap_pool(schools) | |
Array(schools).each do |school| | |
school.responsible_body.add_school_to_virtual_cap_pools!(school) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment