Upgraded Rails
[monkeycharger.git] / vendor / rails / activeresource / test / authorization_test.rb
blob58bd36cb777394aacb6dd11d26afd24f3c2db038
1 require "#{File.dirname(__FILE__)}/abstract_unit"
2 require 'base64'
4 class AuthorizationTest < Test::Unit::TestCase
5   Response = Struct.new(:code)
7   def setup
8     @conn = ActiveResource::Connection.new('http://localhost')
9     @matz  = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
10     @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
11     @authenticated_conn = ActiveResource::Connection.new("http://david:test123@localhost")
12     @authorization_request_header = { 'Authorization' => 'Basic ZGF2aWQ6dGVzdDEyMw==' }
14     ActiveResource::HttpMock.respond_to do |mock|
15       mock.get    "/people/2.xml",           @authorization_request_header, @david
16       mock.put    "/people/2.xml",           @authorization_request_header, nil, 204
17       mock.delete "/people/2.xml",           @authorization_request_header, nil, 200
18       mock.post   "/people/2/addresses.xml", @authorization_request_header, nil, 201, 'Location' => '/people/1/addresses/5'
19     end
20   end
22   def test_authorization_header
23     authorization_header = @authenticated_conn.send!(:authorization_header)
24     assert_equal @authorization_request_header['Authorization'], authorization_header['Authorization']
25     authorization = authorization_header["Authorization"].to_s.split
26     
27     assert_equal "Basic", authorization[0]
28     assert_equal ["david", "test123"], Base64.decode64(authorization[1]).split(":")[0..1]
29   end
30   
31   def test_authorization_header_with_username_but_no_password
32     @conn = ActiveResource::Connection.new("http://david:@localhost")
33     authorization_header = @conn.send!(:authorization_header)
34     authorization = authorization_header["Authorization"].to_s.split
35     
36     assert_equal "Basic", authorization[0]
37     assert_equal ["david"], Base64.decode64(authorization[1]).split(":")[0..1]
38   end
39   
40   def test_authorization_header_with_password_but_no_username
41     @conn = ActiveResource::Connection.new("http://:test123@localhost")
42     authorization_header = @conn.send!(:authorization_header)
43     authorization = authorization_header["Authorization"].to_s.split
44     
45     assert_equal "Basic", authorization[0]
46     assert_equal ["", "test123"], Base64.decode64(authorization[1]).split(":")[0..1]
47   end
48   
49   def test_get
50     david = @authenticated_conn.get("/people/2.xml")
51     assert_equal "David", david["name"]
52   end
53   
54   def test_post
55     response = @authenticated_conn.post("/people/2/addresses.xml")
56     assert_equal "/people/1/addresses/5", response["Location"]
57   end
58   
59   def test_put
60     response = @authenticated_conn.put("/people/2.xml")
61     assert_equal 204, response.code
62   end
63   
64   def test_delete
65     response = @authenticated_conn.delete("/people/2.xml")
66     assert_equal 200, response.code
67   end
69   def test_raises_invalid_request_on_unauthorized_requests
70     assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2.xml") }
71     assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
72     assert_raises(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
73     assert_raises(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
74   end
76   protected
77     def assert_response_raises(klass, code)
78       assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
79         @conn.send!(:handle_response, Response.new(code))
80       end
81     end
82 end