2011-09-08 22:57:52 -04:00
|
|
|
require 'test_helper'
|
2010-02-06 16:48:40 -05:00
|
|
|
|
|
|
|
class UserTest < ActiveSupport::TestCase
|
|
|
|
context "A user" do
|
2011-06-05 04:05:21 -04:00
|
|
|
setup do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2011-06-05 04:05:21 -04:00
|
|
|
CurrentUser.user = @user
|
|
|
|
CurrentUser.ip_addr = "127.0.0.1"
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
CurrentUser.user = nil
|
|
|
|
CurrentUser.ip_addr = nil
|
|
|
|
end
|
2017-02-23 23:06:12 -05:00
|
|
|
|
2014-03-05 20:33:57 -05:00
|
|
|
context "promoting a user" do
|
|
|
|
setup do
|
|
|
|
CurrentUser.user = FactoryGirl.create(:moderator_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "create a neutral feedback" do
|
|
|
|
assert_difference("UserFeedback.count") do
|
|
|
|
@user.promote_to!(User::Levels::GOLD)
|
|
|
|
end
|
|
|
|
|
2016-01-18 20:13:26 -05:00
|
|
|
assert_equal("You have been promoted to a Gold level account from Member.", @user.feedback.last.body)
|
2014-03-05 20:33:57 -05:00
|
|
|
end
|
2014-03-05 20:47:40 -05:00
|
|
|
|
2017-02-23 23:06:12 -05:00
|
|
|
should "send an automated dmail to the user" do
|
|
|
|
bot = FactoryGirl.create(:user)
|
|
|
|
Danbooru.config.stubs(:system_user).returns(bot)
|
|
|
|
|
2017-02-25 17:54:08 -05:00
|
|
|
assert_difference("Dmail.count", 1) do
|
2014-03-05 20:47:40 -05:00
|
|
|
@user.promote_to!(User::Levels::GOLD)
|
|
|
|
end
|
2017-02-23 23:06:12 -05:00
|
|
|
|
|
|
|
assert(@user.dmails.exists?(from: bot, to: @user, title: "You have been promoted"))
|
2014-03-05 20:47:40 -05:00
|
|
|
end
|
2014-03-05 20:33:57 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-04-26 20:54:38 -04:00
|
|
|
context "favoriting a post" do
|
|
|
|
setup do
|
|
|
|
@user.update_column(:favorite_count, 999)
|
|
|
|
@user.stubs(:clean_favorite_count?).returns(true)
|
|
|
|
@post = FactoryGirl.create(:post)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "periodically clean the favorite_count" do
|
|
|
|
@user.add_favorite!(@post)
|
|
|
|
assert_equal(1, @user.favorite_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-29 19:42:25 -04:00
|
|
|
context "that has been invited by a mod" do
|
|
|
|
setup do
|
2012-06-01 19:22:58 -04:00
|
|
|
@mod = FactoryGirl.create(:moderator_user)
|
2011-07-29 19:42:25 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-07-29 19:42:25 -04:00
|
|
|
should "work" do
|
2015-10-15 18:24:24 -04:00
|
|
|
@user.invite!(User::Levels::BUILDER, "1")
|
2011-07-29 19:42:25 -04:00
|
|
|
@user.reload
|
2015-10-15 18:24:24 -04:00
|
|
|
assert_equal(User::Levels::BUILDER, @user.level)
|
|
|
|
assert_equal(true, @user.can_upload_free)
|
2011-07-29 19:42:25 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-02-28 13:19:31 -05:00
|
|
|
should "create a mod action" do
|
|
|
|
assert_difference("ModAction.count") do
|
2015-10-15 18:24:24 -04:00
|
|
|
@user.invite!(User::Levels::BUILDER, "1")
|
2013-02-28 13:19:31 -05:00
|
|
|
end
|
2015-10-15 18:24:24 -04:00
|
|
|
assert_equal(%{"#{@user.name}":/users/#{@user.id} level changed Member -> Builder}, ModAction.last.description)
|
2013-02-28 13:19:31 -05:00
|
|
|
end
|
2011-07-29 19:42:25 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-18 17:55:57 -04:00
|
|
|
should "not validate if the originating ip address is banned" do
|
2012-06-01 19:22:58 -04:00
|
|
|
FactoryGirl.create(:ip_ban)
|
|
|
|
user = FactoryGirl.build(:user)
|
2010-03-18 17:55:57 -04:00
|
|
|
user.save
|
|
|
|
assert(user.errors.any?)
|
|
|
|
assert_equal("IP address is banned", user.errors.full_messages.join)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-12 15:18:30 -05:00
|
|
|
should "limit post uploads" do
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(!@user.can_upload?)
|
2015-09-23 15:13:14 -04:00
|
|
|
@user.update_column(:created_at, 15.days.ago)
|
2015-10-15 18:24:24 -04:00
|
|
|
assert(@user.can_upload?)
|
2014-02-13 14:30:35 -05:00
|
|
|
assert_equal(10, @user.upload_limit)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2014-02-13 14:30:35 -05:00
|
|
|
9.times do
|
|
|
|
FactoryGirl.create(:post, :uploader => @user, :is_pending => true)
|
2010-03-12 15:18:30 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2015-09-23 15:13:14 -04:00
|
|
|
@user = User.find(@user.id)
|
2014-02-13 14:30:35 -05:00
|
|
|
assert_equal(1, @user.upload_limit)
|
|
|
|
assert(@user.can_upload?)
|
|
|
|
FactoryGirl.create(:post, :uploader => @user, :is_pending => true)
|
2015-09-23 15:13:14 -04:00
|
|
|
@user = User.find(@user.id)
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(!@user.can_upload?)
|
2010-03-12 15:18:30 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-12 15:18:30 -05:00
|
|
|
should "limit comment votes" do
|
2011-12-14 11:19:58 -05:00
|
|
|
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
|
2011-10-16 01:40:42 -04:00
|
|
|
Danbooru.config.stubs(:member_comment_limit).returns(10)
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(@user.can_comment_vote?)
|
|
|
|
10.times do
|
2012-06-01 19:22:58 -04:00
|
|
|
comment = FactoryGirl.create(:comment)
|
2016-12-03 21:06:50 -05:00
|
|
|
FactoryGirl.create(:comment_vote, :comment_id => comment.id, :score => -1)
|
2010-03-12 15:18:30 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(!@user.can_comment_vote?)
|
2010-03-12 15:18:30 -05:00
|
|
|
CommentVote.update_all("created_at = '1990-01-01'")
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(@user.can_comment_vote?)
|
2010-03-12 15:18:30 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-12 15:18:30 -05:00
|
|
|
should "limit comments" do
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(!@user.can_comment?)
|
2013-04-28 03:04:52 -04:00
|
|
|
@user.update_column(:level, User::Levels::GOLD)
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(@user.can_comment?)
|
2011-07-16 19:20:02 -04:00
|
|
|
@user.update_column(:level, User::Levels::MEMBER)
|
|
|
|
@user.update_column(:created_at, 1.year.ago)
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(@user.can_comment?)
|
2013-04-03 17:23:25 -04:00
|
|
|
assert(!@user.is_comment_limited?)
|
2011-06-05 04:05:21 -04:00
|
|
|
(Danbooru.config.member_comment_limit).times do
|
2012-06-01 19:22:58 -04:00
|
|
|
FactoryGirl.create(:comment)
|
2010-03-12 15:18:30 -05:00
|
|
|
end
|
2013-04-03 17:23:25 -04:00
|
|
|
assert(@user.is_comment_limited?)
|
2010-03-12 15:18:30 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-09 16:14:29 -05:00
|
|
|
should "verify" do
|
2011-06-05 04:05:21 -04:00
|
|
|
assert(@user.is_verified?)
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2011-06-05 04:05:21 -04:00
|
|
|
@user.generate_email_verification_key
|
|
|
|
@user.save
|
|
|
|
assert(!@user.is_verified?)
|
|
|
|
assert_raise(User::Error) {@user.verify!("bbb")}
|
|
|
|
assert_nothing_raised {@user.verify!(@user.email_verification_key)}
|
|
|
|
assert(@user.is_verified?)
|
2010-03-09 16:14:29 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-06 16:59:38 -05:00
|
|
|
should "authenticate" do
|
2010-02-06 16:48:40 -05:00
|
|
|
assert(User.authenticate(@user.name, "password"), "Authentication should have succeeded")
|
|
|
|
assert(!User.authenticate(@user.name, "password2"), "Authentication should not have succeeded")
|
2013-03-05 16:49:09 -05:00
|
|
|
assert(User.authenticate_hash(@user.name, User.sha1("password")), "Authentication should have succeeded")
|
|
|
|
assert(!User.authenticate_hash(@user.name, User.sha1("xxx")), "Authentication should not have succeeded")
|
2010-02-06 16:48:40 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-19 17:30:11 -05:00
|
|
|
should "normalize its level" do
|
2012-06-01 19:22:58 -04:00
|
|
|
user = FactoryGirl.create(:user, :level => User::Levels::ADMIN)
|
2010-02-19 17:30:11 -05:00
|
|
|
assert(user.is_moderator?)
|
2013-04-28 03:04:52 -04:00
|
|
|
assert(user.is_gold?)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2012-06-01 19:22:58 -04:00
|
|
|
user = FactoryGirl.create(:user, :level => User::Levels::MODERATOR)
|
2010-02-19 17:30:11 -05:00
|
|
|
assert(!user.is_admin?)
|
|
|
|
assert(user.is_moderator?)
|
2013-04-28 03:04:52 -04:00
|
|
|
assert(user.is_gold?)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-04-28 03:04:52 -04:00
|
|
|
user = FactoryGirl.create(:user, :level => User::Levels::GOLD)
|
2010-02-19 17:30:11 -05:00
|
|
|
assert(!user.is_admin?)
|
|
|
|
assert(!user.is_moderator?)
|
2013-04-28 03:04:52 -04:00
|
|
|
assert(user.is_gold?)
|
2013-03-19 08:10:10 -04:00
|
|
|
|
|
|
|
user = FactoryGirl.create(:user)
|
2010-02-19 17:30:11 -05:00
|
|
|
assert(!user.is_admin?)
|
|
|
|
assert(!user.is_moderator?)
|
2013-04-28 03:04:52 -04:00
|
|
|
assert(!user.is_gold?)
|
2010-02-19 17:30:11 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-06 17:10:10 -05:00
|
|
|
context "name" do
|
|
|
|
should "be #{Danbooru.config.default_guest_name} given an invalid user id" do
|
2010-08-26 14:36:02 -04:00
|
|
|
assert_equal(Danbooru.config.default_guest_name, User.id_to_name(-1))
|
2010-02-06 17:10:10 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2017-02-25 02:47:57 -05:00
|
|
|
should "not contain whitespace" do
|
|
|
|
# U+2007: https://en.wikipedia.org/wiki/Figure_space
|
|
|
|
user = FactoryGirl.build(:user, :name => "foo\u2007bar")
|
|
|
|
user.save
|
|
|
|
assert_equal(["Name cannot have whitespace or colons"], user.errors.full_messages)
|
|
|
|
end
|
|
|
|
|
2013-04-19 18:51:38 -04:00
|
|
|
should "not contain a colon" do
|
|
|
|
user = FactoryGirl.build(:user, :name => "a:b")
|
|
|
|
user.save
|
|
|
|
assert_equal(["Name cannot have whitespace or colons"], user.errors.full_messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "not begin with an underscore" do
|
|
|
|
user = FactoryGirl.build(:user, :name => "_x")
|
|
|
|
user.save
|
|
|
|
assert_equal(["Name cannot begin or end with an underscore"], user.errors.full_messages)
|
|
|
|
end
|
|
|
|
|
|
|
|
should "not end with an underscore" do
|
|
|
|
user = FactoryGirl.build(:user, :name => "x_")
|
|
|
|
user.save
|
|
|
|
assert_equal(["Name cannot begin or end with an underscore"], user.errors.full_messages)
|
|
|
|
end
|
|
|
|
|
2010-02-06 17:10:10 -05:00
|
|
|
should "be fetched given a user id" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2010-08-26 14:36:02 -04:00
|
|
|
assert_equal(@user.name, User.id_to_name(@user.id))
|
2010-02-06 17:10:10 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-06 17:10:10 -05:00
|
|
|
should "be updated" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2011-07-16 19:30:42 -04:00
|
|
|
@user.update_attribute(:name, "danzig")
|
2011-07-16 19:20:02 -04:00
|
|
|
assert_equal(@user.name, User.id_to_name(@user.id))
|
2010-02-06 17:10:10 -05:00
|
|
|
end
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
context "ip address" do
|
|
|
|
setup do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2011-09-10 16:02:16 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
context "in the json representation" do
|
|
|
|
should "not appear" do
|
|
|
|
assert(@user.to_json !~ /addr/)
|
|
|
|
end
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
context "in the xml representation" do
|
|
|
|
should "not appear" do
|
|
|
|
assert(@user.to_xml !~ /addr/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-03-04 22:55:41 -05:00
|
|
|
context "password" do
|
|
|
|
should "match the cookie hash" do
|
|
|
|
@user = FactoryGirl.create(:user)
|
|
|
|
@user.password = "zugzug5"
|
|
|
|
@user.password_confirmation = "zugzug5"
|
|
|
|
@user.save
|
|
|
|
@user.reload
|
2013-03-05 16:49:09 -05:00
|
|
|
assert(User.authenticate_cookie_hash(@user.name, @user.bcrypt_cookie_password_hash))
|
2011-10-15 16:36:07 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-06 17:10:10 -05:00
|
|
|
should "match the confirmation" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2013-04-08 14:40:24 -04:00
|
|
|
@user.old_password = "password"
|
2010-02-06 16:59:38 -05:00
|
|
|
@user.password = "zugzug5"
|
|
|
|
@user.password_confirmation = "zugzug5"
|
|
|
|
@user.save
|
|
|
|
@user.reload
|
|
|
|
assert(User.authenticate(@user.name, "zugzug5"), "Authentication should have succeeded")
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2013-04-08 13:44:43 -04:00
|
|
|
should "fail if the confirmation does not match" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2010-02-06 16:59:38 -05:00
|
|
|
@user.password = "zugzug6"
|
|
|
|
@user.password_confirmation = "zugzug5"
|
|
|
|
@user.save
|
2014-04-14 17:32:01 -04:00
|
|
|
assert_equal(["Password confirmation doesn't match Password"], @user.errors.full_messages)
|
2010-02-06 16:59:38 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-06 16:59:38 -05:00
|
|
|
should "not be too short" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2010-02-06 16:59:38 -05:00
|
|
|
@user.password = "x5"
|
|
|
|
@user.password_confirmation = "x5"
|
|
|
|
@user.save
|
|
|
|
assert_equal(["Password is too short (minimum is 5 characters)"], @user.errors.full_messages)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-02-06 16:59:38 -05:00
|
|
|
should "should be reset" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2010-02-06 16:59:38 -05:00
|
|
|
new_pass = @user.reset_password
|
|
|
|
assert(User.authenticate(@user.name, new_pass), "Authentication should have succeeded")
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-14 17:46:42 -04:00
|
|
|
should "not change the password if the password and old password are blank" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user, :password => "67890")
|
2011-09-14 17:46:42 -04:00
|
|
|
@user.update_attributes(:password => "", :old_password => "")
|
2013-03-04 22:55:41 -05:00
|
|
|
assert(@user.bcrypt_password == User.sha1("67890"))
|
2011-09-14 17:46:42 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-14 17:46:42 -04:00
|
|
|
should "not change the password if the old password is incorrect" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user, :password => "67890")
|
2011-09-14 17:46:42 -04:00
|
|
|
@user.update_attributes(:password => "12345", :old_password => "abcdefg")
|
2013-03-04 22:55:41 -05:00
|
|
|
assert(@user.bcrypt_password == User.sha1("67890"))
|
2011-09-14 17:46:42 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-14 17:46:42 -04:00
|
|
|
should "not change the password if the old password is blank" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user, :password => "67890")
|
2011-09-14 17:46:42 -04:00
|
|
|
@user.update_attributes(:password => "12345", :old_password => "")
|
2013-03-04 22:55:41 -05:00
|
|
|
assert(@user.bcrypt_password == User.sha1("67890"))
|
2011-09-14 17:46:42 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-14 17:46:42 -04:00
|
|
|
should "change the password if the old password is correct" do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user, :password => "67890")
|
2011-09-14 17:46:42 -04:00
|
|
|
@user.update_attributes(:password => "12345", :old_password => "67890")
|
2013-03-04 22:55:41 -05:00
|
|
|
assert(@user.bcrypt_password == User.sha1("12345"))
|
2011-09-14 17:46:42 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
context "in the json representation" do
|
|
|
|
setup do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2011-09-10 16:02:16 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
should "not appear" do
|
|
|
|
assert(@user.to_json !~ /password/)
|
|
|
|
end
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
context "in the xml representation" do
|
|
|
|
setup do
|
2012-06-01 19:22:58 -04:00
|
|
|
@user = FactoryGirl.create(:user)
|
2011-09-10 16:02:16 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-09-10 16:02:16 -04:00
|
|
|
should "not appear" do
|
|
|
|
assert(@user.to_xml !~ /password/)
|
|
|
|
end
|
|
|
|
end
|
2010-02-06 16:59:38 -05:00
|
|
|
end
|
2010-02-06 16:48:40 -05:00
|
|
|
end
|
|
|
|
end
|