2871: add input checks for helper methods. start adding functional tests for the...
[arvados.git] / apps / workbench / test / functional / application_controller_test.rb
1 require 'test_helper'
2
3 class ApplicationControllerTest < ActionController::TestCase
4
5   test "links for object" do
6     use_token :active
7
8     ac = ApplicationController.new
9
10     link_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
11
12     links = ac.send :links_for_object, link_head_uuid
13
14     assert links, 'Expected links'
15     assert links.is_a?(Array), 'Expected an array'
16     assert links.size > 0, 'Expected at least one link'
17     assert links[0][:uuid], 'Expected uuid for the head_link'
18   end
19
20   test "links for no such object" do
21     use_token :active
22
23     ac = ApplicationController.new
24
25     links = ac.send :links_for_object, "no-such-uuid"
26
27     assert links, 'Expected links'
28     assert links.is_a?(Array), 'Expected an array'
29     assert links.size == 0, 'Expected no links'
30   end
31
32   test "preload links for given uuids" do
33     use_token :active
34
35     ac = ApplicationController.new
36
37     link_head_uuid1 = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
38     link_head_uuid2 = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
39
40     uuids = [link_head_uuid1, link_head_uuid2]
41     links = ac.send :preload_links_for_objects, uuids
42
43     assert links, 'Expected links'
44     assert links.is_a?(Hash), 'Expected a hash'
45     assert links.size == 2, 'Expected two objects in the preloaded links hash'
46     assert links[link_head_uuid1], 'Expected links for the passed in head_uuid'
47     assert links[link_head_uuid2], 'Expected links for the passed in head_uuid'
48   end
49
50   test "preload links for object and uuids" do
51     use_token :active
52
53     ac = ApplicationController.new
54
55     link1_head_uuid = api_fixture('links')['foo_file_readable_by_active']['head_uuid']
56     link2_uuid = api_fixture('links')['bar_file_readable_by_active']['uuid']
57     link3_head_uuid = api_fixture('links')['bar_file_readable_by_active']['head_uuid']
58
59     link2_object = User.find(api_fixture('users')['active']['uuid'])
60     link2_object_uuid = link2_object['uuid']
61
62     uuids = [link1_head_uuid, link2_object, link3_head_uuid]
63     links = ac.send :preload_links_for_objects, uuids
64
65     assert links, 'Expected links'
66     assert links.is_a?(Hash), 'Expected a hash'
67     assert links.size == 3, 'Expected two objects in the preloaded links hash'
68     assert links[link1_head_uuid], 'Expected links for the passed in link head_uuid'
69     assert links[link2_object_uuid], 'Expected links for the passed in object uuid'
70     assert links[link3_head_uuid], 'Expected links for the passed in link head_uuid'
71   end
72
73   test "preload links for wrong typed input" do
74     use_token :active
75
76     ac = ApplicationController.new
77
78     assert_raise ArgumentError do
79       links = ac.send :preload_links_for_objects, 'input not an array'
80     end
81   end
82
83   test "preload links for nil input" do
84     use_token :active
85
86     ac = ApplicationController.new
87
88     assert_raise ArgumentError do
89       links = ac.send :preload_links_for_objects, nil
90     end
91   end
92
93   test "preload links for empty array input" do
94     use_token :active
95
96     ac = ApplicationController.new
97
98     links = ac.send :preload_links_for_objects, []
99
100     assert links, 'Expected links'
101     assert links.is_a?(Hash), 'Expected a hash'
102     assert links.size == 0, 'Expected no objects in the preloaded links hash'
103   end
104
105 end