3126: Fix test. Avoid excess "git init" by probing with "git branch" first.
[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(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   setup do
15     authorize_with :active
16   end
17
18   test 'find_commit_range does not bypass permissions' do
19     authorize_with :inactive
20     assert_raises ArgumentError do
21       c = Commit.find_commit_range 'foo', nil, 'master', []
22     end
23   end
24
25   [
26    'https://github.com/curoverse/arvados.git',
27    'http://github.com/curoverse/arvados.git',
28    'git://github.com/curoverse/arvados.git',
29   ].each do |url|
30     test "find_commit_range uses fetch_remote_repository to get #{url}" do
31       fake_gitdir = repositories(:foo).server_path
32       Commit.expects(:cache_dir_for).once.with(url).returns fake_gitdir
33       Commit.expects(:fetch_remote_repository).once.with(fake_gitdir, url).returns true
34       c = Commit.find_commit_range url, nil, 'master', []
35       refute_empty c
36     end
37   end
38
39   [
40    'bogus/repo',
41    '/bogus/repo',
42    '/not/allowed/.git',
43    'file:///not/allowed.git',
44    'git.curoverse.com/arvados.git',
45    'github.com/curoverse/arvados.git',
46   ].each do |url|
47     test "find_commit_range skips fetch_remote_repository for #{url}" do
48       Commit.expects(:fetch_remote_repository).never
49       assert_raises ArgumentError do
50         Commit.find_commit_range url, nil, 'master', []
51       end
52     end
53   end
54
55   test 'fetch_remote_repository does not leak commits across repositories' do
56     url = "http://localhost:1/fake/fake.git"
57     fetch_remote_from_local_repo url, :foo
58     c = Commit.find_commit_range url, nil, 'master', []
59     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57'], c
60
61     url = "http://localhost:2/fake/fake.git"
62     fetch_remote_from_local_repo url, 'file://' + File.expand_path('../../.git', Rails.root)
63     c = Commit.find_commit_range url, nil, '077ba2ad3ea24a929091a9e6ce545c93199b8e57', []
64     assert_equal [], c
65   end
66
67   test 'fetch_remote_repository finds versions' do
68     skip 'not written yet'
69   end
70
71   test 'tag_in_internal_repository creates and updates tags in internal.git' do
72     authorize_with :active
73     gitint = "git --git-dir #{Rails.configuration.git_internal_dir}"
74     IO.read("|#{gitint} tag -d testtag 2>/dev/null") # "no such tag", fine
75     assert_match /^fatal: /, IO.read("|#{gitint} show testtag 2>&1")
76     refute $?.success?
77     Commit.tag_in_internal_repository 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', 'testtag'
78     assert_match /^commit 31ce37f/, IO.read("|#{gitint} show testtag")
79     assert $?.success?
80   end
81
82   test "find_commit_range laundry list" do
83     authorize_with :active
84
85     # single
86     a = Commit.find_commit_range('active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
87     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
88
89     #test "test_branch1" do
90     a = Commit.find_commit_range('active/foo', nil, 'master', nil)
91     assert_includes(a, '077ba2ad3ea24a929091a9e6ce545c93199b8e57')
92
93     #test "test_branch2" do
94     a = Commit.find_commit_range('active/foo', nil, 'b1', nil)
95     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
96
97     #test "test_branch3" do
98     a = Commit.find_commit_range('active/foo', nil, 'HEAD', nil)
99     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
100
101     #test "test_single_revision_repo" do
102     a = Commit.find_commit_range('active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
103     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
104     a = Commit.find_commit_range('arvados', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
105     assert_equal [], a
106
107     #test "test_multi_revision" do
108     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
109     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', nil)
110     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
111
112     #test "test_tag" do
113     # complains "fatal: ambiguous argument 'tag1': unknown revision or path
114     # not in the working tree."
115     a = Commit.find_commit_range('active/foo', 'tag1', 'master', nil)
116     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577'], a
117
118     #test "test_multi_revision_exclude" do
119     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['4fe459abe02d9b365932b8f5dc419439ab4e2577'])
120     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
121
122     #test "test_multi_revision_tagged_exclude" do
123     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
124     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['tag1'])
125     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
126
127     Dir.mktmpdir do |touchdir|
128       # invalid input to maximum
129       a = Commit.find_commit_range('active/foo', nil, "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{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 maximum
134       a = Commit.find_commit_range('active/foo', nil, "$(uname>#{touchdir}/uh_oh)", nil)
135       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
136       assert_equal [], a
137
138       # invalid input to minimum
139       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{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 minimum
144       a = Commit.find_commit_range('active/foo', "$(uname>#{touchdir}/uh_oh)", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
145       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
146       assert_equal [], a
147
148       # invalid input to 'excludes'
149       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
150       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["4fe459abe02d9b365932b8f5dc419439ab4e2577 ; touch #{touchdir}/uh_oh"])
151       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
152       assert_equal [], a
153
154       # invalid input to 'excludes'
155       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
156       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["$(uname>#{touchdir}/uh_oh)"])
157       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
158       assert_equal [], a
159     end
160   end
161 end