2013-09-05 06:22:15 -05:00
|
|
|
module Helpers
|
2016-12-12 19:59:38 -06:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2013-09-05 06:22:15 -05:00
|
|
|
def self.next_seq
|
|
|
|
@next_seq = (@next_seq || 0) + 1
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def log_in(fabricator = nil)
|
2013-09-05 06:22:15 -05:00
|
|
|
user = Fabricate(fabricator || :user)
|
|
|
|
log_in_user(user)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_in_user(user)
|
2013-10-08 23:10:37 -05:00
|
|
|
provider = Discourse.current_user_provider.new(request.env)
|
2017-07-27 20:20:09 -05:00
|
|
|
provider.log_on_user(user, session, cookies)
|
2013-09-05 06:22:15 -05: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
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def create_topic(args = {})
|
2013-09-05 06:22:15 -05:00
|
|
|
args[:title] ||= "This is my title #{Helpers.next_seq}"
|
|
|
|
user = args.delete(:user) || Fabricate(:user)
|
|
|
|
guardian = Guardian.new(user)
|
2016-12-05 06:31:43 -06:00
|
|
|
args[:category] = args[:category].id if args[:category].is_a?(Category)
|
2013-09-05 06:22:15 -05:00
|
|
|
TopicCreator.create(user, guardian, args)
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def create_post(args = {})
|
2013-09-05 06:22:15 -05:00
|
|
|
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 06:31:43 -06:00
|
|
|
args[:category] = args[:category].id if args[:category].is_a?(Category)
|
2016-02-03 01:50:05 -06: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 06:22:15 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def generate_username(length = 10)
|
2013-09-05 06:22:15 -05:00
|
|
|
range = [*'a'..'z']
|
2017-07-27 20:20:09 -05:00
|
|
|
Array.new(length) { range.sample }.join
|
2013-09-05 06:22:15 -05:00
|
|
|
end
|
2013-11-08 13:11:41 -06:00
|
|
|
|
|
|
|
def stub_guardian(user)
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
yield(guardian) if block_given?
|
|
|
|
Guardian.stubs(new: guardian).with(user)
|
|
|
|
end
|
2014-11-11 17:27:34 -06:00
|
|
|
|
2017-08-23 09:41:47 -05:00
|
|
|
def wait_for(on_fail: nil, &blk)
|
2014-11-11 17:27:34 -06:00
|
|
|
i = 0
|
|
|
|
result = false
|
2017-07-25 10:29:12 -05:00
|
|
|
while !result && i < 1000
|
2014-11-11 17:27:34 -06:00
|
|
|
result = blk.call
|
|
|
|
i += 1
|
|
|
|
sleep 0.001
|
|
|
|
end
|
|
|
|
|
2017-08-23 09:41:47 -05:00
|
|
|
on_fail&.call
|
2015-04-25 10:18:35 -05:00
|
|
|
expect(result).to eq(true)
|
2014-11-11 17:27:34 -06:00
|
|
|
end
|
|
|
|
|
2015-12-15 18:31:19 -06: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 09:21:17 -05:00
|
|
|
def email(email_name)
|
|
|
|
fixture_file("emails/#{email_name}.eml")
|
|
|
|
end
|
2018-04-20 14:25:28 -05:00
|
|
|
|
|
|
|
def create_staff_tags(tag_names)
|
|
|
|
tag_group = Fabricate(:tag_group, name: 'Staff Tags')
|
|
|
|
TagGroupPermission.create!(
|
|
|
|
tag_group: tag_group,
|
|
|
|
group_id: Group::AUTO_GROUPS[:everyone],
|
|
|
|
permission_type: TagGroupPermission.permission_types[:readonly]
|
|
|
|
)
|
|
|
|
TagGroupPermission.create!(
|
|
|
|
tag_group: tag_group,
|
|
|
|
group_id: Group::AUTO_GROUPS[:staff],
|
|
|
|
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
|
2013-09-05 06:22:15 -05:00
|
|
|
end
|