added user maintenance functional test

This commit is contained in:
albert
2011-02-01 18:33:17 -05:00
parent 8eb3451fb4
commit 494abe30bb
7 changed files with 101 additions and 6 deletions

View File

@@ -1,10 +1,25 @@
class UserMaintenanceController < ApplicationController
def delete_account
end
def login_reminder
if request.post?
@user = User.with_email(params[:user][:email]).first
if @user
UserMaintenanceMailer.login_reminder(@user).deliver
flash[:notice] = "Email sent"
else
flash[:notice] = "No matching user record found"
end
end
end
def reset_password
if request.post?
@user = User.find_for_password_reset(params[:user][:name], params[:user][:email]).first
if @user
@user.reset_password_and_deliver_notice
flash[:notice] = "Email sent"
else
flash[:notice] = "No matching user record found"
end
end
end
end

View File

@@ -26,6 +26,8 @@ class User < ActiveRecord::Base
belongs_to :inviter, :class_name => "User"
scope :named, lambda {|name| where(["lower(name) = ?", name])}
scope :admins, where("is_admin = TRUE")
scope :with_email, lambda {|email| email.blank? ? where("FALSE") : where(["email = ?", email])}
scope :find_for_password_reset, lambda {|name, email| email.blank? ? where("FALSE") : where(["name = ? AND email = ?", name, email])}
module BanMethods
def validate_ip_addr_is_not_banned
@@ -94,6 +96,11 @@ class User < ActiveRecord::Base
execute_sql("UPDATE users SET password_hash = ? WHERE id = ?", self.class.sha1(pass), id)
pass
end
def reset_password_and_deliver_notice
new_password = reset_password()
UserMaintenanceMailer.reset_password(self, new_password).deliver
end
end
module AuthenticationMethods

View File

@@ -0,0 +1,14 @@
class UserMaintenanceMailer < ActionMailer::Base
default :from => Danbooru.config.contact_email
def login_reminder(user)
@user = user
mail(:to => user.email, :subject => "#{Danbooru.config.app_name} login reminder")
end
def reset_password(user, new_password)
@user = user
@new_password = new_password
mail(:to => user.email, :subject => "#{Danbooru.config.app_name} password reset")
end
end

View File

@@ -75,9 +75,8 @@ Danbooru::Application.routes.draw do
match '/dtext/preview' => 'dtext#preview', :via => :post
match "/site_map" => "static#site_map", :as => "site_map"
match "/terms_of_service" => "static#terms_of_service", :as => "terms_of_service"
match "/user_maintenance/delete_account" => "user_maintenance#delete_account", :as => "delete_account_info"
match "/user_maintenance/login_reminder" => "user_maintenance#login_reminder", :as => "login_reminder_info"
match "/user_maintenance/reset_password" => "user_maintenance#reset_password", :as => "reset_password_info"
match "/user_maintenance/login_reminder" => "user_maintenance#login_reminder"
match "/user_maintenance/reset_password" => "user_maintenance#reset_password"
root :to => "posts#index"
end

View File

@@ -0,0 +1,60 @@
require 'test_helper'
class UserMaintenanceControllerTest < ActionController::TestCase
context "The user maintenance controller" do
setup do
@user = Factory.create(:user)
@blank_email_user = Factory.create(:user, :email => "")
CurrentUser.user = nil
CurrentUser.ip_addr = "127.0.0.1"
ActionMailer::Base.deliveries.clear
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "login_reminder action" do
should "deliver an email with the login to the user" do
post :login_reminder, {:user => {:email => @user.email}}
assert_equal(flash[:notice], "Email sent")
assert_equal(1, ActionMailer::Base.deliveries.size)
end
context "for a user with a blank email" do
should "fail" do
post :login_reminder, {:user => {:email => ""}}
assert_equal("No matching user record found", flash[:notice])
@blank_email_user.reload
assert_equal(@blank_email_user.created_at, @blank_email_user.updated_at)
assert_equal(0, ActionMailer::Base.deliveries.size)
end
end
end
context "reset_password action" do
setup do
@old_password = @user.password_hash
end
should "reset the user's password and deliver an email to the user" do
post :reset_password, {:user => {:email => @user.email, :name => @user.name}}
assert_equal("Email sent", flash[:notice])
@user.reload
assert_not_equal(@old_password, @user.password)
assert_equal(1, ActionMailer::Base.deliveries.size)
end
context "for a user with a blank email" do
should "fail" do
post :reset_password, {:user => {:email => "", :name => @blank_email_user.name}}
assert_equal("No matching user record found", flash[:notice])
@blank_email_user.reload
assert_equal(@blank_email_user.created_at, @blank_email_user.updated_at)
assert_equal(0, ActionMailer::Base.deliveries.size)
end
end
end
end
end