19963: Fix crash on no-cache + invalid token.
[arvados.git] / services / keep-web / handler_test.go
index 768013185ae7d50dc450fecd342ebcdc343db059..d368b4447f355c8dd51e7e03adec68900d85df61 100644 (file)
@@ -366,6 +366,24 @@ func (s *IntegrationSuite) TestVhostPortMatch(c *check.C) {
        }
 }
 
+func (s *IntegrationSuite) do(method string, urlstring string, token string, hdr http.Header) (*http.Request, *httptest.ResponseRecorder) {
+       u := mustParseURL(urlstring)
+       if hdr == nil && token != "" {
+               hdr = http.Header{"Authorization": {"Bearer " + token}}
+       } else if hdr == nil {
+               hdr = http.Header{}
+       } else if token != "" {
+               panic("must not pass both token and hdr")
+       }
+       return s.doReq(&http.Request{
+               Method:     method,
+               Host:       u.Host,
+               URL:        u,
+               RequestURI: u.RequestURI(),
+               Header:     hdr,
+       })
+}
+
 func (s *IntegrationSuite) doReq(req *http.Request) (*http.Request, *httptest.ResponseRecorder) {
        resp := httptest.NewRecorder()
        s.handler.ServeHTTP(resp, req)
@@ -409,6 +427,26 @@ func (s *IntegrationSuite) TestSingleOriginSecretLink(c *check.C) {
        )
 }
 
+func (s *IntegrationSuite) TestCollectionSharingToken(c *check.C) {
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               "example.com/c="+arvadostest.FooFileCollectionUUID+"/t="+arvadostest.FooFileCollectionSharingToken+"/foo",
+               "",
+               nil,
+               "",
+               http.StatusOK,
+               "foo",
+       )
+       // Same valid sharing token, but requesting a different collection
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.FooFileCollectionSharingToken+"/foo",
+               "",
+               nil,
+               "",
+               http.StatusNotFound,
+               notFoundMessage+"\n",
+       )
+}
+
 // Bad token in URL is 404 Not Found because it doesn't make sense to
 // retry the same URL with different authorization.
 func (s *IntegrationSuite) TestSingleOriginSecretLinkBadToken(c *check.C) {
@@ -493,6 +531,26 @@ func (s *IntegrationSuite) TestVhostRedirectQueryTokenToBogusCookie(c *check.C)
        )
 }
 
+func (s *IntegrationSuite) TestVhostRedirectWithNoCache(c *check.C) {
+       resp := s.testVhostRedirectTokenToCookie(c, "GET",
+               arvadostest.FooCollection+".example.com/foo",
+               "?api_token=thisisabogustoken",
+               http.Header{
+                       "Sec-Fetch-Mode": {"navigate"},
+                       "Cache-Control":  {"no-cache"},
+               },
+               "",
+               http.StatusSeeOther,
+               "",
+       )
+       u, err := url.Parse(resp.Header().Get("Location"))
+       c.Assert(err, check.IsNil)
+       c.Logf("redirected to %s", u)
+       c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
+       c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
+       c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
+}
+
 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSingleOriginError(c *check.C) {
        s.testVhostRedirectTokenToCookie(c, "GET",
                "example.com/c="+arvadostest.FooCollection+"/foo",
@@ -701,7 +759,7 @@ func (s *IntegrationSuite) TestForwardSlashSubstitution(c *check.C) {
 
        base := "http://download.example.com/by_id/" + coll.OwnerUUID + "/"
        for tryURL, expectRegexp := range map[string]string{
-               base:                          `(?ms).*href="./` + nameShownEscaped + `/"\S+` + nameShown + `.*`,
+               base: `(?ms).*href="./` + nameShownEscaped + `/"\S+` + nameShown + `.*`,
                base + nameShownEscaped + "/": `(?ms).*href="./filename"\S+filename.*`,
        } {
                u, _ := url.Parse(tryURL)
@@ -1245,7 +1303,7 @@ func copyHeader(h http.Header) http.Header {
 }
 
 func (s *IntegrationSuite) checkUploadDownloadRequest(c *check.C, req *http.Request,
-       successCode int, direction string, perm bool, userUuid string, collectionUuid string, filepath string) {
+       successCode int, direction string, perm bool, userUuid, collectionUuid, collectionPDH, filepath string) {
 
        client := arvados.NewClientFromEnv()
        client.AuthToken = arvadostest.AdminToken
@@ -1258,6 +1316,7 @@ func (s *IntegrationSuite) checkUploadDownloadRequest(c *check.C, req *http.Requ
        c.Check(err, check.IsNil)
        c.Check(logentries.Items, check.HasLen, 1)
        lastLogId := logentries.Items[0].ID
+       c.Logf("lastLogId: %d", lastLogId)
 
        var logbuf bytes.Buffer
        logger := logrus.New()
@@ -1274,6 +1333,7 @@ func (s *IntegrationSuite) checkUploadDownloadRequest(c *check.C, req *http.Requ
                deadline := time.Now().Add(time.Second)
                for {
                        c.Assert(time.Now().After(deadline), check.Equals, false, check.Commentf("timed out waiting for log entry"))
+                       logentries = arvados.LogList{}
                        err = client.RequestAndDecode(&logentries, "GET", "arvados/v1/logs", nil,
                                arvados.ResourceListParams{
                                        Filters: []arvados.Filter{
@@ -1288,6 +1348,7 @@ func (s *IntegrationSuite) checkUploadDownloadRequest(c *check.C, req *http.Requ
                                logentries.Items[0].ID > lastLogId &&
                                logentries.Items[0].ObjectUUID == userUuid &&
                                logentries.Items[0].Properties["collection_uuid"] == collectionUuid &&
+                               (collectionPDH == "" || logentries.Items[0].Properties["portable_data_hash"] == collectionPDH) &&
                                logentries.Items[0].Properties["collection_file_path"] == filepath {
                                break
                        }
@@ -1321,7 +1382,7 @@ func (s *IntegrationSuite) TestDownloadLoggingPermission(c *check.C) {
                                },
                        }
                        s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", adminperm,
-                               arvadostest.AdminUserUUID, arvadostest.FooCollection, "foo")
+                               arvadostest.AdminUserUUID, arvadostest.FooCollection, arvadostest.FooCollectionPDH, "foo")
 
                        // Test user permission
                        req = &http.Request{
@@ -1334,7 +1395,7 @@ func (s *IntegrationSuite) TestDownloadLoggingPermission(c *check.C) {
                                },
                        }
                        s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", userperm,
-                               arvadostest.ActiveUserUUID, arvadostest.FooCollection, "foo")
+                               arvadostest.ActiveUserUUID, arvadostest.FooCollection, arvadostest.FooCollectionPDH, "foo")
                }
        }
 
@@ -1354,7 +1415,7 @@ func (s *IntegrationSuite) TestDownloadLoggingPermission(c *check.C) {
                        },
                }
                s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", true,
-                       arvadostest.ActiveUserUUID, arvadostest.MultilevelCollection1, "dir1/subdir/file1")
+                       arvadostest.ActiveUserUUID, arvadostest.MultilevelCollection1, arvadostest.MultilevelCollection1PDH, "dir1/subdir/file1")
        }
 
        u = mustParseURL("http://" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + ".keep-web.example/foo")
@@ -1368,7 +1429,7 @@ func (s *IntegrationSuite) TestDownloadLoggingPermission(c *check.C) {
                },
        }
        s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", true,
-               arvadostest.ActiveUserUUID, arvadostest.FooCollection, "foo")
+               arvadostest.ActiveUserUUID, "", arvadostest.FooCollectionPDH, "foo")
 }
 
 func (s *IntegrationSuite) TestUploadLoggingPermission(c *check.C) {
@@ -1408,7 +1469,7 @@ func (s *IntegrationSuite) TestUploadLoggingPermission(c *check.C) {
                                Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
                        }
                        s.checkUploadDownloadRequest(c, req, http.StatusCreated, "upload", adminperm,
-                               arvadostest.AdminUserUUID, coll.UUID, "bar")
+                               arvadostest.AdminUserUUID, coll.UUID, "", "bar")
 
                        // Test user permission
                        req = &http.Request{
@@ -1422,7 +1483,7 @@ func (s *IntegrationSuite) TestUploadLoggingPermission(c *check.C) {
                                Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
                        }
                        s.checkUploadDownloadRequest(c, req, http.StatusCreated, "upload", userperm,
-                               arvadostest.ActiveUserUUID, coll.UUID, "bar")
+                               arvadostest.ActiveUserUUID, coll.UUID, "", "bar")
                }
        }
 }