2013-09-05 07:22:15 -04:00
|
|
|
module Helpers
|
|
|
|
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)
|
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)
|
2014-03-26 15:20:41 -04:00
|
|
|
args[:category] = args[:category].name 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)
|
2014-03-26 15:20:41 -04:00
|
|
|
args[:category] = args[:category].name if args[:category].is_a?(Category)
|
2013-09-05 07:22:15 -04:00
|
|
|
PostCreator.create(user, args)
|
|
|
|
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?
|
|
|
|
Guardian.stubs(new: guardian).with(user)
|
|
|
|
end
|
2014-11-11 18:27:34 -05:00
|
|
|
|
|
|
|
def wait_for(&blk)
|
|
|
|
i = 0
|
|
|
|
result = false
|
|
|
|
while !result && i < 300
|
|
|
|
result = blk.call
|
|
|
|
i += 1
|
|
|
|
sleep 0.001
|
|
|
|
end
|
|
|
|
|
|
|
|
result.should == true
|
|
|
|
end
|
|
|
|
|
2013-09-05 07:22:15 -04:00
|
|
|
end
|