Froze rails gems
[depot.git] / vendor / rails / activeresource / test / base / custom_methods_test.rb
1 require 'abstract_unit'
2 require 'fixtures/person'
3 require 'fixtures/street_address'
4
5 class CustomMethodsTest < Test::Unit::TestCase
6 def setup
7 @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
8 @matz_deep = { :id => 1, :name => 'Matz', :other => 'other' }.to_xml(:root => 'person')
9 @matz_array = [{ :id => 1, :name => 'Matz' }].to_xml(:root => 'people')
10 @ryan = { :name => 'Ryan' }.to_xml(:root => 'person')
11 @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address')
12 @addy_deep = { :id => 1, :street => '12345 Street', :zip => "27519" }.to_xml(:root => 'address')
13
14 ActiveResource::HttpMock.respond_to do |mock|
15 mock.get "/people/1.xml", {}, @matz
16 mock.get "/people/1/shallow.xml", {}, @matz
17 mock.get "/people/1/deep.xml", {}, @matz_deep
18 mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array
19 mock.get "/people/managers.xml", {}, @matz_array
20 mock.post "/people/hire.xml?name=Matz", {}, nil, 201
21 mock.put "/people/1/promote.xml?position=Manager", {}, nil, 204
22 mock.put "/people/promote.xml?name=Matz", {}, nil, 204, {}
23 mock.put "/people/sort.xml?by=name", {}, nil, 204
24 mock.delete "/people/deactivate.xml?name=Matz", {}, nil, 200
25 mock.delete "/people/1/deactivate.xml", {}, nil, 200
26 mock.post "/people/new/register.xml", {}, @ryan, 201, 'Location' => '/people/5.xml'
27 mock.post "/people/1/register.xml", {}, @matz, 201
28 mock.get "/people/1/addresses/1.xml", {}, @addy
29 mock.get "/people/1/addresses/1/deep.xml", {}, @addy_deep
30 mock.put "/people/1/addresses/1/normalize_phone.xml?locale=US", {}, nil, 204
31 mock.put "/people/1/addresses/sort.xml?by=name", {}, nil, 204
32 mock.post "/people/1/addresses/new/link.xml", {}, { :street => '12345 Street' }.to_xml(:root => 'address'), 201, 'Location' => '/people/1/addresses/2.xml'
33 end
34
35 Person.user = nil
36 Person.password = nil
37 end
38
39 def teardown
40 ActiveResource::HttpMock.reset!
41 end
42
43 def test_custom_collection_method
44 # GET
45 assert_equal([{ "id" => 1, "name" => 'Matz' }], Person.get(:retrieve, :name => 'Matz'))
46
47 # POST
48 assert_equal(ActiveResource::Response.new("", 201, {}), Person.post(:hire, :name => 'Matz'))
49
50 # PUT
51 assert_equal ActiveResource::Response.new("", 204, {}),
52 Person.put(:promote, {:name => 'Matz'}, 'atestbody')
53 assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:sort, :by => 'name')
54
55 # DELETE
56 Person.delete :deactivate, :name => 'Matz'
57
58 # Nested resource
59 assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name')
60 end
61
62 def test_custom_element_method
63 # Test GET against an element URL
64 assert_equal Person.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'}
65 assert_equal Person.find(1).get(:deep), {"id" => 1, "name" => 'Matz', "other" => 'other'}
66
67 # Test PUT against an element URL
68 assert_equal ActiveResource::Response.new("", 204, {}), Person.find(1).put(:promote, {:position => 'Manager'}, 'body')
69
70 # Test DELETE against an element URL
71 assert_equal ActiveResource::Response.new("", 200, {}), Person.find(1).delete(:deactivate)
72
73 # With nested resources
74 assert_equal StreetAddress.find(1, :params => { :person_id => 1 }).get(:deep),
75 { "id" => 1, "street" => '12345 Street', "zip" => "27519" }
76 assert_equal ActiveResource::Response.new("", 204, {}),
77 StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US')
78 end
79
80 def test_custom_new_element_method
81 # Test POST against a new element URL
82 ryan = Person.new(:name => 'Ryan')
83 assert_equal ActiveResource::Response.new(@ryan, 201, {'Location' => '/people/5.xml'}), ryan.post(:register)
84 expected_request = ActiveResource::Request.new(:post, '/people/new/register.xml', @ryan)
85 assert_equal expected_request.body, ActiveResource::HttpMock.requests.first.body
86
87 # Test POST against a nested collection URL
88 addy = StreetAddress.new(:street => '123 Test Dr.', :person_id => 1)
89 assert_equal ActiveResource::Response.new({ :street => '12345 Street' }.to_xml(:root => 'address'),
90 201, {'Location' => '/people/1/addresses/2.xml'}),
91 addy.post(:link)
92
93 matz = Person.new(:id => 1, :name => 'Matz')
94 assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register)
95 end
96
97 def test_find_custom_resources
98 assert_equal 'Matz', Person.find(:all, :from => :managers).first.name
99 end
100 end