2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-09-05 07:22:15 -04:00
|
|
|
module Helpers
|
2016-12-12 20:59:38 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2013-09-05 07:22:15 -04:00
|
|
|
def self.next_seq
|
|
|
|
@next_seq = (@next_seq || 0) + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_in(fabricator = nil)
|
|
|
|
user = Fabricate(fabricator || :user)
|
|
|
|
log_in_user(user)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_in_user(user)
|
2013-10-09 00:10:37 -04:00
|
|
|
provider = Discourse.current_user_provider.new(request.env)
|
|
|
|
provider.log_on_user(user, session, cookies)
|
2019-03-19 08:39:13 -04:00
|
|
|
provider
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_out_user(provider)
|
|
|
|
provider.log_off_user(session, cookies)
|
2013-09-05 07:22:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fixture_file(filename)
|
|
|
|
return '' if filename.blank?
|
|
|
|
file_path = File.expand_path(Rails.root + 'spec/fixtures/' + filename)
|
|
|
|
File.read(file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(*args)
|
|
|
|
Fabricate.build(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_topic(args = {})
|
|
|
|
args[:title] ||= "This is my title #{Helpers.next_seq}"
|
|
|
|
user = args.delete(:user) || Fabricate(:user)
|
|
|
|
guardian = Guardian.new(user)
|
2016-12-05 07:31:43 -05:00
|
|
|
args[:category] = args[:category].id if args[:category].is_a?(Category)
|
2013-09-05 07:22:15 -04:00
|
|
|
TopicCreator.create(user, guardian, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_post(args = {})
|
|
|
|
args[:title] ||= "This is my title #{Helpers.next_seq}"
|
|
|
|
args[:raw] ||= "This is the raw body of my post, it is cool #{Helpers.next_seq}"
|
|
|
|
args[:topic_id] = args[:topic].id if args[:topic]
|
|
|
|
user = args.delete(:user) || Fabricate(:user)
|
2016-12-05 07:31:43 -05:00
|
|
|
args[:category] = args[:category].id if args[:category].is_a?(Category)
|
2016-02-03 02:50:05 -05:00
|
|
|
creator = PostCreator.new(user, args)
|
|
|
|
post = creator.create
|
|
|
|
|
|
|
|
if creator.errors.present?
|
|
|
|
raise StandardError.new(creator.errors.full_messages.join(" "))
|
|
|
|
end
|
|
|
|
|
|
|
|
post
|
2013-09-05 07:22:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_username(length = 10)
|
|
|
|
range = [*'a'..'z']
|
|
|
|
Array.new(length) { range.sample }.join
|
|
|
|
end
|
2013-11-08 14:11:41 -05:00
|
|
|
|
|
|
|
def stub_guardian(user)
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
yield(guardian) if block_given?
|
2018-10-09 10:21:41 -04:00
|
|
|
Guardian.stubs(new: guardian).with(user, anything)
|
2013-11-08 14:11:41 -05:00
|
|
|
end
|
2014-11-11 18:27:34 -05:00
|
|
|
|
2017-08-23 10:41:47 -04:00
|
|
|
def wait_for(on_fail: nil, &blk)
|
2014-11-11 18:27:34 -05:00
|
|
|
i = 0
|
|
|
|
result = false
|
2017-07-25 11:29:12 -04:00
|
|
|
while !result && i < 1000
|
2014-11-11 18:27:34 -05:00
|
|
|
result = blk.call
|
|
|
|
i += 1
|
|
|
|
sleep 0.001
|
|
|
|
end
|
|
|
|
|
2017-08-23 10:41:47 -04:00
|
|
|
on_fail&.call
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(result).to eq(true)
|
2014-11-11 18:27:34 -05:00
|
|
|
end
|
|
|
|
|
2015-12-15 19:31:19 -05:00
|
|
|
def fill_email(mail, from, to, body = nil, subject = nil, cc = nil)
|
|
|
|
result = mail.gsub("FROM", from).gsub("TO", to)
|
|
|
|
result.gsub!(/Hey.*/m, body) if body
|
|
|
|
result.sub!(/We .*/, subject) if subject
|
|
|
|
result.sub!("CC", cc.presence || "")
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2016-04-07 10:21:17 -04:00
|
|
|
def email(email_name)
|
|
|
|
fixture_file("emails/#{email_name}.eml")
|
|
|
|
end
|
2018-04-20 15:25:28 -04:00
|
|
|
|
2020-10-14 13:15:54 -04:00
|
|
|
def create_staff_only_tags(tag_names)
|
|
|
|
create_limited_tags('Staff Tags', Group::AUTO_GROUPS[:staff], tag_names)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_limited_tags(tag_group_name, group_id, tag_names)
|
|
|
|
tag_group = Fabricate(:tag_group, name: tag_group_name)
|
|
|
|
TagGroupPermission.where(
|
2018-04-20 15:25:28 -04:00
|
|
|
tag_group: tag_group,
|
|
|
|
group_id: Group::AUTO_GROUPS[:everyone],
|
2020-10-14 13:15:54 -04:00
|
|
|
permission_type: TagGroupPermission.permission_types[:full]
|
|
|
|
).update(permission_type: TagGroupPermission.permission_types[:readonly])
|
2018-04-20 15:25:28 -04:00
|
|
|
TagGroupPermission.create!(
|
|
|
|
tag_group: tag_group,
|
2020-10-14 13:15:54 -04:00
|
|
|
group_id: group_id,
|
2018-04-20 15:25:28 -04:00
|
|
|
permission_type: TagGroupPermission.permission_types[:full]
|
|
|
|
)
|
|
|
|
tag_names.each do |name|
|
|
|
|
tag_group.tags << (Tag.where(name: name).first || Fabricate(:tag, name: name))
|
|
|
|
end
|
|
|
|
end
|
2019-01-31 23:40:48 -05:00
|
|
|
|
2019-05-06 14:51:51 -04:00
|
|
|
def create_hidden_tags(tag_names)
|
|
|
|
tag_group = Fabricate(:tag_group,
|
|
|
|
name: 'Hidden Tags',
|
|
|
|
permissions: { staff: :full }
|
|
|
|
)
|
|
|
|
tag_names.each do |name|
|
|
|
|
tag_group.tags << (Tag.where(name: name).first || Fabricate(:tag, name: name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-04 13:33:51 -05:00
|
|
|
def sorted_tag_names(tag_records)
|
|
|
|
tag_records.map { |t| t.is_a?(String) ? t : t.name }.sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_same_tag_names(a, b)
|
|
|
|
expect(sorted_tag_names(a)).to eq(sorted_tag_names(b))
|
|
|
|
end
|
|
|
|
|
2020-10-12 12:25:06 -04:00
|
|
|
def capture_output(output_name)
|
2020-03-02 18:03:58 -05:00
|
|
|
if ENV['RAILS_ENABLE_TEST_STDOUT']
|
|
|
|
yield
|
|
|
|
return
|
|
|
|
end
|
2020-10-12 12:25:06 -04:00
|
|
|
|
|
|
|
previous_output = output_name == :stdout ? $stdout : $stderr
|
|
|
|
|
2019-01-31 23:40:48 -05:00
|
|
|
io = StringIO.new
|
2020-10-12 12:25:06 -04:00
|
|
|
output_name == :stdout ? $stdout = io : $stderr = io
|
|
|
|
|
2019-01-31 23:40:48 -05:00
|
|
|
yield
|
|
|
|
io.string
|
|
|
|
ensure
|
2020-10-12 12:25:06 -04:00
|
|
|
output_name == :stdout ? $stdout = previous_output : $stderr = previous_output
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture_stdout(&block)
|
|
|
|
capture_output(:stdout, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture_stderr(&block)
|
|
|
|
capture_output(:stderr, &block)
|
2019-01-31 23:40:48 -05:00
|
|
|
end
|
2019-11-15 00:48:24 -05:00
|
|
|
|
|
|
|
def set_subfolder(f)
|
|
|
|
global_setting :relative_url_root, f
|
|
|
|
old_root = ActionController::Base.config.relative_url_root
|
|
|
|
ActionController::Base.config.relative_url_root = f
|
|
|
|
|
|
|
|
before_next_spec do
|
|
|
|
ActionController::Base.config.relative_url_root = old_root
|
|
|
|
end
|
|
|
|
end
|
2020-01-28 19:11:38 -05:00
|
|
|
|
2020-10-12 12:25:06 -04:00
|
|
|
def setup_git_repo(files)
|
|
|
|
repo_dir = Dir.mktmpdir
|
2020-10-12 13:59:54 -04:00
|
|
|
`cd #{repo_dir} && git init .`
|
2020-10-12 12:25:06 -04:00
|
|
|
`cd #{repo_dir} && git config user.email 'someone@cool.com'`
|
|
|
|
`cd #{repo_dir} && git config user.name 'The Cool One'`
|
|
|
|
`cd #{repo_dir} && git config commit.gpgsign 'false'`
|
|
|
|
files.each do |name, data|
|
|
|
|
FileUtils.mkdir_p(Pathname.new("#{repo_dir}/#{name}").dirname)
|
|
|
|
File.write("#{repo_dir}/#{name}", data)
|
|
|
|
`cd #{repo_dir} && git add #{name}`
|
|
|
|
end
|
|
|
|
`cd #{repo_dir} && git commit -am 'first commit'`
|
|
|
|
repo_dir
|
|
|
|
end
|
|
|
|
|
2020-01-28 19:11:38 -05:00
|
|
|
class StubbedJob
|
|
|
|
def initialize; end
|
|
|
|
def perform(args); end
|
|
|
|
end
|
2013-09-05 07:22:15 -04:00
|
|
|
end
|