Skip to content

Commit

Permalink
api: adds test cases for scans and scanlabels (#63)
Browse files Browse the repository at this point in the history
* adds test cases for scans

* adds test cases for scanlabels
  • Loading branch information
ashwiniag authored Dec 26, 2024
1 parent 9810d82 commit 28746a1
Show file tree
Hide file tree
Showing 14 changed files with 913 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/restapi/v1/policylabels/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/swaggest/usecase/status"
)

// ToDo: To have a requests to create labels in bulk?
type CreatePolicyLabelRequest struct {
PolicyID uuid.UUID `path:"policy_id"`
Key string `json:"key"`
Expand Down
14 changes: 13 additions & 1 deletion internal/restapi/v1/scanlabels/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ func DeleteScanLabel(client *ent.Client) func(ctx context.Context, req DeleteSca
if req.Key == "" {
return status.Wrap(errors.New("invalid key: cannot be nil"), status.InvalidArgument)
}
_, err := client.ScanLabels.Delete().
// Check if the label exists
exists, err := client.ScanLabels.Query().
Where(scanlabels.ScanID(req.ScanID), scanlabels.Key(req.Key)).
Exist(ctx)
if err != nil {
return status.Wrap(err, status.Internal)
}
if !exists {
return status.Wrap(errors.New("label not found"), status.NotFound)
}

// Delete the label
_, err = client.ScanLabels.Delete().
Where(scanlabels.ScanID(req.ScanID), scanlabels.Key(req.Key)).
Exec(ctx)
if err != nil {
Expand Down
120 changes: 120 additions & 0 deletions internal/restapi/v1/scanlabels/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package scanlabels_test

import (
"context"
"github.com/shinobistack/gokakashi/ent/schema"
"testing"

"github.com/google/uuid"
"github.com/shinobistack/gokakashi/ent/enttest"
"github.com/shinobistack/gokakashi/internal/restapi/v1/scanlabels"
"github.com/stretchr/testify/assert"
)

func TestDeleteScanLabel_Valid(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()
policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

// Add a label
client.ScanLabels.Create().
SetScanID(scan.ID).
SetKey("env").
SetValue("prod").
SaveX(context.Background())

req := scanlabels.DeleteScanLabelRequest{
ScanID: scan.ID,
Key: "env",
}
res := &scanlabels.DeleteScanLabelResponse{}

err := scanlabels.DeleteScanLabel(client)(context.Background(), req, res)

assert.NoError(t, err)
assert.Equal(t, "deleted", res.Status)
}

func TestDeleteScanLabel_MissingFields(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()

policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

req := scanlabels.DeleteScanLabelRequest{
ScanID: scan.ID, // Missing ScanID
Key: "",
}
res := &scanlabels.DeleteScanLabelResponse{}

err := scanlabels.DeleteScanLabel(client)(context.Background(), req, res)

assert.Error(t, err)
}

func TestDeleteScanLabel_LabelNotFound(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()
policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

// Create a test scan
scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

client.ScanLabels.Create().
SetScanID(scan.ID).
SetKey("env").
SetValue("prod").
SaveX(context.Background())

req := scanlabels.DeleteScanLabelRequest{
ScanID: scan.ID,
Key: "nonexistent-key",
}
res := &scanlabels.DeleteScanLabelResponse{}

err := scanlabels.DeleteScanLabel(client)(context.Background(), req, res)

assert.Error(t, err)
assert.Contains(t, err.Error(), "label not found")
}

func TestDeleteScanLabel_InvalidScanID(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()

req := scanlabels.DeleteScanLabelRequest{
ScanID: uuid.Nil, // Invalid ScanID
Key: "env",
}
res := &scanlabels.DeleteScanLabelResponse{}

err := scanlabels.DeleteScanLabel(client)(context.Background(), req, res)

assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid UUID")
}
1 change: 1 addition & 0 deletions internal/restapi/v1/scanlabels/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func GetScanLabel(client *ent.Client) func(ctx context.Context, req GetScanLabel
if req.ScanID == uuid.Nil || req.Key == "" {
return status.Wrap(errors.New("invalid Scan ID or Key"), status.InvalidArgument)
}

label, err := client.ScanLabels.Query().
Where(scanlabels.ScanID(req.ScanID), scanlabels.Key(req.Key)).
Only(ctx)
Expand Down
154 changes: 154 additions & 0 deletions internal/restapi/v1/scanlabels/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package scanlabels_test

import (
"context"
"github.com/shinobistack/gokakashi/ent/schema"
"testing"

"github.com/google/uuid"
"github.com/shinobistack/gokakashi/ent/enttest"
"github.com/shinobistack/gokakashi/internal/restapi/v1/scanlabels"
"github.com/stretchr/testify/assert"
)

func TestListScanLabels_Valid(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()
policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

// Create a test scan
scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

// Add labels
client.ScanLabels.Create().
SetScanID(scan.ID).
SetKey("env").
SetValue("prod").
SaveX(context.Background())

client.ScanLabels.Create().
SetScanID(scan.ID).
SetKey("version").
SetValue("v1.0").
SaveX(context.Background())

req := scanlabels.ListScanLabelsRequest{
ScanID: scan.ID,
}
res := &scanlabels.ListScanLabelsResponse{}

err := scanlabels.ListScanLabels(client)(context.Background(), req, res)

assert.NoError(t, err)
assert.Len(t, res.Labels, 2)
}

func TestListScanLabels_NoLabels(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()

policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

// Create a test scan
scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

req := scanlabels.ListScanLabelsRequest{
ScanID: scan.ID,
}
res := &scanlabels.ListScanLabelsResponse{}

err := scanlabels.ListScanLabels(client)(context.Background(), req, res)

assert.NoError(t, err)
assert.Len(t, res.Labels, 0)
}

func TestGetScanLabel_Valid(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()
policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

// Create a test scan
scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

// Add a label
client.ScanLabels.Create().
SetScanID(scan.ID).
SetKey("env").
SetValue("prod").
SaveX(context.Background())

req := scanlabels.GetScanLabelRequest{
ScanID: scan.ID,
Key: "env",
}
res := &scanlabels.GetScanLabelResponse{}

err := scanlabels.GetScanLabel(client)(context.Background(), req, res)

assert.NoError(t, err)
assert.Equal(t, "env", res.Key)
assert.Equal(t, "prod", res.Value)
}

func TestGetScanLabel_NotFound(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()
policy := client.Policies.Create().
SetName("test-policy").
SetImage(schema.Image{Registry: "test-registry", Name: "test-name", Tags: []string{"v1.0"}}).
SaveX(context.Background())

// Create a test scan
scan := client.Scans.Create().
SetPolicyID(policy.ID).
SetImage("example-image:latest").
SetStatus("scan_pending").
SaveX(context.Background())

req := scanlabels.GetScanLabelRequest{
ScanID: scan.ID,
Key: "nonexistent-key",
}
res := &scanlabels.GetScanLabelResponse{}

err := scanlabels.GetScanLabel(client)(context.Background(), req, res)

assert.Error(t, err)
}

func TestGetScanLabel_InvalidScanID(t *testing.T) {
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
defer client.Close()

req := scanlabels.GetScanLabelRequest{
ScanID: uuid.Nil, // Invalid ScanID
Key: "env",
}
res := &scanlabels.GetScanLabelResponse{}

err := scanlabels.GetScanLabel(client)(context.Background(), req, res)

assert.Error(t, err)
}
1 change: 1 addition & 0 deletions internal/restapi/v1/scanlabels/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/swaggest/usecase/status"
)

// ToDo: To have a requests to create labels in bulk?
type CreateScanLabelRequest struct {
ScanID uuid.UUID `path:"scan_id"`
Key string `json:"key"`
Expand Down
Loading

0 comments on commit 28746a1

Please sign in to comment.