forked from sparklemotion/mechanize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mechanize_http_auth_store.rb
195 lines (135 loc) · 4.38 KB
/
test_mechanize_http_auth_store.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# coding: utf-8
require 'mechanize/test_case'
class TestMechanizeHttpAuthStore < Mechanize::TestCase
def setup
super
@store = Mechanize::HTTP::AuthStore.new
@uri = URI.parse 'http://example/'
end
def test_add_auth
@store.add_auth @uri + '/path', 'user', 'pass'
expected = {
@uri => {
nil => ['user', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_auth_domain
@store.add_auth @uri + '/path', 'user1', 'pass', nil, 'domain'
expected = {
@uri => {
nil => %w[user1 pass domain],
}
}
assert_equal expected, @store.auth_accounts
e = assert_raises ArgumentError do
@store.add_auth @uri, 'user3', 'pass', 'realm', 'domain'
end
assert_equal 'NTLM domain given with realm which NTLM does not use',
e.message
end
def test_add_auth_realm
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
expected = {
@uri => {
nil => ['user1', 'pass', nil],
'realm' => ['user2', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_auth_string
@store.add_auth "#{@uri}/path", 'user', 'pass'
expected = {
@uri => {
nil => ['user', 'pass', nil],
}
}
assert_equal expected, @store.auth_accounts
end
def test_add_default_auth
_, err = capture_io do
@store.add_default_auth 'user', 'pass'
end
expected = ['user', 'pass', nil]
assert_equal expected, @store.default_auth
assert_match 'DISCLOSURE WITHOUT YOUR KNOWLEDGE', err
capture_io do
@store.add_default_auth 'user', 'pass', 'realm'
end
expected = %w[user pass realm]
assert_equal expected, @store.default_auth
end
def test_credentials_eh
challenges = [
Mechanize::HTTP::AuthChallenge.new('Basic', 'realm' => 'r'),
Mechanize::HTTP::AuthChallenge.new('Digest', 'realm' => 'r'),
]
refute @store.credentials? @uri, challenges
@store.add_auth @uri, 'user', 'pass'
assert @store.credentials? @uri, challenges
assert @store.credentials? "#{@uri}/path", challenges
end
def test_credentials_for
assert_nil @store.credentials_for(@uri, 'realm')
@store.add_auth @uri, 'user', 'pass', 'realm'
assert_equal ['user', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user', 'pass', nil],
@store.credentials_for(@uri.to_s, 'realm')
assert_nil @store.credentials_for(@uri, 'other')
end
def test_credentials_for_default
assert_nil @store.credentials_for(@uri, 'realm')
capture_io do
@store.add_default_auth 'user1', 'pass'
end
assert_equal ['user1', 'pass', nil], @store.credentials_for(@uri, 'realm')
@store.add_auth @uri, 'user2', 'pass'
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'other')
end
def test_credentials_for_no_realm
@store.add_auth @uri, 'user', 'pass' # no realm set
assert_equal ['user', 'pass', nil], @store.credentials_for(@uri, 'realm')
end
def test_credentials_for_realm
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
assert_equal ['user2', 'pass', nil], @store.credentials_for(@uri, 'realm')
assert_equal ['user1', 'pass', nil], @store.credentials_for(@uri, 'other')
end
def test_credentials_for_path
@store.add_auth @uri, 'user', 'pass', 'realm'
uri = @uri + '/path'
assert_equal ['user', 'pass', nil], @store.credentials_for(uri, 'realm')
end
def test_remove_auth
@store.remove_auth @uri
assert_empty @store.auth_accounts
end
def test_remove_auth_both
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
uri = @uri + '/path'
@store.remove_auth uri
assert_empty @store.auth_accounts
end
def test_remove_auth_realm
@store.add_auth @uri, 'user1', 'pass'
@store.add_auth @uri, 'user2', 'pass', 'realm'
@store.remove_auth @uri, 'realm'
expected = {
@uri => {
nil => ['user1', 'pass', nil]
}
}
assert_equal expected, @store.auth_accounts
end
def test_remove_auth_string
@store.add_auth @uri, 'user1', 'pass'
@store.remove_auth "#{@uri}/path"
assert_empty @store.auth_accounts
end
end