-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: adds test cases for scans and scanlabels (#63)
* adds test cases for scans * adds test cases for scanlabels
- Loading branch information
Showing
14 changed files
with
913 additions
and
4 deletions.
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,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") | ||
} |
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,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) | ||
} |
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
Oops, something went wrong.