forked from TracksApp/tracks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helper.rb
148 lines (125 loc) · 4.74 KB
/
test_helper.rb
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require File.expand_path(File.dirname(__FILE__) + "/../app/controllers/application_controller")
require 'test_help'
require 'flexmock/test_unit' #and the flexmock gem, too!
require 'action_web_service/test_invoke'
module Tracks
class Config
def self.salt
"change-me"
end
end
end
class Test::Unit::TestCase
include AuthenticatedTestHelper
def xml_document
@xml_document ||= HTML::Document.new(@response.body, false, true)
end
def assert_xml_select(*args, &block)
@html_document = xml_document
assert_select(*args, &block)
end
def assert_error_on(model_instance, attribute, expected_error)
actual_error = model_instance.errors.on attribute.to_sym
assert_equal expected_error, actual_error
end
alias_method :assert_errors_on, :assert_error_on
def assert_value_changed(object, method = nil)
initial_value = object.send(method)
yield
assert_not_equal initial_value, object.send(method), "#{object}##{method}"
end
end
class ActiveSupport::TestCase
# Generates a random string of ascii characters (a-z, "1 0")
# of a given length for testing assignment to fields
# for validation purposes
#
def generate_random_string(length)
string = ""
characters = %w(a b c d e f g h i j k l m n o p q r s t u v w z y z 1\ 0)
length.times do
pick = characters[rand(26)]
string << pick
end
return string
end
def next_week
1.week.from_now.utc
end
# Courtesy of http://habtm.com/articles/2006/02/20/assert-yourself-man-redirecting-with-rjs
def assert_js_redirected_to(options={}, message=nil)
clean_backtrace do
assert_response(:success, message)
assert_equal 'text/javascript', @response.content_type, 'Response should be Javascript content-type';
js_regexp = %r{(\w+://)?.*?(/|$|\\\?)(.*)}
url_regexp = %r{^window\.location\.href [=] ['"]#{js_regexp}['"][;]$}
redirected_to = @response.body.match(url_regexp)
assert_not_nil(redirected_to, message)
redirected_to = redirected_to[3]
msg = build_message(message, "expected a JS redirect to <?>, found one to <?>", options, redirected_to)
if options.is_a?(String)
assert_equal(options.gsub(/^\//, ''), redirected_to, message)
elsif options.is_a?(Regexp)
assert(options =~ redirected_to, "#{message} #{options} #{redirected_to}")
else
msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)", redirected_to)
assert_equal(@controller.url_for(options).match(js_regexp)[3], redirected_to, msg)
end
end
end
def set_user_to_current_time_zone(user)
jan_offset = Time.now.beginning_of_year.utc_offset
jul_offset = Time.now.beginning_of_year.change(:month => 7).utc_offset
offset = jan_offset < jul_offset ? jan_offset : jul_offset
offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/)
sign = $1 == '-' ? -1 : 1
hours, minutes = $2.to_f, $3.to_f
((hours * 3600) + (minutes.to_f * 60)) * sign
elsif offset.to_f.abs <= 13
offset.to_f * 3600
else
offset.to_f
end
zone = ActiveSupport::TimeZone.all.find{|t| t.utc_offset == offset}
user.prefs.update_attribute(:time_zone, zone.name)
end
end
class ActionController::IntegrationTest
Tag #avoid errors in integration tests
def assert_test_environment_ok
assert_equal "test", ENV['RAILS_ENV']
assert_equal "change-me", Tracks::Config.salt
end
def authenticated_post_xml(url, username, password, parameters, headers = {})
post url,
parameters,
{'AUTHORIZATION' => "Basic " + Base64.encode64("#{username}:#{password}"),
'ACCEPT' => 'application/xml',
'CONTENT_TYPE' => 'application/xml'
}.merge(headers)
end
def authenticated_get_xml(url, username, password, parameters, headers = {})
get url,
parameters,
{'AUTHORIZATION' => "Basic " + Base64.encode64("#{username}:#{password}"),
'ACCEPT' => 'application/xml',
'CONTENT_TYPE' => 'application/xml'
}.merge(headers)
end
def assert_response_and_body(type, body, message = nil)
assert_equal body, @response.body, message
assert_response type, message
end
def assert_response_and_body_matches(type, body_regex, message = nil)
assert_response type, message
assert_match body_regex, @response.body, message
end
def assert_401_unauthorized
assert_response_and_body 401, "401 Unauthorized: You are not authorized to interact with Tracks."
end
def assert_401_unauthorized_admin
assert_response_and_body 401, "401 Unauthorized: Only admin users are allowed access to this function."
end
end