added user maintenance functional test
This commit is contained in:
@@ -1,10 +1,25 @@
|
|||||||
class UserMaintenanceController < ApplicationController
|
class UserMaintenanceController < ApplicationController
|
||||||
def delete_account
|
|
||||||
end
|
|
||||||
|
|
||||||
def login_reminder
|
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
|
end
|
||||||
|
|
||||||
def reset_password
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ class User < ActiveRecord::Base
|
|||||||
belongs_to :inviter, :class_name => "User"
|
belongs_to :inviter, :class_name => "User"
|
||||||
scope :named, lambda {|name| where(["lower(name) = ?", name])}
|
scope :named, lambda {|name| where(["lower(name) = ?", name])}
|
||||||
scope :admins, where("is_admin = TRUE")
|
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
|
module BanMethods
|
||||||
def validate_ip_addr_is_not_banned
|
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)
|
execute_sql("UPDATE users SET password_hash = ? WHERE id = ?", self.class.sha1(pass), id)
|
||||||
pass
|
pass
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reset_password_and_deliver_notice
|
||||||
|
new_password = reset_password()
|
||||||
|
UserMaintenanceMailer.reset_password(self, new_password).deliver
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module AuthenticationMethods
|
module AuthenticationMethods
|
||||||
|
|||||||
14
app/models/user_maintenance_mailer.rb
Normal file
14
app/models/user_maintenance_mailer.rb
Normal 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
|
||||||
@@ -75,9 +75,8 @@ Danbooru::Application.routes.draw do
|
|||||||
match '/dtext/preview' => 'dtext#preview', :via => :post
|
match '/dtext/preview' => 'dtext#preview', :via => :post
|
||||||
match "/site_map" => "static#site_map", :as => "site_map"
|
match "/site_map" => "static#site_map", :as => "site_map"
|
||||||
match "/terms_of_service" => "static#terms_of_service", :as => "terms_of_service"
|
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"
|
||||||
match "/user_maintenance/login_reminder" => "user_maintenance#login_reminder", :as => "login_reminder_info"
|
match "/user_maintenance/reset_password" => "user_maintenance#reset_password"
|
||||||
match "/user_maintenance/reset_password" => "user_maintenance#reset_password", :as => "reset_password_info"
|
|
||||||
|
|
||||||
root :to => "posts#index"
|
root :to => "posts#index"
|
||||||
end
|
end
|
||||||
|
|||||||
60
test/functional/user_maintenance_controller_test.rb
Normal file
60
test/functional/user_maintenance_controller_test.rb
Normal 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
|
||||||
Reference in New Issue
Block a user