Skip to content

Commit

Permalink
stories work for all three install modes:
Browse files Browse the repository at this point in the history
* stories helper now takes #{} in attributes munger
* stories now work with all 3 flavors of flags (activation, stateful, none)
  • Loading branch information
Philip (flip) Kromer committed May 20, 2008
1 parent 4588bae commit 40c268a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def ToFooFromStory.fix_value value
when value =~ /^"(.*)"$/ then value = $1
when value == 'nil!' then value = nil
when value == 'non-nil!' then value = be_nil
when value =~ /^#\{(.*)\}$/ then value = eval($1)
end
value
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# produces a <%= class_name %> instance stored in @<%= file_name %> with 'mojojojo' as its login
# attribute.
#
Given "a $resource with $attributes" do |resource, attributes|
Given "a $resource instance with $attributes" do |resource, attributes|
klass, instance, attributes = parse_resource_args resource, attributes
instance = klass.new(attributes)
instance.save!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
cookies[token].include?(val).should be_true
end
end

Then "$actor $token cookie should exist but not include $attrlist" do |_, token, attrlist|
attrlist = attrlist.to_array_from_story
cookies.include?(token).should be_true
puts [cookies, attrlist, token].to_yaml
attrlist.each do |val|
cookies[token].include?(val).should_not be_true
end
end

Then "$actor should have $an $token cookie" do |_, _, token|
cookies[token].should_not be_blank
Expand All @@ -68,6 +77,20 @@
cookies[token].should be_blank
end

Given "$actor has $an cookie jar with $attributes" do |_, _, attributes|
attributes = attributes.to_hash_from_story
attributes.each do |attr, val|
cookies[attr] = val
end
end
Given "$actor session store has no $attrlist" do |_, attrlist|
attrlist = attrlist.to_array_from_story
attrlist.each do |attr|
# Note that the comparison passes through 'to_s'
session[attr.to_sym] = nil
end
end

Then "$actor session store should have $attributes" do |_, attributes|
attributes = attributes.to_hash_from_story
attributes.each do |attr, val|
Expand Down
34 changes: 18 additions & 16 deletions generators/authenticated/templates/stories/steps/user_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@
end
When "$actor registers an account as the preloaded '$login'" do |_, login|
create_<%= file_name %> named_<%= file_name %>(login)
<%= file_name %> = named_<%= file_name %>(login)
<%= file_name %>['password_confirmation'] = <%= file_name %>['password']
create_<%= file_name %> <%= file_name %>
end

When "$actor registers an account with $attributes" do |_, attributes|
create_<%= file_name %> attributes.to_hash_from_story
end

<% if options[:include_activation] %>
When "$actor activates with activation code $attributes" do |_, activation_code|
activation_code = '' if activation_code == 'that is blank'
activate
end
end<% end %>
When "$actor logs in with $attributes" do |_, attributes|
log_in_<%= file_name %> attributes.to_hash_from_story
end
Expand All @@ -80,11 +82,9 @@

def named_<%= file_name %> login
<%= file_name %>_params = {
'admin' => {:id => 1, :login => 'addie', :password => 'addie', :email => 'admin@example.com', },
'oona' => { :login => 'oona', :password => 'oona', :email => 'unactivated@example.com'},
'reggie' => { :login => 'reggie', :password => 'reggie', :email => 'registered@example.com' },
'quentin' => {:id => 1, :login => 'quentin',:password => 'test', :email => 'quentin@example.com', :salt => 'c01a01185db60cee742e65fcf9b7e5c3d1082280', :crypted_password => 'bb8b0a46b9e2dd548fe6aa9fc5eecbecfcb479aa', :created_at => 5.days.ago.to_s, :activation_code => '20ad8a28ca9ab987929ef67b1b202767b22da395', :activated_at => 5.days.ago.to_s, },
'aaron' => {:id => 2, :login => 'aaron', :password => 'test', :email => 'aaron@example.com', :salt => 'c0e666527f8ccd6a419b536269be5b3de22ab7f8', :crypted_password => 'dc8a020aa058d880bffd74d8468b8307f79806d6', :created_at => 1.days.ago.to_s, :activation_code => 'c25c6846ac4233fafece3ae9746c900c88e48d2b', },
'admin' => {'id' => 1, 'login' => 'addie', 'password' => '1234addie', 'email' => 'admin@example.com', },
'oona' => { 'login' => 'oona', 'password' => '1234oona', 'email' => 'unactivated@example.com'},
'reggie' => { 'login' => 'reggie', 'password' => 'monkey', 'email' => 'registered@example.com' },
}
<%= file_name %>_params[login.downcase]
end
Expand All @@ -109,20 +109,22 @@ def log_out!
end

def create_<%= file_name %>(<%= file_name %>_params={})
<%= file_name %>_params[:password_confirmation] ||= <%= file_name %>_params[:password] ||= <%= file_name %>_params['password']
@<%= file_name %>_params ||= <%= file_name %>_params
post "/<%= model_controller_file_path %>", :<%= file_name %> => <%= file_name %>_params
@<%= file_name %> = <%= class_name %>.find_by_login(<%= file_name %>_params[:login])
@<%= file_name %> = <%= class_name %>.find_by_login(<%= file_name %>_params['login'])
end
def create_<%= file_name %>!(<%= file_name %>_type, *args)
create_<%= file_name %> *args
def create_<%= file_name %>!(<%= file_name %>_type, <%= file_name %>_params)
<%= file_name %>_params['password_confirmation'] ||= <%= file_name %>_params['password'] ||= <%= file_name %>_params['password']
create_<%= file_name %> <%= file_name %>_params
response.should redirect_to('/')
follow_redirect!
<% if options[:include_activation] %>
# fix the <%= file_name %>'s activation status
activate_<%= file_name %>! if <%= file_name %>_type == 'activated'
activate_<%= file_name %>! if <%= file_name %>_type == 'activated'<% end %>
end

<% if options[:include_activation] %>
def activate_<%= file_name %> activation_code=nil
activation_code = @<%= file_name %>.activation_code if activation_code.nil?
get "/activate/#{activation_code}"
Expand All @@ -133,13 +135,13 @@ def activate_<%= file_name %>! *args
response.should redirect_to('/<%= controller_file_path %>/new')
follow_redirect!
response.should have_flash("notice", /Signup complete!/)
end
end<% end %>
def log_in_<%= file_name %> <%= file_name %>_params=nil
@<%= file_name %>_params ||= <%= file_name %>_params
<%= file_name %>_params ||= @<%= file_name %>_params
post "/<%= controller_file_path %>", <%= file_name %>_params
@<%= file_name %> = <%= class_name %>.find_by_login(<%= file_name %>_params[:login])
@<%= file_name %> = <%= class_name %>.find_by_login(<%= file_name %>_params['login'])
controller.current_<%= file_name %>
end
Expand Down
34 changes: 30 additions & 4 deletions generators/authenticated/templates/stories/users/accounts.story
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ Story: Creating an account
Then she should see a notice message 'Thanks for signing up!'
And a <%= file_name %> with login: 'oona' should exist
And the <%= file_name %> should have login: 'oona', and email: 'unactivated@example.com'
<% if options[:include_activation] %>
And the <%= file_name %>'s activation_code should not be nil
And the <%= file_name %>'s activated_at should be nil
And she should not be logged in
<% else %>
And oona should be logged in
<% end %>
#
# Account Creation Failure: Account exists
#
<% if options[:include_activation] %>
Scenario: Anonymous <%= file_name %> can not create an account replacing a non-activated account
Given an anonymous <%= file_name %>
And a registered <%= file_name %> named 'Reggie'
Expand All @@ -52,7 +57,7 @@ Story: Creating an account
And the <%= file_name %>'s activated_at should be nil
And the <%= file_name %>'s created_at should stay the same under to_s
And the <%= file_name %>'s updated_at should stay the same under to_s
And she should not be logged in
And she should not be logged in<% end %>
Scenario: Anonymous <%= file_name %> can not create an account replacing an activated account
Given an anonymous <%= file_name %>
Expand All @@ -64,8 +69,9 @@ Story: Creating an account
And she should not see an errorExplanation message 'Email has already been taken'
And a <%= file_name %> with login: 'reggie' should exist
And the <%= file_name %> should have email: 'registered@example.com'
<% if options[:include_activation] %>
And the <%= file_name %>'s activation_code should be nil
And the <%= file_name %>'s activated_at should not be nil
And the <%= file_name %>'s activated_at should not be nil<% end %>
And the <%= file_name %>'s created_at should stay the same under to_s
And the <%= file_name %>'s updated_at should stay the same under to_s
And she should not be logged in
Expand All @@ -80,18 +86,34 @@ Story: Creating an account
Then she should be at the new <%= model_controller_file_name %> page
And she should see an errorExplanation message 'Login can't be blank'
And no <%= file_name %> with login: 'oona' should exist

Scenario: Anonymous <%= file_name %> can not create an account with no password
Given an anonymous <%= file_name %>
And no <%= file_name %> with login: 'Oona' exists
When she registers an account with login: 'oona', password: '', password_confirmation: 'monkey' and email: 'unactivated@example.com'
Then she should be at the new <%= model_controller_file_name %> page
And she should see an errorExplanation message 'Password can't be blank'
And no <%= file_name %> with login: 'oona' should exist

Scenario: Anonymous <%= file_name %> can not create an account with no password_confirmation
Given an anonymous <%= file_name %>
And no <%= file_name %> with login: 'Oona' exists
When she registers an account with login: 'oona', password: 'monkey', password_confirmation: '' and email: 'unactivated@example.com'
Then she should be at the new <%= model_controller_file_name %> page
And she should see an errorExplanation message 'Password confirmation can't be blank'
And no <%= file_name %> with login: 'oona' should exist

Scenario: Anonymous <%= file_name %> can not create an account with mismatched password & password_confirmation
Given an anonymous <%= file_name %>
And no <%= file_name %> with login: 'Oona' exists
When she registers an account with login: 'oona', password: 'monkey', password_confirmation: 'monkeY' and email: 'unactivated@example.com'
Then she should be at the new <%= model_controller_file_name %> page
And she should see an errorExplanation message 'Password doesn't match confirmation'
And no <%= file_name %> with login: 'oona' should exist

Scenario: Anonymous <%= file_name %> can not create an account with bad email
Given an anonymous <%= file_name %>
And no <%= file_name %> with login: 'Oona' exists
When she registers an account with login: 'oona', password: 'monkey', password_confirmation: 'monkey' and email: ''
Then she should be at the new <%= model_controller_file_name %> page
And she should see an errorExplanation message 'Email can't be blank'
Expand All @@ -102,11 +124,15 @@ Story: Creating an account
Then she should see a notice message 'Thanks for signing up!'
And a <%= file_name %> with login: 'oona' should exist
And the <%= file_name %> should have login: 'oona', and email: 'unactivated@example.com'
<% if options[:include_activation] %>
And the <%= file_name %>'s activation_code should not be nil
And the <%= file_name %>'s activated_at should be nil
And she should not be logged in
<% else %>
And oona should be logged in
<% end %>
<% if options[:include_activation] %>
Story: Activating an account
As a registered, but not yet activated, <%= file_name %>
I want to be able to activate my account
Expand Down Expand Up @@ -157,4 +183,4 @@ Story: Activating an account
And the <%= file_name %> should have login: 'reggie', activation_code: 'activate_me', and activated_at: nil!
And the <%= file_name %>'s updated_at should stay the same under to_s
And she should not be logged in
<% end %>
20 changes: 11 additions & 9 deletions generators/authenticated/templates/stories/users/sessions.story
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Story: Logging in
Scenario: Anonymous <%= file_name %> can log in
Given an anonymous <%= file_name %>
And an activated <%= file_name %> named 'reggie'
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'reggie', remember me: ''
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'monkey', remember me: ''
Then she should be redirected to the home page
When she follows that redirect!
Then she should see a notice message 'Logged in successfully'
Expand All @@ -33,7 +33,7 @@ Story: Logging in
Scenario: Logged-in <%= file_name %> who logs in should be the new one
Given an activated <%= file_name %> named 'reggie'
And an activated <%= file_name %> logged in as 'oona'
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'reggie', remember me: ''
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'monkey', remember me: ''
Then she should be redirected to the home page
When she follows that redirect!
Then she should see a notice message 'Logged in successfully'
Expand All @@ -46,21 +46,22 @@ Story: Logging in
Scenario: Anonymous <%= file_name %> can log in and be remembered
Given an anonymous <%= file_name %>
And an activated <%= file_name %> named 'reggie'
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'reggie', remember me: '1'
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'monkey', remember me: '1'
Then she should be redirected to the home page
When she follows that redirect!
Then she should see a notice message 'Logged in successfully'
And reggie should be logged in
And she should have an auth_token cookie
And her session store should have <%= file_name %>_id: 3
# assumes fixtures were run sometime
And her session store should have <%= file_name %>_id: 4

#
# Log in unsuccessfully
#

Scenario: Logged-in <%= file_name %> who fails logs in should be logged out
Given an activated <%= file_name %> named 'oona'
When she creates a singular <%= controller_file_name %> with login: 'oona', password: 'oona', remember me: '1'
When she creates a singular <%= controller_file_name %> with login: 'oona', password: '1234oona', remember me: '1'
Then she should be redirected to the home page
When she follows that redirect!
Then she should see a notice message 'Logged in successfully'
Expand All @@ -87,25 +88,26 @@ Story: Logging in
And she should not be logged in
And she should not have an auth_token cookie
And her session store should not have <%= file_name %>_id
When she creates a singular <%= controller_file_name %> with login: '', password: 'reggie'
When she creates a singular <%= controller_file_name %> with login: '', password: 'monkey'
Then she should be at the new <%= controller_file_name %> page
Then she should see an error message 'Couldn't log you in as '''
And she should not be logged in
And she should not have an auth_token cookie
And her session store should not have <%= file_name %>_id
When she creates a singular <%= controller_file_name %> with login: 'leonard_shelby', password: 'reggie'
When she creates a singular <%= controller_file_name %> with login: 'leonard_shelby', password: 'monkey'
Then she should be at the new <%= controller_file_name %> page
Then she should see an error message 'Couldn't log you in as 'leonard_shelby''
And she should not be logged in
And she should not have an auth_token cookie
And her session store should not have <%= file_name %>_id
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'reggie', remember me: '1'
When she creates a singular <%= controller_file_name %> with login: 'reggie', password: 'monkey', remember me: '1'
Then she should be redirected to the home page
When she follows that redirect!
Then she should see a notice message 'Logged in successfully'
And reggie should be logged in
And she should have an auth_token cookie
And her session store should have <%= file_name %>_id: 3
# assumes fixtures were run sometime
And her session store should have <%= file_name %>_id: 4


#
Expand Down

0 comments on commit 40c268a

Please sign in to comment.