Merge branch 'master' into 5375-preview-collection-text-files
[arvados.git] / apps / workbench / test / unit / collection_test.rb
1 require 'test_helper'
2
3 class CollectionTest < ActiveSupport::TestCase
4   include CollectionsHelper
5
6   test 'recognize empty blob locator' do
7     ['d41d8cd98f00b204e9800998ecf8427e+0',
8      'd41d8cd98f00b204e9800998ecf8427e',
9      'd41d8cd98f00b204e9800998ecf8427e+0+Xyzzy'].each do |x|
10       assert_equal true, Collection.is_empty_blob_locator?(x)
11     end
12     ['d41d8cd98f00b204e9800998ecf8427e0',
13      'acbd18db4cc2f85cedef654fccc4a4d8+3',
14      'acbd18db4cc2f85cedef654fccc4a4d8+0'].each do |x|
15       assert_equal false, Collection.is_empty_blob_locator?(x)
16     end
17   end
18
19   def get_files_tree(coll_name)
20     use_token :admin
21     Collection.find(api_fixture('collections')[coll_name]['uuid']).files_tree
22   end
23
24   test "easy files_tree" do
25     files_in = lambda do |dirname|
26       (1..3).map { |n| [dirname, "file#{n}", 0] }
27     end
28     assert_equal([['.', 'dir1', nil], ['./dir1', 'subdir', nil]] +
29                  files_in['./dir1/subdir'] + files_in['./dir1'] +
30                  [['.', 'dir2', nil]] + files_in['./dir2'] + files_in['.'],
31                  get_files_tree('multilevel_collection_1'),
32                  "Collection file tree was malformed")
33   end
34
35   test "files_tree with files deep in subdirectories" do
36     # This test makes sure files_tree generates synthetic directory entries.
37     # The manifest doesn't list directories with no files.
38     assert_equal([['.', 'dir1', nil], ['./dir1', 'sub1', nil],
39                   ['./dir1/sub1', 'a', 0], ['./dir1/sub1', 'b', 0],
40                   ['.', 'dir2', nil], ['./dir2', 'sub2', nil],
41                   ['./dir2/sub2', 'c', 0], ['./dir2/sub2', 'd', 0]],
42                  get_files_tree('multilevel_collection_2'),
43                  "Collection file tree was malformed")
44   end
45
46   test "portable_data_hash never editable" do
47     refute(Collection.new.attribute_editable?("portable_data_hash", :ever))
48   end
49
50   test "admin can edit name" do
51     use_token :admin
52     assert(find_fixture(Collection, "foo_file").attribute_editable?("name"),
53            "admin not allowed to edit collection name")
54   end
55
56   test "project owner can edit name" do
57     use_token :active
58     assert(find_fixture(Collection, "foo_collection_in_aproject")
59              .attribute_editable?("name"),
60            "project owner not allowed to edit collection name")
61   end
62
63   test "project admin can edit name" do
64     use_token :subproject_admin
65     assert(find_fixture(Collection, "baz_file_in_asubproject")
66              .attribute_editable?("name"),
67            "project admin not allowed to edit collection name")
68   end
69
70   test "project viewer cannot edit name" do
71     use_token :project_viewer
72     refute(find_fixture(Collection, "foo_collection_in_aproject")
73              .attribute_editable?("name"),
74            "project viewer allowed to edit collection name")
75   end
76
77   [
78     ["filename.csv", true],
79     ["filename.fa", true],
80     ["filename.fasta", true],
81     ["filename.seq", true],   # another fasta extension
82     ["filename.go", true],
83     ["filename.htm", true],
84     ["filename.html", true],
85     ["filename.json", true],
86     ["filename.md", true],
87     ["filename.pdf", true],
88     ["filename.py", true],
89     ["filename.R", true],
90     ["filename.sam", true],
91     ["filename.sh", true],
92     ["filename.txt", true],
93     ["filename.tiff", true],
94     ["filename.tsv", true],
95     ["filename.vcf", true],
96     ["filename.xml", true],
97     ["filename.xsl", true],
98     ["filename.yml", true],
99
100     ["filename.bam", false],
101     ["filename", false],
102   ].each do |file_name, preview_allowed|
103     test "verify '#{file_name}' is allowed for preview #{preview_allowed}" do
104       assert_equal preview_allowed, preview_allowed_for(file_name)
105     end
106   end
107 end