5824: Add tests.
authorTom Clegg <tom@curoverse.com>
Thu, 29 Oct 2015 16:06:09 +0000 (12:06 -0400)
committerTom Clegg <tom@curoverse.com>
Thu, 29 Oct 2015 20:11:25 +0000 (16:11 -0400)
services/keep-web/handler_test.go
services/keep-web/server_test.go

index 5e38f25f6ce56b75be6e7e41d36bb49c7445843a..f877ee1e9d0ba1eed3eb256a36dbb1963ac6a0e7 100644 (file)
@@ -172,9 +172,48 @@ func (s *IntegrationSuite) TestVhostRedirectQueryTokenToCookie(c *check.C) {
        s.testVhostRedirectTokenToCookie(c, "GET",
                arvadostest.FooCollection+".example.com/foo",
                "?api_token="+arvadostest.ActiveToken,
-               "text/plain",
+               "",
                "",
                http.StatusOK,
+               "foo",
+       )
+}
+
+func (s *IntegrationSuite) TestSingleOriginSecretLink(c *check.C) {
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.ActiveToken+"/foo",
+               "",
+               "",
+               "",
+               http.StatusOK,
+               "foo",
+       )
+}
+
+// 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) {
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               "example.com/c="+arvadostest.FooCollection+"/t=bogus/foo",
+               "",
+               "",
+               "",
+               http.StatusNotFound,
+               "",
+       )
+}
+
+// Bad token in a cookie (even if it got there via our own
+// query-string-to-cookie redirect) is, in principle, retryable at the
+// same URL so it's 401 Unauthorized.
+func (s *IntegrationSuite) TestVhostRedirectQueryTokenToBogusCookie(c *check.C) {
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               arvadostest.FooCollection+".example.com/foo",
+               "?api_token=thisisabogustoken",
+               "",
+               "",
+               http.StatusUnauthorized,
+               "",
        )
 }
 
@@ -182,9 +221,10 @@ func (s *IntegrationSuite) TestVhostRedirectQueryTokenSingleOriginError(c *check
        s.testVhostRedirectTokenToCookie(c, "GET",
                "example.com/c="+arvadostest.FooCollection+"/foo",
                "?api_token="+arvadostest.ActiveToken,
-               "text/plain",
+               "",
                "",
                http.StatusBadRequest,
+               "",
        )
 }
 
@@ -196,9 +236,10 @@ func (s *IntegrationSuite) TestVhostRedirectQueryTokenTrustAllContent(c *check.C
        s.testVhostRedirectTokenToCookie(c, "GET",
                "example.com/c="+arvadostest.FooCollection+"/foo",
                "?api_token="+arvadostest.ActiveToken,
-               "text/plain",
+               "",
                "",
                http.StatusOK,
+               "foo",
        )
 }
 
@@ -211,17 +252,19 @@ func (s *IntegrationSuite) TestVhostRedirectQueryTokenAttachmentOnlyHost(c *chec
        s.testVhostRedirectTokenToCookie(c, "GET",
                "example.com/c="+arvadostest.FooCollection+"/foo",
                "?api_token="+arvadostest.ActiveToken,
-               "text/plain",
+               "",
                "",
                http.StatusBadRequest,
+               "",
        )
 
        resp := s.testVhostRedirectTokenToCookie(c, "GET",
                "example.com:1234/c="+arvadostest.FooCollection+"/foo",
                "?api_token="+arvadostest.ActiveToken,
-               "text/plain",
+               "",
                "",
                http.StatusOK,
+               "foo",
        )
        c.Check(resp.Header().Get("Content-Disposition"), check.Equals, "attachment")
 }
@@ -233,6 +276,7 @@ func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie(c *check.C) {
                "application/x-www-form-urlencoded",
                url.Values{"api_token": {arvadostest.ActiveToken}}.Encode(),
                http.StatusOK,
+               "foo",
        )
 }
 
@@ -243,23 +287,52 @@ func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie404(c *check.C)
                "application/x-www-form-urlencoded",
                url.Values{"api_token": {arvadostest.SpectatorToken}}.Encode(),
                http.StatusNotFound,
+               "",
+       )
+}
+
+func (s *IntegrationSuite) TestAnonymousTokenOK(c *check.C) {
+       anonymousTokens = []string{arvadostest.AnonymousToken}
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               "example.com/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
+               "",
+               "",
+               "",
+               http.StatusOK,
+               "Hello world\n",
+       )
+}
+
+func (s *IntegrationSuite) TestAnonymousTokenError(c *check.C) {
+       anonymousTokens = []string{"anonymousTokenConfiguredButInvalid"}
+       s.testVhostRedirectTokenToCookie(c, "GET",
+               "example.com/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
+               "",
+               "",
+               "",
+               http.StatusNotFound,
+               "",
        )
 }
 
-func (s *IntegrationSuite) testVhostRedirectTokenToCookie(c *check.C, method, hostPath, queryString, contentType, body string, expectStatus int) *httptest.ResponseRecorder {
+func (s *IntegrationSuite) testVhostRedirectTokenToCookie(c *check.C, method, hostPath, queryString, contentType, reqBody string, expectStatus int, expectRespBody string) *httptest.ResponseRecorder {
        u, _ := url.Parse(`http://` + hostPath + queryString)
        req := &http.Request{
                Method: method,
                Host:   u.Host,
                URL:    u,
                Header: http.Header{"Content-Type": {contentType}},
-               Body:   ioutil.NopCloser(strings.NewReader(body)),
+               Body:   ioutil.NopCloser(strings.NewReader(reqBody)),
        }
 
        resp := httptest.NewRecorder()
+       defer func() {
+               c.Check(resp.Code, check.Equals, expectStatus)
+               c.Check(resp.Body.String(), check.Equals, expectRespBody)
+       }()
+
        (&handler{}).ServeHTTP(resp, req)
        if resp.Code != http.StatusSeeOther {
-               c.Assert(resp.Code, check.Equals, expectStatus)
                return resp
        }
        c.Check(resp.Body.String(), check.Matches, `.*href="//`+regexp.QuoteMeta(html.EscapeString(hostPath))+`".*`)
@@ -279,9 +352,5 @@ func (s *IntegrationSuite) testVhostRedirectTokenToCookie(c *check.C, method, ho
        resp = httptest.NewRecorder()
        (&handler{}).ServeHTTP(resp, req)
        c.Check(resp.Header().Get("Location"), check.Equals, "")
-       c.Check(resp.Code, check.Equals, expectStatus)
-       if expectStatus == http.StatusOK {
-               c.Check(resp.Body.String(), check.Equals, "foo")
-       }
        return resp
 }
index 0a38384f076fe7aba70f1cfe6940decb469e0dbe..8e3a21a4c69fde37479263c827049220ba8155d8 100644 (file)
@@ -50,14 +50,18 @@ func (s *IntegrationSuite) TestNoToken(c *check.C) {
 // really works against the server.
 func (s *IntegrationSuite) Test404(c *check.C) {
        for _, uri := range []string{
-               // Routing errors
+               // Routing errors (always 404 regardless of what's stored in Keep)
                "/",
                "/foo",
                "/download",
                "/collections",
                "/collections/",
+               // Implicit/generated index is not implemented yet;
+               // until then, return 404.
                "/collections/" + arvadostest.FooCollection,
                "/collections/" + arvadostest.FooCollection + "/",
+               "/collections/" + arvadostest.FooBarDirCollection + "/dir1",
+               "/collections/" + arvadostest.FooBarDirCollection + "/dir1/",
                // Non-existent file in collection
                "/collections/" + arvadostest.FooCollection + "/theperthcountyconspiracy",
                "/collections/download/" + arvadostest.FooCollection + "/" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
@@ -120,7 +124,6 @@ func (s *IntegrationSuite) test100BlockFile(c *check.C, blocksize int) {
 }
 
 type curlCase struct {
-       id      string
        auth    string
        host    string
        path    string
@@ -137,6 +140,12 @@ func (s *IntegrationSuite) Test200(c *check.C) {
                        path:    "/foo",
                        dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
                },
+               {
+                       auth:    arvadostest.ActiveToken,
+                       host:    arvadostest.FooCollection + ".collections.example.com",
+                       path:    "/foo",
+                       dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
+               },
                {
                        host:    strings.Replace(arvadostest.FooPdh, "+", "-", 1) + ".collections.example.com",
                        path:    "/t=" + arvadostest.ActiveToken + "/foo",
@@ -170,7 +179,7 @@ func (s *IntegrationSuite) Test200(c *check.C) {
                        dataMD5: "acbd18db4cc2f85cedef654fccc4a4d8",
                },
 
-               // Anonymously accessible user agreement
+               // Anonymously accessible data
                {
                        path:    "/c=" + arvadostest.HelloWorldCollection + "/Hello%20world.txt",
                        dataMD5: "f0ef7081e1539ac00ef5b761b4fb01b3",