12483: Error 405 if writing to PDH, 403 if lacking write permission.
[arvados.git] / services / keep-web / cadaver_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "io"
10         "io/ioutil"
11         "net/url"
12         "os"
13         "os/exec"
14         "strings"
15
16         "git.curoverse.com/arvados.git/sdk/go/arvados"
17         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
18         check "gopkg.in/check.v1"
19 )
20
21 func (s *IntegrationSuite) TestWebdavWithCadaver(c *check.C) {
22         testdata := []byte("the human tragedy consists in the necessity of living with the consequences of actions performed under the pressure of compulsions we do not understand")
23
24         localfile, err := ioutil.TempFile("", "localfile")
25         c.Assert(err, check.IsNil)
26         defer os.Remove(localfile.Name())
27         localfile.Write(testdata)
28
29         emptyfile, err := ioutil.TempFile("", "emptyfile")
30         c.Assert(err, check.IsNil)
31         defer os.Remove(emptyfile.Name())
32
33         checkfile, err := ioutil.TempFile("", "checkfile")
34         c.Assert(err, check.IsNil)
35         defer os.Remove(checkfile.Name())
36
37         var newCollection arvados.Collection
38         arv := arvados.NewClientFromEnv()
39         arv.AuthToken = arvadostest.ActiveToken
40         err = arv.RequestAndDecode(&newCollection, "POST", "/arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
41         c.Assert(err, check.IsNil)
42         writePath := "/c=" + newCollection.UUID + "/t=" + arv.AuthToken + "/"
43
44         readPath := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
45         type testcase struct {
46                 path  string
47                 cmd   string
48                 match string
49                 data  []byte
50         }
51         for _, trial := range []testcase{
52                 {
53                         path:  readPath,
54                         cmd:   "ls\n",
55                         match: `(?ms).*dir1 *0 .*`,
56                 },
57                 {
58                         path:  readPath,
59                         cmd:   "ls dir1\n",
60                         match: `(?ms).*bar *3.*foo *3 .*`,
61                 },
62                 {
63                         path:  readPath + "_/dir1",
64                         cmd:   "ls\n",
65                         match: `(?ms).*bar *3.*foo *3 .*`,
66                 },
67                 {
68                         path:  readPath + "dir1/",
69                         cmd:   "ls\n",
70                         match: `(?ms).*bar *3.*foo *3 .*`,
71                 },
72                 {
73                         path:  writePath,
74                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
75                         match: `(?ms).*Not Found.*`,
76                 },
77                 {
78                         path:  writePath,
79                         cmd:   "put '" + emptyfile.Name() + "' emptyfile\n",
80                         match: `(?ms).*Uploading .* succeeded.*`,
81                 },
82                 {
83                         path:  writePath,
84                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
85                         match: `(?ms).*Downloading .* succeeded.*`,
86                         data:  []byte{},
87                 },
88                 {
89                         path:  writePath,
90                         cmd:   "put '" + localfile.Name() + "' testfile\n",
91                         match: `(?ms).*Uploading .* succeeded.*`,
92                 },
93                 {
94                         path:  writePath,
95                         cmd:   "get testfile '" + checkfile.Name() + "'\n",
96                         match: `(?ms).*succeeded.*`,
97                         data:  testdata,
98                 },
99                 {
100                         path:  writePath,
101                         cmd:   "move testfile newdir0/\n",
102                         match: `(?ms).*Moving .* succeeded.*`,
103                 },
104                 {
105                         path:  writePath,
106                         cmd:   "move testfile newdir0/\n",
107                         match: `(?ms).*Moving .* failed.*`,
108                 },
109                 {
110                         path:  writePath,
111                         cmd:   "ls\n",
112                         match: `(?ms).*newdir0.* 0 .*`,
113                 },
114                 {
115                         path:  writePath,
116                         cmd:   "move newdir0/testfile emptyfile/bogus/\n",
117                         match: `(?ms).*Moving .* failed.*`,
118                 },
119                 {
120                         path:  writePath,
121                         cmd:   "mkcol newdir1\n",
122                         match: `(?ms).*Creating .* succeeded.*`,
123                 },
124                 {
125                         path:  writePath,
126                         cmd:   "move newdir0/testfile newdir1/\n",
127                         match: `(?ms).*Moving .* succeeded.*`,
128                 },
129                 {
130                         path:  writePath,
131                         cmd:   "put '" + localfile.Name() + "' newdir1/testfile1\n",
132                         match: `(?ms).*Uploading .* succeeded.*`,
133                 },
134                 {
135                         path:  writePath,
136                         cmd:   "mkcol newdir2\n",
137                         match: `(?ms).*Creating .* succeeded.*`,
138                 },
139                 {
140                         path:  writePath,
141                         cmd:   "put '" + localfile.Name() + "' newdir2/testfile2\n",
142                         match: `(?ms).*Uploading .* succeeded.*`,
143                 },
144                 {
145                         path:  writePath,
146                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
147                         match: `(?ms).*succeeded.*`,
148                         data:  testdata,
149                 },
150                 {
151                         path:  writePath,
152                         cmd:   "rmcol newdir2\n",
153                         match: `(?ms).*Deleting collection .* succeeded.*`,
154                 },
155                 {
156                         path:  writePath,
157                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
158                         match: `(?ms).*Downloading .* failed.*`,
159                 },
160                 {
161                         path:  "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
162                         cmd:   "put '" + localfile.Name() + "' foo\n",
163                         match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
164                 },
165                 {
166                         path:  "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/t=" + arv.AuthToken + "/",
167                         cmd:   "put '" + localfile.Name() + "' foo\n",
168                         match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
169                 },
170         } {
171                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
172
173                 os.Remove(checkfile.Name())
174
175                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
176                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
177                 stdout, err := cmd.StdoutPipe()
178                 c.Assert(err, check.Equals, nil)
179                 cmd.Stderr = cmd.Stdout
180                 go cmd.Start()
181
182                 var buf bytes.Buffer
183                 _, err = io.Copy(&buf, stdout)
184                 c.Check(err, check.Equals, nil)
185                 err = cmd.Wait()
186                 c.Check(err, check.Equals, nil)
187                 c.Check(buf.String(), check.Matches, trial.match)
188
189                 if trial.data == nil {
190                         continue
191                 }
192                 checkfile, err = os.Open(checkfile.Name())
193                 c.Assert(err, check.IsNil)
194                 checkfile.Seek(0, os.SEEK_SET)
195                 got, err := ioutil.ReadAll(checkfile)
196                 c.Check(got, check.DeepEquals, trial.data)
197                 c.Check(err, check.IsNil)
198         }
199 }