diff --git a/test/factories/account.rb b/test/factories/account.rb index a267ad3c04..dd974cf6fd 100644 --- a/test/factories/account.rb +++ b/test/factories/account.rb @@ -23,12 +23,7 @@ factory(:account, :parent => :account_without_users) do after(:create) do |account| - if account.users.empty? - username = account.org_name.gsub(/[^a-zA-Z0-9_\.]+/, '_') - - admin = FactoryBot.create(:admin, :username => username, :account_id => account.id) - admin.activate! - end + FactoryBot.create(:active_admin, account: account) if account.users.empty? end after(:stub) do |account| @@ -55,6 +50,52 @@ factory :buyer do buyer { true } end + + factory :master_account, traits: [:approved] do + master { true } + org_name { 'Master account' } + payment_gateway_type { :bogus } + association :settings + billing_strategy factory: :postpaid_with_charging + + after(:build) do |account| + if account.users.empty? + account.users << FactoryBot.build(:active_admin, account_id: account.id, username: "superadmin") + else + account.admins.each { |user| user.activate! if user.can_activate? } + end + end + + after(:create) do |account| + account.provider_account = account + + if account.users.empty? + account.users << FactoryBot.create(:active_admin, account_id: account.id, username: "superadmin") + else + account.admins.each { |user| user.activate! if user.can_activate? } + end + + service = FactoryBot.create(:service, account: account) + + # Defaults + app_plan = FactoryBot.create(:published_application_plan, issuer: service, default: true) + account_plan = FactoryBot.create(:account_plan, :published, issuer: account, default: true) + account.update_attribute :default_account_plan, account_plan + + service.update_attribute(:default_service_plan, service.service_plans.first) # rubocop:disable Rails/SkipsModelValidations + + %w[prepaid_billing postpaid_billing anonymous_clients liquid].each do |feature| + service.features.create!(system_name: feature, name: feature.humanize) + end + + FactoryBot.create(:cinstance, plan: app_plan, user_account: account) + end + + after(:stub) do |account| + Account.stubs(:master).returns(account) + Account.stubs(:find_by_domain).with(account.internal_domain).returns(account) + end + end end factory(:pending_buyer_account, traits: [:pending], parent: :buyer) do @@ -132,7 +173,7 @@ # assign tenant id manualy, because we cannot do it by trigger account.tenant_id = account.id - account.approve! + account.approve! if account.pending? account.services.first.update_attribute :mandatory_app_key, false account.settings.update!( @@ -193,56 +234,4 @@ a.save end end - - factory(:master_account, :parent => :account) do - master { true } - org_name { 'Master account' } - payment_gateway_type { :bogus } - association :settings - - after(:build) do |account| - account.billing_strategy = FactoryBot.build(:postpaid_with_charging) - if account.users.empty? - account.users << FactoryBot.build(:admin, :account_id => account.id, :username => "superadmin", state: 'active') - end - account.admins.each { |user| user.activate! if user.can_activate? } - end - - after(:create) do |account| - account.provider_account = account - - if account.users.empty? - account.users << FactoryBot.create(:admin, :account_id => account.id, :username => "superadmin", state: 'active') - end - account.admins.each { |user| user.activate! if user.can_activate? } - account.approve! if account.can_approve? - - #[multiservice] First service is the default - service = FactoryBot.create(:service, :account => account) - - # Defaults - application_plan = FactoryBot.create(:application_plan, issuer: service, state: 'published') - account_plan = FactoryBot.create(:account_plan, issuer: account, state: 'published') - - service.update_attribute :default_application_plan, application_plan - service.update_attribute :default_service_plan, service.service_plans.first - account.update_attribute :default_account_plan, account_plan - - # TODO: add more master features here, if needed - %w[prepaid_billing postpaid_billing anonymous_clients liquid].each do |feature| - account.default_service.features.create!(:system_name => feature, :name => feature.humanize) - end - - FactoryBot.create(:cinstance, :plan => account.default_service.application_plans.published.first, - :user_account => account) - - account.reload - end - - - after(:stub) do |account| - Account.stubs(:master).returns(account) - Account.stubs(:find_by_domain).with(account.internal_domain).returns(account) - end - end end diff --git a/test/factories/plan.rb b/test/factories/plan.rb index ffde182b64..dee6fc89cd 100644 --- a/test/factories/plan.rb +++ b/test/factories/plan.rb @@ -32,6 +32,10 @@ after(:build) do |plan, evaluator| plan.issuer.update(default_account_plan: plan) if evaluator.default end + + trait :published do + state { :published } + end end factory(:service_plan, parent: :plan, class: ServicePlan) do diff --git a/test/factories/user.rb b/test/factories/user.rb index 96ffc22369..d2fa9bc6fc 100644 --- a/test/factories/user.rb +++ b/test/factories/user.rb @@ -21,6 +21,7 @@ factory(:admin, :parent => :user) do role { :admin } + username { account.org_name.gsub(/[^a-zA-Z0-9_\.]+/, '_') } end factory(:active_admin, :parent => :admin) do