releaf-content 2.0.0 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,229 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::Content::Route do
4
- let(:node_route) { FactoryBot.build(:node_route, node_class: Node, node_id: 12, locale: "en", path: "/en") }
5
-
6
-
7
- describe ".default_controller" do
8
- context "when given node class inherits `ActionController::Base`" do
9
- it "returns undercored, stripped down controller class" do
10
- expect(described_class.default_controller(HomePagesController)).to eq("home_pages")
11
- end
12
- end
13
-
14
- context "when given node class does not inherit `ActionController::Base`" do
15
- it "returns pluralized, underscorized class" do
16
- expect(described_class.default_controller(TextPage)).to eq("text_pages")
17
- end
18
- end
19
- end
20
-
21
-
22
- describe '#params' do
23
- let(:options_argument) { { foo: :bar } }
24
-
25
- it "returns params for router method" do
26
- expect(node_route.params("home#index")).to eq ['/en', {to: "home#index", node_class: "Node", node_id: "12", locale: "en", as: nil }]
27
- end
28
-
29
- it "uses #path_for to calculate path" do
30
- allow(node_route).to receive(:path_for).with("home#index", options_argument).and_return( "foo_path" )
31
- expect(node_route.params("home#index", options_argument)[0]).to eq "foo_path"
32
- end
33
-
34
- it "uses #options_for to calculate options" do
35
- allow(node_route).to receive(:options_for).with("home#index", options_argument).and_return( some: :options )
36
- expect(node_route.params("home#index", options_argument)[1]).to eq( some: :options )
37
- end
38
-
39
- it "converts method_or_path argument to string" do
40
- expect(node_route).to receive(:path_for).with("foo", options_argument)
41
- expect(node_route).to receive(:options_for).with("foo", options_argument)
42
- node_route.params(:foo, options_argument)
43
- end
44
-
45
- it "does not require options argument" do
46
- expect(node_route).to receive(:path_for).with("home#index", {})
47
- expect(node_route).to receive(:options_for).with("home#index", {})
48
- node_route.params("home#index")
49
- end
50
-
51
- end
52
-
53
- describe "#path_for" do
54
-
55
- it "returns route path" do
56
- expect( node_route.path_for( "foo", {} ) ).to eq "/en"
57
- end
58
-
59
- context "when options contain a :to key" do
60
- it "returns route path combined with given method_or_path" do
61
- expect( node_route.path_for( "foo", { to: "bar" } ) ).to eq "/en/foo"
62
- end
63
- end
64
-
65
- context "when first argument contains a controller#action or #action string" do
66
- it "returns the route path" do
67
- expect( node_route.path_for( "home#index", { to: "bar" } ) ).to eq "/en"
68
- end
69
- end
70
-
71
- end
72
-
73
- describe "#options_for" do
74
-
75
- it "returns route options hash" do
76
- expect( node_route.options_for( "home#index", {} ) ).to be_a Hash
77
- end
78
-
79
- it "sets :node_class to route node class name string" do
80
- expect( node_route.options_for( "home#index", {} ) ).to include( node_class: "Node" )
81
- end
82
-
83
- it "sets :node_id to route node id string" do
84
- expect( node_route.options_for( "home#index", {} ) ).to include( node_id: "12" )
85
- end
86
-
87
- it "sets :locale to route locale" do
88
- expect( node_route.options_for( "home#index", {} ) ).to include( locale: "en" )
89
- end
90
-
91
- it "sets :to to the result of #controller_and_action_for" do
92
- allow(node_route).to receive(:controller_and_action_for).with( "home#index", foo: :bar).and_return("ok")
93
- expect( node_route.options_for( "home#index", foo: :bar ) ).to include( to: "ok" )
94
- end
95
-
96
- it "sets :as to the result of #name by passing all other calculated options" do
97
- expected_name_options = {
98
- foo: :bar,
99
- to: "home#index",
100
- node_class: "Node",
101
- node_id: "12",
102
- locale: "en"
103
- }
104
-
105
- allow(node_route).to receive(:name).with( expected_name_options ).and_return("ok")
106
- expect( node_route.options_for( "home#index", foo: :bar ) ).to include( as: "ok" )
107
- end
108
-
109
- it "preserves unrecognized option keys" do
110
- expect( node_route.options_for( "home#index", foo: :bar ) ).to include( foo: :bar )
111
- end
112
-
113
- it "uses calculated keys in case conflicting option keys given" do
114
- in_options = { to: "invalid", node_class: "invalid", node_id: "invalid", locale: "invalid" }
115
-
116
- expect( node_route.options_for( "home#index", in_options ) ).to include({
117
- to: "home#index",
118
- node_class: "Node",
119
- node_id: "12",
120
- locale: "en"
121
- })
122
- end
123
-
124
- context "when node route has site" do
125
-
126
- it "sets :site to route site" do
127
- node_route.site = "ok"
128
- expect( node_route.options_for( "home#index", {} ) ).to include( site: "ok" )
129
- end
130
-
131
- it "overrides site given in options" do
132
- node_route.site = "ok"
133
- expect( node_route.options_for( "home#index", { site: "foo" } ) ).to include( site: "ok" )
134
- end
135
-
136
- end
137
-
138
- context "when node route does not have site" do
139
-
140
- it "does not set :site" do
141
- expect( node_route.options_for( "home#index", {} ) ).to_not include( :site )
142
- end
143
-
144
- it "allows site from options" do
145
- expect( node_route.options_for( "home#index", { site: "ok" } ) ).to include( site: "ok" )
146
- end
147
-
148
- end
149
-
150
-
151
- end
152
-
153
- describe "#name" do
154
-
155
- context "when route options contain :as option" do
156
-
157
- context "when neither :site nor :locale are set or are blank in route options" do
158
- it "returns the value intact" do
159
- expect(node_route.name( { as: "foo" } )).to eq "foo"
160
- expect(node_route.name( { as: "foo", site: nil, locale: nil } )).to eq "foo"
161
- end
162
- end
163
-
164
- context "when :site is set in route options" do
165
- it "returns the value with site prepended" do
166
- expect(node_route.name( { as: "foo", site: "sss" } )).to eq "sss_foo"
167
- end
168
- end
169
-
170
- context "when :locale is set in route options" do
171
- it "returns the value with locale prepended" do
172
- expect(node_route.name( { as: "foo", locale: "lll" } )).to eq "lll_foo"
173
- end
174
- end
175
-
176
- context "when both :site and :locale are set in route options" do
177
- it "returns the value with site and locale prepended" do
178
- expect(node_route.name( { as: "foo", site: "sss", locale: "lll" } )).to eq "sss_lll_foo"
179
- end
180
- end
181
-
182
- end
183
-
184
- context "when route options do not contain :as option" do
185
- it "returns nil" do
186
- expect(node_route.name( {} )).to be_nil
187
- end
188
- end
189
-
190
- end
191
-
192
- describe ".for" do
193
- before do
194
- create(:home_page_node)
195
- end
196
-
197
- it "returns an array" do
198
- expect(described_class.for(Node, HomePage, 'foo')).to be_a Array
199
- end
200
-
201
- context "when database doesn't exists" do
202
- it "returns an empty array" do
203
- allow(Node).to receive(:where).and_raise(ActiveRecord::NoDatabaseError.new("xxx"))
204
- expect(described_class.for(Node, HomePage, 'foo')).to eq([])
205
- end
206
- end
207
-
208
- context "when node table doesn't exist" do
209
- it "returns an empty array" do
210
- allow(Node).to receive(:where).and_raise(ActiveRecord::StatementInvalid.new("xxx"))
211
- expect(described_class.for(Node, HomePage, 'foo')).to eq([])
212
- end
213
- end
214
-
215
- context "when node table exists" do
216
- it "returns an array of Node::Route objects processed by Releaf::Content::BuildRouteObjects" do
217
- expect(Releaf::Content::BuildRouteObjects).to receive(:call).with(node_class: Node, node_content_class: HomePage, default_controller: 'foo').and_call_original
218
- result = described_class.for(Node, HomePage, 'foo')
219
- expect(result.count).to eq(1)
220
- expect(result.first.class).to eq(described_class)
221
- end
222
-
223
- it "accepts node_class as string also" do
224
- result = described_class.for('Node', HomePage, 'foo')
225
- expect(result.count).to eq(1)
226
- end
227
- end
228
- end
229
- end
@@ -1,96 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::Content::RoutesReloader do
4
- let(:app) { ->(env) { [200, env, "app"] } }
5
-
6
- let :request do
7
- described_class.new(app).call(Rack::MockRequest.env_for('http://example.com'))
8
- end
9
-
10
- describe "on application startup" do
11
- it "sets routes loading time" do
12
- expect(described_class.routes_loaded).to_not be_nil
13
- end
14
- end
15
-
16
- describe "on each request" do
17
- it "compares latest updates" do
18
- expect(described_class).to receive(:reload_if_needed)
19
- request
20
- end
21
- end
22
-
23
- describe ".reset!" do
24
-
25
- it "changes class @updated_at instance to nil" do
26
- described_class.instance_variable_set(:@updated_at, "x")
27
- expect{ described_class.reset! }.to change{ described_class.instance_variable_get(:@updated_at) }.to(nil)
28
- end
29
-
30
- end
31
-
32
- describe ".reload_if_needed" do
33
-
34
- context "when reload is needed" do
35
- it "reloads routes" do
36
- allow(described_class).to receive(:needs_reload?).and_return true
37
- expect(Rails.application).to receive(:reload_routes!)
38
- described_class.reload_if_needed
39
- end
40
- end
41
-
42
- context "when reload is not needed" do
43
- it "does not reload routes" do
44
- allow(described_class).to receive(:needs_reload?).and_return false
45
- expect(Rails.application).to_not receive(:reload_routes!)
46
- described_class.reload_if_needed
47
- end
48
- end
49
-
50
- end
51
-
52
- describe ".needs_reload?" do
53
-
54
- context "when no nodes exist" do
55
- it "returns false" do
56
- allow(Node).to receive(:updated_at).and_return(nil)
57
- expect(described_class.needs_reload?).to be false
58
- end
59
- end
60
-
61
- context "when node routes has not been loaded" do
62
- it "returns true" do
63
- described_class.instance_variable_set(:@updated_at, nil)
64
- allow(Node).to receive(:updated_at).and_return(Time.parse("1991-01-01"))
65
- expect(described_class.needs_reload?).to be true
66
- end
67
- end
68
-
69
- context "when node routes are up to date" do
70
- it "returns false" do
71
- described_class.instance_variable_set(:@updated_at, Time.parse("1991-01-01"))
72
- allow(Node).to receive(:updated_at).and_return(Time.parse("1991-01-01"))
73
- expect(described_class.needs_reload?).to be false
74
- end
75
- end
76
-
77
- context "when node routes are outdated" do
78
- it "returns true" do
79
- allow(Node).to receive(:updated_at).and_return(Time.now + 1.minute)
80
- expect(described_class.needs_reload?).to be true
81
- end
82
- end
83
-
84
- it "checks all content model classes" do
85
- class RouteReloaderDummyNode
86
- def self.updated_at
87
- Time.now + 1.minute
88
- end
89
- end
90
- allow(Releaf::Content).to receive(:models).and_return [ Node, RouteReloaderDummyNode ]
91
- allow(Node).to receive(:updated_at).and_return(nil)
92
- expect(described_class.needs_reload?).to be true
93
- end
94
-
95
- end
96
- end
@@ -1,311 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::Content::NodeMapper do
4
-
5
- let(:multiple_node_resources) {{
6
- 'Node' => {
7
- controller: 'Releaf::Content::NodesController',
8
- routing: { site: "main_site", constraints: { host: /^releaf\.local$/ } }
9
- },
10
- 'OtherSite::OtherNode' => {
11
- controller: 'Admin::OtherSite::OtherNodesController',
12
- routing: { site: "other_site", constraints: { host: /^other\.releaf\.local$/ } }
13
- }
14
- }}
15
-
16
- before do
17
- @text_page = create(:text_page)
18
- @lv_root_node = create(:home_page_node, name: "lv", locale: "lv", slug: 'lv')
19
- @node = create(:node, slug: 'test-page', content: @text_page, parent: @lv_root_node)
20
- end
21
-
22
- before with_multiple_node_classes: true do
23
- allow( Releaf.application.config ).to receive(:content).and_return(
24
- Releaf::Content::Configuration.new(resources: multiple_node_resources)
25
- )
26
- @other_text_page = create(:text_page)
27
- @other_lv_root_node = create(:other_home_page_node, name: "lv", locale: "lv", slug: 'lv')
28
- @other_node = create(:other_node, slug: 'test-page', content: @other_text_page, parent: @other_lv_root_node)
29
- end
30
-
31
- after do
32
- Dummy::Application.reload_routes!
33
- end
34
-
35
- after(:all) do
36
- # without this the test environent remains polluted with test node class config.
37
- # the routing configuration gets already reset after each test in the after block above
38
- # but that seems to not be enough
39
- Dummy::Application.reload_routes!
40
- end
41
-
42
- describe "#node_routes_for", create_nodes: true do
43
-
44
- it "draws public website routes for default node class" do
45
-
46
- routes.draw do
47
- node_routes_for(TextPage) do |route|
48
- get 'show'
49
- delete :destroy
50
- end
51
- end
52
-
53
- expect(get: '/lv/test-page').to route_to(
54
- "controller" => "text_pages",
55
- "action" => "show",
56
- "node_class" => "Node",
57
- "node_id" => @node.id.to_s,
58
- "locale" => 'lv'
59
- )
60
-
61
- expect(delete: '/lv/test-page').to route_to(
62
- "controller" => "text_pages",
63
- "action" => "destroy",
64
- "node_class" => "Node",
65
- "node_id" => @node.id.to_s,
66
- "locale" => 'lv'
67
- )
68
- end
69
-
70
- context "when a controller is given in arguments" do
71
-
72
- it "draws the public website route to given controller" do
73
- routes.draw do
74
- node_routes_for(TextPage, controller: 'home_pages') do |route|
75
- get 'show'
76
- end
77
- end
78
-
79
- expect(get: '/lv/test-page').to route_to(
80
- "controller" => "home_pages",
81
- "action" => "show",
82
- "node_class" => "Node",
83
- "node_id" => @node.id.to_s,
84
- "locale" => 'lv'
85
- )
86
- end
87
-
88
- end
89
-
90
- context "when a controller is passed in route options" do
91
- it "draws the public website route using the controller from options" do
92
- routes.draw do
93
- node_routes_for(TextPage, controller: 'doesnt_matter') do |route|
94
- get 'home_pages#hide'
95
- end
96
- end
97
-
98
- expect(get: '/lv/test-page').to route_to(
99
- "controller" => "home_pages",
100
- "action" => "hide",
101
- "node_class" => "Node",
102
- "node_id" => @node.id.to_s,
103
- "locale" => 'lv'
104
- )
105
- end
106
- end
107
-
108
- context "when custom action is given in :to" do
109
- it "draws the public website route using the action from :to" do
110
- routes.draw do
111
- node_routes_for(TextPage) do |route|
112
- get '' , to: '#list'
113
- end
114
- end
115
-
116
- expect(get: '/lv/test-page').to route_to(
117
- "controller" => "text_pages",
118
- "action" => "list",
119
- "node_class" => "Node",
120
- "node_id" => @node.id.to_s,
121
- "locale" => 'lv'
122
- )
123
-
124
- end
125
- end
126
-
127
- context "when custom controller and action are given in :to" do
128
- it "draws the public website route using the given controller and action from :to" do
129
- routes.draw do
130
- node_routes_for(TextPage) do |route|
131
- get '' , to: 'home_pages#list'
132
- end
133
- end
134
-
135
- expect(get: '/lv/test-page').to route_to(
136
- "controller" => "home_pages",
137
- "action" => "list",
138
- "node_class" => "Node",
139
- "node_id" => @node.id.to_s,
140
- "locale" => 'lv'
141
- )
142
- end
143
- end
144
-
145
- context "when additional params are given in route path" do
146
- it "passes the params to the public website route" do
147
- routes.draw do
148
- node_routes_for(TextPage) do |route|
149
- get ':some_id', to: "#view"
150
- end
151
- end
152
-
153
- expect(get: '/lv/test-page/8888').to route_to(
154
- "controller" => "text_pages",
155
- "action" => "view",
156
- "node_class" => "Node",
157
- "node_id" => @node.id.to_s,
158
- "locale" => 'lv',
159
- "some_id" => '8888'
160
- )
161
- end
162
- end
163
-
164
- context "when custom path is given for route" do
165
- it "uses the custom path for public website route" do
166
- routes.draw do
167
- node_routes_for(TextPage) do |route|
168
- route.path += "/abc/:my_id"
169
- get 'home_pages#show'
170
- end
171
- end
172
-
173
- expect(get: '/lv/test-page/abc/333').to route_to(
174
- "controller" => "home_pages",
175
- "action" => "show",
176
- "node_class" => "Node",
177
- "node_id" => @node.id.to_s,
178
- "locale" => 'lv',
179
- "my_id" => '333'
180
- )
181
- end
182
- end
183
-
184
- context "when custom node class is given as an argument", with_multiple_node_classes: true do
185
-
186
- it "uses that node class for the public website route" do
187
- routes.draw do
188
- node_routes_for(TextPage, node_class: "OtherSite::OtherNode") do |route|
189
- get 'show'
190
- end
191
- end
192
-
193
- expect(get: '/lv/test-page').to route_to(
194
- "controller" => "text_pages",
195
- "action" => "show",
196
- "node_class" => "OtherSite::OtherNode",
197
- "node_id" => @other_node.id.to_s,
198
- "locale" => 'lv',
199
- "site" => 'other_site'
200
- )
201
- end
202
-
203
- end
204
-
205
- end
206
-
207
- describe "#for_node_class", with_multiple_node_classes: true do
208
-
209
- it "uses given node class as a default when drawing public website routes in the given block" do
210
- routes.draw do
211
- for_node_class "OtherSite::OtherNode" do
212
- node_routes_for(TextPage) do |route|
213
- get 'show'
214
- end
215
- end
216
- end
217
-
218
- expect(get: '/lv/test-page').to route_to(
219
- "controller" => "text_pages",
220
- "action" => "show",
221
- "node_class" => "OtherSite::OtherNode",
222
- "node_id" => @other_node.id.to_s,
223
- "locale" => 'lv',
224
- "site" => 'other_site'
225
- )
226
- end
227
-
228
- it "restores default node class after block has been executed" do
229
- routes.draw do
230
- for_node_class "OtherSite::OtherNode" do
231
- node_routes_for(TextPage) do |route|
232
- get 'show'
233
- end
234
- end
235
- node_routes_for(HomePage) do |route|
236
- get 'show'
237
- end
238
- end
239
-
240
- expect(get: '/lv').to route_to(
241
- "controller" => "home_pages",
242
- "action" => "show",
243
- "node_class" => "Node",
244
- "node_id" => @lv_root_node.id.to_s,
245
- "locale" => 'lv',
246
- "site" => 'main_site'
247
- )
248
- end
249
-
250
- end
251
-
252
- describe "#node_routing", with_multiple_node_classes: true do
253
-
254
- it "draws public website routes for all node classes in the given routing hash using respective route constraints" do
255
-
256
- routes.draw do
257
- node_routing( Releaf::Content.routing ) do
258
- node_routes_for(TextPage) do |route|
259
- get 'show'
260
- end
261
- end
262
- end
263
-
264
- expect(get: 'http://releaf.local/lv/test-page').to route_to(
265
- "controller" => "text_pages",
266
- "action" => "show",
267
- "node_class" => "Node",
268
- "node_id" => @node.id.to_s,
269
- "locale" => 'lv',
270
- "site" => 'main_site'
271
- )
272
-
273
- expect(get: 'http://other.releaf.local/lv/test-page').to route_to(
274
- "controller" => "text_pages",
275
- "action" => "show",
276
- "node_class" => "OtherSite::OtherNode",
277
- "node_id" => @other_node.id.to_s,
278
- "locale" => 'lv',
279
- "site" => 'other_site'
280
- )
281
-
282
- end
283
-
284
- context "when passed a routing hash for only a subset of available node classes" do
285
- it "draws constrained public website routes only for the given node classes" do
286
- routes.draw do
287
- routing_hash = Releaf::Content.routing.except('Node')
288
- node_routing( routing_hash ) do
289
- node_routes_for(TextPage) do |route|
290
- get 'show'
291
- end
292
- end
293
- end
294
-
295
- expect(get: 'http://other.releaf.local/lv/test-page').to route_to(
296
- "controller" => "text_pages",
297
- "action" => "show",
298
- "node_class" => "OtherSite::OtherNode",
299
- "node_id" => @other_node.id.to_s,
300
- "locale" => 'lv',
301
- "site" => 'other_site'
302
- )
303
-
304
- expect(get: 'http://releaf.local/lv/test-page').to_not be_routable
305
-
306
- end
307
- end
308
-
309
- end
310
-
311
- end