-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: Include tests for pkg/api
#138
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright 2023 The Archivista Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/in-toto/archivista/pkg/api" | ||
"github.com/in-toto/go-witness/dsse" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// Test Suite: UT APIDownload | ||
type UTAPIDownloadSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestAPIDownloadSuite(t *testing.T) { | ||
suite.Run(t, new(UTAPIDownloadSuite)) | ||
} | ||
|
||
func (ut *UTAPIDownloadSuite) Test_Download() { | ||
|
||
testEnvelope, err := os.ReadFile("../../test/package.attestation.json") | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
expectedEnvelop := dsse.Envelope{} | ||
err = json.Unmarshal(testEnvelope, &expectedEnvelop) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
|
||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write(testEnvelope) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
ctx := context.TODO() | ||
|
||
// test api.Download happy flow | ||
resp, err := api.Download(ctx, testServer.URL, "gitoid_test") | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
|
||
ut.Equal(expectedEnvelop, resp) | ||
} | ||
|
||
func (ut *UTAPIDownloadSuite) Test_Download_DecodeFailure() { | ||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(`invalid`)) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
ctx := context.TODO() | ||
|
||
// test api.Download happy flow | ||
resp, err := api.Download(ctx, testServer.URL, "gitoid_test") | ||
ut.Error(err) | ||
ut.Equal(dsse.Envelope{}, resp) | ||
} | ||
|
||
func (ut *UTAPIDownloadSuite) Test_DownloadWithReader() { | ||
|
||
// mock server | ||
expected := `{"body":"body"}` | ||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(expected)) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
|
||
// context | ||
ctx := context.TODO() | ||
|
||
// temporary file | ||
tempDir := os.TempDir() | ||
dst, err := os.Create(path.Join(tempDir, "test")) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
err = api.DownloadWithWriter(ctx, testServer.URL, "gitoid", dst) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
|
||
// validate result | ||
result, err := os.ReadFile(path.Join(tempDir, "test")) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
ut.Equal(expected, string(result)) | ||
} | ||
|
||
func (ut *UTAPIDownloadSuite) Test_DownloadWithWriter_NoServer() { | ||
|
||
// context | ||
ctx := context.TODO() | ||
|
||
// dst as stdout | ||
var dst io.Writer = os.Stdout | ||
|
||
err := api.DownloadWithWriter(ctx, "http://invalid-archivista", "gitoid_test", dst) | ||
ut.Error(err) | ||
} | ||
|
||
func (ut *UTAPIDownloadSuite) Test_DownloadWithWriter_BadStatusCode() { | ||
|
||
// mock server | ||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
_, err := w.Write([]byte(`Internal Server Error`)) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
|
||
// dst as stdout | ||
var dst io.Writer = os.Stdout | ||
|
||
// context | ||
ctx := context.TODO() | ||
|
||
err := api.DownloadWithWriter(ctx, testServer.URL, "gitoid_test", dst) | ||
ut.ErrorContains(err, "Internal Server Error") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright 2023 The Archivista Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/in-toto/archivista/pkg/api" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
// Test Suite: UT APIStore | ||
type UTAPIGraphQLSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestAPIGraphQLSuite(t *testing.T) { | ||
suite.Run(t, new(UTAPIGraphQLSuite)) | ||
} | ||
|
||
func (ut *UTAPIGraphQLSuite) Test_Store() { | ||
|
||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(`{"data": {"data": "test"}}`)) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
ctx := context.TODO() | ||
|
||
type testSubjectVar struct { | ||
Gitoid string `json:"gitoid"` | ||
} | ||
|
||
type testSubjectResult struct { | ||
Data string `json:"data"` | ||
} | ||
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) | ||
ut.NoError(err) | ||
ut.Equal(testSubjectResult{Data: "test"}, result) | ||
} | ||
|
||
func (ut *UTAPIGraphQLSuite) Test_Store_NoServer() { | ||
|
||
ctx := context.TODO() | ||
|
||
type testSubjectVar struct { | ||
Gitoid string `json:"gitoid"` | ||
} | ||
|
||
type testSubjectResult struct { | ||
Data string `json:"data"` | ||
} | ||
result, err := api.GraphQlQuery[testSubjectResult](ctx, "http://invalid-archivista", `query`, testSubjectVar{Gitoid: "test_Gitoid"}) | ||
ut.Error(err) | ||
ut.Equal(testSubjectResult{Data: ""}, result) | ||
} | ||
|
||
func (ut *UTAPIGraphQLSuite) Test_Store_BadStatusCode_NoMsg() { | ||
|
||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
ctx := context.TODO() | ||
|
||
type testSubjectVar struct { | ||
Gitoid string `json:"gitoid"` | ||
} | ||
|
||
type testSubjectResult struct { | ||
Data string `json:"data"` | ||
} | ||
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) | ||
ut.Error(err) | ||
ut.Equal(testSubjectResult{Data: ""}, result) | ||
} | ||
|
||
func (ut *UTAPIGraphQLSuite) Test_Store_InvalidData() { | ||
|
||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(``)) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
ctx := context.TODO() | ||
|
||
type testSubjectVar struct { | ||
Gitoid string `json:"gitoid"` | ||
} | ||
|
||
type testSubjectResult struct { | ||
Data string `json:"data"` | ||
} | ||
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) | ||
ut.Error(err) | ||
ut.Equal(testSubjectResult{Data: ""}, result) | ||
} | ||
|
||
func (ut *UTAPIGraphQLSuite) Test_Store_QLReponseWithErrors() { | ||
|
||
testServer := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(`{"data": {"data": "test"}, "errors": [{"message": "test_error"}]}}`)) | ||
if err != nil { | ||
ut.FailNow(err.Error()) | ||
} | ||
}, | ||
), | ||
) | ||
defer testServer.Close() | ||
ctx := context.TODO() | ||
|
||
type testSubjectVar struct { | ||
Gitoid string `json:"gitoid"` | ||
} | ||
|
||
type testSubjectResult struct { | ||
Data string `json:"data"` | ||
Errors string `json:"errors"` | ||
} | ||
|
||
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) | ||
ut.Error(err) | ||
ut.EqualError(err, "graph ql query failed: [{test_error}]") | ||
ut.Equal(testSubjectResult{Data: ""}, result) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth a helper function for all these eventually. Especially if the surface area of the API expands more. But, I'm not super worried about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm filing a separate issue for this as an improvement.
We can use some testify fixtures such as setup tests, tear down, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.