Skip to content

Commit

Permalink
fix the erroring password change. move admin user to seed-fu
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderpantsGnome committed Aug 19, 2009
1 parent 07984a1 commit b2fc03a
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 25 deletions.
1 change: 0 additions & 1 deletion app/controllers/password_reset_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class PasswordResetController < ApplicationController

def update
@user.password = params[:user][:password]
@user.password_confirmation = params[:user][:password_confirmation]
if @user.save
flash[:notice] = "Password successfully updated"
redirect_to account_url
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def edit
end

def create
params[:user][:password_confirmation] = params[:user][:password] if params[:user]
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account registered!"
Expand All @@ -26,7 +25,6 @@ def create
end

def update
params[:user][:password_confirmation] = params[:user][:password] if params[:user]
@user = @current_user # makes our views "cleaner" and more consistent
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
Expand Down
4 changes: 3 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class User < ActiveRecord::Base
acts_as_authentic
acts_as_authentic do |c|
self.validate_password_field(false)
end

def deliver_password_reset_instructions!
reset_perishable_token!
Expand Down
9 changes: 9 additions & 0 deletions base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def file_inject(file_name, sentinel, string, before_after=:after)
# install haml rake tasks
rakefile 'haml.rake', open("#{SOURCE}/lib/tasks/haml.rake").read

# install seed_fu rake tasks
rakefile 'seed_fu.rake', open("#{SOURCE}/lib/tasks/seed_fu.rake").read

# RSpec
generate 'rspec'
file 'spec/rcov.opts', open("#{SOURCE}/spec/rcov.opts").read
Expand Down Expand Up @@ -238,6 +241,9 @@ def file_inject(file_name, sentinel, string, before_after=:after)
file "app/views/#{name}", open("#{SOURCE}/app/views/#{name}").read
end

# fixtures
file "db/fixtures/users.rb", open("#{SOURCE}/db/fixtures/users.rb").read

# testing goodies
file_inject('/spec/spec_helper.rb',
"require 'spec/rails'",
Expand All @@ -254,6 +260,7 @@ def file_inject(file_name, sentinel, string, before_after=:after)
controllers/password_reset_controller_spec.rb
controllers/user_sessions_controller_spec.rb
controllers/users_controller_spec.rb
models/user_spec.rb
views/home/index.html.haml_spec.rb
).each do |name|
file "spec/#{name}", open("#{SOURCE}/spec/#{name}").read
Expand Down Expand Up @@ -384,6 +391,8 @@ def file_inject(file_name, sentinel, string, before_after=:after)
git :add => "."
git :commit => "-a -m 'Added Action images'"

rake('db:seed')

puts "\n#{'*' * 80}\n\n"
puts "All done. Enjoy."
puts "\n#{'*' * 80}\n\n"
6 changes: 6 additions & 0 deletions db/fixtures/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
User.seed(:login) do |s|
s.login = 'admin'
s.email = '[email protected]'
s.password = 'changeme'
s.admin = true
end
9 changes: 0 additions & 9 deletions db/migrate/create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ def self.up
add_index :users, :last_request_at
add_index :users, :perishable_token
add_index :users, :email

say "adding defalt admin user"
user = User.new(:login => 'admin',
:email => '[email protected]',
:password => 'changeme',
:password_confirmation => 'changeme'
)
user.admin = true
user.save!
end

def self.down
Expand Down
44 changes: 44 additions & 0 deletions lib/tasks/seed_fu.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace :db do
desc <<-EOS
Loads seed data for the current environment. It will look for
ruby seed files in <RAILS_ROOT>/db/fixtures/ and
<RAILS_ROOT>/db/fixtures/<RAILS_ENV>/.
By default it will load any ruby files found. You can filter the files
loaded by passing in the SEED environment variable with a comma-delimited
list of patterns to include. Any files not matching the pattern will
not be loaded.
You can also change the directory where seed files are looked for
with the FIXTURE_PATH environment variable.
Examples:
# default, to load all seed files for the current environment
rake db:seed
# to load seed files matching orders or customers
rake db:seed SEED=orders,customers
# to load files from RAILS_ROOT/features/fixtures
rake db:seed FIXTURE_PATH=features/fixtures
EOS
task :seed => :environment do
fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures"

global_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort
env_specific_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')]
potential_seed_files = (global_seed_files + env_specific_seed_files).uniq

if ENV["SEED"]
filter = ENV["SEED"].gsub(/,/, "|")
potential_seed_files.reject!{ |file| true unless file =~ /#{filter}/ }
puts "\n == Filtering seed files against regexp: #{filter}"
end

potential_seed_files.each do |file|
pretty_name = file.sub("#{RAILS_ROOT}/", "")
puts "\n== Seed from #{pretty_name} " + ("=" * (60 - (17 + File.split(file).last.length)))
load file
end
end
end
13 changes: 3 additions & 10 deletions spec/controllers/password_reset_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,16 @@
response.should_not be_redirect
end

it "should change password with a valid token and matching passwords" do
it "should change password with a valid token" do
users(:mmoen).reset_perishable_token!
post :update, :id => users(:mmoen).perishable_token, :user => {
:password => 'new_pass', :password_confirmation => 'new_pass' }
:password => 'new_pass' }
response.should redirect_to(account_url)
end

it "should not change password with a valid token and non-matching passwords" do
users(:mmoen).reset_perishable_token!
post :update, :id => users(:mmoen).perishable_token, :user => {
:password => 'new_pass', :password_confirmation => 'new' }
response.should_not be_redirect
end

it "should not change password with an invalid token" do
post :update, :id => 'not_a_valid_token', :user => {
:password => 'new_pass', :password_confirmation => 'new' }
:password => 'new_pass' }
response.should redirect_to(root_url)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/user_sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
describe "session management" do
it "should redirect to the account page on successful login" do
post :create, :user_session => { :login => 'mmoen', :password => 'password' }
response.should redirect_to(account_path)
response.should redirect_to(root_path)
end

it "should redirect to the login page on session deletion" do
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

it "should redirect to account on successful :create" do
post :create, :user => { :login => 'bob', :email => '[email protected]',
:password => 'bobs_pass', :password_confirmation => 'bobs_pass' }
:password => 'bobs_pass' }
response.should redirect_to(account_path)
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe User do
fixtures :users

describe "changing password" do
it "should succeed without a confirmation field" do
user = users(:mmoen)
user.password = 'some new password'
user.save!
end
end
end

0 comments on commit b2fc03a

Please sign in to comment.