Skip to content

Commit 0a012ef

Browse files
authored
Generator specs and fixes (#56)
* Add specs for generators * Comment out config.auth.user_admin when --skip-account passed to install generator * Ensure auth config is inserted before final end in trestle.rb, even when improperly indented
1 parent 4a6c7ee commit 0a012ef

File tree

11 files changed

+323
-6
lines changed

11 files changed

+323
-6
lines changed

lib/generators/trestle/auth/install/install_generator.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def check_trestle_installed
1818
end
1919

2020
def insert_configuration
21-
inject_into_file "config/initializers/trestle.rb", before: /^end/ do
21+
inject_into_file "config/initializers/trestle.rb", before: /end(?!.*end.*)/m do
2222
format_configuration(template_content(configuration_template))
2323
end
2424
end
@@ -28,17 +28,21 @@ def generate_model
2828
end
2929

3030
def generate_admin
31-
generate "trestle:auth:admin", model, ("--devise" if devise?)
31+
generate "trestle:auth:admin", model, *("--devise" if devise?)
3232
end
3333

3434
def generate_account
35-
generate "trestle:auth:account", model, ("--devise" if devise?) unless options[:skip_account]
35+
generate "trestle:auth:account", model, *("--devise" if devise?) unless skip_account?
3636
end
3737

3838
def devise?
3939
options[:devise]
4040
end
4141

42+
def skip_account?
43+
options[:skip_account]
44+
end
45+
4246
def configuration_template
4347
devise? ? "devise.rb.erb" : "basic.rb.erb"
4448
end

lib/generators/trestle/auth/install/templates/basic.rb.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ config.auth.user_class = -> { <%= model %> }
1111

1212
# Specify the Trestle admin for managing the current user (My Account).
1313
#
14-
config.auth.user_admin = -> { :"auth/account" }
14+
<% if skip_account? %># <% end %>config.auth.user_admin = -> { :"auth/account" }
1515

1616
# Specify the parameter (along with a password) to be used to
1717
# authenticate an administrator. Defaults to :email.

lib/generators/trestle/auth/install/templates/devise.rb.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ config.auth.user_class = -> { <%= model %> }
1414

1515
# Specify the Trestle admin for managing the current user (My Account).
1616
#
17-
config.auth.user_admin = -> { :"auth/account" }
17+
<% if skip_account? %># <% end %>config.auth.user_admin = -> { :"auth/account" }
1818

1919
# Specify the parameter (along with a password) to be used to
2020
# authenticate an administrator. Defaults to :email if not specified below.

lib/generators/trestle/auth/model/model_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ModelGenerator < ::Rails::Generators::Base
77
argument :name, type: :string, default: "Administrator"
88

99
def create_model
10-
generate "model", "#{name} email:string password_digest:string first_name:string last_name:string remember_token:string remember_token_expires_at:datetime"
10+
generate "model", name, "email:string password_digest:string first_name:string last_name:string remember_token:string remember_token_expires_at:datetime"
1111
end
1212

1313
def inject_model_methods
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require "spec_helper"
2+
3+
require "generators/trestle/auth/account/account_generator"
4+
5+
describe Trestle::Auth::Generators::AccountGenerator, type: :generator do
6+
destination File.expand_path("../../../tmp", __FILE__)
7+
8+
before do
9+
prepare_destination
10+
end
11+
12+
context "for a regular user model" do
13+
describe "the generated files" do
14+
before do
15+
run_generator
16+
end
17+
18+
describe "the admin resource" do
19+
subject { file("app/admin/auth/account_admin.rb") }
20+
21+
it { is_expected.to exist }
22+
it { is_expected.to have_correct_syntax }
23+
it { is_expected.to contain "Trestle.resource(:account, model: Administrator, scope: Auth, singular: true) do" }
24+
it { is_expected.to contain "params.require(:account).permit(:email, :first_name, :last_name, :password, :password_confirmation)" }
25+
it { is_expected.not_to contain "if Devise.sign_in_after_reset_password" }
26+
end
27+
end
28+
end
29+
30+
context "for a Devise user model" do
31+
describe "the generated files" do
32+
before do
33+
run_generator %w(User --devise)
34+
end
35+
36+
describe "the admin resource" do
37+
subject { file("app/admin/auth/account_admin.rb") }
38+
39+
it { is_expected.to exist }
40+
it { is_expected.to have_correct_syntax }
41+
it { is_expected.to contain "Trestle.resource(:account, model: User, scope: Auth, singular: true) do" }
42+
it { is_expected.to contain "params.require(:account).permit(:email, :password, :password_confirmation)" }
43+
it { is_expected.to contain "if Devise.sign_in_after_reset_password" }
44+
end
45+
end
46+
end
47+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require "spec_helper"
2+
3+
require "generators/trestle/auth/admin/admin_generator"
4+
5+
describe Trestle::Auth::Generators::AdminGenerator, type: :generator do
6+
destination File.expand_path("../../../tmp", __FILE__)
7+
8+
before do
9+
prepare_destination
10+
end
11+
12+
context "for a regular user model" do
13+
describe "the generated files" do
14+
before do
15+
run_generator
16+
end
17+
18+
describe "the admin resource" do
19+
subject { file("app/admin/auth/administrators_admin.rb") }
20+
21+
it { is_expected.to exist }
22+
it { is_expected.to have_correct_syntax }
23+
it { is_expected.to contain "Trestle.resource(:administrators, model: Administrator, scope: Auth) do" }
24+
it { is_expected.not_to contain "if Devise.sign_in_after_reset_password" }
25+
end
26+
end
27+
end
28+
29+
context "for a Devise user model" do
30+
describe "the generated files" do
31+
before do
32+
run_generator %w(User --devise)
33+
end
34+
35+
describe "the admin resource" do
36+
subject { file("app/admin/auth/users_admin.rb") }
37+
38+
it { is_expected.to exist }
39+
it { is_expected.to have_correct_syntax }
40+
it { is_expected.to contain "Trestle.resource(:users, model: User, scope: Auth) do" }
41+
it { is_expected.to contain "if Devise.sign_in_after_reset_password" }
42+
end
43+
end
44+
end
45+
end
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
require "spec_helper"
2+
3+
require "generators/trestle/auth/install/install_generator"
4+
5+
describe Trestle::Auth::Generators::InstallGenerator, type: :generator do
6+
destination File.expand_path("../../../tmp", __FILE__)
7+
8+
let(:generator_params) { [] }
9+
10+
before do
11+
prepare_destination
12+
13+
allow(generator(generator_params)).to receive(:generate)
14+
stub_file "config/initializers/trestle.rb", configuration
15+
end
16+
17+
let(:configuration) do
18+
<<~EOF
19+
Trestle.configure do |config|
20+
end
21+
EOF
22+
end
23+
24+
context "regular mode" do
25+
it "generates a model" do
26+
expect(generator(generator_params)).to receive(:generate).with("trestle:auth:model", "Administrator")
27+
run_generator generator_params
28+
end
29+
30+
it "generates an admin resource" do
31+
expect(generator(generator_params)).to receive(:generate).with("trestle:auth:admin", "Administrator")
32+
run_generator generator_params
33+
end
34+
35+
it "generates an account resource" do
36+
expect(generator(generator_params)).to receive(:generate).with("trestle:auth:account", "Administrator")
37+
run_generator generator_params
38+
end
39+
40+
context "when --skip-account is passed" do
41+
let(:generator_params) { %w(--skip-account) }
42+
43+
it "does not generate an account resource" do
44+
expect(generator(generator_params)).not_to receive(:generate).with("trestle:auth:account", "Administrator")
45+
run_generator generator_params
46+
end
47+
48+
describe "the generated files" do
49+
before do
50+
run_generator generator_params
51+
end
52+
53+
describe "the Trestle configuration" do
54+
subject { file("config/initializers/trestle.rb") }
55+
56+
it { is_expected.to contain "# config.auth.user_admin = -> { :\"auth/account\" }" }
57+
end
58+
end
59+
end
60+
61+
describe "the generated files" do
62+
before do
63+
run_generator generator_params
64+
end
65+
66+
describe "the Trestle configuration" do
67+
subject { file("config/initializers/trestle.rb") }
68+
69+
it { is_expected.to exist }
70+
it { is_expected.to have_correct_syntax }
71+
it { is_expected.to contain "config.auth.user_class = -> { Administrator }" }
72+
it { is_expected.to contain "config.auth.user_admin = -> { :\"auth/account\" }" }
73+
it { is_expected.not_to contain "config.auth.backend = :devise" }
74+
end
75+
end
76+
end
77+
78+
context "Devise mode (--devise)" do
79+
let(:generator_params) { %w(User --devise) }
80+
81+
it "does not generate a model" do
82+
expect(generator(generator_params)).not_to receive(:generate).with("trestle:auth:model", "User")
83+
run_generator generator_params
84+
end
85+
86+
it "generates an admin resource" do
87+
expect(generator(generator_params)).to receive(:generate).with("trestle:auth:admin", "User", "--devise")
88+
run_generator generator_params
89+
end
90+
91+
it "generates an account resource" do
92+
expect(generator(generator_params)).to receive(:generate).with("trestle:auth:account", "User", "--devise")
93+
run_generator generator_params
94+
end
95+
96+
context "when --skip-account is passed" do
97+
let(:generator_params) { %w(User --devise --skip-account) }
98+
99+
it "does not generate an account resource" do
100+
expect(generator(generator_params)).not_to receive(:generate).with("trestle:auth:account", "User", "--devise")
101+
run_generator generator_params
102+
end
103+
104+
describe "the generated files" do
105+
before do
106+
run_generator generator_params
107+
end
108+
109+
describe "the Trestle configuration" do
110+
subject { file("config/initializers/trestle.rb") }
111+
112+
it { is_expected.to contain "# config.auth.user_admin = -> { :\"auth/account\" }" }
113+
end
114+
end
115+
end
116+
117+
describe "the generated files" do
118+
before do
119+
run_generator generator_params
120+
end
121+
122+
describe "the Trestle configuration" do
123+
subject { file("config/initializers/trestle.rb") }
124+
125+
it { is_expected.to exist }
126+
it { is_expected.to have_correct_syntax }
127+
it { is_expected.to contain "config.auth.backend = :devise" }
128+
it { is_expected.to contain "config.auth.warden.scope = :user" }
129+
it { is_expected.to contain "config.auth.user_class = -> { User }" }
130+
it { is_expected.to contain "config.auth.user_admin = -> { :\"auth/account\" }" }
131+
it { is_expected.to contain "config.auth.authenticate_with = -> { Devise.authentication_keys.first }" }
132+
end
133+
end
134+
end
135+
136+
context "when the existing Trestle configuration is improperly indented" do
137+
let(:configuration) do
138+
<<~EOF
139+
Trestle.configure do |config|
140+
config.menu do
141+
end
142+
end
143+
EOF
144+
end
145+
146+
describe "the generated files" do
147+
before do
148+
run_generator generator_params
149+
end
150+
151+
describe "the Trestle configuration" do
152+
subject { file("config/initializers/trestle.rb") }
153+
154+
it { is_expected.to have_correct_syntax }
155+
it { is_expected.to contain "# == Authentication Options" }
156+
it { is_expected.not_to contain /config.menu do\n\s*# == Authentication Options/ }
157+
end
158+
end
159+
end
160+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "spec_helper"
2+
3+
require "generators/trestle/auth/model/model_generator"
4+
5+
describe Trestle::Auth::Generators::ModelGenerator, type: :generator do
6+
destination File.expand_path("../../../tmp", __FILE__)
7+
8+
before do
9+
prepare_destination
10+
end
11+
12+
it "generates an ActiveRecord model with the default name" do
13+
expect(generator).to receive(:generate).with("model", "Administrator", "email:string password_digest:string first_name:string last_name:string remember_token:string remember_token_expires_at:datetime") { stub_model_file }
14+
run_generator
15+
end
16+
17+
it "generates an ActiveRecord model with the specified name" do
18+
expect(generator(%w(TrestleAdmin))).to receive(:generate).with("model", "TrestleAdmin", "email:string password_digest:string first_name:string last_name:string remember_token:string remember_token_expires_at:datetime") { stub_model_file("TrestleAdmin") }
19+
run_generator %w(TrestleAdmin)
20+
end
21+
22+
describe "the generated files" do
23+
before do
24+
allow(generator).to receive(:generate) { stub_model_file }
25+
run_generator
26+
end
27+
28+
describe "the model" do
29+
subject { file("app/models/administrator.rb") }
30+
31+
it { is_expected.to exist }
32+
it { is_expected.to have_correct_syntax }
33+
it { is_expected.to contain "include Trestle::Auth::ModelMethods" }
34+
it { is_expected.to contain "include Trestle::Auth::ModelMethods::Rememberable" }
35+
end
36+
end
37+
end

spec/rails_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'rspec/rails'
88
require 'show_me_the_cookies'
99
require 'timecop'
10+
require 'ammeter/init'
1011

1112
# Checks for pending migrations and applies them before tests are run.
1213
# If you are not using ActiveRecord, you can remove these lines.
@@ -46,4 +47,5 @@
4647

4748
config.include ShowMeTheCookies, type: :feature
4849
config.include Trestle::Auth::Test::LoginHelpers, type: :feature
50+
config.include Trestle::Auth::Test::GeneratorHelpers, type: :generator
4951
end

spec/support/generator_helpers.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Trestle
2+
module Auth
3+
module Test
4+
module GeneratorHelpers
5+
def stub_model_file(name="Administrator")
6+
stub_file "app/models/#{name.underscore}.rb", <<~EOF
7+
class #{name} < ApplicationRecord
8+
end
9+
EOF
10+
end
11+
12+
def stub_file(path, contents)
13+
filepath = file(path)
14+
15+
FileUtils.mkdir_p(filepath.dirname)
16+
File.open(filepath, "w") { |f| f.write(contents) }
17+
end
18+
end
19+
end
20+
end
21+
end

trestle-auth.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
2727
spec.add_development_dependency "rspec-rails"
2828
spec.add_development_dependency "show_me_the_cookies", "~> 6.0"
2929
spec.add_development_dependency "timecop", "~> 0.9.1"
30+
spec.add_development_dependency "ammeter", "~> 1.1.7"
3031
end

0 commit comments

Comments
 (0)