From 92d7561d49804455edec1678a9305f328c4a3c12 Mon Sep 17 00:00:00 2001 From: Pierig Le Saux Date: Sat, 11 Apr 2026 01:44:19 -0400 Subject: [PATCH] test(api): add coverage for NumPackages list handlers and error paths --- api/mirror_test.go | 17 ++++++++++++- api/package_count_test_helpers_test.go | 19 ++++++++++++++ api/repos_test.go | 34 ++++++++++++++++++++------ api/snapshot_test.go | 28 +++++++++++++-------- 4 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 api/package_count_test_helpers_test.go diff --git a/api/mirror_test.go b/api/mirror_test.go index 6f2c04c0..16c4a0ef 100644 --- a/api/mirror_test.go +++ b/api/mirror_test.go @@ -66,6 +66,7 @@ func (s *MirrorSuite) TestGetMirrorsIncludesNumPackages(c *C) { err = collection.Add(repo) c.Assert(err, IsNil) + putRawDBValue(c, &s.APISuite, repo.RefKey(), makePackageRefList(c).Encode()) response, err := s.HTTPRequest("GET", "/api/mirrors", nil) c.Assert(err, IsNil) @@ -81,10 +82,24 @@ func (s *MirrorSuite) TestGetMirrorsIncludesNumPackages(c *C) { found = true value, ok := mirror["NumPackages"] c.Assert(ok, Equals, true) - c.Assert(value, Equals, float64(0)) + c.Assert(value, Equals, float64(2)) break } } c.Assert(found, Equals, true) } + +func (s *MirrorSuite) TestGetMirrorsReturns500OnCorruptRefList(c *C) { + collection := s.context.NewCollectionFactory().RemoteRepoCollection() + + repo, err := deb.NewRemoteRepo("broken-mirror", "http://example.com/debian", "stable", []string{"main"}, []string{}, false, false, false) + c.Assert(err, IsNil) + c.Assert(collection.Add(repo), IsNil) + putRawDBValue(c, &s.APISuite, repo.RefKey(), []byte("not-msgpack")) + + response, err := s.HTTPRequest("GET", "/api/mirrors", nil) + c.Assert(err, IsNil) + c.Assert(response.Code, Equals, 500) + c.Assert(response.Body.String(), Matches, ".*unable to show:.*") +} diff --git a/api/package_count_test_helpers_test.go b/api/package_count_test_helpers_test.go new file mode 100644 index 00000000..fc86ef3c --- /dev/null +++ b/api/package_count_test_helpers_test.go @@ -0,0 +1,19 @@ +package api + +import ( + "github.com/aptly-dev/aptly/deb" + . "gopkg.in/check.v1" +) + +func makePackageRefList(c *C) *deb.PackageRefList { + list := deb.NewPackageList() + c.Assert(list.Add(&deb.Package{Name: "libcount", Version: "1.0", Architecture: "amd64"}), IsNil) + c.Assert(list.Add(&deb.Package{Name: "appcount", Version: "2.0", Architecture: "all"}), IsNil) + return deb.NewPackageRefListFromPackageList(list) +} + +func putRawDBValue(c *C, s *APISuite, key []byte, value []byte) { + db, err := s.context.Database() + c.Assert(err, IsNil) + c.Assert(db.Put(key, value), IsNil) +} \ No newline at end of file diff --git a/api/repos_test.go b/api/repos_test.go index 267221cc..55117316 100644 --- a/api/repos_test.go +++ b/api/repos_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" + "github.com/aptly-dev/aptly/deb" "github.com/gin-gonic/gin" . "gopkg.in/check.v1" ) @@ -15,14 +16,12 @@ type ReposSuite struct { var _ = Suite(&ReposSuite{}) func (s *ReposSuite) TestGetReposIncludesNumPackages(c *C) { - body, err := json.Marshal(gin.H{"Name": "count-repo-list"}) - c.Assert(err, IsNil) + collection := s.context.NewCollectionFactory().LocalRepoCollection() + repo := deb.NewLocalRepo("count-repo-list", "") + repo.UpdateRefList(makePackageRefList(c)) + c.Assert(collection.Add(repo), IsNil) - response, err := s.HTTPRequest("POST", "/api/repos", bytes.NewReader(body)) - c.Assert(err, IsNil) - c.Assert(response.Code, Equals, 201) - - response, err = s.HTTPRequest("GET", "/api/repos", nil) + response, err := s.HTTPRequest("GET", "/api/repos", nil) c.Assert(err, IsNil) c.Assert(response.Code, Equals, 200) @@ -36,10 +35,29 @@ func (s *ReposSuite) TestGetReposIncludesNumPackages(c *C) { found = true value, ok := repo["NumPackages"] c.Assert(ok, Equals, true) - c.Assert(value, Equals, float64(0)) + c.Assert(value, Equals, float64(2)) break } } c.Assert(found, Equals, true) } + +func (s *ReposSuite) TestGetReposReturns500OnCorruptRefList(c *C) { + body, err := json.Marshal(gin.H{"Name": "broken-repo-list"}) + c.Assert(err, IsNil) + + response, err := s.HTTPRequest("POST", "/api/repos", bytes.NewReader(body)) + c.Assert(err, IsNil) + c.Assert(response.Code, Equals, 201) + + collection := s.context.NewCollectionFactory().LocalRepoCollection() + repo, err := collection.ByName("broken-repo-list") + c.Assert(err, IsNil) + putRawDBValue(c, &s.APISuite, repo.RefKey(), []byte("not-msgpack")) + + response, err = s.HTTPRequest("GET", "/api/repos", nil) + c.Assert(err, IsNil) + c.Assert(response.Code, Equals, 500) + c.Assert(response.Body.String(), Matches, ".*msgpack.*|.*decode.*") +} diff --git a/api/snapshot_test.go b/api/snapshot_test.go index d3ee3535..36c50809 100644 --- a/api/snapshot_test.go +++ b/api/snapshot_test.go @@ -1,10 +1,9 @@ package api import ( - "bytes" "encoding/json" - "github.com/gin-gonic/gin" + "github.com/aptly-dev/aptly/deb" . "gopkg.in/check.v1" ) @@ -15,14 +14,11 @@ type SnapshotsSuite struct { var _ = Suite(&SnapshotsSuite{}) func (s *SnapshotsSuite) TestGetSnapshotsIncludesNumPackages(c *C) { - body, err := json.Marshal(gin.H{"Name": "count-snapshot-list"}) - c.Assert(err, IsNil) + collection := s.context.NewCollectionFactory().SnapshotCollection() + snapshot := deb.NewSnapshotFromRefList("count-snapshot-list", nil, makePackageRefList(c), "") + c.Assert(collection.Add(snapshot), IsNil) - response, err := s.HTTPRequest("POST", "/api/snapshots", bytes.NewReader(body)) - c.Assert(err, IsNil) - c.Assert(response.Code, Equals, 201) - - response, err = s.HTTPRequest("GET", "/api/snapshots", nil) + response, err := s.HTTPRequest("GET", "/api/snapshots", nil) c.Assert(err, IsNil) c.Assert(response.Code, Equals, 200) @@ -36,10 +32,22 @@ func (s *SnapshotsSuite) TestGetSnapshotsIncludesNumPackages(c *C) { found = true value, ok := snapshot["NumPackages"] c.Assert(ok, Equals, true) - c.Assert(value, Equals, float64(0)) + c.Assert(value, Equals, float64(2)) break } } c.Assert(found, Equals, true) } + +func (s *SnapshotsSuite) TestGetSnapshotsReturns500OnCorruptRefList(c *C) { + collection := s.context.NewCollectionFactory().SnapshotCollection() + snapshot := deb.NewSnapshotFromRefList("broken-snapshot-list", nil, makePackageRefList(c), "") + c.Assert(collection.Add(snapshot), IsNil) + putRawDBValue(c, &s.APISuite, snapshot.RefKey(), []byte("not-msgpack")) + + response, err := s.HTTPRequest("GET", "/api/snapshots", nil) + c.Assert(err, IsNil) + c.Assert(response.Code, Equals, 500) + c.Assert(response.Body.String(), Matches, ".*msgpack.*|.*decode.*") +}