integration_support.rb
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module IntegrationSupport
@@default_user = nil
# todo: make automatically included in integration tests
Spec::Runner.configure do |config|
config.before(:each, :type => :integration) do
# compatibility with old tests using @api_user, remove this
@api_user = self.default_user = Factory(:email_confirmed_user)
end
end
def default_user=(user)
@api_user = @@default_user = user
end
# generate _auth variation of get/put/post, etc. to automatically
# send requests with the authentication and accept headers set
%w(get put post delete head).each do |method|
define_method(method + "_auth") do |*args|
if args[0].is_a? User
user, path, parameters, headers, *ignored = *args
else
path, parameters, headers, *ignored = *args
end
user ||= @@default_user
raise ArgumentError, "No user provided and default user not set" unless user
auth = ActionController::HttpAuthentication::
Basic.encode_credentials(user.email, user.password)
(headers ||= {}).merge!( :authorization => auth,
:accept => "application/xml" )
send(method, path, parameters, headers)
end
end
# need a way to easily fetch content of a Tag
class HTML::Tag
def content(tag)
n = self.find(:tag => tag) or return nil
n.children.each{ |c| return c.content if c.is_a? HTML::Text }
nil
end
end
# have_tag doesn't let you iterate over individual nodes like
# assert_select does for some reason, and using css matchers
# to do this is ugly. Time for a patch!
class Spec::Rails::Matchers::AssertSelect
def doc_from_with_node(node)
return node if node.is_a? HTML::Node
doc_from_without_node(node)
end
alias_method_chain :doc_from, :node
end
end