2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-10-03 23:28:49 -04:00
|
|
|
require_dependency 'user'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe User do
|
|
|
|
|
2016-12-21 04:00:45 -05:00
|
|
|
context 'validations' do
|
|
|
|
it { is_expected.to validate_presence_of :username }
|
2017-07-30 22:45:18 -04:00
|
|
|
it { is_expected.to validate_presence_of :primary_email }
|
2016-12-21 04:00:45 -05:00
|
|
|
|
|
|
|
describe 'emails' do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
|
|
|
|
|
|
|
describe 'when record has a valid email' do
|
|
|
|
it "should be valid" do
|
|
|
|
user.email = 'test@gmail.com'
|
2017-04-26 14:47:36 -04:00
|
|
|
|
2016-12-21 04:00:45 -05:00
|
|
|
expect(user).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when record has an invalid email' do
|
|
|
|
it 'should not be valid' do
|
|
|
|
user.email = 'test@gmailcom'
|
2017-04-26 14:47:36 -04:00
|
|
|
|
2016-12-21 04:00:45 -05:00
|
|
|
expect(user).to_not be_valid
|
2017-07-30 23:16:08 -04:00
|
|
|
expect(user.errors.messages).to include(:primary_email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user is staged' do
|
2017-08-08 22:56:08 -04:00
|
|
|
it 'should still validate presence of primary_email' do
|
2017-07-30 23:16:08 -04:00
|
|
|
user.staged = true
|
2017-08-08 22:56:08 -04:00
|
|
|
user.email = nil
|
2017-07-30 23:16:08 -04:00
|
|
|
|
|
|
|
expect(user).to_not be_valid
|
2017-04-26 14:47:36 -04:00
|
|
|
expect(user.errors.messages).to include(:primary_email)
|
2016-12-21 04:00:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-12-30 09:06:15 -05:00
|
|
|
describe '#count_by_signup_date' do
|
|
|
|
before(:each) do
|
|
|
|
User.destroy_all
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time
|
2014-12-30 09:06:15 -05:00
|
|
|
Fabricate(:user)
|
|
|
|
Fabricate(:user, created_at: 1.day.ago)
|
|
|
|
Fabricate(:user, created_at: 1.day.ago)
|
|
|
|
Fabricate(:user, created_at: 2.days.ago)
|
|
|
|
Fabricate(:user, created_at: 4.days.ago)
|
|
|
|
end
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:signups_by_day) { { 1.day.ago.to_date => 2, 2.days.ago.to_date => 1, Time.now.utc.to_date => 1 } }
|
2014-12-30 09:06:15 -05:00
|
|
|
|
|
|
|
it 'collect closed interval signups' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(User.count_by_signup_date(2.days.ago, Time.now)).to include(signups_by_day)
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(User.count_by_signup_date(2.days.ago, Time.now)).not_to include(4.days.ago.to_date => 1)
|
2014-12-30 09:06:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context '.enqueue_welcome_message' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it 'enqueues the system message' do
|
|
|
|
Jobs.expects(:enqueue).with(:send_system_message, user_id: user.id, message_type: 'welcome_user')
|
|
|
|
user.enqueue_welcome_message('welcome_user')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't enqueue the system message when the site settings disable it" do
|
|
|
|
SiteSetting.expects(:send_welcome_message?).returns(false)
|
|
|
|
Jobs.expects(:enqueue).with(:send_system_message, user_id: user.id, message_type: 'welcome_user').never
|
|
|
|
user.enqueue_welcome_message('welcome_user')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-28 08:08:56 -05:00
|
|
|
describe '.approve' do
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
2015-05-04 15:30:25 -04:00
|
|
|
it "enqueues a 'signup after approval' email if must_approve_users is true" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.must_approve_users = true
|
2013-06-05 23:16:31 -04:00
|
|
|
Jobs.expects(:enqueue).with(
|
2016-04-07 00:38:43 -04:00
|
|
|
:critical_user_email, has_entries(type: :signup_after_approval)
|
2013-06-05 23:16:31 -04:00
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
user.approve(admin)
|
|
|
|
end
|
|
|
|
|
2015-05-04 15:30:25 -04:00
|
|
|
it "doesn't enqueue a 'signup after approval' email if must_approve_users is false" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.must_approve_users = false
|
2015-05-04 15:30:25 -04:00
|
|
|
Jobs.expects(:enqueue).never
|
|
|
|
user.approve(admin)
|
|
|
|
end
|
|
|
|
|
2017-08-04 12:12:10 -04:00
|
|
|
it 'triggers extensibility events' do
|
2016-09-13 04:03:17 -04:00
|
|
|
user && admin # bypass the user_created event
|
2017-08-04 12:12:10 -04:00
|
|
|
user_updated_event, user_approved_event = DiscourseEvent.track_events { user.approve(admin) }
|
2017-06-01 03:19:43 -04:00
|
|
|
|
2017-08-04 12:12:10 -04:00
|
|
|
expect(user_updated_event[:event_name]).to eq(:user_updated)
|
|
|
|
expect(user_updated_event[:params].first).to eq(user)
|
|
|
|
|
|
|
|
expect(user_approved_event[:event_name]).to eq(:user_approved)
|
|
|
|
expect(user_approved_event[:params].first).to eq(user)
|
2016-09-13 04:03:17 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'after approval' do
|
|
|
|
before do
|
|
|
|
user.approve(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'marks the user as approved' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user).to be_approved
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the admin as the approved by' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.approved_by).to eq(admin)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a value for approved_at' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.approved_at).to be_present
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'bookmark' do
|
|
|
|
before do
|
|
|
|
@post = Fabricate(:post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a bookmark with the true parameter" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(@post.user, @post, PostActionType.types[:bookmark])
|
2014-12-31 09:55:03 -05:00
|
|
|
}.to change(PostAction, :count).by(1)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when removing a bookmark' do
|
|
|
|
before do
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.act(@post.user, @post, PostActionType.types[:bookmark])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'reduces the bookmark count of the post' do
|
|
|
|
active = PostAction.where(deleted_at: nil)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.remove_act(@post.user, @post, PostActionType.types[:bookmark])
|
2014-12-31 09:55:03 -05:00
|
|
|
}.to change(active, :count).by(-1)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-02-28 13:54:21 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe 'delete posts' do
|
|
|
|
before do
|
2013-02-07 02:11:56 -05:00
|
|
|
@post1 = Fabricate(:post)
|
|
|
|
@user = @post1.user
|
|
|
|
@post2 = Fabricate(:post, topic: @post1.topic, user: @user)
|
|
|
|
@post3 = Fabricate(:post, user: @user)
|
|
|
|
@posts = [@post1, @post2, @post3]
|
|
|
|
@guardian = Guardian.new(Fabricate(:admin))
|
2015-04-24 16:04:44 -04:00
|
|
|
@queued_post = Fabricate(:queued_post, user: @user)
|
2013-02-07 02:11:56 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'allows moderator to delete all posts' do
|
2013-02-07 02:11:56 -05:00
|
|
|
@user.delete_all_posts!(@guardian)
|
2013-07-22 14:44:11 -04:00
|
|
|
expect(Post.where(id: @posts.map(&:id))).to be_empty
|
2015-04-24 16:04:44 -04:00
|
|
|
expect(QueuedPost.where(user_id: @user.id).count).to eq(0)
|
2013-02-07 02:11:56 -05:00
|
|
|
@posts.each do |p|
|
2015-04-23 13:33:29 -04:00
|
|
|
if p.is_first_post?
|
2014-10-31 13:44:26 -04:00
|
|
|
expect(Topic.find_by(id: p.topic_id)).to be_nil
|
2013-02-07 02:11:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-03-28 19:24:35 -04:00
|
|
|
|
|
|
|
it 'does not allow non moderators to delete all posts' do
|
|
|
|
invalid_guardian = Guardian.new(Fabricate(:user))
|
|
|
|
|
|
|
|
expect do
|
|
|
|
@user.delete_all_posts!(invalid_guardian)
|
|
|
|
end.to raise_error Discourse::InvalidAccess
|
|
|
|
|
|
|
|
@posts.each do |p|
|
|
|
|
p.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(p).to be_present
|
|
|
|
expect(p.topic).to be_present
|
2013-03-28 19:24:35 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 02:11:56 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe 'new' do
|
|
|
|
|
|
|
|
subject { Fabricate.build(:user) }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to be_valid }
|
|
|
|
it { is_expected.not_to be_admin }
|
|
|
|
it { is_expected.not_to be_approved }
|
2014-10-29 11:06:50 -04:00
|
|
|
|
|
|
|
it "is properly initialized" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject.approved_at).to be_blank
|
|
|
|
expect(subject.approved_by_id).to be_blank
|
2014-10-29 11:06:50 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-09-13 04:03:17 -04:00
|
|
|
it 'triggers an extensibility event' do
|
2017-06-01 03:19:43 -04:00
|
|
|
event = DiscourseEvent.track_events { subject.save! }.first
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:user_created)
|
|
|
|
expect(event[:params].first).to eq(subject)
|
2016-09-13 04:03:17 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'after_save' do
|
2016-09-13 04:03:17 -04:00
|
|
|
before { subject.save! }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
it "has correct settings" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject.email_tokens).to be_present
|
2017-02-20 04:37:01 -05:00
|
|
|
expect(subject.user_stat).to be_present
|
|
|
|
expect(subject.user_profile).to be_present
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(subject.user_option.email_private_messages).to eq(true)
|
|
|
|
expect(subject.user_option.email_direct).to eq(true)
|
2014-10-29 11:06:50 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-14 10:16:24 -04:00
|
|
|
|
|
|
|
it "downcases email addresses" do
|
|
|
|
user = Fabricate.build(:user, email: 'Fancy.Caps.4.U@gmail.com')
|
2015-01-29 14:41:41 -05:00
|
|
|
user.valid?
|
|
|
|
expect(user.email).to eq('fancy.caps.4.u@gmail.com')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "strips whitespace from email addresses" do
|
|
|
|
user = Fabricate.build(:user, email: ' example@gmail.com ')
|
|
|
|
user.valid?
|
|
|
|
expect(user.email).to eq('example@gmail.com')
|
2014-07-14 10:16:24 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-21 14:49:51 -04:00
|
|
|
describe 'ip address validation' do
|
|
|
|
it 'validates ip_address for new users' do
|
|
|
|
u = Fabricate.build(:user)
|
|
|
|
AllowedIpAddressValidator.any_instance.expects(:validate_each).with(u, :ip_address, u.ip_address)
|
|
|
|
u.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not validate ip_address when updating an existing user' do
|
|
|
|
u = Fabricate(:user)
|
|
|
|
u.ip_address = '87.123.23.11'
|
|
|
|
AllowedIpAddressValidator.any_instance.expects(:validate_each).never
|
|
|
|
u.valid?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
describe "trust levels" do
|
2013-03-20 00:05:19 -04:00
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# NOTE be sure to use build to avoid db calls
|
2014-09-05 01:20:39 -04:00
|
|
|
let(:user) { Fabricate.build(:user, trust_level: TrustLevel[0]) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "sets to the default trust level setting" do
|
2014-09-05 01:20:39 -04:00
|
|
|
SiteSetting.default_trust_level = TrustLevel[4]
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(User.new.trust_level).to eq(TrustLevel[4])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-28 08:08:56 -05:00
|
|
|
describe 'has_trust_level?' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "raises an error with an invalid level" do
|
2016-05-29 23:38:04 -04:00
|
|
|
expect { user.has_trust_level?(:wat) }.to raise_error(InvalidTrustLevel)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is true for your basic level" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.has_trust_level?(TrustLevel[0])).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false for a higher level" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.has_trust_level?(TrustLevel[2])).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is true if you exceed the level" do
|
2014-09-05 01:20:39 -04:00
|
|
|
user.trust_level = TrustLevel[4]
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.has_trust_level?(TrustLevel[1])).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is true for an admin even with a low trust level" do
|
2014-09-05 01:20:39 -04:00
|
|
|
user.trust_level = TrustLevel[0]
|
2013-02-05 14:16:51 -05:00
|
|
|
user.admin = true
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.has_trust_level?(TrustLevel[1])).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'moderator' do
|
|
|
|
it "isn't a moderator by default" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.moderator?).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is a moderator if the user level is moderator" do
|
2013-03-20 00:05:19 -04:00
|
|
|
user.moderator = true
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.has_trust_level?(TrustLevel[4])).to eq(true)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-02 03:22:27 -04:00
|
|
|
it "is staff if the user is an admin" do
|
2013-02-05 14:16:51 -05:00
|
|
|
user.admin = true
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.staff?).to eq(true)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-18 04:07:07 -04:00
|
|
|
describe 'staff and regular users' do
|
|
|
|
let(:user) { Fabricate.build(:user) }
|
|
|
|
|
|
|
|
describe '#staff?' do
|
|
|
|
subject { user.staff? }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to eq(false) }
|
2013-05-18 04:07:07 -04:00
|
|
|
|
|
|
|
context 'for a moderator user' do
|
|
|
|
before { user.moderator = true }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to eq(true) }
|
2013-05-18 04:07:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for an admin user' do
|
|
|
|
before { user.admin = true }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to eq(true) }
|
2013-05-18 04:07:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#regular?' do
|
|
|
|
subject { user.regular? }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to eq(true) }
|
2013-05-18 04:07:07 -04:00
|
|
|
|
|
|
|
context 'for a moderator user' do
|
|
|
|
before { user.moderator = true }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to eq(false) }
|
2013-05-18 04:07:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for an admin user' do
|
|
|
|
before { user.admin = true }
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to eq(false) }
|
2013-05-18 04:07:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
describe 'email_hash' do
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@user = Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
it 'should have a sane email hash' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(@user.email_hash).to match(/^[0-9a-f]{32}$/)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use downcase email' do
|
|
|
|
@user.email = "example@example.com"
|
|
|
|
@user2 = Fabricate(:user)
|
|
|
|
@user2.email = "ExAmPlE@eXaMpLe.com"
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(@user.email_hash).to eq(@user2.email_hash)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should trim whitespace before hashing' do
|
|
|
|
@user.email = "example@example.com"
|
|
|
|
@user2 = Fabricate(:user)
|
|
|
|
@user2.email = " example@example.com "
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(@user.email_hash).to eq(@user2.email_hash)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-09-25 01:50:54 -04:00
|
|
|
describe 'associated_accounts' do
|
|
|
|
it 'should correctly find social associations' do
|
|
|
|
user = Fabricate(:user)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.associated_accounts).to eq(I18n.t("user.no_accounts_associated"))
|
2014-09-25 01:50:54 -04:00
|
|
|
|
|
|
|
TwitterUserInfo.create(user_id: user.id, screen_name: "sam", twitter_user_id: 1)
|
|
|
|
FacebookUserInfo.create(user_id: user.id, username: "sam", facebook_user_id: 1)
|
|
|
|
GoogleUserInfo.create(user_id: user.id, email: "sam@sam.com", google_user_id: 1)
|
|
|
|
GithubUserInfo.create(user_id: user.id, screen_name: "sam", github_user_id: 1)
|
2016-07-18 03:02:41 -04:00
|
|
|
Oauth2UserInfo.create(user_id: user.id, provider: "linkedin", email: "sam@sam.com", uid: 1)
|
2014-09-25 01:50:54 -04:00
|
|
|
|
|
|
|
user.reload
|
2016-07-18 03:02:41 -04:00
|
|
|
expect(user.associated_accounts).to eq("Twitter(sam), Facebook(sam), Google(sam@sam.com), Github(sam), linkedin(sam@sam.com)")
|
2014-09-25 01:50:54 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-26 13:08:19 -04:00
|
|
|
describe '.is_singular_admin?' do
|
|
|
|
it 'returns true if user is singular admin' do
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
expect(admin.is_singular_admin?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if user is not the only admin' do
|
|
|
|
admin = Fabricate(:admin)
|
2016-07-28 13:54:17 -04:00
|
|
|
Fabricate(:admin)
|
2016-04-26 13:08:19 -04:00
|
|
|
|
|
|
|
expect(admin.is_singular_admin?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
describe 'name heuristics' do
|
|
|
|
it 'is able to guess a decent name from an email' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(User.suggest_name('sam.saffron@gmail.com')).to eq('Sam Saffron')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
describe 'username format' do
|
2015-09-01 22:13:44 -04:00
|
|
|
def assert_bad(username)
|
|
|
|
user = Fabricate.build(:user)
|
|
|
|
user.username = username
|
|
|
|
expect(user.valid?).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-09-01 22:13:44 -04:00
|
|
|
def assert_good(username)
|
|
|
|
user = Fabricate.build(:user)
|
|
|
|
user.username = username
|
|
|
|
expect(user.valid?).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-09-01 22:13:44 -04:00
|
|
|
it "should be SiteSetting.min_username_length chars or longer" do
|
|
|
|
SiteSetting.min_username_length = 5
|
|
|
|
assert_bad("abcd")
|
|
|
|
assert_good("abcde")
|
|
|
|
end
|
|
|
|
|
|
|
|
%w{ first.last
|
|
|
|
first first-last
|
|
|
|
_name first_last
|
|
|
|
mc.hammer_nose
|
|
|
|
UPPERCASE
|
|
|
|
sgif
|
|
|
|
}.each do |username|
|
|
|
|
it "allows #{username}" do
|
|
|
|
assert_good(username)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-09-01 22:13:44 -04:00
|
|
|
%w{
|
|
|
|
traildot.
|
|
|
|
has\ space
|
|
|
|
double__underscore
|
|
|
|
with%symbol
|
|
|
|
Exclamation!
|
|
|
|
@twitter
|
|
|
|
my@email.com
|
|
|
|
.tester
|
|
|
|
sa$sy
|
|
|
|
sam.json
|
|
|
|
sam.xml
|
|
|
|
sam.html
|
|
|
|
sam.htm
|
|
|
|
sam.js
|
|
|
|
sam.woff
|
|
|
|
sam.Png
|
|
|
|
sam.gif
|
|
|
|
}.each do |username|
|
|
|
|
it "disallows #{username}" do
|
|
|
|
assert_bad(username)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'username uniqueness' do
|
|
|
|
before do
|
|
|
|
@user = Fabricate.build(:user)
|
|
|
|
@user.save!
|
|
|
|
@codinghorror = Fabricate.build(:coding_horror)
|
|
|
|
end
|
2013-02-05 21:44:49 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it "should not allow saving if username is reused" do
|
2017-07-27 21:20:09 -04:00
|
|
|
@codinghorror.username = @user.username
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(@codinghorror.save).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
it "should not allow saving if username is reused in different casing" do
|
2017-07-27 21:20:09 -04:00
|
|
|
@codinghorror.username = @user.username.upcase
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(@codinghorror.save).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
describe '.username_available?' do
|
2013-02-05 14:16:51 -05:00
|
|
|
it "returns true for a username that is available" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(User.username_available?('BruceWayne')).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when a username is taken' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(User.username_available?(Fabricate(:user).username)).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2016-08-31 09:49:45 -04:00
|
|
|
|
|
|
|
it 'returns false when a username is reserved' do
|
|
|
|
SiteSetting.reserved_usernames = 'test|donkey'
|
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
expect(User.username_available?('tESt')).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.reserved_username?' do
|
|
|
|
it 'returns true when a username is reserved' do
|
|
|
|
SiteSetting.reserved_usernames = 'test|donkey'
|
|
|
|
|
|
|
|
expect(User.reserved_username?('donkey')).to eq(true)
|
|
|
|
expect(User.reserved_username?('DonKey')).to eq(true)
|
|
|
|
expect(User.reserved_username?('test')).to eq(true)
|
2016-08-31 09:49:45 -04:00
|
|
|
end
|
2017-02-27 15:28:56 -05:00
|
|
|
|
|
|
|
it 'should not allow usernames matched against an expession' do
|
|
|
|
SiteSetting.reserved_usernames = 'test)|*admin*|foo*|*bar|abc.def'
|
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
expect(User.reserved_username?('test')).to eq(false)
|
|
|
|
expect(User.reserved_username?('abc9def')).to eq(false)
|
2017-02-27 15:28:56 -05:00
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
expect(User.reserved_username?('admin')).to eq(true)
|
|
|
|
expect(User.reserved_username?('foo')).to eq(true)
|
|
|
|
expect(User.reserved_username?('bar')).to eq(true)
|
2017-02-27 15:28:56 -05:00
|
|
|
|
2017-04-12 22:44:26 -04:00
|
|
|
expect(User.reserved_username?('admi')).to eq(false)
|
|
|
|
expect(User.reserved_username?('bar.foo')).to eq(false)
|
|
|
|
expect(User.reserved_username?('foo.bar')).to eq(true)
|
|
|
|
expect(User.reserved_username?('baz.bar')).to eq(true)
|
2017-02-27 15:28:56 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-13 12:28:23 -05:00
|
|
|
describe 'email_validator' do
|
|
|
|
it 'should allow good emails' do
|
|
|
|
user = Fabricate.build(:user, email: 'good@gmail.com')
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user).to be_valid
|
2013-02-13 12:28:23 -05:00
|
|
|
end
|
|
|
|
|
2013-02-13 22:24:32 -05:00
|
|
|
it 'should reject some emails based on the email_domains_blacklist site setting' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'mailinator.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'notgood@mailinator.com')).not_to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'mailinator@gmail.com')).to be_valid
|
2013-02-13 12:28:23 -05:00
|
|
|
end
|
|
|
|
|
2013-02-13 22:24:32 -05:00
|
|
|
it 'should reject some emails based on the email_domains_blacklist site setting' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'mailinator.com|trashmail.net'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'notgood@mailinator.com')).not_to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'notgood@trashmail.net')).not_to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'mailinator.com@gmail.com')).to be_valid
|
2013-02-13 12:28:23 -05:00
|
|
|
end
|
2013-03-31 12:51:13 -04:00
|
|
|
|
2013-03-18 15:48:56 -04:00
|
|
|
it 'should not reject partial matches' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'mail.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'mailinator@gmail.com')).to be_valid
|
2013-03-18 15:48:56 -04:00
|
|
|
end
|
2013-03-31 12:51:13 -04:00
|
|
|
|
2013-02-13 22:24:32 -05:00
|
|
|
it 'should reject some emails based on the email_domains_blacklist site setting ignoring case' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'trashmail.net'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'notgood@TRASHMAIL.NET')).not_to be_valid
|
2013-02-13 22:24:32 -05:00
|
|
|
end
|
|
|
|
|
2015-05-13 08:08:10 -04:00
|
|
|
it 'should reject emails based on the email_domains_blacklist site setting matching subdomain' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'domain.com'
|
2015-05-13 08:08:10 -04:00
|
|
|
expect(Fabricate.build(:user, email: 'notgood@sub.domain.com')).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2016-09-07 14:05:46 -04:00
|
|
|
it 'skips the blacklist if skip_email_validation is set' do
|
|
|
|
SiteSetting.email_domains_blacklist = 'domain.com'
|
|
|
|
user = Fabricate.build(:user, email: 'notgood@sub.domain.com')
|
|
|
|
user.skip_email_validation = true
|
|
|
|
expect(user).to be_valid
|
|
|
|
end
|
|
|
|
|
2015-01-29 12:52:59 -05:00
|
|
|
it 'blacklist should not reject developer emails' do
|
|
|
|
Rails.configuration.stubs(:developer_emails).returns('developer@discourse.org')
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'discourse.org'
|
2015-01-29 12:52:59 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'developer@discourse.org')).to be_valid
|
|
|
|
end
|
|
|
|
|
2013-02-13 22:24:32 -05:00
|
|
|
it 'should not interpret a period as a wildcard' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'trashmail.net'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'good@trashmailinet.com')).to be_valid
|
2013-02-13 12:28:23 -05:00
|
|
|
end
|
2013-02-20 12:16:01 -05:00
|
|
|
|
|
|
|
it 'should not be used to validate existing records' do
|
|
|
|
u = Fabricate(:user, email: 'in_before_blacklisted@fakemail.com')
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'fakemail.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(u).to be_valid
|
2013-02-20 12:16:01 -05:00
|
|
|
end
|
2013-02-20 16:24:52 -05:00
|
|
|
|
|
|
|
it 'should be used when email is being changed' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'mailinator.com'
|
2013-02-20 16:24:52 -05:00
|
|
|
u = Fabricate(:user, email: 'good@gmail.com')
|
|
|
|
u.email = 'nope@mailinator.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(u).not_to be_valid
|
2013-03-18 15:48:56 -04:00
|
|
|
end
|
2013-03-31 12:51:13 -04:00
|
|
|
|
2013-03-18 15:48:56 -04:00
|
|
|
it 'whitelist should reject some emails based on the email_domains_whitelist site setting' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_whitelist = 'vaynermedia.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'notgood@mailinator.com')).not_to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'sbauch@vaynermedia.com')).to be_valid
|
2013-03-18 15:48:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should reject some emails based on the email_domains_whitelist site setting when whitelisting multiple domains' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_whitelist = 'vaynermedia.com|gmail.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'notgood@mailinator.com')).not_to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'notgood@trashmail.net')).not_to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'mailinator.com@gmail.com')).to be_valid
|
|
|
|
expect(Fabricate.build(:user, email: 'mailinator.com@vaynermedia.com')).to be_valid
|
2013-03-18 15:48:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should accept some emails based on the email_domains_whitelist site setting ignoring case' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_whitelist = 'vaynermedia.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'good@VAYNERMEDIA.COM')).to be_valid
|
2013-03-18 15:48:56 -04:00
|
|
|
end
|
|
|
|
|
2015-01-29 12:52:59 -05:00
|
|
|
it 'whitelist should accept developer emails' do
|
|
|
|
Rails.configuration.stubs(:developer_emails).returns('developer@discourse.org')
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_whitelist = 'awesome.org'
|
2015-01-29 12:52:59 -05:00
|
|
|
expect(Fabricate.build(:user, email: 'developer@discourse.org')).to be_valid
|
|
|
|
end
|
|
|
|
|
2013-03-18 15:48:56 -04:00
|
|
|
it 'email whitelist should not be used to validate existing records' do
|
|
|
|
u = Fabricate(:user, email: 'in_before_whitelisted@fakemail.com')
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_blacklist = 'vaynermedia.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(u).to be_valid
|
2013-03-18 15:48:56 -04:00
|
|
|
end
|
|
|
|
|
2013-03-31 12:51:13 -04:00
|
|
|
it 'email whitelist should be used when email is being changed' do
|
2016-07-04 10:05:01 -04:00
|
|
|
SiteSetting.email_domains_whitelist = 'vaynermedia.com'
|
2017-07-31 18:27:29 -04:00
|
|
|
u = Fabricate(:user_single_email, email: 'good@vaynermedia.com')
|
2013-03-18 15:48:56 -04:00
|
|
|
u.email = 'nope@mailinator.com'
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(u).not_to be_valid
|
2013-02-20 16:24:52 -05:00
|
|
|
end
|
2016-07-04 10:05:01 -04:00
|
|
|
|
|
|
|
it "doesn't validate email address for staged users" do
|
|
|
|
SiteSetting.email_domains_whitelist = "foo.com"
|
|
|
|
SiteSetting.email_domains_blacklist = "bar.com"
|
2017-08-08 22:56:08 -04:00
|
|
|
|
|
|
|
user = Fabricate.build(:user, staged: true, email: "foo@bar.com")
|
|
|
|
|
|
|
|
expect(user.save).to eq(true)
|
2016-07-04 10:05:01 -04:00
|
|
|
end
|
2013-02-13 12:28:23 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
describe 'passwords' do
|
2015-06-05 13:09:02 -04:00
|
|
|
|
|
|
|
it "should not have an active account with a good password" do
|
2013-07-11 18:47:06 -04:00
|
|
|
@user = Fabricate.build(:user, active: false)
|
2013-02-05 21:44:49 -05:00
|
|
|
@user.password = "ilovepasta"
|
2013-02-05 14:16:51 -05:00
|
|
|
@user.save!
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(@user.active).to eq(false)
|
2015-06-05 13:09:02 -04:00
|
|
|
expect(@user.confirm_password?("ilovepasta")).to eq(true)
|
|
|
|
|
2015-06-05 13:50:06 -04:00
|
|
|
email_token = @user.email_tokens.create(email: 'pasta@delicious.com')
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
UserAuthToken.generate!(user_id: @user.id)
|
|
|
|
|
2015-06-05 13:09:02 -04:00
|
|
|
@user.password = "passwordT"
|
|
|
|
@user.save!
|
|
|
|
|
|
|
|
# must expire old token on password change
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(@user.user_auth_tokens.count).to eq(0)
|
2015-06-05 13:50:06 -04:00
|
|
|
|
|
|
|
email_token.reload
|
|
|
|
expect(email_token.expired).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "previous_visit_at" do
|
2013-06-27 17:23:06 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2013-06-28 01:38:47 -04:00
|
|
|
let!(:first_visit_date) { Time.zone.now }
|
|
|
|
let!(:second_visit_date) { 2.hours.from_now }
|
|
|
|
let!(:third_visit_date) { 5.hours.from_now }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
before do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.active_user_rate_limit_secs = 0
|
|
|
|
SiteSetting.previous_visit_timeout_hours = 1
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-28 01:38:47 -04:00
|
|
|
it "should act correctly" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.previous_visit_at).to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-28 01:38:47 -04:00
|
|
|
# first visit
|
|
|
|
user.update_last_seen!(first_visit_date)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.previous_visit_at).to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-28 02:07:36 -04:00
|
|
|
# updated same time
|
|
|
|
user.update_last_seen!(first_visit_date)
|
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.previous_visit_at).to eq(nil)
|
2013-06-28 02:07:36 -04:00
|
|
|
|
2013-06-28 01:38:47 -04:00
|
|
|
# second visit
|
|
|
|
user.update_last_seen!(second_visit_date)
|
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.previous_visit_at).to be_within_one_second_of(first_visit_date)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-28 01:38:47 -04:00
|
|
|
# third visit
|
|
|
|
user.update_last_seen!(third_visit_date)
|
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.previous_visit_at).to be_within_one_second_of(second_visit_date)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-05-21 09:17:54 -04:00
|
|
|
describe "update_last_seen!" do
|
|
|
|
let (:user) { Fabricate(:user) }
|
|
|
|
let!(:first_visit_date) { Time.zone.now }
|
|
|
|
let!(:second_visit_date) { 2.hours.from_now }
|
|
|
|
|
|
|
|
it "should update the last seen value" do
|
|
|
|
expect(user.last_seen_at).to eq nil
|
|
|
|
user.update_last_seen!(first_visit_date)
|
|
|
|
expect(user.reload.last_seen_at).to be_within_one_second_of(first_visit_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should update the first seen value if it doesn't exist" do
|
|
|
|
user.update_last_seen!(first_visit_date)
|
|
|
|
expect(user.reload.first_seen_at).to be_within_one_second_of(first_visit_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not update the first seen value if it doesn't exist" do
|
|
|
|
user.update_last_seen!(first_visit_date)
|
|
|
|
user.update_last_seen!(second_visit_date)
|
|
|
|
expect(user.reload.first_seen_at).to be_within_one_second_of(first_visit_date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
describe "last_seen_at" do
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2013-02-05 21:44:49 -05:00
|
|
|
|
|
|
|
it "should have a blank last seen on creation" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.last_seen_at).to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should have 0 for days_visited" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat.days_visited).to eq(0)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with no previous values' do
|
2013-06-27 17:23:06 -04:00
|
|
|
let!(:date) { Time.zone.now }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
before do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time date
|
2013-02-05 14:16:51 -05:00
|
|
|
user.update_last_seen!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates last_seen_at" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.last_seen_at).to be_within_one_second_of(date)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should have 0 for days_visited" do
|
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat.days_visited).to eq(1)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "should log a user_visit with the date" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_visits.first.visited_at).to eq(date.to_date)
|
2013-02-05 21:44:49 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
context "called twice" do
|
|
|
|
|
|
|
|
before do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time date
|
2013-02-05 14:16:51 -05:00
|
|
|
user.update_last_seen!
|
|
|
|
user.update_last_seen!
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't increase days_visited twice" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_stat.days_visited).to eq(1)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "after 3 days" do
|
|
|
|
let!(:future_date) { 3.days.from_now }
|
|
|
|
|
|
|
|
before do
|
2017-07-24 09:17:42 -04:00
|
|
|
freeze_time future_date
|
2013-02-05 14:16:51 -05:00
|
|
|
user.update_last_seen!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should log a second visited_at record when we log an update later" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.user_visits.count).to eq(2)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
describe 'email_confirmed?' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
context 'when email has not been confirmed yet' do
|
|
|
|
it 'should return false' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.email_confirmed?).to eq(false)
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when email has been confirmed' do
|
|
|
|
it 'should return true' do
|
2014-05-06 09:41:59 -04:00
|
|
|
token = user.email_tokens.find_by(email: user.email)
|
2013-02-11 11:18:26 -05:00
|
|
|
EmailToken.confirm(token.token)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.email_confirmed?).to eq(true)
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user has no email tokens for some reason' do
|
|
|
|
it 'should return false' do
|
2017-07-27 21:20:09 -04:00
|
|
|
user.email_tokens.each { |t| t.destroy }
|
2013-02-11 11:18:26 -05:00
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.email_confirmed?).to eq(true)
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
describe "flag_linked_posts_as_spam" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let!(:admin) { Fabricate(:admin) }
|
|
|
|
let!(:post) { PostCreator.new(user, title: "this topic contains spam", raw: "this post has a link: http://discourse.org").create }
|
|
|
|
let!(:another_post) { PostCreator.new(user, title: "this topic also contains spam", raw: "this post has a link: http://discourse.org/asdfa").create }
|
|
|
|
let!(:post_without_link) { PostCreator.new(user, title: "this topic shouldn't be spam", raw: "this post has no links in it.").create }
|
|
|
|
|
|
|
|
it "has flagged all the user's posts as spam" do
|
|
|
|
user.flag_linked_posts_as_spam
|
|
|
|
|
|
|
|
post.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(post.spam_count).to eq(1)
|
2013-05-10 16:58:23 -04:00
|
|
|
|
|
|
|
another_post.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(another_post.spam_count).to eq(1)
|
2013-05-10 16:58:23 -04:00
|
|
|
|
|
|
|
post_without_link.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(post_without_link.spam_count).to eq(0)
|
2013-05-10 16:58:23 -04:00
|
|
|
|
|
|
|
# It doesn't raise an exception if called again
|
|
|
|
user.flag_linked_posts_as_spam
|
2015-10-16 15:16:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not flags post as spam if the previous flag for that post was disagreed" do
|
|
|
|
user.flag_linked_posts_as_spam
|
2013-05-10 16:58:23 -04:00
|
|
|
|
2015-10-16 15:16:44 -04:00
|
|
|
post.reload
|
|
|
|
expect(post.spam_count).to eq(1)
|
|
|
|
|
|
|
|
PostAction.clear_flags!(post, admin)
|
|
|
|
user.flag_linked_posts_as_spam
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
expect(post.spam_count).to eq(0)
|
2013-05-10 16:58:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-02-25 02:42:42 -05:00
|
|
|
|
2013-02-21 13:20:00 -05:00
|
|
|
describe '#readable_name' do
|
|
|
|
context 'when name is missing' do
|
|
|
|
it 'returns just the username' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate(:user, username: 'foo', name: nil).readable_name).to eq('foo')
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
context 'when name and username are identical' do
|
|
|
|
it 'returns just the username' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate(:user, username: 'foo', name: 'foo').readable_name).to eq('foo')
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
context 'when name and username are not identical' do
|
|
|
|
it 'returns the name and username' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Fabricate(:user, username: 'foo', name: 'Bar Baz').readable_name).to eq('Bar Baz (foo)')
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-16 14:40:30 -04:00
|
|
|
describe '.find_by_username_or_email' do
|
2013-10-28 01:29:07 -04:00
|
|
|
it 'finds users' do
|
|
|
|
bob = Fabricate(:user, username: 'bob', email: 'bob@example.com')
|
|
|
|
found_user = User.find_by_username_or_email('Bob')
|
2013-08-16 14:40:30 -04:00
|
|
|
expect(found_user).to eq bob
|
|
|
|
|
2013-10-28 01:29:07 -04:00
|
|
|
found_user = User.find_by_username_or_email('bob@Example.com')
|
2013-08-16 14:40:30 -04:00
|
|
|
expect(found_user).to eq bob
|
|
|
|
|
2013-10-28 01:29:07 -04:00
|
|
|
found_user = User.find_by_username_or_email('Bob@Example.com')
|
2014-07-14 10:16:24 -04:00
|
|
|
expect(found_user).to eq bob
|
2013-08-16 14:40:30 -04:00
|
|
|
|
2013-10-28 01:29:07 -04:00
|
|
|
found_user = User.find_by_username_or_email('bob1')
|
2014-10-31 13:44:26 -04:00
|
|
|
expect(found_user).to be_nil
|
2013-06-18 20:31:19 -04:00
|
|
|
|
2013-10-28 01:29:07 -04:00
|
|
|
found_user = User.find_by_email('bob@Example.com')
|
|
|
|
expect(found_user).to eq bob
|
2014-07-14 10:16:24 -04:00
|
|
|
|
|
|
|
found_user = User.find_by_email('BOB@Example.com')
|
|
|
|
expect(found_user).to eq bob
|
2013-08-16 14:40:30 -04:00
|
|
|
|
2013-10-28 01:29:07 -04:00
|
|
|
found_user = User.find_by_email('bob')
|
2014-10-31 13:44:26 -04:00
|
|
|
expect(found_user).to be_nil
|
2013-08-16 14:40:30 -04:00
|
|
|
|
2013-10-28 01:29:07 -04:00
|
|
|
found_user = User.find_by_username('bOb')
|
|
|
|
expect(found_user).to eq bob
|
2013-08-16 14:40:30 -04:00
|
|
|
end
|
|
|
|
|
2013-06-18 20:31:19 -04:00
|
|
|
end
|
2013-10-16 05:28:18 -04:00
|
|
|
|
2016-06-20 16:38:15 -04:00
|
|
|
describe "#new_user_posting_on_first_day?" do
|
2013-10-16 05:28:18 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def test_user?(opts = {})
|
|
|
|
Fabricate.build(:user, { created_at: Time.zone.now }.merge(opts)).new_user_posting_on_first_day?
|
2013-10-16 05:28:18 -04:00
|
|
|
end
|
|
|
|
|
2016-06-20 16:38:15 -04:00
|
|
|
it "handles when user has never posted" do
|
2015-03-26 01:48:36 -04:00
|
|
|
expect(test_user?).to eq(true)
|
|
|
|
expect(test_user?(moderator: true)).to eq(false)
|
|
|
|
expect(test_user?(trust_level: TrustLevel[2])).to eq(false)
|
2016-06-20 16:38:15 -04:00
|
|
|
expect(test_user?(created_at: 2.days.ago)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is true for a user who posted less than 24 hours ago but was created over 1 day ago" do
|
|
|
|
u = Fabricate(:user, created_at: 28.hours.ago)
|
|
|
|
u.user_stat.first_post_created_at = 1.hour.ago
|
|
|
|
expect(u.new_user_posting_on_first_day?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if first post was more than 24 hours ago" do
|
|
|
|
u = Fabricate(:user, created_at: 28.hours.ago)
|
|
|
|
u.user_stat.first_post_created_at = 25.hours.ago
|
|
|
|
expect(u.new_user_posting_on_first_day?).to eq(false)
|
2013-10-16 05:28:18 -04:00
|
|
|
end
|
2013-10-19 05:48:11 -04:00
|
|
|
end
|
|
|
|
|
2013-10-22 15:53:08 -04:00
|
|
|
describe 'api keys' do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
let(:other_admin) { Fabricate(:admin) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
describe '.generate_api_key' do
|
|
|
|
|
|
|
|
it "generates an api key when none exists, and regenerates when it does" do
|
|
|
|
expect(user.api_key).to be_blank
|
|
|
|
|
|
|
|
# Generate a key
|
|
|
|
api_key = user.generate_api_key(admin)
|
|
|
|
expect(api_key.user).to eq(user)
|
|
|
|
expect(api_key.key).to be_present
|
|
|
|
expect(api_key.created_by).to eq(admin)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.api_key).to eq(api_key)
|
|
|
|
|
|
|
|
# Regenerate a key. Keeps the same record, updates the key
|
|
|
|
new_key = user.generate_api_key(other_admin)
|
|
|
|
expect(new_key.id).to eq(api_key.id)
|
|
|
|
expect(new_key.key).to_not eq(api_key.key)
|
|
|
|
expect(new_key.created_by).to eq(other_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.revoke_api_key' do
|
|
|
|
|
|
|
|
it "revokes an api key when exists" do
|
|
|
|
expect(user.api_key).to be_blank
|
|
|
|
|
|
|
|
# Revoke nothing does nothing
|
|
|
|
user.revoke_api_key
|
|
|
|
user.reload
|
|
|
|
expect(user.api_key).to be_blank
|
|
|
|
|
|
|
|
# When a key is present it is removed
|
|
|
|
user.generate_api_key(admin)
|
|
|
|
user.reload
|
|
|
|
user.revoke_api_key
|
|
|
|
user.reload
|
|
|
|
expect(user.api_key).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-01-02 12:57:40 -05:00
|
|
|
describe "posted too much in topic" do
|
2014-09-05 01:20:39 -04:00
|
|
|
let!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }
|
2014-01-02 12:57:40 -05:00
|
|
|
let!(:topic) { Fabricate(:post).topic }
|
|
|
|
|
|
|
|
before do
|
|
|
|
# To make testing easier, say 1 reply is too much
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.newuser_max_replies_per_topic = 1
|
2016-12-21 23:03:40 -05:00
|
|
|
UserActionCreator.enable
|
2014-01-02 12:57:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "for a user who didn't create the topic" do
|
|
|
|
let!(:post) { Fabricate(:post, topic: topic, user: user) }
|
|
|
|
|
|
|
|
it "does not return true for staff" do
|
|
|
|
user.stubs(:staff?).returns(true)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.posted_too_much_in_topic?(topic.id)).to eq(false)
|
2014-01-02 12:57:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when the user has posted too much" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.posted_too_much_in_topic?(topic.id)).to eq(true)
|
2014-01-02 12:57:40 -05:00
|
|
|
end
|
2014-04-29 12:59:14 -04:00
|
|
|
|
|
|
|
context "with a reply" do
|
|
|
|
before do
|
2016-12-21 23:03:40 -05:00
|
|
|
PostCreator.new(Fabricate(:user),
|
|
|
|
raw: 'whatever this is a raw post',
|
|
|
|
topic_id: topic.id,
|
|
|
|
reply_to_post_number: post.post_number).create
|
2014-04-29 12:59:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "resets the `posted_too_much` threshold" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.posted_too_much_in_topic?(topic.id)).to eq(false)
|
2014-04-29 12:59:14 -04:00
|
|
|
end
|
|
|
|
end
|
2014-01-02 12:57:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for a user who created the topic" do
|
|
|
|
topic_user = topic.user
|
2014-09-05 01:20:39 -04:00
|
|
|
topic_user.trust_level = TrustLevel[0]
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(topic.user.posted_too_much_in_topic?(topic.id)).to eq(false)
|
2014-01-02 12:57:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
describe "#find_email" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user, email: "bob@example.com") }
|
|
|
|
|
|
|
|
context "when email is exists in the email logs" do
|
|
|
|
before { user.stubs(:last_sent_email_address).returns("bob@lastemail.com") }
|
|
|
|
|
|
|
|
it "returns email from the logs" do
|
|
|
|
expect(user.find_email).to eq("bob@lastemail.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when email does not exist in the email logs" do
|
|
|
|
before { user.stubs(:last_sent_email_address).returns(nil) }
|
|
|
|
|
|
|
|
it "fetches the user's email" do
|
|
|
|
expect(user.find_email).to eq(user.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-22 13:18:45 -05:00
|
|
|
describe "#gravatar_template" do
|
|
|
|
|
|
|
|
it "returns a gravatar based template" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(User.gravatar_template("em@il.com")).to eq("//www.gravatar.com/avatar/6dc2fde946483a1d8a84b89345a1b638.png?s={size}&r=pg&d=identicon")
|
2013-11-22 13:18:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".small_avatar_url" do
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
let(:user) { build(:user, username: 'Sam') }
|
2013-11-22 13:18:45 -05:00
|
|
|
|
|
|
|
it "returns a 45-pixel-wide avatar" do
|
2015-09-14 23:25:15 -04:00
|
|
|
SiteSetting.external_system_avatars_enabled = false
|
2015-04-19 23:07:12 -04:00
|
|
|
expect(user.small_avatar_url).to eq("//test.localhost/letter_avatar/sam/45/#{LetterAvatar.version}.png")
|
2015-09-14 23:25:15 -04:00
|
|
|
|
|
|
|
SiteSetting.external_system_avatars_enabled = true
|
2015-09-28 07:41:57 -04:00
|
|
|
expect(user.small_avatar_url).to eq("//test.localhost/letter_avatar_proxy/v2/letter/s/5f9b8f/45.png")
|
2013-11-22 13:18:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
describe ".avatar_template_url" do
|
2013-11-22 13:18:45 -05:00
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
let(:user) { build(:user, uploaded_avatar_id: 99, username: 'Sam') }
|
2013-11-22 13:18:45 -05:00
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
it "returns a schemaless avatar template with correct id" do
|
2015-05-29 12:51:17 -04:00
|
|
|
expect(user.avatar_template_url).to eq("//test.localhost/user_avatar/test.localhost/sam/{size}/99_#{OptimizedImage::VERSION}.png")
|
2013-11-22 13:18:45 -05:00
|
|
|
end
|
|
|
|
|
2014-01-07 11:45:06 -05:00
|
|
|
it "returns a schemaless cdn-based avatar template" do
|
|
|
|
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
|
2015-05-29 12:51:17 -04:00
|
|
|
expect(user.avatar_template_url).to eq("//my.cdn.com/user_avatar/test.localhost/sam/{size}/99_#{OptimizedImage::VERSION}.png")
|
2013-11-22 13:18:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-01-24 15:19:20 -05:00
|
|
|
describe "update_posts_read!" do
|
|
|
|
context "with a UserVisit record" do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
let!(:now) { Time.zone.now }
|
|
|
|
before { user.update_last_seen!(now) }
|
|
|
|
|
|
|
|
it "with existing UserVisit record, increments the posts_read value" do
|
|
|
|
expect {
|
|
|
|
user_visit = user.update_posts_read!(2)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user_visit.posts_read).to eq(2)
|
2014-01-24 15:19:20 -05:00
|
|
|
}.to_not change { UserVisit.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with no existing UserVisit record, creates a new UserVisit record and increments the posts_read count" do
|
|
|
|
expect {
|
2015-07-07 12:31:07 -04:00
|
|
|
user_visit = user.update_posts_read!(3, at: 5.days.ago)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user_visit.posts_read).to eq(3)
|
2014-01-24 15:19:20 -05:00
|
|
|
}.to change { UserVisit.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-10 16:59:36 -05:00
|
|
|
describe "primary_group_id" do
|
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "has no primary_group_id by default" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.primary_group_id).to eq(nil)
|
2014-02-10 16:59:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user has a group" do
|
|
|
|
let!(:group) { Fabricate(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.usernames = user.username
|
|
|
|
group.save
|
|
|
|
user.primary_group_id = group.id
|
|
|
|
user.save
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow us to use it as a primary group" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.primary_group_id).to eq(group.id)
|
2014-02-10 16:59:36 -05:00
|
|
|
|
|
|
|
# If we remove the user from the group
|
|
|
|
group.usernames = ""
|
|
|
|
group.save
|
|
|
|
|
|
|
|
# It should unset it from the primary_group_id
|
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.primary_group_id).to eq(nil)
|
2014-02-10 16:59:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-07 12:58:53 -05:00
|
|
|
|
2014-05-26 05:46:43 -04:00
|
|
|
describe "automatic avatar creation" do
|
|
|
|
it "sets a system avatar for new users" do
|
2015-09-14 23:25:15 -04:00
|
|
|
SiteSetting.external_system_avatars_enabled = false
|
|
|
|
|
2014-05-26 05:46:43 -04:00
|
|
|
u = User.create!(username: "bob", email: "bob@bob.com")
|
|
|
|
u.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(u.uploaded_avatar_id).to eq(nil)
|
2015-04-19 23:07:12 -04:00
|
|
|
expect(u.avatar_template).to eq("/letter_avatar/bob/{size}/#{LetterAvatar.version}.png")
|
2014-05-26 05:46:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-21 23:52:13 -04:00
|
|
|
describe "custom fields" do
|
|
|
|
it "allows modification of custom fields" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.custom_fields["a"]).to eq(nil)
|
2014-04-21 23:52:13 -04:00
|
|
|
|
|
|
|
user.custom_fields["bob"] = "marley"
|
|
|
|
user.custom_fields["jack"] = "black"
|
|
|
|
user.save
|
|
|
|
|
|
|
|
user = User.find(user.id)
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.custom_fields["bob"]).to eq("marley")
|
|
|
|
expect(user.custom_fields["jack"]).to eq("black")
|
2014-04-21 23:52:13 -04:00
|
|
|
|
|
|
|
user.custom_fields.delete("bob")
|
|
|
|
user.custom_fields["jack"] = "jill"
|
|
|
|
|
|
|
|
user.save
|
|
|
|
user = User.find(user.id)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(user.custom_fields).to eq("jack" => "jill")
|
2014-04-21 23:52:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-28 02:54:21 -04:00
|
|
|
describe "refresh_avatar" do
|
2014-12-15 16:10:27 -05:00
|
|
|
it "enqueues the update_gravatar job when automatically downloading gravatars" do
|
2014-05-28 02:54:21 -04:00
|
|
|
SiteSetting.automatically_download_gravatars = true
|
2017-04-15 00:11:02 -04:00
|
|
|
SiteSetting.queue_jobs = true
|
2014-05-28 02:54:21 -04:00
|
|
|
|
2014-12-15 16:10:27 -05:00
|
|
|
user = Fabricate(:user)
|
2014-05-28 02:54:21 -04:00
|
|
|
|
2014-12-15 16:10:27 -05:00
|
|
|
Jobs.expects(:enqueue).with(:update_gravatar, anything)
|
2014-05-30 00:17:35 -04:00
|
|
|
|
|
|
|
user.refresh_avatar
|
2014-05-28 02:54:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-03 00:36:25 -05:00
|
|
|
describe "#purge_unactivated" do
|
2014-08-13 14:13:41 -04:00
|
|
|
let!(:user) { Fabricate(:user) }
|
|
|
|
let!(:inactive) { Fabricate(:user, active: false) }
|
|
|
|
let!(:inactive_old) { Fabricate(:user, active: false, created_at: 1.month.ago) }
|
|
|
|
|
2014-12-03 00:36:25 -05:00
|
|
|
it 'should only remove old, unactivated users' do
|
|
|
|
User.purge_unactivated
|
2014-08-13 14:13:41 -04:00
|
|
|
all_users = User.all
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(all_users.include?(user)).to eq(true)
|
|
|
|
expect(all_users.include?(inactive)).to eq(true)
|
|
|
|
expect(all_users.include?(inactive_old)).to eq(false)
|
2014-08-13 14:13:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-11 15:22:11 -04:00
|
|
|
describe "hash_passwords" do
|
|
|
|
|
|
|
|
let(:too_long) { "x" * (User.max_password_length + 1) }
|
|
|
|
|
|
|
|
def hash(password, salt)
|
|
|
|
User.new.send(:hash_password, password, salt)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the same hash for the same password and salt" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(hash('poutine', 'gravy')).to eq(hash('poutine', 'gravy'))
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a different hash for the same salt and different password" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(hash('poutine', 'gravy')).not_to eq(hash('fries', 'gravy'))
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a different hash for the same password and different salt" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(hash('poutine', 'gravy')).not_to eq(hash('poutine', 'cheese'))
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when passwords are too long" do
|
2016-05-29 23:38:04 -04:00
|
|
|
expect { hash(too_long, 'gravy') }.to raise_error(StandardError)
|
2014-09-11 15:22:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-01-23 12:25:43 -05:00
|
|
|
describe "automatic group membership" do
|
|
|
|
|
2017-02-06 11:49:27 -05:00
|
|
|
let!(:group) {
|
|
|
|
Fabricate(:group,
|
|
|
|
automatic_membership_email_domains: "bar.com|wat.com",
|
|
|
|
grant_trust_level: 1,
|
|
|
|
title: "bars and wats",
|
|
|
|
primary_group: true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
it "doesn't automatically add staged users" do
|
|
|
|
staged_user = Fabricate(:user, active: true, staged: true, email: "wat@wat.com")
|
2017-06-14 13:20:18 -04:00
|
|
|
EmailToken.confirm(staged_user.email_tokens.last.token)
|
2017-02-06 11:49:27 -05:00
|
|
|
group.reload
|
|
|
|
expect(group.users.include?(staged_user)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2015-01-23 12:25:43 -05:00
|
|
|
it "is automatically added to a group when the email matches" do
|
2017-02-06 11:49:27 -05:00
|
|
|
user = Fabricate(:user, active: true, email: "foo@bar.com")
|
2017-06-14 13:20:18 -04:00
|
|
|
EmailToken.confirm(user.email_tokens.last.token)
|
2015-01-23 12:25:43 -05:00
|
|
|
group.reload
|
|
|
|
expect(group.users.include?(user)).to eq(true)
|
2016-12-11 10:36:15 -05:00
|
|
|
|
|
|
|
group_history = GroupHistory.last
|
|
|
|
|
|
|
|
expect(group_history.action).to eq(GroupHistory.actions[:add_user_to_group])
|
|
|
|
expect(group_history.acting_user).to eq(Discourse.system_user)
|
|
|
|
expect(group_history.target_user).to eq(user)
|
2015-01-23 12:25:43 -05:00
|
|
|
end
|
|
|
|
|
2017-01-16 03:40:05 -05:00
|
|
|
it "get attributes from the group" do
|
|
|
|
user = Fabricate.build(:user,
|
2017-02-06 11:49:27 -05:00
|
|
|
active: true,
|
2017-01-16 03:40:05 -05:00
|
|
|
trust_level: 0,
|
|
|
|
email: "foo@bar.com",
|
|
|
|
password: "strongpassword4Uguys"
|
|
|
|
)
|
|
|
|
|
|
|
|
user.password_required!
|
|
|
|
user.save!
|
2017-06-14 13:20:18 -04:00
|
|
|
EmailToken.confirm(user.email_tokens.last.token)
|
2017-01-16 03:40:05 -05:00
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.title).to eq("bars and wats")
|
|
|
|
expect(user.trust_level).to eq(1)
|
|
|
|
expect(user.trust_level_locked).to eq(true)
|
|
|
|
end
|
2015-01-23 12:25:43 -05:00
|
|
|
end
|
|
|
|
|
2015-02-19 12:11:07 -05:00
|
|
|
describe "number_of_flags_given" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
|
|
|
it "doesn't count disagreed flags" do
|
|
|
|
post_agreed = Fabricate(:post)
|
|
|
|
PostAction.act(user, post_agreed, PostActionType.types[:off_topic])
|
|
|
|
PostAction.agree_flags!(post_agreed, moderator)
|
|
|
|
|
|
|
|
post_deferred = Fabricate(:post)
|
|
|
|
PostAction.act(user, post_deferred, PostActionType.types[:inappropriate])
|
|
|
|
PostAction.defer_flags!(post_deferred, moderator)
|
|
|
|
|
|
|
|
post_disagreed = Fabricate(:post)
|
|
|
|
PostAction.act(user, post_disagreed, PostActionType.types[:spam])
|
|
|
|
PostAction.clear_flags!(post_disagreed, moderator)
|
|
|
|
|
|
|
|
expect(user.number_of_flags_given).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-03-11 13:07:47 -04:00
|
|
|
describe "number_of_deleted_posts" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user, id: 2) }
|
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
|
|
|
it "counts all the posts" do
|
|
|
|
# at least 1 "unchanged" post
|
|
|
|
Fabricate(:post, user: user)
|
|
|
|
|
|
|
|
post_deleted_by_moderator = Fabricate(:post, user: user)
|
|
|
|
PostDestroyer.new(moderator, post_deleted_by_moderator).destroy
|
|
|
|
|
|
|
|
post_deleted_by_user = Fabricate(:post, user: user, post_number: 2)
|
|
|
|
PostDestroyer.new(user, post_deleted_by_user).destroy
|
|
|
|
|
|
|
|
# fake stub deletion
|
|
|
|
post_deleted_by_user.update_columns(updated_at: 2.days.ago)
|
|
|
|
PostDestroyer.destroy_stubs
|
|
|
|
|
|
|
|
expect(user.number_of_deleted_posts).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-03-26 01:04:32 -04:00
|
|
|
describe "new_user?" do
|
|
|
|
it "correctly detects new user" do
|
|
|
|
user = User.new(created_at: Time.now, trust_level: TrustLevel[0])
|
|
|
|
|
|
|
|
expect(user.new_user?).to eq(true)
|
|
|
|
|
|
|
|
user.trust_level = TrustLevel[1]
|
|
|
|
|
|
|
|
expect(user.new_user?).to eq(true)
|
|
|
|
|
|
|
|
user.trust_level = TrustLevel[2]
|
|
|
|
|
|
|
|
expect(user.new_user?).to eq(false)
|
|
|
|
|
|
|
|
user.trust_level = TrustLevel[0]
|
|
|
|
user.moderator = true
|
|
|
|
|
|
|
|
expect(user.new_user?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-21 14:39:21 -04:00
|
|
|
context "when user preferences are overriden" do
|
|
|
|
|
|
|
|
before do
|
2016-03-02 15:26:27 -05:00
|
|
|
SiteSetting.default_email_digest_frequency = 1440 # daily
|
2016-02-16 23:46:19 -05:00
|
|
|
SiteSetting.default_email_private_messages = false
|
|
|
|
SiteSetting.default_email_direct = false
|
|
|
|
SiteSetting.default_email_mailing_list_mode = true
|
|
|
|
SiteSetting.default_email_always = true
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
SiteSetting.default_other_new_topic_duration_minutes = -1 # not viewed
|
|
|
|
SiteSetting.default_other_auto_track_topics_after_msecs = 0 # immediately
|
2016-09-30 12:36:43 -04:00
|
|
|
SiteSetting.default_other_notification_level_when_replying = 3 # immediately
|
2016-02-16 23:46:19 -05:00
|
|
|
SiteSetting.default_other_external_links_in_new_tab = true
|
|
|
|
SiteSetting.default_other_enable_quoting = false
|
|
|
|
SiteSetting.default_other_dynamic_favicon = true
|
|
|
|
SiteSetting.default_other_disable_jump_reply = true
|
2015-08-21 14:39:21 -04:00
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
SiteSetting.default_topics_automatic_unpin = false
|
2015-11-17 12:21:40 -05:00
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
SiteSetting.default_categories_watching = "1"
|
|
|
|
SiteSetting.default_categories_tracking = "2"
|
|
|
|
SiteSetting.default_categories_muted = "3"
|
2016-11-09 13:37:54 -05:00
|
|
|
SiteSetting.default_categories_watching_first_post = "4"
|
2015-08-21 14:39:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "has overriden preferences" do
|
|
|
|
user = Fabricate(:user)
|
2016-02-16 23:46:19 -05:00
|
|
|
options = user.user_option
|
|
|
|
expect(options.email_always).to eq(true)
|
|
|
|
expect(options.mailing_list_mode).to eq(true)
|
2016-03-02 15:26:27 -05:00
|
|
|
expect(options.digest_after_minutes).to eq(1440)
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(options.email_private_messages).to eq(false)
|
|
|
|
expect(options.external_links_in_new_tab).to eq(true)
|
|
|
|
expect(options.enable_quoting).to eq(false)
|
|
|
|
expect(options.dynamic_favicon).to eq(true)
|
|
|
|
expect(options.disable_jump_reply).to eq(true)
|
|
|
|
expect(options.automatically_unpin_topics).to eq(false)
|
|
|
|
expect(options.email_direct).to eq(false)
|
2016-02-18 00:57:22 -05:00
|
|
|
expect(options.new_topic_duration_minutes).to eq(-1)
|
|
|
|
expect(options.auto_track_topics_after_msecs).to eq(0)
|
2016-09-30 12:36:43 -04:00
|
|
|
expect(options.notification_level_when_replying).to eq(3)
|
2015-11-17 12:21:40 -05:00
|
|
|
|
2015-08-21 14:39:21 -04:00
|
|
|
expect(CategoryUser.lookup(user, :watching).pluck(:category_id)).to eq([1])
|
|
|
|
expect(CategoryUser.lookup(user, :tracking).pluck(:category_id)).to eq([2])
|
|
|
|
expect(CategoryUser.lookup(user, :muted).pluck(:category_id)).to eq([3])
|
2016-11-09 13:37:54 -05:00
|
|
|
expect(CategoryUser.lookup(user, :watching_first_post).pluck(:category_id)).to eq([4])
|
2015-08-21 14:39:21 -04:00
|
|
|
end
|
2016-06-14 10:45:47 -04:00
|
|
|
|
|
|
|
it "does not set category preferences for staged users" do
|
|
|
|
user = Fabricate(:user, staged: true)
|
|
|
|
expect(CategoryUser.lookup(user, :watching).pluck(:category_id)).to eq([])
|
|
|
|
expect(CategoryUser.lookup(user, :tracking).pluck(:category_id)).to eq([])
|
|
|
|
expect(CategoryUser.lookup(user, :muted).pluck(:category_id)).to eq([])
|
2016-11-09 13:37:54 -05:00
|
|
|
expect(CategoryUser.lookup(user, :watching_first_post).pluck(:category_id)).to eq([])
|
2016-06-14 10:45:47 -04:00
|
|
|
end
|
2016-02-16 23:46:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context UserOption do
|
|
|
|
|
|
|
|
it "Creates a UserOption row when a user record is created and destroys once done" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect(user.user_option.email_always).to eq(false)
|
|
|
|
|
|
|
|
user_id = user.id
|
|
|
|
user.destroy!
|
|
|
|
expect(UserOption.find_by(user_id: user_id)).to eq(nil)
|
|
|
|
|
|
|
|
end
|
2015-08-21 14:39:21 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-07-04 05:20:30 -04:00
|
|
|
describe "#logged_out" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it 'should publish the right message' do
|
2017-08-24 13:24:52 -04:00
|
|
|
message = MessageBus.track_publish { user.logged_out }.find { |m| m.channel == '/logout' }
|
2016-07-04 05:20:30 -04:00
|
|
|
|
|
|
|
expect(message.channel).to eq('/logout')
|
|
|
|
expect(message.data).to eq(user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-08 03:12:40 -05:00
|
|
|
describe '#read_first_notification?' do
|
2016-11-16 01:17:47 -05:00
|
|
|
let(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }
|
2016-11-16 20:44:00 -05:00
|
|
|
let(:notification) { Fabricate(:private_message_notification) }
|
2016-11-16 03:20:38 -05:00
|
|
|
|
2016-11-16 20:44:00 -05:00
|
|
|
describe 'when first notification has not been seen' do
|
2016-11-08 03:12:40 -05:00
|
|
|
it 'should return the right value' do
|
|
|
|
expect(user.read_first_notification?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-16 20:44:00 -05:00
|
|
|
describe 'when first notification has been seen' do
|
2016-11-08 03:12:40 -05:00
|
|
|
it 'should return the right value' do
|
2016-11-16 20:44:00 -05:00
|
|
|
user.update_attributes!(seen_notification_id: notification.id)
|
2016-11-16 01:17:47 -05:00
|
|
|
expect(user.reload.read_first_notification?).to eq(true)
|
2016-11-08 03:12:40 -05:00
|
|
|
end
|
|
|
|
end
|
2016-11-08 04:00:44 -05:00
|
|
|
|
2017-02-17 10:28:38 -05:00
|
|
|
describe 'when user is trust level 1' do
|
2016-11-16 01:17:47 -05:00
|
|
|
it 'should return the right value' do
|
|
|
|
user.update_attributes!(trust_level: TrustLevel[1])
|
|
|
|
|
2017-02-17 10:28:38 -05:00
|
|
|
expect(user.read_first_notification?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user is trust level 2' do
|
|
|
|
it 'should return the right value' do
|
|
|
|
user.update_attributes!(trust_level: TrustLevel[2])
|
|
|
|
|
2016-11-16 01:17:47 -05:00
|
|
|
expect(user.read_first_notification?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when user is an old user' do
|
|
|
|
it 'should return the right value' do
|
|
|
|
user.update_attributes!(created_at: 1.year.ago)
|
|
|
|
|
|
|
|
expect(user.read_first_notification?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2016-11-08 03:12:40 -05:00
|
|
|
end
|
2016-11-14 15:53:24 -05:00
|
|
|
|
|
|
|
describe "#featured_user_badges" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let!(:user_badge_tl1) { UserBadge.create(badge_id: 1, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
|
|
|
let!(:user_badge_tl2) { UserBadge.create(badge_id: 2, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
|
|
|
|
|
|
|
it 'should display highest trust level badge first' do
|
|
|
|
expect(user.featured_user_badges[0].badge_id).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should display only 1 trust level badge' do
|
|
|
|
expect(user.featured_user_badges.length).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2017-02-13 10:53:45 -05:00
|
|
|
|
|
|
|
describe ".clear_global_notice_if_needed" do
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.has_login_hint = true
|
|
|
|
SiteSetting.global_notice = "some notice"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't clear the login hint when a regular user is saved" do
|
|
|
|
user.save
|
|
|
|
expect(SiteSetting.has_login_hint).to eq(true)
|
|
|
|
expect(SiteSetting.global_notice).to eq("some notice")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't clear the notice when a system user is saved" do
|
|
|
|
Discourse.system_user.save
|
|
|
|
expect(SiteSetting.has_login_hint).to eq(true)
|
|
|
|
expect(SiteSetting.global_notice).to eq("some notice")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "clears the notice when the admin is saved" do
|
|
|
|
admin.save
|
|
|
|
expect(SiteSetting.has_login_hint).to eq(false)
|
|
|
|
expect(SiteSetting.global_notice).to eq("")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-03-11 01:25:09 -05:00
|
|
|
describe '.human_users' do
|
|
|
|
it 'should only return users with a positive primary key' do
|
2017-07-24 09:17:42 -04:00
|
|
|
Fabricate(:user, id: -1979)
|
2017-03-11 01:25:09 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
expect(User.human_users).to eq([user])
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|