Skip to content

Instantly share code, notes, and snippets.

@JeanMertz
Created August 31, 2011 07:26
Show Gist options
  • Save JeanMertz/1182996 to your computer and use it in GitHub Desktop.
Save JeanMertz/1182996 to your computer and use it in GitHub Desktop.
Feature: Authentication
In order to be able to manage my website
As a client of a designer using the service
I want to be able to log in and log out of my website backend
Background: Website exists with a user and I am on the login page
Given a "website" exists with name: "twitter"
And for that website, a "user" exists with email: "joe@twitter.dev" and password: "password123"
And for that website, a "domain" exists with name: "twitter-dev.manager.dev"
And for that website, a "domain" exists with name: "twitter.dev"
Scenario: Log in from my website domain (manager.dev)
Given I am on the login page for my website with domain: "twitter.dev"
When I submit the login form with my user credentials
Then I should be logged in
Given /^I am on the login page for my website(?: with (sub)?domain: "(\S+)")?$/ do |subdomain, domain|
domain = "#{domain}.#{ENV["APP_HOST"]}" if subdomain
domain ? host = @website.domains.find_by_name(domain).name : host = @website.domains.first.name
visit login_url(host: host)
current_path.should eq(login_path)
end
When /^I submit the login form with my user credentials$/ do
When %{I fill in "#{@user.email}" for "#{I18n.t("sessions.new.email")}"}
And %{I fill in "#{@user.password}" for "#{I18n.t("sessions.new.password")}"}
click_button "sessions_submit"
end
When /^I submit the login form with false user credentials$/ do
When %{I fill in "mail@invalid_domain.dev" for "#{I18n.t("sessions.new.email")}"}
And %{I fill in "#{@user.password}" for "#{I18n.t("sessions.new.password")}"}
click_button "sessions_submit"
end
When /^I visit the log out link on my website$/ do
visit logout_url(host: @website.domains.first.name)
end
Then /^I should be logged in$/ do
cookie_jar = Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar
cookie_jar.instance_variable_get(:@cookies).each do |cookie|
@auth_cookie = cookie if cookie.name == "website_auth_token"
end
@auth_cookie.value.should be_present
end
When /^I fill in "(.+)" for "(.+)"$/ do |value, field|
fill_in field, :with => value
end
When /^I submit the "(\S+)" form$/ do |model|
click_button I18n.t("helpers.submit.create", model: model)
end
# a "WEBSITE" exists with NAME: "MYDOMAIN"
# a "USER" exists with EMAIL: "USER@MYDOMAIN" and PASSWORD: "PASSWORD123"
Given /^an? "(.+)" exists with(?: the)? (.+?): "(.+?)"(?:(?:,| and|, and)+(?: the)? (.+?): "(.+?)")?(?:(?:,| and|, and)+(?: the)? (.+?): "(.+?)")?$/ do |model, attr1, value1, attr2, value2, attr3, value3|
model.gsub!(" ", "_")
const_model = model.camelize.constantize
attributes = "#{attr1}: \"#{value1}\""
attributes << ", #{attr2}: \"#{value2}\"" if attr2
attributes << ", #{attr3}: \"#{value3}\"" if attr3
eval("@#{model} = Factory(:#{model}, #{attributes})")
eval("#{const_model}.find_by_#{attr1}(\"#{value1}\").should be_present")
end
# Given for my WEBSITE, a "DOMAIN" exists with NAME: "MYDOMAIN.DEV" and LOCALE: "EN"
Given /^for (?:that|this|my|the) (.+?), an? "(.+?)" exists with(?: the)? (.+?): "(.+?)"(?:(?:,| and|, and)+(?: the)? (.+?): "(.+?)")?(?:(?:,| and|, and)+(?: the)? (.+?): "(.+?)")?$/ do |parent, model, attr1, value1, attr2, value2, attr3, value3|
parent.gsub!(" ", "_")
parent_name = eval("@#{parent}.class.to_s.underscore")
model.gsub!(" ", "_")
model_name = eval("@#{parent}.#{model.pluralize}.new.class.to_s.underscore")
attributes = "#{attr1}: \"#{value1}\""
attributes << ", #{attr2}: \"#{value2}\"" if attr2
attributes << ", #{attr3}: \"#{value3}\"" if attr3
eval("@#{model} = Factory(:#{model_name}, #{attributes}, #{parent_name}_id: @#{parent}.id)")
eval("@#{parent}.#{model.pluralize}.find_by_#{attr1}(\"#{value1}\").should be_present")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment