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