Debian Codegraph @

pkg/api/upload_test.go

raw
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 "net/http"
21 "net/http/httptest"
22 "os"
23 "testing"
24
25 "github.com/in-toto/archivista/pkg/api"
26 "github.com/in-toto/go-witness/dsse"
27 "github.com/stretchr/testify/suite"
28 )
29
30 // Test Suite: UT APIStore
31 type UTAPIStoreSuite struct {
32 suite.Suite
33 }
34
35 func TestAPIStoreSuite(t *testing.T) {
36 suite.Run(t, new(UTAPIStoreSuite))
37 }
38
39 func (ut *UTAPIStoreSuite) Test_Store() {
40
41 testServer := httptest.NewServer(
42 http.HandlerFunc(
43 func(w http.ResponseWriter, r *http.Request) {
44 w.WriteHeader(http.StatusOK)
45 _, err := w.Write([]byte(`{"gitoid":"test"}`))
46 if err != nil {
47 ut.FailNow(err.Error())
48 }
49 },
50 ),
51 )
52 defer testServer.Close()
53 ctx := context.TODO()
54
55 // load test valid test file and parse the dsse envelop
56 attFile, err := os.ReadFile("../../test/package.attestation.json")
57 if err != nil {
58 ut.FailNow(err.Error())
59 }
60 attEnvelop := &dsse.Envelope{}
61 err = json.Unmarshal(attFile, &attEnvelop)
62 if err != nil {
63 ut.FailNow(err.Error())
64 }
65
66 // test api.Store happy flow
67 resp, err := api.Store(ctx, testServer.URL, *attEnvelop)
68 if err != nil {
69 ut.FailNow(err.Error())
70 }
71
72 ut.Equal(resp, api.StoreResponse{Gitoid: "test"})
73 }
74
75 func (ut *UTAPIStoreSuite) Test_StoreWithReader() {
76
77 // mock server
78 testServer := httptest.NewServer(
79 http.HandlerFunc(
80 func(w http.ResponseWriter, r *http.Request) {
81 w.WriteHeader(http.StatusOK)
82 _, err := w.Write([]byte(`{"gitoid":"test"}`))
83 if err != nil {
84 ut.FailNow(err.Error())
85 }
86 },
87 ),
88 )
89 defer testServer.Close()
90
91 // io.Reader file
92 attIo, err := os.Open("../../test/package.attestation.json")
93 if err != nil {
94 ut.FailNow(err.Error())
95 }
96
97 // context
98 ctx := context.TODO()
99
100 resp, err := api.StoreWithReader(ctx, testServer.URL, attIo)
101 if err != nil {
102 ut.FailNow(err.Error())
103 }
104 ut.Equal(resp, api.StoreResponse{Gitoid: "test"})
105 }
106
107 func (ut *UTAPIStoreSuite) Test_StoreWithReader_NoServer() {
108
109 // io.Reader file
110 attIo, err := os.Open("../../test/package.attestation.json")
111 if err != nil {
112 ut.FailNow(err.Error())
113 }
114
115 // context
116 ctx := context.TODO()
117
118 resp, err := api.StoreWithReader(ctx, "http://invalid-archivista", attIo)
119 ut.Error(err)
120 ut.Equal(resp, api.StoreResponse{})
121 }
122
123 func (ut *UTAPIStoreSuite) Test_StoreWithReader_InvalidResponseBody() {
124
125 // mock server
126 testServer := httptest.NewServer(
127 http.HandlerFunc(
128 func(w http.ResponseWriter, r *http.Request) {
129 w.WriteHeader(http.StatusOK)
130 _, err := w.Write([]byte(`{"invalid":"invalid"}`))
131 if err != nil {
132 ut.FailNow(err.Error())
133 }
134 },
135 ),
136 )
137 defer testServer.Close()
138
139 // io.Reader file
140 attIo, err := os.Open("../../test/package.attestation.json")
141 if err != nil {
142 ut.FailNow(err.Error())
143 }
144
145 // context
146 ctx := context.TODO()
147
148 resp, err := api.StoreWithReader(ctx, testServer.URL, attIo)
149 if err != nil {
150 ut.FailNow(err.Error())
151 }
152 ut.Equal(resp, api.StoreResponse{})
153 }
154
155 func (ut *UTAPIStoreSuite) Test_StoreWithReader_BadStatusCode() {
156
157 // mock server
158 testServer := httptest.NewServer(
159 http.HandlerFunc(
160 func(w http.ResponseWriter, r *http.Request) {
161 w.WriteHeader(http.StatusInternalServerError)
162 _, err := w.Write([]byte(`Internal Server Error`))
163 if err != nil {
164 ut.FailNow(err.Error())
165 }
166 },
167 ),
168 )
169 defer testServer.Close()
170
171 // io.Reader file
172 attIo, err := os.Open("../../test/package.attestation.json")
173 if err != nil {
174 ut.FailNow(err.Error())
175 }
176
177 // context
178 ctx := context.TODO()
179
180 resp, err := api.StoreWithReader(ctx, testServer.URL, attIo)
181 ut.ErrorContains(err, "Internal Server Error")
182 ut.Equal(resp, api.StoreResponse{})
183 }
184
185 func (ut *UTAPIStoreSuite) Test_StoreWithReader_BadJSONBody() {
186
187 // mock server
188 testServer := httptest.NewServer(
189 http.HandlerFunc(
190 func(w http.ResponseWriter, r *http.Request) {
191 w.WriteHeader(http.StatusOK)
192 },
193 ),
194 )
195 defer testServer.Close()
196
197 // io.Reader file
198 attIo, err := os.Open("../../test/package.attestation.json")
199 if err != nil {
200 ut.FailNow(err.Error())
201 }
202
203 // context
204 ctx := context.TODO()
205
206 resp, err := api.StoreWithReader(ctx, testServer.URL, attIo)
207 ut.ErrorContains(err, "unexpected end of JSON input")
208 ut.Equal(resp, api.StoreResponse{})
209 }