Skip to content

Commit

Permalink
write cucumber tests to illustrate how to deal with a contact form + …
Browse files Browse the repository at this point in the history
…fix a tiny bug about it
  • Loading branch information
Didier Lafforgue committed Apr 17, 2012
1 parent 1d19d60 commit 47015d3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ PATH
dragonfly (~> 0.9.8)
flash_cookie_session (~> 1.1.1)
fog (~> 1.3.1)
formtastic (~> 2.2.0)
formtastic (~> 2.0.2)
haml (~> 3.1.4)
highline (~> 1.6.2)
httparty (~> 0.8.1)
Expand Down Expand Up @@ -176,8 +176,8 @@ GEM
nokogiri (~> 1.5.0)
ruby-hmac
formatador (0.2.1)
formtastic (2.2.0)
actionpack (>= 3.0)
formtastic (2.0.2)
rails (~> 3.0)
fssm (0.2.9)
gherkin (2.9.3)
json (>= 1.4.6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ class ContentEntriesController < BaseController

skip_before_filter :verify_authenticity_token

skip_load_and_authorize_resource

self.responder = Locomotive::ActionController::PublicResponder # custom responder

respond_to :html, :json

def create
@entry = @content_type.entries.create(params[:entry] || params[:content])
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
end

Expand Down
62 changes: 62 additions & 0 deletions features/public/contact_form.feature
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions features/step_definitions/content_types_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
content_type.save.should be_true
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|
content_type = Locomotive::ContentType.where(:name => name).first
field = content_type.entries_custom_fields.detect { |f| f.label == field }
Expand Down
3 changes: 2 additions & 1 deletion features/step_definitions/page_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def create_content_page(page_slug, page_contents, template = nil)

# checks if the rendered body matches a string
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

Then /^I should see delete page buttons$/ do
Expand Down
2 changes: 1 addition & 1 deletion locomotive_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Gem::Specification.new do |s|
s.add_dependency 'flash_cookie_session', '~> 1.1.1'

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 'cells', '~> 3.8.0'
s.add_dependency 'RedCloth', '~> 4.2.8'
Expand Down

0 comments on commit 47015d3

Please sign in to comment.