From cb2afaa4c084a62f5876595a034393d71ed84fd4 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Mon, 29 Aug 2022 22:45:07 +0200 Subject: [PATCH] test that inaccessible files result in status 'internal error' --- handlers_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/handlers_test.go b/handlers_test.go index e5931d7..a1616cc 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -10,6 +10,7 @@ import ( "net/http" "net/http/httptest" "os" + "path" "path/filepath" "reflect" "strings" @@ -269,6 +270,74 @@ func TestResticHandler(t *testing.T) { } } +// TestResticErrorHandler runs tests on the restic handler error handling. +func TestResticErrorHandler(t *testing.T) { + var tests = []struct { + seq []TestRequest + }{ + // Test inaccessible file + { + []TestRequest{{ + req: newRequest(t, "GET", "/config", nil), + want: []wantFunc{wantCode(http.StatusInternalServerError)}, + }}, + }, + { + []TestRequest{{ + req: newRequest(t, "GET", "/parent4/config", nil), + want: []wantFunc{wantCode(http.StatusNotFound)}, + }}, + }, + } + + // setup the server with a local backend in a temporary directory + tempdir, err := ioutil.TempDir("", "rest-server-test-") + if err != nil { + t.Fatal(err) + } + + // make sure the tempdir is properly removed + defer func() { + err := os.RemoveAll(tempdir) + if err != nil { + t.Fatal(err) + } + }() + + // set append-only mode and configure path + mux, err := NewHandler(&Server{ + AppendOnly: true, + Path: tempdir, + NoAuth: true, + Debug: true, + }) + if err != nil { + t.Fatalf("error from NewHandler: %v", err) + } + + // create the repo + checkRequest(t, mux.ServeHTTP, + newRequest(t, "POST", "/?create=true", nil), + []wantFunc{wantCode(http.StatusOK)}) + // create inaccessible config + checkRequest(t, mux.ServeHTTP, + newRequest(t, "POST", "/config", strings.NewReader("example")), + []wantFunc{wantCode(http.StatusOK)}) + err = os.Chmod(path.Join(tempdir, "config"), 0o000) + if err != nil { + t.Fatal(err) + } + + for _, test := range tests { + t.Run("", func(t *testing.T) { + for i, seq := range test.seq { + t.Logf("request %v: %v %v", i, seq.req.Method, seq.req.URL.Path) + checkRequest(t, mux.ServeHTTP, seq.req, seq.want) + } + }) + } +} + func TestSplitURLPath(t *testing.T) { var tests = []struct { // Params