discourse/spec/components/post_locker_spec.rb
Daniel Waterworth e219588142 DEV: Prefabrication (test optimization) (#7414)
* Introduced fab!, a helper that creates database state for a group

It's almost identical to let_it_be, except:

 1. It creates a new object for each test by default,
 2. You can disable it using PREFABRICATION=0
2019-05-07 13:12:20 +10:00

52 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
require_dependency 'post_locker'
describe PostLocker do
fab!(:moderator) { Fabricate(:moderator) }
fab!(:post) { Fabricate(:post) }
it "doesn't allow regular users to lock posts" do
expect {
PostLocker.new(post, post.user).lock
}.to raise_error(Discourse::InvalidAccess)
expect(post).not_to be_locked
expect(post.locked_by_id).to be_blank
end
it "doesn't allow regular users to unlock posts" do
PostLocker.new(post, moderator).lock
expect {
PostLocker.new(post, post.user).lock
}.to raise_error(Discourse::InvalidAccess)
expect(post).to be_locked
expect(post.locked_by_id).to eq(moderator.id)
end
it "allows staff to lock and unlock posts" do
expect(post).not_to be_locked
expect(post.locked_by_id).to be_blank
PostLocker.new(post, moderator).lock
expect(post).to be_locked
expect(post.locked_by_id).to eq(moderator.id)
expect(UserHistory.where(
acting_user_id: moderator.id,
action: UserHistory.actions[:post_locked]
).exists?).to eq(true)
PostLocker.new(post, moderator).unlock
expect(post).not_to be_locked
expect(post.locked_by_id).to be_blank
expect(UserHistory.where(
acting_user_id: moderator.id,
action: UserHistory.actions[:post_unlocked]
).exists?).to eq(true)
end
end