write specs to submit a content entry (+ fix code)
did
committed Feb 17, 2015
commit c4ed2c4e8a5ff6c842aeaeda82ffa90da36ab096
Showing 11
changed files with
98 additions
and 81 deletions
locomotive/steam/middlewares/entry_submission.rb b/lib/locomotive/steam/middlewares/entry_submission.rb
+12
-12
| @@ | @@ -13,16 +13,16 @@ module Locomotive::Steam |
| SUBMITTED_PARAM = 'submitted_entry_slug' | |
| def _call | |
| - | if slug = get_content_type_slug |
| - | # we didn't go through the locale middleware yet, |
| - | # so set the locale manually. Needed to build a localized |
| - | # version of the entry + error messages (if present). |
| - | with_locale do |
| + | # we didn't go through the locale middleware yet, |
| + | # so set the locale manually. Needed to build a localized |
| + | # version of the entry + error messages (if present). |
| + | with_locale do |
| + | if slug = get_content_type_slug |
| entry = create_entry(slug) | |
| navigation_behavior(entry) | |
| + | else |
| + | fetch_entry |
| end | |
| - | else |
| - | fetch_entry |
| end | |
| end | |
| @@ | @@ -41,7 +41,7 @@ module Locomotive::Steam |
| def navigation_success(entry) | |
| if html? | |
| - | redirect_to success_location(entry_to_query_string(query)) |
| + | redirect_to success_location(entry_to_query_string(entry)) |
| elsif json? | |
| json_response(entry) | |
| end | |
| @@ | @@ -79,7 +79,7 @@ module Locomotive::Steam |
| def with_locale(&block) | |
| locale = default_locale || params[:locale] | |
| - | if path =~ /^\/(#{site.locales.join('|')})+(\/|$)/ |
| + | if request.path_info =~ /^\/(#{site.locales.join('|')})+(\/|$)/ |
| locale = $1 | |
| end | |
| @@ | @@ -120,7 +120,7 @@ module Locomotive::Steam |
| # | |
| # | |
| def create_entry(slug) | |
| - | attributes = self.params[:entry] || self.params[:content] || {} |
| + | attributes = HashConverter.to_sym(params[:entry] || params[:content] || {}) |
| if entry = services.entry_submission.submit(slug, attributes) | |
| entry | |
| @@ | @@ -132,7 +132,7 @@ module Locomotive::Steam |
| # Get the content entry from the params. | |
| # | |
| def fetch_entry | |
| - | if type_slug = params[SUBMITTED_TYPE_PARAM] && slug = params[SUBMITTED_PARAM] |
| + | if (type_slug = params[SUBMITTED_TYPE_PARAM]) && (slug = params[SUBMITTED_PARAM]) |
| if entry = services.entry_submission.find(type_slug, slug) | |
| store_in_liquid(entry) | |
| end | |
| @@ | @@ -147,7 +147,7 @@ module Locomotive::Steam |
| # | |
| def json_response(entry, status = 200) | |
| json = services.entry_submission.to_json(entry) | |
| - | [status, { 'Content-Type' => 'application/json' }, [json]] |
| + | render_response(json, status, 'application/json') |
| end | |
| end | |
locomotive/steam/middlewares/helpers.rb b/lib/locomotive/steam/middlewares/helpers.rb
+2
-2
| @@ | @@ -22,8 +22,8 @@ module Locomotive::Steam |
| @next_response = [type, { 'Content-Type' => 'text/html', 'Location' => location }, []] | |
| end | |
| - | def log(msg) |
| - | Locomotive::Common::Logger.info msg |
| + | def log(msg, offset = 2) |
| + | Locomotive::Common::Logger.info (' ' * offset) + msg |
| end | |
| end | |
locomotive/steam/middlewares/logging.rb b/lib/locomotive/steam/middlewares/logging.rb
+3
-1
| @@ | @@ -10,7 +10,8 @@ module Locomotive::Steam |
| def call(env) | |
| now = Time.now | |
| - | log "Started #{env['REQUEST_METHOD'].upcase} \"#{env['PATH_INFO']}\" at #{now}".light_white |
| + | log "Started #{env['REQUEST_METHOD'].upcase} \"#{env['PATH_INFO']}\" at #{now}".light_white, 0 |
| + | log "Params: #{env.fetch('steam.request').params.inspect}" |
| app.call(env).tap do |response| | |
| done_in_ms = ((Time.now - now) * 10000).truncate / 10.0 | |
| @@ | @@ -26,6 +27,7 @@ module Locomotive::Steam |
| when 301 then '301 Found' | |
| when 302 then '302 Found' | |
| when 404 then '404 Not Found' | |
| + | when 422 then '422 Unprocessable Entity' |
| end | |
| end | |
locomotive/steam/middlewares/threadsafe.rb b/lib/locomotive/steam/middlewares/threadsafe.rb
+1
-1
| @@ | @@ -57,7 +57,7 @@ module Locomotive::Steam::Middlewares |
| end | |
| def params | |
| - | @params ||= HashConverter.to_sym(self.request.params) |
| + | @params ||= self.request.params.with_indifferent_access |
| end | |
| end | |
locomotive/steam/repositories/filesystem/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/content_entry.rb
+4
-1
| @@ | @@ -40,7 +40,10 @@ module Locomotive |
| # slugify entry | |
| sanitizer.set_slug(entry, collection) | |
| - | collection << entry |
| + | collection << entry # immediate result |
| + | |
| + | # make sure we write it back to the data source |
| + | loader.write(entry.content_type, entry.attributes) |
| end | |
| # Engine: all(conditions).count > 0 | |
locomotive/steam/repositories/filesystem/memory_adapter/simple_cache_store.rb b/lib/locomotive/steam/repositories/filesystem/memory_adapter/simple_cache_store.rb
+5
-0
| @@ | @@ -28,6 +28,11 @@ module Locomotive |
| @@store.clear | |
| end | |
| + | #:nocov: |
| + | def _store |
| + | @@store |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/repositories/filesystem/sanitizers/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/content_entry.rb
+1
-1
| @@ | @@ -53,7 +53,7 @@ module Locomotive |
| if entry.attributes[name].is_a?(Hash) # localized? | |
| entry.attributes[name][default_locale] = value | |
| else | |
| - | entry.attributes[name] = value |
| + | entry.attributes[name] ||= value |
| end | |
| end | |
locomotive/steam/repositories/filesystem/yaml_loaders/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/content_entry.rb
+6
-0
| @@ | @@ -12,6 +12,12 @@ module Locomotive |
| cache.fetch("data/#{content_type.slug}") { load_list(content_type) } | |
| end | |
| + | def write(content_type, attributes) |
| + | list = cache.read("data/#{content_type.slug}") |
| + | |
| + | list << attributes.merge(content_type: content_type) |
| + | end |
| + | |
| private | |
| def load_list(content_type) | |
locomotive/steam/services/entry_submission.rb b/lib/locomotive/steam/services/entry_submission.rb
+1
-1
| @@ | @@ -19,7 +19,7 @@ module Locomotive |
| end | |
| def find(type_slug, slug) | |
| - | type = get_type(slug) |
| + | type = get_type(type_slug) |
| return nil if type.nil? | |
spec/integration/server/contact_form_spec.rb
+61
-61
| @@ | @@ -13,95 +13,95 @@ describe 'ContactForm' do |
| expect(last_response.body).to include '/entry_submissions/messages.json' | |
| end | |
| - | # describe '#submit' do |
| + | describe '#submit' do |
| - | # let(:params) { { |
| - | # 'entry' => { 'name' => 'John', 'email' => 'j@doe.net', 'message' => 'Bla bla' }, |
| - | # 'success_callback' => '/events', |
| - | # 'error_callback' => '/contact' } } |
| - | # let(:response) { post_contact_form(params, false) } |
| - | # let(:status) { response.status } |
| + | let(:params) { { |
| + | 'entry' => { 'name' => 'John', 'email' => 'j@doe.net', 'message' => 'Bla bla' }, |
| + | 'success_callback' => '/events', |
| + | 'error_callback' => '/contact' } } |
| + | let(:response) { post_contact_form(params, false) } |
| + | let(:status) { response.status } |
| - | # describe 'with json request' do |
| + | describe 'with json request' do |
| - | # let(:response) { post_contact_form(params, true) } |
| - | # let(:entry) { JSON.parse(response.body) } |
| + | let(:response) { post_contact_form(params, true) } |
| + | let(:entry) { JSON.parse(response.body) } |
| - | # context 'when not valid' do |
| + | context 'when not valid' do |
| - | # let(:params) { {} } |
| + | let(:params) { {} } |
| - | # it 'returns an error status' do |
| - | # expect(response.status).to eq 422 |
| - | # end |
| + | it 'returns an error status' do |
| + | expect(response.status).to eq 422 |
| + | end |
| - | # describe 'errors' do |
| + | describe 'errors' do |
| - | # subject { entry['errors'] } |
| + | subject { entry['errors'] } |
| - | # it { should have_key_with_value('name', "can't not be blank") } |
| + | it 'lists all the errors' do |
| + | expect(subject['name']).to eq ["can't not be blank"] |
| + | expect(subject['email']).to eq ["can't not be blank"] |
| + | expect(subject['email']).to eq ["can't not be blank"] |
| + | end |
| - | # it { should have_key_with_value('email', "can't not be blank") } |
| + | end |
| - | # it { should have_key_with_value('message', "can't not be blank") } |
| + | end |
| - | # end |
| + | context 'when valid' do |
| - | # end |
| + | it 'returns a success status' do |
| + | expect(response.status).to eq 200 |
| + | end |
| - | # context 'when valid' do |
| + | end |
| - | # it 'returns a success status' do |
| - | # response.status.should == 200 |
| - | # end |
| + | end |
| - | # end |
| + | describe 'with html request' do |
| - | # end |
| + | context 'when not valid' do |
| - | # describe 'with html request' do |
| + | let(:params) { { 'error_callback' => '/contact' } } |
| - | # context 'when not valid' do |
| + | it 'returns a success status' do |
| + | expect(response.status).to eq 200 |
| + | end |
| - | # let(:params) { { 'error_callback' => '/contact' } } |
| + | it 'displays errors' do |
| + | expect(response.body.to_s).to include "can't not be blank" |
| + | end |
| - | # it 'returns a success status' do |
| - | # response.status.should == 200 |
| - | # end |
| + | end |
| - | # it 'displays errors' do |
| - | # response.body.to_s.should =~ /can't not be blank/ |
| - | # end |
| + | context 'when valid' do |
| - | # end |
| + | let(:response) { post_contact_form(params, false, true) } |
| - | # context 'when valid' do |
| + | it 'returns a success status' do |
| + | expect(response.status).to eq 200 |
| + | end |
| - | # let(:response) { post_contact_form(params, false, true) } |
| + | it 'displays a success message' do |
| + | expect(response.body.to_s).to include 'Thank you John' |
| + | end |
| - | # it 'returns a success status' do |
| - | # response.status.should == 200 |
| - | # end |
| + | end |
| - | # it 'displays a success message' do |
| - | # response.body.should =~ /Thank you John/ |
| - | # end |
| + | end |
| - | # end |
| - | |
| - | # end |
| - | |
| - | # end |
| + | end |
| - | # def post_contact_form(params, json = false, follow_redirect = false) |
| - | # url = '/entry_submissions/messages' |
| - | # url += '.json' if json |
| - | # params = params.symbolize_keys if json |
| - | # post url, params |
| - | # if follow_redirect |
| - | # follow_redirect! |
| - | # end |
| - | # last_response |
| - | # end |
| + | def post_contact_form(params, json = false, follow_redirect = false) |
| + | url = '/entry_submissions/messages' |
| + | url += '.json' if json |
| + | params = params.symbolize_keys if json |
| + | post url, params |
| + | if follow_redirect |
| + | follow_redirect! |
| + | end |
| + | last_response |
| + | end |
| end | |
spec/unit/repositories/filesystem/content_entry_spec.rb
+2
-1
| @@ | @@ -50,12 +50,13 @@ describe Locomotive::Steam::Repositories::Filesystem::ContentEntry do |
| describe '#persist' do | |
| - | let(:entry) { instance_double('NewEntry', _visible: true, content_type: type, _label: 'Hello world') } |
| + | let(:entry) { instance_double('NewEntry', _visible: true, content_type: type, _label: 'Hello world', attributes: { title: 'Hello world' }) } |
| subject { repository.persist(entry) } | |
| before do | |
| expect(entry).to receive(:[]).with(:_slug).and_return(nil) | |
| expect(entry).to receive(:[]=).with(:_slug, 'hello-world') | |
| + | expect(loader).to receive(:write).with(type, { title: 'Hello world' }) |
| end | |
| it { expect { subject }.to change { repository.all(type).size }.by(1) } | |