|
| 1 | +require 'spec_helper' |
| 2 | +require 'support/generator_helper' |
| 3 | + |
| 4 | +require 'generators/sorcery/install_generator' |
| 5 | + |
| 6 | +describe 'install generator' do |
| 7 | + include GeneratorHelper |
| 8 | + |
| 9 | + class TestInstallGenerator < Sorcery::Generators::InstallGenerator |
| 10 | + source_root File.expand_path('../../lib/generators/sorcery/templates', __dir__) |
| 11 | + |
| 12 | + def self.next_migration_number(dirname) |
| 13 | + current_migration_number(dirname) + 1 |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + tests TestInstallGenerator |
| 18 | + |
| 19 | + context 'given deprecated --migrations option' do |
| 20 | + let(:installer) do |
| 21 | + generator(options: { migrations: true }) |
| 22 | + end |
| 23 | + |
| 24 | + it 'shows warning' do |
| 25 | + expect(installer).to receive(:warn).with(/\[DEPRECATED\] `--migrations` option is deprecated/) |
| 26 | + installer.check_deprecated_options |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context 'given invalid submodule' do |
| 31 | + it 'raises error' do |
| 32 | + expect { |
| 33 | + generator('invalid_submodule').check_available_submodules |
| 34 | + }.to raise_error(ArgumentError).with_message(/invalid_submodule is not a Sorcery submodule/) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + describe 'initializer' do |
| 39 | + describe 'installation' do |
| 40 | + let(:installation) { invoke!(:install_initializer) } |
| 41 | + |
| 42 | + it 'creates initializer' do |
| 43 | + expect(installation).to match(/create config\/initializers\/sorcery.rb/) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe 'configuration' do |
| 48 | + context 'given submodule(s)' do |
| 49 | + let(:initializer_contents) do |
| 50 | + File.read(initializer_path) |
| 51 | + end |
| 52 | + |
| 53 | + before do |
| 54 | + invoke!(:install_initializer, 'activity_logging') |
| 55 | + end |
| 56 | + |
| 57 | + it 'adds submodule(s) to initializer' do |
| 58 | + expect(initializer_contents).to match(/Rails\.application\.config\.sorcery\.submodules = \[:activity_logging\]/) |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe 'uninstallation' do |
| 64 | + let(:uninstallation) { revoke!(:install_initializer) } |
| 65 | + |
| 66 | + before do |
| 67 | + invoke!(:install_initializer) |
| 68 | + end |
| 69 | + |
| 70 | + it 'removes initializer' do |
| 71 | + expect(uninstallation).to match(/remove config\/initializers\/sorcery.rb/) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe 'model' do |
| 77 | + describe 'installation' do |
| 78 | + let(:installation) { invoke!(:install_model) } |
| 79 | + |
| 80 | + it 'skips migration' do |
| 81 | + expect(installation).to match(/generate model User \-\-skip\-migration/) |
| 82 | + end |
| 83 | + |
| 84 | + it 'creates model' do |
| 85 | + expect(installation).to match(/create app\/models\/user\.rb/) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe 'configuration' do |
| 90 | + let(:model_contents) do |
| 91 | + File.read(model_path(:user)) |
| 92 | + end |
| 93 | + |
| 94 | + before do |
| 95 | + invoke!(:install_model) |
| 96 | + end |
| 97 | + |
| 98 | + it 'adds `authenticates_with_sorcery!`' do |
| 99 | + expect(model_contents).to match(/authenticates_with_sorcery!/) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + describe 'uninstallation' do |
| 104 | + let(:uninstallation) { revoke!(:install_model) } |
| 105 | + |
| 106 | + before do |
| 107 | + invoke!(:install_model) |
| 108 | + end |
| 109 | + |
| 110 | + it 'removes `authenticates_with_sorcery!`' do |
| 111 | + expect(uninstallation).to match(/subtract app\/models\/user.rb/) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + describe 'migrations' do |
| 117 | + describe 'installation' do |
| 118 | + let(:installation) { invoke!(:install_migrations, 'activity_logging', options: { only_submodules: true }) } |
| 119 | + |
| 120 | + it 'creates migration' do |
| 121 | + expect(installation).to match(/create db\/migrate\/1_sorcery_activity_logging.rb/) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + describe 'uninstallation' do |
| 126 | + let(:uninstallation) { revoke!(:install_migrations, 'activity_logging', options: { only_submodules: true }) } |
| 127 | + |
| 128 | + it 'removes migration' do |
| 129 | + expect(uninstallation).to match(/remove db\/migrate\/1_sorcery_activity_logging.rb/) |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments