diff --git a/spec/lib/auto_wizard_spec.rb b/spec/lib/auto_wizard_spec.rb new file mode 100644 index 000000000..c727c0450 --- /dev/null +++ b/spec/lib/auto_wizard_spec.rb @@ -0,0 +1,350 @@ +require 'rails_helper' + +RSpec.describe AutoWizard do + describe '.enabled?' do + context 'with no "auto_wizard.json" file in project root' do + before { FileUtils.rm(Rails.root.join('auto_wizard.json'), force: true) } + + it 'returns false' do + expect(described_class.enabled?).to be(false) + end + end + + context 'with "auto_wizard.json" file in project root' do + around do |example| + FileUtils.touch(Rails.root.join('auto_wizard.json')) + example.run + FileUtils.rm(Rails.root.join('auto_wizard.json')) + end + + it 'returns true' do + expect(described_class.enabled?).to be(true) + end + end + end + + describe '.setup' do + around do |example| + File.write(Rails.root.join('auto_wizard.json'), seed_data.to_json) + example.run + FileUtils.rm(Rails.root.join('auto_wizard.json'), force: true) + end + + let(:seed_data) { {} } + + it 'removes "auto_wizard.json" file when complete' do + expect { described_class.setup } + .to change { File.exist?(Rails.root.join('auto_wizard.json')) }.to(false) + end + + context 'when "auto_wizard.json" contains a set of User attributes and associations (Role names)' do + let(:seed_data) do + { + Users: [ + { + login: 'master_unit_test01@example.com', + firstname: 'Test Master', + lastname: 'Agent', + email: 'master_unit_test01@example.com', + password: 'test', + roles: ['Agent'] + } + ] + } + end + + it 'creates a user with those attributes and roles' do + expect { described_class.setup } + .to change(User, :count).by(1) + .and change { User.last.roles }.to(Role.where(name: 'Agent')) + .and change { User.last.login }.to('master_unit_test01@example.com') + .and change { User.last.firstname }.to('Test Master') + .and change { User.last.lastname }.to('Agent') + .and change { User.last.email }.to('master_unit_test01@example.com') + .and change { User.authenticate(User.last.email, 'test') }.from(nil) + end + end + + context 'when "auto_wizard.json" contains a set of User attributes without associations' do + let(:seed_data) do + { + Users: [ + { + login: 'master_unit_test01@example.com', + firstname: 'Test Master', + lastname: 'Agent', + email: 'master_unit_test01@example.com', + password: 'test' + } + ] + } + end + + it 'creates a user with those attributes and Admin + Agent roles' do + expect { described_class.setup } + .to change(User, :count).by(1) + .and change { User.last.roles }.to(Role.where(name: %w[Admin Agent])) + .and change { User.last.login }.to('master_unit_test01@example.com') + .and change { User.last.firstname }.to('Test Master') + .and change { User.last.lastname }.to('Agent') + .and change { User.last.email }.to('master_unit_test01@example.com') + .and change { User.authenticate(User.last.email, 'test') }.from(nil) + end + end + + context 'when "auto_wizard.json" contains a set of Group attributes and associations (User emails, Signature name, & EmailAddress id)' do + let(:seed_data) do + { + Groups: [ + { + name: 'some group1', + note: 'Lorem ipsum dolor', + users: [group_agent.email], + signature: group_signature.name, + email_address_id: group_email.id, + } + ] + } + end + + let(:group_agent) { create(:agent_user) } + let(:group_signature) { create(:signature) } + let(:group_email) { create(:email_address) } + + it 'creates a group with those attributes and associations' do + expect { described_class.setup } + .to change(Group, :count).by(1) + .and change { Group.last.name }.to('some group1') + .and change { Group.last.note }.to('Lorem ipsum dolor') + .and change { Group.last.users }.to([group_agent]) + .and change { Group.last.signature }.to(group_signature) + end + end + + context 'when "auto_wizard.json" contains a set of EmailAddress attributes' do + let(:seed_data) do + { + EmailAddresses: [ + { + channel_id: channel.id, + realname: 'John Doe', + email: 'johndoe@example.com', + } + ], + } + end + + let(:channel) { create(:email_channel) } + + it 'creates an email address with the given attributes' do + expect { described_class.setup } + .to change(EmailAddress, :count) + .and change { EmailAddress.last&.realname }.to('John Doe') + .and change { EmailAddress.last&.email }.to('johndoe@example.com') + .and change { EmailAddress.last&.channel }.to(channel) + end + end + + context 'when "auto_wizard.json" contains a set of EmailAddress attributes, including an existing ID' do + let(:seed_data) do + { + EmailAddresses: [ + { + id: email_address.id, + channel_id: new_channel.id, + realname: 'John Doe', + email: 'johndoe@example.com', + } + ], + } + end + + let(:email_address) { create(:email_address) } + let(:new_channel) { create(:email_channel) } + + it 'updates the specified email address with the given attributes' do + expect { described_class.setup } + .to not_change(EmailAddress, :count) + .and change { email_address.reload.realname }.to('John Doe') + .and change { email_address.reload.email }.to('johndoe@example.com') + .and change { email_address.reload.channel }.to(new_channel) + end + end + + context 'when "auto_wizard.json" contains a set of Channel attributes' do + let(:seed_data) do + { + Channels: [ + { + id: 100, + area: 'Email::Account', + group: 'Users', + options: { + inbound: { + adapter: 'imap', + options: { + host: 'mx1.example.com', + user: 'not_existing', + password: 'some_pass', + ssl: true + } + }, + outbound: { + adapter: 'sendmail' + } + }, + preferences: { + online_service_disable: true, + }, + active: true + } + ], + } + end + + it 'creates a new channel with the given attributes' do + expect { described_class.setup } + .to change(Channel, :count) + .and change { Channel.last&.group }.to(Group.find_by(name: 'Users')) + .and change { Channel.last&.area }.to('Email::Account') + end + end + + context 'when "auto_wizard.json" contains a set of Channel attributes, including an existing ID' do + let(:seed_data) do + { + Channels: [ + { + id: channel.id, + area: 'Email::Account', + group: new_group.name, + options: { + inbound: { + adapter: 'imap', + options: { + host: 'mx1.example.com', + user: 'not_existing', + password: 'some_pass', + ssl: true + } + }, + outbound: { + adapter: 'sendmail' + } + }, + preferences: { + online_service_disable: true, + }, + active: true + } + ], + } + end + + let(:channel) { create(:twitter_channel) } + let(:new_group) { create(:group) } + + it 'updates the specified channel with the given attributes' do + expect { described_class.setup } + .to not_change(Channel, :count) + .and change { channel.reload.group }.to(new_group) + .and change { channel.reload.area }.to('Email::Account') + end + end + + context 'when "auto_wizard.json" contains a set of existing permission names and active-statuses' do + let(:seed_data) do + { + Permissions: [ + { + name: 'admin.session', + active: false, + }, + ], + } + end + + it 'sets the specified permissions to the given active-statuses' do + expect { described_class.setup } + .to not_change(Permission, :count) + .and change { Permission.find_by(name: 'admin.session').active }.to(false) + end + end + + context 'when "auto_wizard.json" contains a set of new permission names and active-statuses' do + let(:seed_data) do + { + Permissions: [ + { + name: 'admin.session.new', + active: false, + }, + ], + } + end + + it 'creates a new permission with the given active-status' do + expect { described_class.setup } + .to change(Permission, :count).by(1) + .and change { Permission.last.name }.to('admin.session.new') + .and change { Permission.last.active }.to(false) + end + end + + context 'when "auto_wizard.json" contains sets of existing Setting names and values' do + let(:seed_data) do + { + Settings: [ + { + name: 'developer_mode', + value: true + }, + { + name: 'product_name', + value: 'Zammad UnitTest01 System' + } + ] + } + end + + it 'sets the specified settings to the given values' do + expect { described_class.setup } + .to change { Setting.get('developer_mode') }.to(true) + .and change { Setting.get('product_name') }.to('Zammad UnitTest01 System') + end + end + + context 'when "auto_wizard.json" contains a TextModule locale' do + let(:seed_data) do + { + TextModuleLocale: { + Locale: 'de-de' + } + } + end + + it 'creates a full set of text modules for the specified locale' do + expect { described_class.setup } + .to change(TextModule, :count) + end + end + + context 'when "auto_wizard.json" contains a Calendar IP' do + let(:seed_data) do + { + CalendarSetup: { + Ip: '195.65.29.254', + }, + } + end + + it 'updates the existing calendar with the specified IP' do + expect { described_class.setup } + .to not_change(Calendar, :count) + .and change { Calendar.last.name }.to('Switzerland') + .and change { Calendar.last.timezone }.to('Europe/Zurich') + end + end + + end +end diff --git a/test/unit/auto_wizard_test.rb b/test/unit/auto_wizard_test.rb deleted file mode 100644 index 940489f76..000000000 --- a/test/unit/auto_wizard_test.rb +++ /dev/null @@ -1,255 +0,0 @@ -require 'test_helper' - -class AutoWizardTest < ActiveSupport::TestCase - - test 'a simple' do - auto_wizard_data = { - Users: [ - { - login: 'master_unit_test01@example.com', - firstname: 'Test Master', - lastname: 'Agent', - email: 'master_unit_test01@example.com', - password: 'test', - }, - { - login: 'agent1_unit_test01@example.com', - firstname: 'Agent 1', - lastname: 'Test', - email: 'agent1_unit_test01@example.com', - password: 'test', - roles: ['Agent'], - } - ], - Groups: [ - { - name: 'some group1', - users: ['master_unit_test01@example.com', 'agent1_unit_test01@example.com'] - } - ], - Settings: [ - { - name: 'developer_mode', - value: true - }, - { - name: 'product_name', - value: 'Zammad UnitTest01 System' - }, - ] - } - assert_equal(false, AutoWizard.enabled?) - auto_wizard_file_write(auto_wizard_data) - assert_equal(true, AutoWizard.enabled?) - AutoWizard.setup - assert_equal(false, AutoWizard.enabled?) - - # check first user roles - auto_wizard_data[:Users][0][:roles] = %w[Agent Admin] - - auto_wizard_data[:Users].each do |local_user| - user = User.find_by(login: local_user[:login]) - assert_equal(local_user[:login], user.login) - assert_equal(local_user[:firstname], user.firstname) - assert_equal(local_user[:lastname], user.lastname) - assert_equal(local_user[:email], user.email) - assert_equal(local_user[:roles].count, user.role_ids.count) - next if !local_user[:roles] - - local_user[:roles].each do |local_role_name| - local_role = Role.find_by(name: local_role_name) - assert(user.role_ids.include?(local_role.id)) - end - end - auto_wizard_data[:Groups].each do |local_group| - group = Group.find_by(name: local_group[:name]) - assert_equal(local_group[:name], group.name) - next if !local_group[:users] - - local_group[:users].each do |local_user_login| - local_user = User.find_by(login: local_user_login) - assert(group.user_ids.include?(local_user.id)) - end - end - auto_wizard_data[:Settings].each do |local_setting| - setting_value = Setting.get(local_setting[:name]) - assert_equal(local_setting[:value], setting_value) - end - end - - test 'b complex' do - auto_wizard_data = { - Organizations: [ - { - name: 'Auto Wizard Test Org', - shared: false, - } - ], - Users: [ - { - login: 'master_unit_test01@example.com', - firstname: 'Test Master', - lastname: 'Agent', - email: 'master_unit_test01@example.com', - password: 'test', - organization: 'Auto Wizard Test Org', - roles: ['Admin'], - }, - { - login: 'agent1_unit_test01@example.com', - firstname: 'Agent 1', - lastname: 'Test', - email: 'agent1_unit_test01@example.com', - password: 'test', - roles: ['Agent'], - } - ], - Groups: [ - { - name: 'some group1', - users: ['master_unit_test01@example.com', 'agent1_unit_test01@example.com'] - }, - { - name: 'Users', - users: ['master_unit_test01@example.com', 'agent1_unit_test01@example.com'], - signature: 'default', - email_address_id: 1, - note: 'Standard Group/Pool for Tickets.', - } - ], - Settings: [ - { - name: 'developer_mode', - value: false, - }, - { - name: 'product_name', - value: 'Zammad UnitTest02 System' - }, - ], - Permissions: [ - { - name: 'admin.session', - active: false, - }, - { - name: 'admin.session.new', - active: true, - }, - ], - Channels: [ - { - id: 1, - area: 'Email::Account', - group: 'Users', - options: { - inbound: { - adapter: 'imap', - options: { - host: 'mx1.example.com', - user: 'not_existing', - password: 'some_pass', - ssl: true - } - }, - outbound: { - adapter: 'sendmail' - } - }, - preferences: { - online_service_disable: true, - }, - active: true - } - ], - EmailAddresses: [ - { - id: 1, - channel_id: 1, - realname: 'Zammad', - email: 'zammad@localhost', - } - ], - TextModuleLocale: { - Locale: 'de-de', - }, - CalendarSetup: { - Ip: '195.65.29.254', - }, - } - assert_equal(false, AutoWizard.enabled?) - auto_wizard_file_write(auto_wizard_data) - assert_equal(true, AutoWizard.enabled?) - AutoWizard.setup - assert_equal(false, AutoWizard.enabled?) - - assert_not_equal(0, TextModule.count) - assert_equal(1, Calendar.count) - assert_equal('Switzerland', Calendar.first.name) - assert_equal('Europe/Zurich', Calendar.first.timezone) - - auto_wizard_data[:Users].each do |local_user| - user = User.find_by(login: local_user[:login]) - assert_equal(local_user[:login], user.login) - assert_equal(local_user[:firstname], user.firstname) - assert_equal(local_user[:lastname], user.lastname) - assert_equal(local_user[:email], user.email) - next if !local_user[:roles] - - assert_equal(local_user[:roles].count, user.role_ids.count) - local_user[:roles].each do |local_role_name| - local_role = Role.find_by(name: local_role_name) - assert(user.role_ids.include?(local_role.id)) - end - end - auto_wizard_data[:Groups].each do |local_group| - group = Group.find_by(name: local_group[:name]) - assert_equal(local_group[:name], group.name) - local_group[:users]&.each do |local_user_login| - local_user = User.find_by(login: local_user_login) - assert(group.user_ids.include?(local_user.id)) - end - if local_group[:signature] - signature = group.signature - assert_equal('default', signature.name) - end - end - auto_wizard_data[:EmailAddresses].each do |local_email_address| - email_address = EmailAddress.find_by(email: local_email_address[:email]) - assert_equal(local_email_address[:email], email_address.email) - assert_equal(local_email_address[:realname], email_address.realname) - channel = email_address.channel - assert_equal(local_email_address[:channel_id], email_address.channel.id) - end - auto_wizard_data[:Channels].each do |local_channel| - channel = Channel.find_by(id: local_channel[:id]) - assert_equal(local_channel[:area], channel.area) - group = channel.group - assert_equal(local_channel[:group], group.name) - end - auto_wizard_data[:Settings].each do |local_setting| - setting_value = Setting.get(local_setting[:name]) - assert_equal(local_setting[:value], setting_value) - end - auto_wizard_data[:Permissions].each do |local_permission| - permission = Permission.find_by(name: local_permission[:name]) - assert_equal(local_permission[:name], permission.name) - assert_equal(local_permission[:active], permission.active) - end - end - - def auto_wizard_file_write(data) - location = Rails.root.join('auto_wizard.json') - file = File.new(location, 'wb') - file.write(data.to_json) - file.close - end - - def auto_wizard_file_exists? - location = Rails.root.join('auto_wizard.json') - return false if File.exist?(location) - - true - end - -end