forked from Fosome/garb
/
authentication_request_test.rb
121 lines (86 loc) · 3.89 KB
/
authentication_request_test.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
require File.join(File.dirname(__FILE__), '..', '/test_helper')
CA_CERT_FILE = File.join(File.dirname(__FILE__), '..', '/cacert.pem')
module Garb
class AuthenticationRequestTest < Test::Unit::TestCase
context "An instance of the AuthenticationRequest class" do
setup { @request = AuthenticationRequest.new('email', 'password') }
should "have a collection of parameters that include the email and password" do
expected =
{
'Email' => 'user@example.com',
'Passwd' => 'fuzzybunnies',
'accountType' => 'HOSTED_OR_GOOGLE',
'service' => 'analytics',
'source' => 'vigetLabs-garb-001'
}
request = AuthenticationRequest.new('user@example.com', 'fuzzybunnies')
assert_equal expected, request.parameters
end
should "have a URI" do
assert_equal URI.parse('https://www.google.com/accounts/ClientLogin'), @request.uri
end
should "be able to send a request to the GAAPI service with proper ssl" do
@request.expects(:build_request).returns('post')
response = mock {|m| m.expects(:is_a?).with(Net::HTTPOK).returns(true) }
http = mock do |m|
m.expects(:use_ssl=).with(true)
m.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
m.expects(:ca_file=).with(CA_CERT_FILE)
m.expects(:request).with('post').yields(response)
end
Net::HTTP.expects(:new).with('www.google.com', 443).returns(http)
@request.send_request(OpenSSL::SSL::VERIFY_PEER)
end
should "be able to send a request to the GAAPI service with ignoring ssl" do
@request.expects(:build_request).returns('post')
response = mock {|m| m.expects(:is_a?).with(Net::HTTPOK).returns(true) }
http = mock do |m|
m.expects(:use_ssl=).with(true)
m.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
m.expects(:request).with('post').yields(response)
end
Net::HTTP.expects(:new).with('www.google.com', 443).returns(http)
@request.send_request(OpenSSL::SSL::VERIFY_NONE)
end
should "be able to build a request for the GAAPI service" do
params = "param"
@request.expects(:parameters).with().returns(params)
post = mock
post.expects(:set_form_data).with(params)
Net::HTTP::Post.expects(:new).with('/accounts/ClientLogin').returns(post)
@request.build_request
end
should "be able to retrieve an auth_token from the body" do
response_data =
"SID=mysid\n" +
"LSID=mylsid\n" +
"Auth=auth_token\n"
@request.expects(:send_request).with(OpenSSL::SSL::VERIFY_NONE).returns(stub(:body => response_data))
assert_equal 'auth_token', @request.auth_token
end
should "use VERIFY_PEER if auth_token needs to be secure" do
response_data =
"SID=mysid\n" +
"LSID=mylsid\n" +
"Auth=auth_token\n"
@request.expects(:send_request).with(OpenSSL::SSL::VERIFY_PEER).returns(stub(:body => response_data))
assert_equal 'auth_token', @request.auth_token(:secure => true)
end
should "raise an exception when requesting an auth_token when the authorization fails" do
@request.stubs(:build_request)
response = mock do |m|
m.expects(:is_a?).with(Net::HTTPOK).returns(false)
end
http = stub do |s|
s.stubs(:use_ssl=)
s.stubs(:verify_mode=)
s.stubs(:request).yields(response)
end
Net::HTTP.stubs(:new).with('www.google.com', 443).returns(http)
assert_raise(Garb::AuthenticationRequest::AuthError) do
@request.send_request(OpenSSL::SSL::VERIFY_NONE)
end
end
end
end
end