write cucumber tests to illustrate how to deal with a contact form + fix a tiny bug about it

This commit is contained in:
Didier Lafforgue 2012-04-17 17:48:42 +02:00
parent 1d19d606b8
commit 47015d3473
6 changed files with 79 additions and 5 deletions

View File

@ -42,7 +42,7 @@ PATH
dragonfly (~> 0.9.8) dragonfly (~> 0.9.8)
flash_cookie_session (~> 1.1.1) flash_cookie_session (~> 1.1.1)
fog (~> 1.3.1) fog (~> 1.3.1)
formtastic (~> 2.2.0) formtastic (~> 2.0.2)
haml (~> 3.1.4) haml (~> 3.1.4)
highline (~> 1.6.2) highline (~> 1.6.2)
httparty (~> 0.8.1) httparty (~> 0.8.1)
@ -176,8 +176,8 @@ GEM
nokogiri (~> 1.5.0) nokogiri (~> 1.5.0)
ruby-hmac ruby-hmac
formatador (0.2.1) formatador (0.2.1)
formtastic (2.2.0) formtastic (2.0.2)
actionpack (>= 3.0) rails (~> 3.0)
fssm (0.2.9) fssm (0.2.9)
gherkin (2.9.3) gherkin (2.9.3)
json (>= 1.4.6) json (>= 1.4.6)

View File

@ -8,6 +8,8 @@ module Locomotive
skip_before_filter :verify_authenticity_token skip_before_filter :verify_authenticity_token
skip_load_and_authorize_resource
self.responder = Locomotive::ActionController::PublicResponder # custom responder self.responder = Locomotive::ActionController::PublicResponder # custom responder
respond_to :html, :json respond_to :html, :json
@ -15,6 +17,7 @@ module Locomotive
def create def create
@entry = @content_type.entries.create(params[:entry] || params[:content]) @entry = @content_type.entries.create(params[:entry] || params[:content])
flash[@content_type.slug.singularize] = @entry.to_presenter(:include_errors => true).as_json flash[@content_type.slug.singularize] = @entry.to_presenter(:include_errors => true).as_json
Rails.logger.debug @entry.to_presenter(:include_errors => true).as_json
respond_with @entry, :location => self.callback_url respond_with @entry, :location => self.callback_url
end end

View File

@ -0,0 +1,62 @@
Feature: Contact form
As a visitor
In order to keep in touch with the site
I want to be able to send them a message
Background:
Given I have the site: "test site" set up
And I have a custom model named "Messages" with
| label | type | required |
| Email | string | true |
| Message | text | true |
And I enable the public submission of the "Messages" model
And a page named "contact" with the template:
"""
<html>
<head></head>
<body>
<form action="{{ contents.messages.public_submission_url }}" method="post">
<input type="hidden" value="/success" name="success_callback" />
<input type="hidden" value="/contact" name="error_callback" />
<label for="email">E-Mail Address</label>
<input type="text" id="email" name="content[email]" />
{% if message.errors.email %}Email is required{% endif %}
<label for="message">Message</label>
<textarea name="content[message]" id="message"></textarea>
<input name="submit" type="submit" id="submit" value="Submit" />
</form>
</body>
</html>
"""
And a page named "success" with the template:
"""
Thanks {{ message.email }}
"""
Scenario: Setting the right url for the contact form
When I view the rendered page at "/contact"
Then the rendered output should look like:
"""
<form action="http://test.example.com/entry_submissions/messages" method="post">
"""
Scenario: Prevents users to post messages if the public submission option is disabled
Given I disable the public submission of the "Messages" model
When I view the rendered page at "/contact"
And I fill in "E-Mail Address" with "did@locomotivecms.com"
And I fill in "Message" with "LocomotiveCMS rocks"
And I press "Submit"
Then I should not see "Thanks did@locomotivecms.com"
Scenario: Sending a message with success
When I view the rendered page at "/contact"
And I fill in "E-Mail Address" with "did@locomotivecms.com"
And I fill in "Message" with "LocomotiveCMS rocks"
And I press "Submit"
Then I should see "Thanks did@locomotivecms.com"
Scenario: Display errors
When I view the rendered page at "/contact"
And I fill in "Message" with "LocomotiveCMS rocks"
And I press "Submit"
Then I should see "Email is required"

View File

@ -39,6 +39,14 @@ Given %r{^I have entries for "([^"]*)" with$} do |name, entries|
content_type.save.should be_true content_type.save.should be_true
end end
Given %r{^I (enable|disable) the public submission of the "([^"]*)" model$} do |action, name|
enabled = action == 'enable'
content_type = Locomotive::ContentType.where(:name => name).first
content_type.public_submission_enabled = enabled
content_type.public_submission_accounts = enabled ? [Locomotive::Account.first._id] : []
content_type.save.should be_true
end
When %r{^I change the presentation of the "([^"]*)" model by grouping items by "([^"]*)"$} do |name, field| When %r{^I change the presentation of the "([^"]*)" model by grouping items by "([^"]*)"$} do |name, field|
content_type = Locomotive::ContentType.where(:name => name).first content_type = Locomotive::ContentType.where(:name => name).first
field = content_type.entries_custom_fields.detect { |f| f.label == field } field = content_type.entries_custom_fields.detect { |f| f.label == field }

View File

@ -54,7 +54,8 @@ end
# checks if the rendered body matches a string # checks if the rendered body matches a string
Then /^the rendered output should look like:$/ do |body_contents| Then /^the rendered output should look like:$/ do |body_contents|
page.source.should == body_contents # page.source.should == body_contents
page.source.index(body_contents).should_not be_nil
end end
Then /^I should see delete page buttons$/ do Then /^I should see delete page buttons$/ do

View File

@ -43,7 +43,7 @@ Gem::Specification.new do |s|
s.add_dependency 'flash_cookie_session', '~> 1.1.1' s.add_dependency 'flash_cookie_session', '~> 1.1.1'
s.add_dependency 'locomotive_liquid', '2.2.2' s.add_dependency 'locomotive_liquid', '2.2.2'
s.add_dependency 'formtastic', '~> 2.2.0' s.add_dependency 'formtastic', '~> 2.0.2'
s.add_dependency 'responders', '~> 0.6.4' s.add_dependency 'responders', '~> 0.6.4'
s.add_dependency 'cells', '~> 3.8.0' s.add_dependency 'cells', '~> 3.8.0'
s.add_dependency 'RedCloth', '~> 4.2.8' s.add_dependency 'RedCloth', '~> 4.2.8'