3126: Accept remote http/https/git url as repository attr in jobs.create/save.
[arvados.git] / services / api / test / unit / commit_test.rb
1 require 'test_helper'
2 require 'helpers/git_test_helper'
3
4 # NOTE: calling Commit.find_commit_range(user, nil, nil, 'rev')
5 # produces an error message "fatal: bad object 'rev'" on stderr if
6 # 'rev' does not exist in a given repository.  Many of these tests
7 # report such errors; their presence does not represent a fatal
8 # condition.
9
10 class CommitTest < ActiveSupport::TestCase
11   # See git_setup.rb for the commit log for test.git.tar
12   include GitTestHelper
13
14   test 'find_commit_range does not bypass permissions' do
15     assert_raises ArgumentError do
16       c = Commit.find_commit_range users(:inactive), 'foo', nil, 'master', []
17     end
18   end
19
20   [
21    'https://github.com/curoverse/arvados.git',
22    'http://github.com/curoverse/arvados.git',
23    'git://github.com/curoverse/arvados.git',
24   ].each do |url|
25     test "find_commit_range uses fetch_remote_repository to get #{url}" do
26       fake_gitdir = repositories(:foo).server_path
27       Commit.expects(:fetch_remote_repository).
28         once.with(Commit.cache_dir_for(url), url).returns fake_gitdir
29       c = Commit.find_commit_range users(:active), url, nil, 'master', []
30       refute_empty c
31     end
32   end
33
34   [
35    'bogus/repo',
36    '/bogus/repo',
37    '/not/allowed/.git',
38    'file:///not/allowed.git',
39    'git.curoverse.com/arvados.git',
40    'github.com/curoverse/arvados.git',
41   ].each do |url|
42     test "find_commit_range skips fetch_remote_repository for #{url}" do
43       Commit.expects(:fetch_remote_repository).never
44       assert_raises ArgumentError do
45         Commit.find_commit_range users(:active), url, nil, 'master', []
46       end
47     end
48   end
49
50   test 'fetch_remote_repository does not leak commits across repositories' do
51     url = "http://localhost:1/fake/fake.git"
52     fetch_remote_from_local_repo url, :foo
53     c = Commit.find_commit_range users(:active), url, nil, 'master', []
54     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57'], c
55
56     url = "http://localhost:2/fake/fake.git"
57     fetch_remote_from_local_repo url, 'file://' + File.expand_path('../../.git', Rails.root)
58     c = Commit.find_commit_range users(:active), url, nil, '077ba2ad3ea24a929091a9e6ce545c93199b8e57', []
59     assert_equal [], c
60   end
61
62   test 'fetch_remote_repository finds versions' do
63     skip 'not written yet'
64   end
65
66   test 'tag_in_internal_repository creates and updates tags in internal.git' do
67     authorize_with :active
68     gitint = "git --git-dir #{Rails.configuration.git_internal_dir}"
69     IO.read("|#{gitint} tag -d testtag 2>/dev/null") # "no such tag", fine
70     assert_match /^fatal: /, IO.read("|#{gitint} show testtag 2>&1")
71     refute $?.success?
72     Commit.tag_in_internal_repository 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', 'testtag'
73     assert_match /^commit 31ce37f/, IO.read("|#{gitint} show testtag")
74     assert $?.success?
75   end
76
77   test "find_commit_range laundry list" do
78     authorize_with :active
79
80     # single
81     a = Commit.find_commit_range(users(:active), 'active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
82     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
83
84     #test "test_branch1" do
85     a = Commit.find_commit_range(users(:active), 'active/foo', nil, 'master', nil)
86     assert_includes(a, '077ba2ad3ea24a929091a9e6ce545c93199b8e57')
87
88     #test "test_branch2" do
89     a = Commit.find_commit_range(users(:active), 'active/foo', nil, 'b1', nil)
90     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
91
92     #test "test_branch3" do
93     a = Commit.find_commit_range(users(:active), 'active/foo', nil, 'HEAD', nil)
94     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
95
96     #test "test_single_revision_repo" do
97     a = Commit.find_commit_range(users(:active), 'active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
98     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
99     a = Commit.find_commit_range(users(:active), 'arvados', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
100     assert_equal [], a
101
102     #test "test_multi_revision" do
103     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
104     a = Commit.find_commit_range(users(:active), 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', nil)
105     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
106
107     #test "test_tag" do
108     # complains "fatal: ambiguous argument 'tag1': unknown revision or path
109     # not in the working tree."
110     a = Commit.find_commit_range(users(:active), 'active/foo', 'tag1', 'master', nil)
111     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577'], a
112
113     #test "test_multi_revision_exclude" do
114     a = Commit.find_commit_range(users(:active), 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['4fe459abe02d9b365932b8f5dc419439ab4e2577'])
115     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
116
117     #test "test_multi_revision_tagged_exclude" do
118     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
119     a = Commit.find_commit_range(users(:active), 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['tag1'])
120     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
121
122     Dir.mktmpdir do |touchdir|
123       # invalid input to maximum
124       a = Commit.find_commit_range(users(:active), 'active/foo', nil, "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", nil)
125       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
126       assert_equal [], a
127
128       # invalid input to maximum
129       a = Commit.find_commit_range(users(:active), 'active/foo', nil, "$(uname>#{touchdir}/uh_oh)", nil)
130       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
131       assert_equal [], a
132
133       # invalid input to minimum
134       a = Commit.find_commit_range(users(:active), 'active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
135       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
136       assert_equal [], a
137
138       # invalid input to minimum
139       a = Commit.find_commit_range(users(:active), 'active/foo', "$(uname>#{touchdir}/uh_oh)", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
140       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
141       assert_equal [], a
142
143       # invalid input to 'excludes'
144       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
145       a = Commit.find_commit_range(users(:active), 'active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["4fe459abe02d9b365932b8f5dc419439ab4e2577 ; touch #{touchdir}/uh_oh"])
146       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
147       assert_equal [], a
148
149       # invalid input to 'excludes'
150       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
151       a = Commit.find_commit_range(users(:active), 'active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["$(uname>#{touchdir}/uh_oh)"])
152       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
153       assert_equal [], a
154     end
155   end
156 end