| 1 |
// Copyright 2023 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package api_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"encoding/json" |
| 20 |
"io" |
| 21 |
"net/http" |
| 22 |
"net/http/httptest" |
| 23 |
"os" |
| 24 |
"path" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/in-toto/archivista/pkg/api" |
| 28 |
"github.com/in-toto/go-witness/dsse" |
| 29 |
"github.com/stretchr/testify/suite" |
| 30 |
) |
| 31 |
|
| 32 |
// Test Suite: UT APIDownload |
| 33 |
type UTAPIDownloadSuite struct { |
| 34 |
suite.Suite |
| 35 |
} |
| 36 |
|
| 37 |
func TestAPIDownloadSuite(t *testing.T) { |
| 38 |
suite.Run(t, new(UTAPIDownloadSuite)) |
| 39 |
} |
| 40 |
|
| 41 |
func (ut *UTAPIDownloadSuite) Test_Download() { |
| 42 |
|
| 43 |
testEnvelope, err := os.ReadFile("../../test/package.attestation.json") |
| 44 |
if err != nil { |
| 45 |
ut.FailNow(err.Error()) |
| 46 |
} |
| 47 |
expectedEnvelop := dsse.Envelope{} |
| 48 |
err = json.Unmarshal(testEnvelope, &expectedEnvelop) |
| 49 |
if err != nil { |
| 50 |
ut.FailNow(err.Error()) |
| 51 |
} |
| 52 |
|
| 53 |
testServer := httptest.NewServer( |
| 54 |
http.HandlerFunc( |
| 55 |
func(w http.ResponseWriter, r *http.Request) { |
| 56 |
w.WriteHeader(http.StatusOK) |
| 57 |
_, err = w.Write(testEnvelope) |
| 58 |
if err != nil { |
| 59 |
ut.FailNow(err.Error()) |
| 60 |
} |
| 61 |
}, |
| 62 |
), |
| 63 |
) |
| 64 |
defer testServer.Close() |
| 65 |
ctx := context.TODO() |
| 66 |
|
| 67 |
// test api.Download happy flow |
| 68 |
resp, err := api.Download(ctx, testServer.URL, "gitoid_test") |
| 69 |
if err != nil { |
| 70 |
ut.FailNow(err.Error()) |
| 71 |
} |
| 72 |
|
| 73 |
ut.Equal(expectedEnvelop, resp) |
| 74 |
} |
| 75 |
|
| 76 |
func (ut *UTAPIDownloadSuite) Test_Download_DecodeFailure() { |
| 77 |
testServer := httptest.NewServer( |
| 78 |
http.HandlerFunc( |
| 79 |
func(w http.ResponseWriter, r *http.Request) { |
| 80 |
w.WriteHeader(http.StatusOK) |
| 81 |
_, err := w.Write([]byte(`invalid`)) |
| 82 |
if err != nil { |
| 83 |
ut.FailNow(err.Error()) |
| 84 |
} |
| 85 |
}, |
| 86 |
), |
| 87 |
) |
| 88 |
defer testServer.Close() |
| 89 |
ctx := context.TODO() |
| 90 |
|
| 91 |
// test api.Download happy flow |
| 92 |
resp, err := api.Download(ctx, testServer.URL, "gitoid_test") |
| 93 |
ut.Error(err) |
| 94 |
ut.Equal(dsse.Envelope{}, resp) |
| 95 |
} |
| 96 |
|
| 97 |
func (ut *UTAPIDownloadSuite) Test_DownloadWithReader() { |
| 98 |
|
| 99 |
// mock server |
| 100 |
expected := `{"body":"body"}` |
| 101 |
testServer := httptest.NewServer( |
| 102 |
http.HandlerFunc( |
| 103 |
func(w http.ResponseWriter, r *http.Request) { |
| 104 |
w.WriteHeader(http.StatusOK) |
| 105 |
_, err := w.Write([]byte(expected)) |
| 106 |
if err != nil { |
| 107 |
ut.FailNow(err.Error()) |
| 108 |
} |
| 109 |
}, |
| 110 |
), |
| 111 |
) |
| 112 |
defer testServer.Close() |
| 113 |
|
| 114 |
// context |
| 115 |
ctx := context.TODO() |
| 116 |
|
| 117 |
// temporary file |
| 118 |
tempDir := os.TempDir() |
| 119 |
dst, err := os.Create(path.Join(tempDir, "test")) |
| 120 |
if err != nil { |
| 121 |
ut.FailNow(err.Error()) |
| 122 |
} |
| 123 |
err = api.DownloadWithWriter(ctx, testServer.URL, "gitoid", dst) |
| 124 |
if err != nil { |
| 125 |
ut.FailNow(err.Error()) |
| 126 |
} |
| 127 |
|
| 128 |
// validate result |
| 129 |
result, err := os.ReadFile(path.Join(tempDir, "test")) |
| 130 |
if err != nil { |
| 131 |
ut.FailNow(err.Error()) |
| 132 |
} |
| 133 |
ut.Equal(expected, string(result)) |
| 134 |
} |
| 135 |
|
| 136 |
func (ut *UTAPIDownloadSuite) Test_DownloadWithWriter_NoServer() { |
| 137 |
|
| 138 |
// context |
| 139 |
ctx := context.TODO() |
| 140 |
|
| 141 |
// dst as stdout |
| 142 |
var dst io.Writer = os.Stdout |
| 143 |
|
| 144 |
err := api.DownloadWithWriter(ctx, "http://invalid-archivista", "gitoid_test", dst) |
| 145 |
ut.Error(err) |
| 146 |
} |
| 147 |
|
| 148 |
func (ut *UTAPIDownloadSuite) Test_DownloadWithWriter_BadStatusCode() { |
| 149 |
|
| 150 |
// mock server |
| 151 |
testServer := httptest.NewServer( |
| 152 |
http.HandlerFunc( |
| 153 |
func(w http.ResponseWriter, r *http.Request) { |
| 154 |
w.WriteHeader(http.StatusInternalServerError) |
| 155 |
_, err := w.Write([]byte(`Internal Server Error`)) |
| 156 |
if err != nil { |
| 157 |
ut.FailNow(err.Error()) |
| 158 |
} |
| 159 |
}, |
| 160 |
), |
| 161 |
) |
| 162 |
defer testServer.Close() |
| 163 |
|
| 164 |
// dst as stdout |
| 165 |
var dst io.Writer = os.Stdout |
| 166 |
|
| 167 |
// context |
| 168 |
ctx := context.TODO() |
| 169 |
|
| 170 |
err := api.DownloadWithWriter(ctx, testServer.URL, "gitoid_test", dst) |
| 171 |
ut.ErrorContains(err, "Internal Server Error") |
| 172 |
} |