forked from rails/globalid
/
railtie_test.rb
88 lines (71 loc) · 2.66 KB
/
railtie_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'rails'
require 'global_id/railtie'
require 'active_support/testing/isolation'
module BlogApp
class Application < Rails::Application; end
end
class RailtieTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
Rails.env = 'development'
@app = BlogApp::Application.new
@app.config.eager_load = false
@app.config.logger = Logger.new(nil)
@app.config.secret_key_base = ('x' * 30)
end
test 'GlobalID.app for Blog::Application defaults to blog' do
@app.initialize!
assert_equal 'blog-app', GlobalID.app
end
test 'GlobalID.app can be set with config.global_id.app =' do
@app.config.global_id.app = 'foo'
@app.initialize!
assert_equal 'foo', GlobalID.app
end
test 'SignedGlobalID.expires_in can be explicitly set to nil with config.global_id.expires_in' do
@app.config.global_id.expires_in = nil
@app.initialize!
assert_nil SignedGlobalID.expires_in
end
test 'config.global_id can be used to set configurations after the railtie has been loaded' do
@app.config.eager_load = true
@app.config.before_eager_load do
@app.config.global_id.app = 'foobar'
@app.config.global_id.expires_in = 1.year
end
@app.initialize!
assert_equal 'foobar', GlobalID.app
assert_equal 1.year, SignedGlobalID.expires_in
end
test 'config.global_id can be used to explicitly set SignedGlobalID.expires_in to nil after the railtie has been loaded' do
@app.config.eager_load = true
@app.config.before_eager_load do
@app.config.global_id.expires_in = nil
end
@app.initialize!
assert_nil SignedGlobalID.expires_in
end
test 'SignedGlobalID.verifier defaults to Blog::Application.message_verifier(:signed_global_ids) when secret_key_base is present' do
@app.initialize!
message = {id: 42}
signed_message = SignedGlobalID.verifier.generate(message)
assert_equal @app.message_verifier(:signed_global_ids).generate(message), signed_message
end
test 'SignedGlobalID.verifier defaults to nil when secret_key_base is not present' do
original_env, Rails.env = Rails.env, 'production'
begin
@app.config.secret_key_base = nil
@app.initialize!
assert_nil SignedGlobalID.verifier
ensure
Rails.env = original_env
end
end
test 'SignedGlobalID.verifier can be set with config.global_id.verifier =' do
custom_verifier = @app.config.global_id.verifier = ActiveSupport::MessageVerifier.new('muchSECRETsoHIDDEN', serializer: SERIALIZER)
@app.initialize!
message = {id: 42}
signed_message = SignedGlobalID.verifier.generate(message)
assert_equal custom_verifier.generate(message), signed_message
end
end