2871: more testing
[arvados.git] / apps / workbench / test / functional / application_controller_test.rb
1 require 'test_helper'
2
3 class ApplicationControllerTest < ActionController::TestCase
4
5   setup do
6     @user_dataclass = ArvadosBase.resource_class_for_uuid(api_fixture('users')['active']['uuid'])
7   end
8
9   test "links for object" do
10     use_token :active
11
12     ac = ApplicationController.new
13
14     link_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
15
16     links = ac.send :links_for_object, link_head_uuid
17
18     assert links, 'Expected links'
19     assert links.is_a?(Array), 'Expected an array'
20     assert links.size > 0, 'Expected at least one link'
21     assert links[0][:uuid], 'Expected uuid for the head_link'
22   end
23
24   test "links for no such object" do
25     use_token :active
26
27     ac = ApplicationController.new
28
29     links = ac.send :links_for_object, "no-such-uuid"
30
31     assert links, 'Expected links'
32     assert links.is_a?(Array), 'Expected an array'
33     assert links.size == 0, 'Expected no links'
34   end
35
36   test "preload links for objects and uuids" do
37     use_token :active
38
39     ac = ApplicationController.new
40
41     link1_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
42     link2_uuid = api_fixture('links')['bar_file_readable_by_active']['uuid']
43     link3_head_uuid = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
44
45     link2_object = User.find(api_fixture('users')['active']['uuid'])
46     link2_object_uuid = link2_object['uuid']
47
48     uuids = [link1_head_uuid, link2_object, link3_head_uuid]
49     links = ac.send :preload_links_for_objects, uuids
50
51     assert links, 'Expected links'
52     assert links.is_a?(Hash), 'Expected a hash'
53     assert links.size == 3, 'Expected two objects in the preloaded links hash'
54     assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
55     assert links[link2_object_uuid], 'Expected links for the passed in object uuid'
56     assert links[link3_head_uuid], 'Expected links for the passed in link head_uuid'
57
58     # invoke again for this same input. this time, the preloaded data will be returned
59     links = ac.send :preload_links_for_objects, uuids
60     assert links, 'Expected links'
61     assert links.is_a?(Hash), 'Expected a hash'
62     assert links.size == 3, 'Expected two objects in the preloaded links hash'
63     assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
64   end
65
66   test "preload links for empty array input" do
67     use_token :active
68
69     ac = ApplicationController.new
70
71     links = ac.send :preload_links_for_objects, []
72
73     assert links, 'Expected links'
74     assert links.is_a?(Hash), 'Expected a hash'
75     assert links.size == 0, 'Expected no objects in the preloaded links hash'
76   end
77
78   [ [:preload_links_for_objects, 'input not an array'],
79     [:preload_links_for_objects, nil],
80     [:links_for_object, nil],
81     [:preload_collections_for_objects, 'input not an array'],
82     [:preload_collections_for_objects, nil],
83     [:collections_for_object, nil],
84     [:preload_log_collections_for_objects, 'input not an array'],
85     [:preload_log_collections_for_objects, nil],
86     [:log_collections_for_object, nil],
87     [:preload_objects_for_dataclass, 'input not an array'],
88     [:preload_objects_for_dataclass, nil],    
89     [:object_for_dataclass, 'some_dataclass', nil],
90     [:object_for_dataclass, nil, 'some_uuid'],
91   ].each do |input|
92     test "preload data for wrong type input #{input}" do
93       use_token :active
94
95       ac = ApplicationController.new
96
97       if input[0] == :object_for_dataclass
98         assert_raise ArgumentError do
99           ac.send input[0], input[1], input[2]
100         end
101       else
102         assert_raise ArgumentError do
103           ac.send input[0], input[1]
104         end
105       end
106     end
107   end
108
109   test "get 10 objects of data class user" do
110     use_token :active
111
112     ac = ApplicationController.new
113
114     objects = ac.send :get_n_objects_of_class, @user_dataclass, 10
115
116     assert objects, 'Expected objects'
117     assert objects.is_a?(ArvadosResourceList), 'Expected an ArvadosResourceList'
118
119     first_object = objects.first
120     assert first_object, 'Expected at least one object'
121     assert_equal 'User', first_object.class.name, 'Expected user object'
122
123     # invoke it again. this time, the preloaded info will be returned
124     objects = ac.send :get_n_objects_of_class, @user_dataclass, 10
125     assert objects, 'Expected objects'
126     assert_equal 'User', objects.first.class.name, 'Expected user object'
127   end
128
129   [ ['User', 10],
130     [nil, 10],
131     [@user_dataclass, 0],
132     [@user_dataclass, -1],
133     [@user_dataclass, nil] ].each do |input|
134     test "get_n_objects for incorrect input #{input}" do
135       use_token :active
136
137       ac = ApplicationController.new
138
139       assert_raise ArgumentError do
140         ac.send :get_n_objects_of_class, input[0], input[1]
141       end
142     end
143   end
144
145   test "collections for object" do
146     use_token :active
147
148     ac = ApplicationController.new
149
150     uuid = api_fixture('collections')['foo_file']['uuid']
151
152     collections = ac.send :collections_for_object, uuid
153
154     assert collections, 'Expected collections'
155     assert collections.is_a?(Array), 'Expected an array'
156     assert collections.size == 1, 'Expected one collection object'
157     assert_equal collections[0][:uuid], uuid, 'Expected uuid not found in collections'
158   end
159
160   test "collections for no such object" do
161     use_token :active
162
163     ac = ApplicationController.new
164
165     collections = ac.send :collections_for_object, "no-such-uuid"
166
167     assert collections, 'Expected collections'
168     assert collections.is_a?(Array), 'Expected an array'
169     assert collections.size == 0, 'Expected no collections in response'
170   end
171
172   test "preload collections for given uuids" do
173     use_token :active
174
175     ac = ApplicationController.new
176
177     uuid1 = api_fixture('collections')['foo_file']['uuid']
178     uuid2 = api_fixture('collections')['bar_file']['uuid']
179
180     uuids = [uuid1, uuid2]
181     collections = ac.send :preload_collections_for_objects, uuids
182
183     assert collections, 'Expected collection'
184     assert collections.is_a?(Hash), 'Expected a hash'
185     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
186     assert collections[uuid1], 'Expected collections for the passed in uuid'
187     assert_equal collections[uuid1].size, 1, 'Expected one collection for the passed in uuid'
188     assert collections[uuid2], 'Expected collections for the passed in uuid'
189     assert_equal collections[uuid2].size, 1, 'Expected one collection for the passed in uuid'
190
191     # invoke again for this same input. this time, the preloaded data will be returned
192     collections = ac.send :preload_collections_for_objects, uuids
193     assert collections, 'Expected collection'
194     assert collections.is_a?(Hash), 'Expected a hash'
195     assert collections.size == 2, 'Expected two objects in the preloaded collection hash'
196     assert collections[uuid1], 'Expected collections for the passed in uuid'
197   end
198
199   test "preload collections for empty array input" do
200     use_token :active
201
202     ac = ApplicationController.new
203
204     collections = ac.send :preload_links_for_objects, []
205
206     assert collections, 'Expected collections'
207     assert collections.is_a?(Hash), 'Expected a hash'
208     assert collections.size == 0, 'Expected no objects in the preloaded collections hash'
209   end
210
211 end