-
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.
- Loading branch information
Showing
6 changed files
with
385 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package policies_test | ||
|
||
import ( | ||
"context" | ||
"github.com/google/uuid" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/shinobistack/gokakashi/ent/enttest" | ||
policyent "github.com/shinobistack/gokakashi/ent/policies" | ||
"github.com/shinobistack/gokakashi/ent/schema" | ||
"github.com/shinobistack/gokakashi/internal/restapi/v1/policies" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestDeletePolicy_ValidDeletion(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
// Create a policy | ||
policy := client.Policies.Create(). | ||
SetName("to-be-deleted-test-policy"). | ||
SetImage(schema.Image{Registry: "example-registry", Name: "example-name", Tags: []string{"v1.0"}}). | ||
SaveX(context.Background()) | ||
req := policies.DeletePolicyRequest{ID: policy.ID} | ||
res := &policies.DeletePolicyResponse{} | ||
err := policies.DeletePolicy(client)(context.Background(), req, res) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, policy.ID, res.ID) | ||
|
||
// Verify deletion | ||
exists := client.Policies.Query().Where(policyent.ID(policy.ID)).ExistX(context.Background()) | ||
assert.False(t, exists) | ||
} | ||
|
||
func TestDeletePolicy_InvalidUUID(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.DeletePolicyRequest{ID: uuid.Nil} | ||
res := &policies.DeletePolicyResponse{} | ||
err := policies.DeletePolicy(client)(context.Background(), req, res) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func TestDeletePolicy_NonExistentID(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.DeletePolicyRequest{ID: uuid.New()} | ||
res := &policies.DeletePolicyResponse{} | ||
err := policies.DeletePolicy(client)(context.Background(), req, res) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
// ToDo: TestDeletePolicy_WithDependentRecords |
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,86 @@ | ||
package policies_test | ||
|
||
import ( | ||
"context" | ||
"github.com/google/uuid" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/shinobistack/gokakashi/ent/enttest" | ||
"github.com/shinobistack/gokakashi/ent/schema" | ||
"github.com/shinobistack/gokakashi/internal/restapi/v1/policies" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestListPolicies_EmptyDatabase(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.ListPoliciesRequest{} | ||
res := &[]policies.GetPolicyResponse{} | ||
err := policies.ListPolicies(client)(context.Background(), req, res) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, 0, len(*res)) | ||
} | ||
|
||
func TestListPolicies(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
// Create test data | ||
client.Policies.Create(). | ||
SetName("test-policy1"). | ||
SetImage(schema.Image{Registry: "example-registry", Name: "example-name", Tags: []string{"v1.0"}}). | ||
Save(context.Background()) | ||
client.Policies.Create(). | ||
SetName("test-policy2"). | ||
SetImage(schema.Image{Registry: "example-registry", Name: "example-name", Tags: []string{"v1.0"}}). | ||
Save(context.Background()) | ||
|
||
req := policies.ListPoliciesRequest{} | ||
res := &[]policies.GetPolicyResponse{} | ||
err := policies.ListPolicies(client)(context.Background(), req, res) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, 2, len(*res)) | ||
} | ||
|
||
func TestGetPolicy_ValidID(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
// Create a policy | ||
policy := client.Policies.Create(). | ||
SetName("test-policy"). | ||
SetImage(schema.Image{Registry: "example-registry", Name: "example-name", Tags: []string{"v1.0"}}). | ||
SaveX(context.Background()) | ||
|
||
req := policies.GetPolicyRequests{ID: policy.ID} | ||
res := &policies.GetPolicyResponse{} | ||
err := policies.GetPolicy(client)(context.Background(), req, res) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, policy.Name, res.Name) | ||
} | ||
|
||
func TestGetPolicy_InvalidUUID(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.GetPolicyRequests{ID: uuid.Nil} | ||
res := &policies.GetPolicyResponse{} | ||
err := policies.GetPolicy(client)(context.Background(), req, res) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetPolicy_NonExistentID(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.GetPolicyRequests{ID: uuid.New()} | ||
res := &policies.GetPolicyResponse{} | ||
err := policies.GetPolicy(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
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,124 @@ | ||
package policies_test | ||
|
||
import ( | ||
"context" | ||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/shinobistack/gokakashi/ent/enttest" | ||
"github.com/shinobistack/gokakashi/ent/schema" | ||
"github.com/shinobistack/gokakashi/internal/restapi/v1/policies" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestCreatepolicy_InvalidPolicyNameFormat(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.CreatePolicyRequest{ | ||
Name: "tTest-policy", | ||
Image: schema.Image{ | ||
Registry: "example-registry", | ||
Name: "example-name", | ||
Tags: []string{"v1.0"}, | ||
}, | ||
Trigger: map[string]interface{}{"type": "cron", "schedule": "0 0 * * *"}, | ||
Check: &schema.Check{ | ||
Condition: "sev.high > 0", | ||
Notify: []string{"team-slack"}, | ||
}, | ||
} | ||
res := &policies.CreatePolicyResponse{} | ||
err := policies.CreatePolicy(client)(context.Background(), req, res) | ||
assert.Error(t, err) | ||
|
||
req = policies.CreatePolicyRequest{ | ||
Name: "#test policy", | ||
Image: schema.Image{ | ||
Registry: "example-registry", | ||
Name: "example-name", | ||
Tags: []string{"v1.0"}, | ||
}, | ||
Trigger: map[string]interface{}{"type": "cron", "schedule": "0 0 * * *"}, | ||
Check: &schema.Check{ | ||
Condition: "sev.high > 0", | ||
Notify: []string{"team-slack"}, | ||
}, | ||
} | ||
err = policies.CreatePolicy(client)(context.Background(), req, res) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "invalid id format") | ||
} | ||
|
||
func TestCreatePolicy_ValidInput(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.CreatePolicyRequest{ | ||
Name: "test-policy", | ||
Image: schema.Image{ | ||
Registry: "example-registry", | ||
Name: "example-name", | ||
Tags: []string{"v1.0"}, | ||
}, | ||
Trigger: map[string]interface{}{"type": "cron", "schedule": "0 0 * * *"}, | ||
Check: &schema.Check{ | ||
Condition: "sev.high > 0", | ||
Notify: []string{"team-slack"}, | ||
}, | ||
} | ||
res := &policies.CreatePolicyResponse{} | ||
err := policies.CreatePolicy(client)(context.Background(), req, res) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, res.ID) | ||
assert.Equal(t, "created", res.Status) | ||
} | ||
|
||
func TestCreatePolicy_MissingFields(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
req := policies.CreatePolicyRequest{ | ||
Name: "incomplete-policy", | ||
Image: schema.Image{ | ||
Registry: "", | ||
Name: "example-name", | ||
Tags: []string{}, | ||
}, | ||
Trigger: nil, | ||
Check: nil, | ||
} | ||
res := &policies.CreatePolicyResponse{} | ||
err := policies.CreatePolicy(client)(context.Background(), req, res) | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func TestCreatePolicy_DuplicateName(t *testing.T) { | ||
client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") | ||
defer client.Close() | ||
|
||
// Seed database | ||
req := policies.CreatePolicyRequest{ | ||
Name: "duplicate-policy", | ||
Image: schema.Image{ | ||
Registry: "example-registry", | ||
Name: "example-name", | ||
Tags: []string{"v1.0"}, | ||
}, | ||
Trigger: map[string]interface{}{"type": "cron", "schedule": "0 0 * * *"}, | ||
Check: &schema.Check{ | ||
Condition: "sev.high > 0", | ||
Notify: []string{"team-slack"}, | ||
}, | ||
} | ||
res := &policies.CreatePolicyResponse{} | ||
handler := policies.CreatePolicy(client) | ||
err := handler(context.Background(), req, res) | ||
assert.NoError(t, err) | ||
|
||
// Test case: Duplicate name | ||
err = handler(context.Background(), req, res) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "policy with the same name already exists") | ||
} |
Oops, something went wrong.