locomotivecms_steam 1.4.1 → 1.5.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +31 -25
  4. data/Rakefile +2 -2
  5. data/config/locales/pt-BR.yml +43 -13
  6. data/lib/locomotive/steam.rb +9 -4
  7. data/lib/locomotive/steam/adapters/filesystem.rb +2 -7
  8. data/lib/locomotive/steam/adapters/filesystem/sanitizer.rb +0 -2
  9. data/lib/locomotive/steam/adapters/filesystem/sanitizers/page.rb +11 -0
  10. data/lib/locomotive/steam/adapters/filesystem/sanitizers/section.rb +37 -0
  11. data/lib/locomotive/steam/adapters/filesystem/sanitizers/site.rb +18 -0
  12. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_entry.rb +4 -3
  13. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/content_type.rb +4 -3
  14. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/section.rb +40 -0
  15. data/lib/locomotive/steam/adapters/filesystem/yaml_loaders/snippet.rb +0 -1
  16. data/lib/locomotive/steam/adapters/memory.rb +1 -1
  17. data/lib/locomotive/steam/entities/content_entry.rb +15 -3
  18. data/lib/locomotive/steam/entities/content_type.rb +2 -1
  19. data/lib/locomotive/steam/entities/page.rb +21 -19
  20. data/lib/locomotive/steam/entities/section.rb +24 -0
  21. data/lib/locomotive/steam/entities/site.rb +3 -1
  22. data/lib/locomotive/steam/liquid.rb +2 -0
  23. data/lib/locomotive/steam/liquid/drops/page.rb +4 -2
  24. data/lib/locomotive/steam/liquid/drops/params.rb +51 -0
  25. data/lib/locomotive/steam/liquid/drops/section.rb +56 -0
  26. data/lib/locomotive/steam/liquid/drops/section_block.rb +47 -0
  27. data/lib/locomotive/steam/liquid/drops/section_content_proxy.rb +97 -0
  28. data/lib/locomotive/steam/liquid/drops/section_editor_setting_data.rb +65 -0
  29. data/lib/locomotive/steam/liquid/drops/site.rb +2 -1
  30. data/lib/locomotive/steam/liquid/errors.rb +2 -0
  31. data/lib/locomotive/steam/liquid/filters/base.rb +3 -3
  32. data/lib/locomotive/steam/liquid/filters/date.rb +1 -1
  33. data/lib/locomotive/steam/liquid/filters/html.rb +7 -2
  34. data/lib/locomotive/steam/liquid/filters/json.rb +3 -1
  35. data/lib/locomotive/steam/liquid/tags/action.rb +2 -2
  36. data/lib/locomotive/steam/liquid/tags/alt_page_links.rb +63 -0
  37. data/lib/locomotive/steam/liquid/tags/concerns/i18n_page.rb +1 -1
  38. data/lib/locomotive/steam/liquid/tags/concerns/section.rb +117 -0
  39. data/lib/locomotive/steam/liquid/tags/global_section.rb +32 -0
  40. data/lib/locomotive/steam/liquid/tags/model_form.rb +1 -1
  41. data/lib/locomotive/steam/liquid/tags/section.rb +87 -0
  42. data/lib/locomotive/steam/liquid/tags/sections_dropzone.rb +56 -0
  43. data/lib/locomotive/steam/liquid/tags/snippet.rb +3 -2
  44. data/lib/locomotive/steam/liquid/template.rb +0 -5
  45. data/lib/locomotive/steam/middlewares.rb +1 -1
  46. data/lib/locomotive/steam/middlewares/auth.rb +29 -13
  47. data/lib/locomotive/steam/middlewares/concerns/helpers.rb +59 -0
  48. data/lib/locomotive/steam/middlewares/concerns/liquid_context.rb +94 -0
  49. data/lib/locomotive/steam/middlewares/encoded_link_redirection.rb +45 -0
  50. data/lib/locomotive/steam/middlewares/entry_submission.rb +1 -1
  51. data/lib/locomotive/steam/middlewares/favicon.rb +1 -1
  52. data/lib/locomotive/steam/middlewares/locale.rb +63 -15
  53. data/lib/locomotive/steam/middlewares/locale_redirection.rb +18 -6
  54. data/lib/locomotive/steam/middlewares/logging.rb +1 -1
  55. data/lib/locomotive/steam/middlewares/page.rb +35 -6
  56. data/lib/locomotive/steam/middlewares/path.rb +1 -1
  57. data/lib/locomotive/steam/middlewares/private_access.rb +1 -1
  58. data/lib/locomotive/steam/middlewares/redirection.rb +1 -1
  59. data/lib/locomotive/steam/middlewares/renderer.rb +2 -82
  60. data/lib/locomotive/steam/middlewares/robots.rb +1 -1
  61. data/lib/locomotive/steam/middlewares/section.rb +56 -0
  62. data/lib/locomotive/steam/middlewares/site.rb +1 -1
  63. data/lib/locomotive/steam/middlewares/sitemap.rb +58 -45
  64. data/lib/locomotive/steam/middlewares/templatized_page.rb +1 -1
  65. data/lib/locomotive/steam/middlewares/thread_safe.rb +85 -2
  66. data/lib/locomotive/steam/middlewares/timezone.rb +1 -1
  67. data/lib/locomotive/steam/middlewares/url_redirection.rb +1 -1
  68. data/lib/locomotive/steam/models/entity.rb +0 -2
  69. data/lib/locomotive/steam/models/mapper.rb +5 -3
  70. data/lib/locomotive/steam/models/scope.rb +8 -0
  71. data/lib/locomotive/steam/repositories.rb +4 -0
  72. data/lib/locomotive/steam/repositories/content_entry_repository.rb +6 -2
  73. data/lib/locomotive/steam/repositories/page_repository.rb +4 -1
  74. data/lib/locomotive/steam/repositories/section_repository.rb +14 -0
  75. data/lib/locomotive/steam/repositories/site_repository.rb +1 -1
  76. data/lib/locomotive/steam/repositories/snippet_repository.rb +0 -3
  77. data/lib/locomotive/steam/server.rb +3 -1
  78. data/lib/locomotive/steam/services.rb +17 -1
  79. data/lib/locomotive/steam/services/action_service.rb +10 -0
  80. data/lib/locomotive/steam/services/concerns/decorator.rb +0 -2
  81. data/lib/locomotive/steam/services/image_resizer_service.rb +1 -1
  82. data/lib/locomotive/steam/services/page_finder_service.rb +6 -0
  83. data/lib/locomotive/steam/services/section_finder_service.rb +17 -0
  84. data/lib/locomotive/steam/services/url_builder_service.rb +31 -17
  85. data/lib/locomotive/steam/services/url_finder_service.rb +87 -0
  86. data/lib/locomotive/steam/version.rb +2 -2
  87. data/locomotivecms_steam.gemspec +1 -1
  88. data/spec/fixtures/default/app/views/pages/tags/section.liquid.haml +11 -0
  89. data/spec/fixtures/default/app/views/sections/carousel.liquid +20 -0
  90. data/spec/fixtures/default/app/views/sections/footer.liquid +48 -0
  91. data/spec/fixtures/default/app/views/sections/header.liquid +54 -0
  92. data/spec/fixtures/default/config/deploy.yml +1 -1
  93. data/spec/fixtures/errors/section_bad_json_content.liquid +9 -0
  94. data/spec/fixtures/errors/section_bad_json_header.liquid +8 -0
  95. data/spec/fixtures/mongodb/locomotive_accounts.bson +0 -0
  96. data/spec/fixtures/mongodb/locomotive_accounts.metadata.json +1 -1
  97. data/spec/fixtures/mongodb/locomotive_activities.bson +0 -0
  98. data/spec/fixtures/mongodb/locomotive_activities.metadata.json +1 -1
  99. data/spec/fixtures/mongodb/locomotive_content_assets.bson +0 -0
  100. data/spec/fixtures/mongodb/locomotive_content_assets.metadata.json +1 -1
  101. data/spec/fixtures/mongodb/locomotive_content_entries.bson +0 -0
  102. data/spec/fixtures/mongodb/locomotive_content_entries.metadata.json +1 -1
  103. data/spec/fixtures/mongodb/locomotive_content_types.bson +0 -0
  104. data/spec/fixtures/mongodb/locomotive_content_types.metadata.json +1 -1
  105. data/spec/fixtures/mongodb/locomotive_pages.bson +0 -0
  106. data/spec/fixtures/mongodb/locomotive_pages.metadata.json +1 -1
  107. data/spec/fixtures/mongodb/locomotive_sections.bson +0 -0
  108. data/spec/fixtures/mongodb/{sessions.metadata.json → locomotive_sections.metadata.json} +1 -1
  109. data/spec/fixtures/mongodb/locomotive_sites.bson +0 -0
  110. data/spec/fixtures/mongodb/locomotive_sites.metadata.json +1 -1
  111. data/spec/fixtures/mongodb/locomotive_snippets.bson +0 -0
  112. data/spec/fixtures/mongodb/locomotive_snippets.metadata.json +1 -1
  113. data/spec/fixtures/mongodb/locomotive_theme_assets.bson +0 -0
  114. data/spec/fixtures/mongodb/locomotive_theme_assets.metadata.json +1 -1
  115. data/spec/fixtures/mongodb/locomotive_translations.bson +0 -0
  116. data/spec/fixtures/mongodb/locomotive_translations.metadata.json +1 -1
  117. data/spec/integration/repositories/content_entry_repository_spec.rb +1 -1
  118. data/spec/integration/repositories/page_repository_spec.rb +1 -1
  119. data/spec/integration/repositories/theme_asset_repository_spec.rb +1 -1
  120. data/spec/integration/server/auth_spec.rb +0 -4
  121. data/spec/integration/server/basic_spec.rb +8 -0
  122. data/spec/integration/server/contact_form_spec.rb +15 -2
  123. data/spec/integration/server/sitemap_spec.rb +5 -3
  124. data/spec/integration/services/content_entry_service_spec.rb +1 -1
  125. data/spec/support/helpers.rb +3 -3
  126. data/spec/unit/adapters/filesystem/sanitizers/section_spec.rb +65 -0
  127. data/spec/unit/adapters/filesystem/sanitizers/site_spec.rb +28 -2
  128. data/spec/unit/adapters/filesystem/yaml_loaders/content_entry_spec.rb +8 -4
  129. data/spec/unit/adapters/filesystem/yaml_loaders/content_type_spec.rb +2 -2
  130. data/spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb +1 -1
  131. data/spec/unit/adapters/filesystem/yaml_loaders/section_spec.rb +27 -0
  132. data/spec/unit/entities/section_spec.rb +29 -0
  133. data/spec/unit/liquid/drops/params_spec.rb +38 -0
  134. data/spec/unit/liquid/drops/section_content_proxy_spec.rb +88 -0
  135. data/spec/unit/liquid/drops/section_spec.rb +26 -0
  136. data/spec/unit/liquid/filters/html_spec.rb +198 -170
  137. data/spec/unit/liquid/filters/json_spec.rb +7 -0
  138. data/spec/unit/liquid/tags/action_spec.rb +12 -2
  139. data/spec/unit/liquid/tags/alt_page_links_spec.rb +58 -0
  140. data/spec/unit/liquid/tags/global_section_spec.rb +130 -0
  141. data/spec/unit/liquid/tags/link_to_spec.rb +1 -1
  142. data/spec/unit/liquid/tags/locale_switcher_spec.rb +1 -1
  143. data/spec/unit/liquid/tags/model_form_spec.rb +8 -0
  144. data/spec/unit/liquid/tags/path_to_spec.rb +26 -3
  145. data/spec/unit/liquid/tags/section_spec.rb +231 -0
  146. data/spec/unit/liquid/tags/sections_dropzone_spec.rb +107 -0
  147. data/spec/unit/middlewares/auth_spec.rb +9 -2
  148. data/spec/unit/middlewares/encoded_link_redirection_spec.rb +73 -0
  149. data/spec/unit/middlewares/entry_submission_spec.rb +1 -1
  150. data/spec/unit/middlewares/helpers_spec.rb +3 -64
  151. data/spec/unit/middlewares/locale_redirection_spec.rb +13 -2
  152. data/spec/unit/middlewares/locale_spec.rb +52 -10
  153. data/spec/unit/middlewares/page_spec.rb +57 -16
  154. data/spec/unit/middlewares/private_access_spec.rb +1 -1
  155. data/spec/unit/middlewares/redirection_spec.rb +1 -1
  156. data/spec/unit/middlewares/renderer_spec.rb +2 -1
  157. data/spec/unit/middlewares/section_spec.rb +70 -0
  158. data/spec/unit/middlewares/site_spec.rb +1 -1
  159. data/spec/unit/middlewares/sitemap_spec.rb +4 -42
  160. data/spec/unit/middlewares/url_redirection_spec.rb +1 -1
  161. data/spec/unit/repositories/content_entry_repository_spec.rb +13 -13
  162. data/spec/unit/repositories/section_repository_spec.rb +38 -0
  163. data/spec/unit/repositories/snippet_repository_spec.rb +0 -3
  164. data/spec/unit/services/action_service_spec.rb +19 -1
  165. data/spec/unit/services/asset_host_service_spec.rb +8 -0
  166. data/spec/unit/services/parent_finder_service_spec.rb +1 -2
  167. data/spec/unit/services/section_finder_service_spec.rb +28 -0
  168. data/spec/unit/services/url_builder_service_spec.rb +14 -4
  169. data/spec/unit/services/url_finder_service_spec.rb +100 -0
  170. data/spec/unit/services_spec.rb +34 -20
  171. metadata +72 -11
  172. data/lib/locomotive/steam/middlewares/helpers.rb +0 -140
  173. data/spec/fixtures/mongodb/sessions.bson +0 -0
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe Locomotive::Steam::Liquid::Tags::SectionsDropzone do
4
+
5
+ let(:services) { Locomotive::Steam::Services.build_instance(nil) }
6
+ let(:finder) { services.section_finder }
7
+ let(:source) { '{% sections_dropzone %}' }
8
+ let(:live_editing) { true }
9
+ let(:page) { liquid_instance_double('Page', sections_dropzone_content: content) }
10
+ let(:assigns) { { 'page' => page } }
11
+ let(:registers) { { services: services, live_editing: live_editing } }
12
+ let(:context) { ::Liquid::Context.new(assigns, {}, registers) }
13
+
14
+ describe 'rendering' do
15
+
16
+ subject { render_template(source, context) }
17
+
18
+ context 'no sections' do
19
+
20
+ let(:content) { [] }
21
+
22
+ it 'renders an empty string' do
23
+ is_expected.to eq '<div class="locomotive-sections"></div>'
24
+ end
25
+
26
+ end
27
+
28
+ context 'with sections' do
29
+
30
+ let(:content) { [
31
+ {
32
+ type: 'hero',
33
+ settings: { title: 'Hello world' },
34
+ blocks: []
35
+ }.deep_stringify_keys,
36
+ {
37
+ type: 'slideshow',
38
+ settings: {},
39
+ blocks: [{ settings: { title: 'Slide 1' } }, { settings: { title: 'Slide 2' } }]
40
+ }.deep_stringify_keys
41
+ ] }
42
+
43
+ let(:hero_source) { %(<h1>{{ section.settings.title }}</h1>) }
44
+ let(:slideshow_source) { %({% for block in section.blocks %}<div {{ block.locomotive_attributes }}><p>{{ block.settings.title }}</p></div>{% endfor %}) }
45
+
46
+ let(:hero_section) {
47
+ instance_double('Hero',
48
+ slug: 'hero',
49
+ type: 'hero',
50
+ definition: { settings: [{ id: 'title', type: 'text' }], blocks: [] }.deep_stringify_keys,
51
+ liquid_source: hero_source)
52
+ }
53
+ let(:slideshow_section) {
54
+ instance_double('Slideshow',
55
+ slug: 'slideshow',
56
+ type: 'slideshow',
57
+ definition: {
58
+ settings: [],
59
+ blocks: [{ settings: [{ id: 'title', type: 'text' }] }]
60
+ }.deep_stringify_keys,
61
+ liquid_source: slideshow_source) }
62
+
63
+ before do
64
+ allow(finder).to receive(:find).with('hero').and_return(hero_section)
65
+ allow(finder).to receive(:find).with('slideshow').and_return(slideshow_section)
66
+ end
67
+
68
+ it 'renders the list of sections' do
69
+ is_expected.to eq <<-HTML
70
+ <div class="locomotive-sections">
71
+ <div id="locomotive-section-dropzone-0" class="locomotive-section" data-locomotive-section-type="hero">
72
+ <h1 data-locomotive-editor-setting="section-dropzone-0.title">Hello world</h1>
73
+ </div>
74
+ <div id="locomotive-section-dropzone-1" class="locomotive-section" data-locomotive-section-type="slideshow">
75
+ <div data-locomotive-block="section-dropzone-1-block-0"><p data-locomotive-editor-setting="section-dropzone-1-block.0.title">Slide 1</p></div>
76
+ <div data-locomotive-block="section-dropzone-1-block-1"><p data-locomotive-editor-setting="section-dropzone-1-block.1.title">Slide 2</p></div>
77
+ </div>
78
+ </div>
79
+ HTML
80
+ .strip.gsub(/\n\s+/, '')
81
+ end
82
+
83
+ context 'live editing is off' do
84
+
85
+ let(:live_editing) { false }
86
+
87
+ it 'renders the list of sections' do
88
+ is_expected.to eq <<-HTML
89
+ <div class="locomotive-sections">
90
+ <div id="locomotive-section-dropzone-0" class="locomotive-section" data-locomotive-section-type="hero">
91
+ <h1>Hello world</h1>
92
+ </div>
93
+ <div id="locomotive-section-dropzone-1" class="locomotive-section" data-locomotive-section-type="slideshow">
94
+ <div ><p>Slide 1</p></div>
95
+ <div ><p>Slide 2</p></div>
96
+ </div>
97
+ </div>
98
+ HTML
99
+ .strip.gsub(/\n\s+/, '')
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
4
- require_relative '../../../lib/locomotive/steam/middlewares/helpers'
4
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
5
5
  require_relative '../../../lib/locomotive/steam/middlewares/auth'
6
6
 
7
7
  describe Locomotive::Steam::Middlewares::Auth::AuthOptions do
@@ -16,7 +16,14 @@ describe Locomotive::Steam::Middlewares::Auth::AuthOptions do
16
16
 
17
17
  subject { options.smtp }
18
18
 
19
- it { is_expected.to eq('address': '127.0.0.1', 'user_name': 'John', 'password': 'doe', 'port': 25) }
19
+ it { is_expected.to eq(
20
+ address: '127.0.0.1',
21
+ user_name: 'John',
22
+ password: 'doe',
23
+ port: 25,
24
+ authentication: 'plain',
25
+ enable_starttls_auto: false,
26
+ ) }
20
27
 
21
28
  context 'no smtp metafields' do
22
29
 
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
4
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
5
+ require_relative '../../../lib/locomotive/steam/middlewares/encoded_link_redirection'
6
+
7
+ describe Locomotive::Steam::Middlewares::EncodedLinkRedirection do
8
+
9
+ let(:url_finder) { instance_double('UrlFinder') }
10
+ let(:services) { instance_double('Services', url_finder: url_finder) }
11
+ let(:site) { instance_double('Site') }
12
+ let(:url) { 'http://models.example.com' }
13
+ let(:mounted_on) { nil }
14
+ let(:locomotive_path) { nil }
15
+ let(:app) { ->(env) { [200, env, 'app'] } }
16
+ let(:middleware) { described_class.new(app) }
17
+
18
+ subject do
19
+ env = env_for(url, 'PATH_INFO' => locomotive_path, 'steam.site' => site)
20
+ env['steam.request'] = Rack::Request.new(env)
21
+ env['steam.mounted_on'] = mounted_on
22
+ env['steam.services'] = services
23
+ env['locomotive.path'] = locomotive_path
24
+ code, env = middleware.call(env)
25
+ [code, env['Location']]
26
+ end
27
+
28
+ describe 'no redirections' do
29
+
30
+ it { is_expected.to eq [200, nil] }
31
+
32
+ end
33
+
34
+ describe 'valid encoded link' do
35
+
36
+ let(:encoded_link) { 'eyJ0eXBlIjoiX2V4dGVybmFsIiwidmFsdWUiOiJodHRwczovL3d3dy5ub2NvZmZlZS5mciIsImxhYmVsIjpbImV4dGVybmFsIiwiaHR0cHM6Ly93d3cubm9jb2ZmZWUuZnIiXX0' }
37
+ let(:locomotive_path) { "/_locomotive-link/#{encoded_link}" }
38
+
39
+ context 'external url' do
40
+
41
+ it 'redirects (302) to the url stored in the encoded link' do
42
+ expect(url_finder).to receive(:decode_link).with('eyJ0eXBlIjoiX2V4dGVybmFsIiwidmFsdWUiOiJodHRwczovL3d3dy5ub2NvZmZlZS5mciIsImxhYmVsIjpbImV4dGVybmFsIiwiaHR0cHM6Ly93d3cubm9jb2ZmZWUuZnIiXX0').and_return({
43
+ 'type' => 'external',
44
+ 'value' => 'https://www.nocoffee.fr',
45
+ 'locale' => 'fr'
46
+ })
47
+ expect(services).to receive(:locale=).with('fr')
48
+ allow(url_finder).to receive(:url_for).and_return(['https://www.nocoffee.fr', false])
49
+ is_expected.to eq [302, 'https://www.nocoffee.fr']
50
+ end
51
+
52
+ end
53
+
54
+ context 'local page' do
55
+
56
+ let(:mounted_on) { '/my_app' }
57
+
58
+ it 'redirects (302) to the page stored in the encoded link' do
59
+ expect(url_finder).to receive(:decode_link).with('eyJ0eXBlIjoiX2V4dGVybmFsIiwidmFsdWUiOiJodHRwczovL3d3dy5ub2NvZmZlZS5mciIsImxhYmVsIjpbImV4dGVybmFsIiwiaHR0cHM6Ly93d3cubm9jb2ZmZWUuZnIiXX0').and_return({
60
+ 'type' => 'page',
61
+ 'value' => '42',
62
+ 'locale' => 'fr'
63
+ })
64
+ expect(services).to receive(:locale=).with('fr')
65
+ allow(url_finder).to receive(:url_for).and_return(['/about-us', false])
66
+ is_expected.to eq [302, '/my_app/about-us']
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
4
- require_relative '../../../lib/locomotive/steam/middlewares/helpers'
4
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
5
5
  require_relative '../../../lib/locomotive/steam/middlewares/entry_submission'
6
6
 
7
7
  describe Locomotive::Steam::Middlewares::EntrySubmission do
@@ -1,10 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
- require_relative '../../../lib/locomotive/steam/middlewares/helpers'
3
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
4
4
 
5
- describe Locomotive::Steam::Middlewares::Helpers do
5
+ describe Locomotive::Steam::Middlewares::Concerns::Helpers do
6
6
 
7
- let(:middleware) { Class.new { include Locomotive::Steam::Middlewares::Helpers } }
7
+ let(:middleware) { Class.new { include Locomotive::Steam::Middlewares::Concerns::Helpers } }
8
8
  let(:instance) { middleware.new }
9
9
 
10
10
  describe '#make_local_path' do
@@ -58,65 +58,4 @@ describe Locomotive::Steam::Middlewares::Helpers do
58
58
 
59
59
  end
60
60
 
61
- describe '#params' do
62
-
63
- let(:url) { 'http://models.example.com' }
64
- let(:app) { ->(env) { [200, env, 'app'] } }
65
- let(:options) { {} }
66
-
67
- before do
68
- env = env_for(url, options)
69
- env['steam.request'] = Rack::Request.new(env)
70
- allow(instance).to receive(:app).and_return(app)
71
- allow(instance).to receive(:env).and_return(env)
72
- end
73
-
74
- subject { instance.params }
75
-
76
- context 'from a GET' do
77
-
78
- let(:url) { 'http://models.example.com?foo=bar' }
79
-
80
- it { is_expected.to eq('foo' => 'bar') }
81
-
82
- end
83
-
84
- context 'from a GET (JSON)' do
85
-
86
- let(:url) { 'http://models.example.com/api.json?foo=bar' }
87
-
88
- it { is_expected.to eq('foo' => 'bar') }
89
-
90
- end
91
-
92
- context 'from the body of JSON POST request' do
93
-
94
- let(:input) { '{"foo": { "bar": 42 } }' }
95
-
96
- let(:options) { {
97
- method: 'POST',
98
- input: input,
99
- 'CONTENT_TYPE' => 'application/json'
100
- } }
101
-
102
- it { is_expected.to eq('foo' => { 'bar' => 42 }) }
103
-
104
- it 'builds a hash with indifferent access' do
105
- expect(subject[:foo][:bar]).to eq 42
106
- end
107
-
108
- context 'the JSON is invalid' do
109
-
110
- let(:input) { '{ a: 2 }' }
111
-
112
- it 'returns an empty hash' do
113
- is_expected.to eq({})
114
- end
115
-
116
- end
117
-
118
- end
119
-
120
- end
121
-
122
61
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
4
- require_relative '../../../lib/locomotive/steam/middlewares/helpers'
4
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
5
5
  require_relative '../../../lib/locomotive/steam/middlewares/locale_redirection'
6
6
 
7
7
  describe Locomotive::Steam::Middlewares::LocaleRedirection do
@@ -32,6 +32,17 @@ describe Locomotive::Steam::Middlewares::LocaleRedirection do
32
32
  let(:locale_in_path) { false }
33
33
  it { is_expected.to eq [200, nil] }
34
34
 
35
+ context 'the lang elected is not the default lang' do
36
+ let(:locale) { 'fr' }
37
+ it { is_expected.to eq [302, '/fr'] }
38
+ end
39
+
40
+ end
41
+
42
+ describe 'locale is part of the path' do
43
+ let(:url) { 'http://models.example.com/fr' }
44
+ let(:locale) { 'fr' }
45
+ it { is_expected.to eq [200, nil] }
35
46
  end
36
47
 
37
48
  describe 'for seo purpose redirect to the path without the locale' do
@@ -53,7 +64,7 @@ describe Locomotive::Steam::Middlewares::LocaleRedirection do
53
64
 
54
65
  describe 'add default locale to root path' do
55
66
  let(:url) { 'http://models.example.com/' }
56
- it { is_expected.to eq [301, '/de'] }
67
+ it { is_expected.to eq [302, '/de'] }
57
68
  end
58
69
 
59
70
  describe 'add default locale to long path' do
@@ -1,23 +1,67 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
4
- require_relative '../../../lib/locomotive/steam/middlewares/helpers'
4
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
5
5
  require_relative '../../../lib/locomotive/steam/middlewares/locale'
6
6
 
7
7
  describe Locomotive::Steam::Middlewares::Locale do
8
8
 
9
- let(:site) { instance_double('Site', default_locale: :de, locales: %w(de fr)) }
9
+ let(:site) { instance_double('Site', default_locale: :de, locales: [:de, :fr, :en]) }
10
10
  let(:url) { 'http://models.example.com' }
11
11
  let(:app) { ->(env) { [200, env, 'app'] } }
12
12
  let(:services) { instance_double('Services', :locale= => 'en') }
13
13
  let(:middleware) { Locomotive::Steam::Middlewares::Locale.new(app) }
14
+ let(:session) { {} }
15
+ let(:accept_language) { '' }
14
16
 
15
17
  subject do
16
- env = env_for(url, 'steam.site' => site)
18
+ env = env_for(
19
+ url,
20
+ 'steam.site' => site,
21
+ 'rack.session' => session,
22
+ 'HTTP_ACCEPT_LANGUAGE' => accept_language)
17
23
  env['steam.request'] = Rack::Request.new(env)
18
24
  env['steam.services'] = services
19
25
  code, env = middleware.call(env)
20
- env['steam.locale']
26
+ [env['steam.locale'], session['steam-locale']&.to_sym]
27
+ end
28
+
29
+ describe 'no locale defined in the path' do
30
+
31
+ describe 'first connexion' do
32
+
33
+ context 'without accept-language header' do
34
+
35
+ it { is_expected.to eq [:de, :de] }
36
+
37
+ end
38
+
39
+ context 'with accept-language header' do
40
+
41
+ let(:accept_language) { 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7' }
42
+
43
+ it { is_expected.to eq [:fr, :fr] }
44
+
45
+ context 'with url path' do
46
+
47
+ let(:url) { 'http://models.example.com/werkzeug' }
48
+
49
+ it { is_expected.to eq [:de, :de] }
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ context 'user with session, use it' do
58
+
59
+ let(:session) { {'steam-locale' => 'en'} }
60
+
61
+ it { is_expected.to eq [:en, :en] }
62
+
63
+ end
64
+
21
65
  end
22
66
 
23
67
  describe 'locale asked in the request params' do
@@ -26,15 +70,15 @@ describe Locomotive::Steam::Middlewares::Locale do
26
70
 
27
71
  let(:url) { 'http://models.example.com?locale=' }
28
72
 
29
- it { is_expected.to eq :de }
73
+ it { is_expected.to eq [:de, :de] }
30
74
 
31
75
  end
32
76
 
33
77
  context 'the locale exists' do
34
78
 
35
- let(:url) { 'http://models.example.com?locale=fr' }
79
+ let(:url) { 'http://models.example.com?locale=en' }
36
80
 
37
- it { is_expected.to eq 'fr' }
81
+ it { is_expected.to eq [:en, :en] }
38
82
 
39
83
  end
40
84
 
@@ -42,11 +86,9 @@ describe Locomotive::Steam::Middlewares::Locale do
42
86
 
43
87
  let(:url) { 'http://models.example.com?locale=onload' }
44
88
 
45
- it { is_expected.to eq :de }
89
+ it { is_expected.to eq [:de, :de] }
46
90
 
47
91
  end
48
92
 
49
-
50
93
  end
51
-
52
94
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
4
- require_relative '../../../lib/locomotive/steam/middlewares/helpers'
4
+ require_relative '../../../lib/locomotive/steam/middlewares/concerns/helpers'
5
5
  require_relative '../../../lib/locomotive/steam/middlewares/page'
6
6
 
7
7
  describe Locomotive::Steam::Middlewares::Page do
@@ -11,20 +11,22 @@ describe Locomotive::Steam::Middlewares::Page do
11
11
  let(:page_not_found) { instance_double('PageNotFound', not_found?: true) }
12
12
  let(:page) { instance_double('Page', title: 'Hello world', fullpath: 'hello-world', not_found?: false, published?: published) }
13
13
  let(:pages) { [page] }
14
- let(:site) { instance_double('Site', default_locale: 'en') }
14
+ let(:site) { instance_double('Site', default_locale: 'en', routes: routes) }
15
15
  let(:service) { instance_double('PageFinder', match: pages) }
16
+ let(:routes) { {} }
16
17
  let(:url) { 'http://models.example.com' }
18
+ let(:path) { 'hello-world' }
17
19
  let(:app) { ->(env) { [200, env, 'app'] } }
18
20
  let(:middleware) { described_class.new(app) }
19
21
 
20
22
  subject do
21
23
  env = env_for(url, 'steam.site' => site)
22
- env['steam.path'] = 'hello-world'
24
+ env['steam.path'] = path
23
25
  env['steam.locale'] = 'en'
24
- env['steam.request'] = Rack::Request.new(env)
25
26
  env['steam.live_editing'] = live_editing
27
+ env['steam.request'] = Rack::Request.new(env)
26
28
  code, env = middleware.call(env)
27
- env['steam.page']
29
+ [env['steam.page'], env['steam.request'].params]
28
30
  end
29
31
 
30
32
  before do
@@ -32,28 +34,67 @@ describe Locomotive::Steam::Middlewares::Page do
32
34
  allow(service).to receive(:find).with('404').and_return(page_not_found)
33
35
  end
34
36
 
35
- it { is_expected.to eq page }
37
+ describe 'named route' do
38
+
39
+ let(:path) { 'posts/2018/09' }
40
+ let(:routes) { [{ 'route' => '/posts/:year', 'page_handle' => 'posts' }, { 'route' => '/posts/:year/:month', 'page_handle' => 'posts' }] }
41
+
42
+ it 'returns the page based on the handle returned by the routing hash' do
43
+ expect(service).to receive(:by_handle).with('posts', false).and_return(page)
44
+ is_expected.to eq([page, { 'year' => '2018', 'month' => '09' }])
45
+ end
46
+
47
+ describe 'wrong route syntax' do
36
48
 
37
- context "page doesn't exist" do
49
+ let(:routes) { [{ 'route' => '/posts/:_year/:month', 'page_handle' => 'posts' }] }
38
50
 
39
- let(:pages) { [] }
51
+ it "doesn't match the route" do
52
+ is_expected.to eq([page, {}])
53
+ end
54
+
55
+ end
40
56
 
41
- it { is_expected.to eq page_not_found }
57
+ describe 'path with a dash' do
58
+
59
+ let(:path) { 'resources/hello-world' }
60
+ let(:routes) { [{ 'route' => '/resources/:slug', 'page_handle' => 'resources' }] }
61
+
62
+ it 'returns the page based on the handle returned by the routing hash' do
63
+ expect(service).to receive(:by_handle).with('resources', false).and_return(page)
64
+ is_expected.to eq([page, { 'slug' => 'hello-world' }])
65
+ end
66
+
67
+
68
+ end
42
69
 
43
70
  end
44
71
 
45
- context 'page is unpublished' do
72
+ describe 'a page outside the site routes' do
73
+
74
+ it { is_expected.to eq([page, {}]) }
75
+
76
+ context "page doesn't exist" do
77
+
78
+ let(:pages) { [] }
79
+
80
+ it { is_expected.to eq([page_not_found, {}]) }
81
+
82
+ end
83
+
84
+ context 'page is unpublished' do
85
+
86
+ let(:published) { false }
46
87
 
47
- let(:published) { false }
88
+ it { is_expected.to eq([page_not_found, {}]) }
48
89
 
49
- it { is_expected.to eq page_not_found }
90
+ context 'the live editing mode is on' do
50
91
 
51
- context 'the live editing mode is on' do
92
+ let(:live_editing) { true }
52
93
 
53
- let(:live_editing) { true }
94
+ it 'has to display it' do
95
+ is_expected.to eq([page, {}])
96
+ end
54
97
 
55
- it 'has to display it' do
56
- is_expected.to eq page
57
98
  end
58
99
 
59
100
  end