Skip to content

Commit db4266e

Browse files
committed
Fixed issue with secret token not being saved if DB does not exist.
1 parent 84ece18 commit db4266e

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

lib/fat_free_crm/secret_token_generator.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ class << self
1616
# If there is no secret token defined, we generate one and save it as a setting
1717
# If a token has been already been saved, we tell Rails to use it and move on.
1818
def setup!
19-
if token.blank?
19+
if !token_exists?
2020
Rails.logger.info("No secret key defined yet... generating and saving to Setting.secret_token")
21-
generate_and_persist_token!
21+
new_token!
2222
end
23-
FatFreeCRM::Application.config.secret_token = token
24-
raise(FAIL_MESSAGE) if FatFreeCRM::Application.config.secret_token.blank?# and !Rails.env.test?
23+
# If db isn't setup yet, token will return nil, provide a randomly generated one for now.
24+
FatFreeCRM::Application.config.secret_token = ( token || generate_token )
2525
end
2626

2727
private
2828

29-
FAIL_MESSAGE = ::I18n.t('secret_token_generator.fail_message', default: "There was a problem generating the secret token. Please see lib/fat_free_crm/secret_token_generator.rb")
29+
def token_exists?
30+
Setting.secret_token.present?
31+
end
3032

3133
#
3234
# Read the current token from settings
@@ -36,12 +38,16 @@ def token
3638

3739
#
3840
# Create a new secret token and save it as a setting.
39-
def generate_and_persist_token!
41+
def new_token!
4042
quietly do
41-
Setting.secret_token = SecureRandom.hex(64)
43+
Setting.secret_token = generate_token
4244
end
4345
end
4446

47+
def generate_token
48+
SecureRandom.hex(64)
49+
end
50+
4551
#
4652
# Yields to a block that executes with the logging turned off
4753
# This stops the secret token from being appended to the log

spec/lib/secret_token_generator_spec.rb

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,65 @@
1313

1414
describe "setup!" do
1515

16-
it "should not generate a token if one already exists" do
17-
FatFreeCRM::SecretTokenGenerator.stub(:token).and_return(nil)
18-
expect(FatFreeCRM::SecretTokenGenerator).to receive(:generate_and_persist_token!)
19-
FatFreeCRM::Application.config.stub(:secret_token).and_return(token)
16+
it "should not generate a new token if one exists" do
17+
FatFreeCRM::SecretTokenGenerator.stub(:token_exists?).and_return(true)
18+
FatFreeCRM::SecretTokenGenerator.should_not_receive(:new_token!)
2019
FatFreeCRM::SecretTokenGenerator.setup!
2120
end
2221

23-
it "should generate a token if none exists already" do
24-
FatFreeCRM::SecretTokenGenerator.stub(:token).and_return(token)
25-
expect(FatFreeCRM::SecretTokenGenerator).not_to receive(:generate_and_persist_token!)
22+
it "should generate a token if none exists" do
23+
FatFreeCRM::SecretTokenGenerator.stub(:token_exists?).and_return(false)
24+
FatFreeCRM::SecretTokenGenerator.should_receive(:new_token!)
2625
FatFreeCRM::SecretTokenGenerator.setup!
2726
end
2827

29-
it "should raise an error if the token is still blank (should never happen)" do
30-
FatFreeCRM::SecretTokenGenerator.stub(:token).and_return(nil)
31-
lambda { FatFreeCRM::SecretTokenGenerator.setup! }.should raise_error(RuntimeError)
28+
it "should generate a random token if not persisted" do
29+
FatFreeCRM::SecretTokenGenerator.stub(:token_exists?).and_return(false)
30+
FatFreeCRM::SecretTokenGenerator.stub(:new_token)
31+
FatFreeCRM::SecretTokenGenerator.should_receive(:generate_token).exactly(:twice)
32+
FatFreeCRM::SecretTokenGenerator.setup!
33+
end
34+
35+
end
36+
37+
describe "token_exists?" do
38+
39+
it "should be true" do
40+
Setting.stub(:secret_token).and_return(token)
41+
FatFreeCRM::SecretTokenGenerator.send(:token_exists?).should eql(true)
42+
end
43+
44+
it "should be false" do
45+
Setting.stub(:secret_token).and_return(nil)
46+
FatFreeCRM::SecretTokenGenerator.send(:token_exists?).should eql(false)
3247
end
3348

3449
end
3550

3651
describe "token" do
3752

3853
it "should delegate to Setting" do
39-
expect(Setting).to receive(:secret_token).and_return(token)
40-
expect(FatFreeCRM::SecretTokenGenerator.send(:token)).to eql(token)
54+
Setting.should_receive(:secret_token).and_return(token)
55+
FatFreeCRM::SecretTokenGenerator.send(:token).should eql(token)
56+
end
57+
58+
end
59+
60+
describe "new_token!" do
61+
62+
it "should generate and set a new token" do
63+
FatFreeCRM::SecretTokenGenerator.should_receive(:generate_token).and_return(token)
64+
Setting.should_receive(:secret_token=).with(token)
65+
FatFreeCRM::SecretTokenGenerator.send(:new_token!)
4166
end
4267

4368
end
4469

45-
describe "generate_and_persist_token!" do
70+
describe "generate_token!" do
4671

4772
it "should generate a random token" do
48-
expect(SecureRandom).to receive(:hex).with(64).and_return(token)
49-
expect(Setting).to receive(:secret_token=).with(token)
50-
FatFreeCRM::SecretTokenGenerator.send(:generate_and_persist_token!)
73+
SecureRandom.should_receive(:hex).with(64).and_return(token)
74+
FatFreeCRM::SecretTokenGenerator.send(:generate_token)
5175
end
5276

5377
end

0 commit comments

Comments
 (0)