-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathintegration.rb
95 lines (82 loc) · 2.57 KB
/
integration.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
# frozen_string_literal: true
require 'action_dispatch/testing/integration'
class ActionDispatch::IntegrationTest
def warden
request.env['warden']
end
def create_user(options = {})
@user ||= begin
user = User.create!(
username: 'usertest',
email: options[:email] || 'user@test.com',
password: options[:password] || '12345678',
password_confirmation: options[:password] || '12345678',
created_at: Time.now.utc
)
user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
user.confirm unless options[:confirm] == false
user.lock_access! if options[:locked] == true
User.validations_performed = false
user
end
end
def create_admin(options = {})
@admin ||= begin
admin = Admin.create!(
email: options[:email] || 'admin@test.com',
password: '123456', password_confirmation: '123456',
active: options[:active]
)
admin.confirm unless options[:confirm] == false
admin
end
end
def sign_in_as_user(options = {}, &block)
user = create_user(options)
visit_with_option options[:visit], new_user_session_path
fill_in 'email', with: options[:email] || 'user@test.com'
fill_in 'password', with: options[:password] || '12345678'
check 'remember me' if options[:remember_me] == true
yield if block_given?
click_button 'Log In'
user
end
def sign_in_as_admin(options = {}, &block)
admin = create_admin(options)
visit_with_option options[:visit], new_admin_session_path
fill_in 'email', with: 'admin@test.com'
fill_in 'password', with: '123456'
yield if block_given?
click_button 'Log In'
admin
end
# Fix assert_redirect_to in integration sessions because they don't take into
# account Middleware redirects.
#
def assert_redirected_to(url)
assert_includes [301, 302, 303], @integration_session.status,
"Expected status to be 301, 302, or 303, got #{@integration_session.status}"
assert_url url, @integration_session.headers["Location"]
end
def assert_current_url(expected)
assert_url expected, current_url
end
def assert_url(expected, actual)
assert_equal prepend_host(expected), prepend_host(actual)
end
protected
def visit_with_option(given, default)
case given
when String
visit given
when FalseClass
# Do nothing
else
visit default
end
end
def prepend_host(url)
url = "http://#{request.host}#{url}" if url[0] == ?/
url
end
end