diff --git a/Dockerfile b/Dockerfile index 94c3958..9419bc9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,11 @@ FROM golang:1.23-alpine AS builder SHELL ["/bin/ash", "-o", "pipefail", "-c"] # Install build dependencies -RUN apk add --no-cache git bash +RUN apk add --no-cache git bash gcc sqlite-dev musl-dev libc-dev + +# Set CGO_ENABLED for sqlite3 compatibility +# ENV CGO_ENABLED=1 +ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" # Set the working directory WORKDIR /app diff --git a/go.mod b/go.mod index 693ab32..61c5546 100644 --- a/go.mod +++ b/go.mod @@ -5,34 +5,36 @@ go 1.23.0 toolchain go1.23.1 require ( + entgo.io/ent v0.14.1 + github.com/google/uuid v1.6.0 + github.com/lib/pq v1.10.9 + github.com/mattn/go-sqlite3 v1.14.16 github.com/robfig/cron/v3 v3.0.1 github.com/scriptnull/jsonseal v0.3.0 github.com/spf13/cobra v1.8.1 + github.com/stretchr/testify v1.10.0 github.com/swaggest/openapi-go v0.2.54 github.com/swaggest/rest v0.2.68 github.com/swaggest/swgui v1.8.2 github.com/swaggest/usecase v1.3.1 - golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c + golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 gopkg.in/yaml.v2 v2.4.0 ) require ( ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43 // indirect - entgo.io/ent v0.14.1 // indirect github.com/agext/levenshtein v1.2.1 // indirect github.com/agnivade/levenshtein v1.1.1 // indirect github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/go-openapi/inflect v0.19.0 // indirect github.com/google/go-cmp v0.6.0 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/hashicorp/hcl/v2 v2.13.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kr/pretty v0.3.1 // indirect - github.com/lib/pq v1.10.9 // indirect - github.com/mattn/go-runewidth v0.0.9 // indirect github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect - github.com/olekukonko/tablewriter v0.0.5 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/santhosh-tekuri/jsonschema/v3 v3.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect @@ -41,9 +43,8 @@ require ( github.com/swaggest/refl v1.3.0 // indirect github.com/vearutop/statigz v1.4.0 // indirect github.com/zclconf/go-cty v1.8.0 // indirect - golang.org/x/mod v0.21.0 // indirect - golang.org/x/sync v0.8.0 // indirect + golang.org/x/mod v0.22.0 // indirect golang.org/x/text v0.13.0 // indirect - golang.org/x/tools v0.26.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 21bdb7a..3341a90 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43 h1:GwdJbXydHCYPedeeLt4x/lrl ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43/go.mod h1:uj3pm+hUTVN/X5yfdBexHlZv+1Xu5u5ZbZx7+CDavNU= entgo.io/ent v0.14.1 h1:fUERL506Pqr92EPHJqr8EYxbPioflJo6PudkrEA8a/s= entgo.io/ent v0.14.1/go.mod h1:MH6XLG0KXpkcDQhKiHfANZSzR55TJyPL5IGNpI8wpco= +github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= +github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8= @@ -28,13 +30,15 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNPXu/4= github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4= +github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= +github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl/v2 v2.13.0 h1:0Apadu1w6M11dyGFxWnmhhcMjkbAiKCv7G1r/2QgCNc= github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0= github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc= @@ -49,14 +53,14 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -78,8 +82,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/swaggest/assertjson v1.9.0 h1:dKu0BfJkIxv/xe//mkCrK5yZbs79jL7OVf9Ija7o2xQ= github.com/swaggest/assertjson v1.9.0/go.mod h1:b+ZKX2VRiUjxfUIal0HDN85W0nHPAYUbYH5WkkSsFsU= github.com/swaggest/form/v5 v5.1.1 h1:ct6/rOQBGrqWUQ0FUv3vW5sHvTUb31AwTUWj947N6cY= @@ -107,14 +111,12 @@ github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDf github.com/zclconf/go-cty v1.8.0 h1:s4AvqaeQzJIu3ndv4gVIhplVD0krU+bgrcLSVUnaWuA= github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY= -golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8= -golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= -golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo= +golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c= +golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -122,8 +124,6 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ= -golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/restapi/v1/integrations/get.go b/internal/restapi/v1/integrations/get.go index 8f61736..c5280bd 100644 --- a/internal/restapi/v1/integrations/get.go +++ b/internal/restapi/v1/integrations/get.go @@ -3,6 +3,7 @@ package integrations import ( "context" "errors" + "fmt" "github.com/google/uuid" "github.com/shinobistack/gokakashi/ent" "github.com/swaggest/usecase/status" @@ -31,9 +32,13 @@ func GetIntegration(client *ent.Client) func(ctx context.Context, req GetIntegra return status.Wrap(errors.New("invalid UUID format"), status.InvalidArgument) } + // Fetch integration by ID integration, err := client.Integrations.Get(ctx, uid) if err != nil { - return status.Wrap(errors.New("integration not found"), status.NotFound) + if ent.IsNotFound(err) { + return status.Wrap(errors.New("integration not found"), status.NotFound) + } + return status.Wrap(fmt.Errorf("unexpected error: %v", err), status.Internal) } res.ID = integration.ID.String() @@ -46,24 +51,22 @@ func GetIntegration(client *ent.Client) func(ctx context.Context, req GetIntegra } } -func ListIntegrations(client *ent.Client) func(ctx context.Context, req struct{}, res *ListIntegrationResponse) error { - return func(ctx context.Context, req struct{}, res *ListIntegrationResponse) error { +func ListIntegrations(client *ent.Client) func(ctx context.Context, req struct{}, res *[]GetIntegrationResponse) error { + return func(ctx context.Context, req struct{}, res *[]GetIntegrationResponse) error { integrations, err := client.Integrations.Query().All(ctx) if err != nil { return status.Wrap(errors.New("failed to fetch integrations"), status.Internal) } - responses := make([]GetIntegrationResponse, len(integrations)) + *res = make([]GetIntegrationResponse, len(integrations)) for i, integration := range integrations { - responses[i] = GetIntegrationResponse{ + (*res)[i] = GetIntegrationResponse{ ID: integration.ID.String(), Name: integration.Name, Type: integration.Type, Config: integration.Config, } } - - res.Integrations = responses return nil } } diff --git a/internal/restapi/v1/integrations/get_test.go b/internal/restapi/v1/integrations/get_test.go new file mode 100644 index 0000000..99ce4f7 --- /dev/null +++ b/internal/restapi/v1/integrations/get_test.go @@ -0,0 +1,98 @@ +package integrations_test + +import ( + "context" + "github.com/google/uuid" + _ "github.com/mattn/go-sqlite3" + "github.com/shinobistack/gokakashi/ent/enttest" + "github.com/shinobistack/gokakashi/internal/restapi/v1/integrations" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetIntegration(t *testing.T) { + // client := enttest.Open(t, "postgres", "host=localhost port=5432 user=postgres password=secret dbname=testdb sslmode=disable") + // This requires a running DB with pre-configured testdb + + // sqlite3 lightweight, file based db faster to test + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Seed the DB + id := uuid.New() + _, err := client.Integrations.Create(). + SetID(id). + SetName("Test Integration"). + SetType("linear"). + SetConfig(map[string]interface{}{"Key": "value"}). + Save(context.Background()) + assert.NoError(t, err) + + // Test case: Valid ID + req := integrations.GetIntegrationRequests{ID: id.String()} + res := &integrations.GetIntegrationResponse{} + handler := integrations.GetIntegration(client) + err = handler(context.Background(), req, res) + + assert.NoError(t, err) + assert.Equal(t, "Test Integration", res.Name) + assert.Equal(t, "linear", res.Type) + + // Test case: Invalid UUID + req = integrations.GetIntegrationRequests{ID: "invalid-uuid"} + err = handler(context.Background(), req, res) + assert.Error(t, err) + + // Test case: Non-existent ID + req = integrations.GetIntegrationRequests{ID: uuid.New().String()} + err = handler(context.Background(), req, res) + assert.Error(t, err) + +} + +func TestListIntegrations(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Seed the db + _, err := client.Integrations.Create(). + SetName("Integration 1"). + SetType("linear"). + SetConfig(map[string]interface{}{"key": "value1"}). + Save(context.Background()) + assert.NoError(t, err) + + _, err = client.Integrations.Create(). + SetName("Integration 2"). + SetType("jira"). + SetConfig(map[string]interface{}{"key": "value2"}). + Save(context.Background()) + assert.NoError(t, err) + + // Test case: List integrations + req := struct{}{} + var res []integrations.GetIntegrationResponse + handler := integrations.ListIntegrations(client) + err = handler(context.Background(), req, &res) + + assert.NoError(t, err) + assert.Len(t, res, 2) + assert.Equal(t, "Integration 1", res[0].Name) + assert.Equal(t, "Integration 2", res[1].Name) +} + +func TestListIntegrations_EmptyDB(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Prepare response + var res []integrations.GetIntegrationResponse + + // Execute ListIntegrations handler + handler := integrations.ListIntegrations(client) + err := handler(context.Background(), struct{}{}, &res) + + // Validate response + assert.NoError(t, err) + assert.Len(t, res, 0) +} diff --git a/internal/restapi/v1/integrations/post.go b/internal/restapi/v1/integrations/post.go index 6572533..fb17d66 100644 --- a/internal/restapi/v1/integrations/post.go +++ b/internal/restapi/v1/integrations/post.go @@ -2,8 +2,10 @@ package integrations import ( "context" + "errors" "fmt" "github.com/shinobistack/gokakashi/ent" + "github.com/shinobistack/gokakashi/ent/integrations" "github.com/swaggest/usecase/status" ) @@ -20,6 +22,22 @@ type CreateIntegrationResponse struct { func CreateIntegration(client *ent.Client) func(ctx context.Context, req CreateIntegrationRequest, res *CreateIntegrationResponse) error { return func(ctx context.Context, req CreateIntegrationRequest, res *CreateIntegrationResponse) error { + // Validate required fields + if req.Name == "" || req.Type == "" { + return status.Wrap(errors.New("missing required fields: name and/or type"), status.InvalidArgument) + } + + // Check for duplicate name + exists, err := client.Integrations.Query(). + Where(integrations.Name(req.Name)). + Exist(ctx) + if err != nil { + return status.Wrap(fmt.Errorf("failed to check for duplicate integration name: %v", err), status.Internal) + } + if exists { + return status.Wrap(errors.New("integration with the same name already exists"), status.AlreadyExists) + } + integration, err := client.Integrations.Create(). SetName(req.Name). SetType(req.Type). diff --git a/internal/restapi/v1/integrations/post_test.go b/internal/restapi/v1/integrations/post_test.go new file mode 100644 index 0000000..3df0b77 --- /dev/null +++ b/internal/restapi/v1/integrations/post_test.go @@ -0,0 +1,120 @@ +package integrations_test + +import ( + "context" + "github.com/google/uuid" + "github.com/shinobistack/gokakashi/internal/restapi/v1/integrations" + "testing" + + "github.com/shinobistack/gokakashi/ent/enttest" + "github.com/stretchr/testify/assert" +) + +func TestCreateIntegration_ValidInput(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Test case: Valid input + req := integrations.CreateIntegrationRequest{ + Name: "Valid Integration", + Type: "linear", + Config: map[string]interface{}{"key": "value"}, + } + res := &integrations.CreateIntegrationResponse{} + handler := integrations.CreateIntegration(client) + err := handler(context.Background(), req, res) + + assert.NoError(t, err) + assert.NotEmpty(t, res.ID) + assert.Equal(t, "created", res.Status) + + // Verify database + integrationID, err := uuid.Parse(res.ID) + assert.NoError(t, err) + integration, err := client.Integrations.Get(context.Background(), integrationID) + assert.NoError(t, err) + assert.Equal(t, "Valid Integration", integration.Name) + assert.Equal(t, "linear", integration.Type) + assert.Equal(t, "value", integration.Config["key"]) +} + +func TestCreateIntegration_DuplicateName(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Seed database + req := integrations.CreateIntegrationRequest{ + Name: "Duplicate Integration", + Type: "jira", + Config: map[string]interface{}{"key": "value"}, + } + res := &integrations.CreateIntegrationResponse{} + handler := integrations.CreateIntegration(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(), "integration with the same name already exists") +} + +func TestCreateIntegration_MissingFields(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Test case: Missing fields + req := integrations.CreateIntegrationRequest{ + Name: "", + Type: "", + Config: nil, + } + res := &integrations.CreateIntegrationResponse{} + handler := integrations.CreateIntegration(client) + err := handler(context.Background(), req, res) + + assert.Error(t, err) +} + +// TODO: To intorduce this test case post writing Integration Types with right test case condition +//func TestCreateIntegration_InvalidType(t *testing.T) { +// client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") +// defer client.Close() +// +// // Test case: Invalid type +// req := integrations.CreateIntegrationRequest{ +// Name: "Invalid Type Integration", +// Type: "nonexistent-type", +// Config: map[string]interface{}{"key": "value"}, +// } +// res := &integrations.CreateIntegrationResponse{} +// handler := integrations.CreateIntegration(client) +// err := handler(context.Background(), req, res) +// +// assert.Error(t, err) +//} + +func TestCreateIntegration_EmptyConfig(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Test case: Empty config + req := integrations.CreateIntegrationRequest{ + Name: "Empty Config Integration", + Type: "linear", + Config: map[string]interface{}{}, + } + res := &integrations.CreateIntegrationResponse{} + handler := integrations.CreateIntegration(client) + err := handler(context.Background(), req, res) + + assert.NoError(t, err) + assert.NotEmpty(t, res.ID) + + // Verify database + integrationID, err := uuid.Parse(res.ID) + assert.NoError(t, err) + integration, err := client.Integrations.Get(context.Background(), integrationID) + assert.NoError(t, err) + assert.Equal(t, 0, len(integration.Config)) +} diff --git a/internal/restapi/v1/integrations/put.go b/internal/restapi/v1/integrations/put.go index 101b33f..277694f 100644 --- a/internal/restapi/v1/integrations/put.go +++ b/internal/restapi/v1/integrations/put.go @@ -2,9 +2,11 @@ package integrations import ( "context" + "errors" "fmt" "github.com/google/uuid" "github.com/shinobistack/gokakashi/ent" + "github.com/shinobistack/gokakashi/ent/integrations" "github.com/swaggest/usecase/status" ) @@ -20,13 +22,22 @@ type UpdateIntegrationResponse struct { Status string `json:"status"` } -func UpdateIntegration(client *ent.Client) func(ctx context.Context, req UpdateIntegrationRequest, res *UpdateIntegrationResponse) error { - return func(ctx context.Context, req UpdateIntegrationRequest, res *UpdateIntegrationResponse) error { +func UpdateIntegration(client *ent.Client) func(ctx context.Context, req UpdateIntegrationRequest, res *GetIntegrationResponse) error { + return func(ctx context.Context, req UpdateIntegrationRequest, res *GetIntegrationResponse) error { uid, err := uuid.Parse(req.ID) if err != nil { return status.Wrap(fmt.Errorf("invalid UUID format: %v", err), status.InvalidArgument) } + // Check if integration exists + exists, err := client.Integrations.Query().Where(integrations.ID(uid)).Exist(ctx) + if err != nil { + return status.Wrap(fmt.Errorf("unexpected database error: %v", err), status.Internal) + } + if !exists { + return status.Wrap(errors.New("integration not found"), status.NotFound) + } + update := client.Integrations.UpdateOneID(uid) if req.Name != nil { update = update.SetName(*req.Name) @@ -41,7 +52,9 @@ func UpdateIntegration(client *ent.Client) func(ctx context.Context, req UpdateI } res.ID = integration.ID.String() - res.Status = "updated" + res.Name = integration.Name + res.Type = integration.Type + res.Config = integration.Config return nil } diff --git a/internal/restapi/v1/integrations/put_test.go b/internal/restapi/v1/integrations/put_test.go new file mode 100644 index 0000000..c56e325 --- /dev/null +++ b/internal/restapi/v1/integrations/put_test.go @@ -0,0 +1,109 @@ +package integrations_test + +import ( + "context" + "github.com/shinobistack/gokakashi/internal/restapi/v1/integrations" + "testing" + + "github.com/google/uuid" + "github.com/shinobistack/gokakashi/ent/enttest" + "github.com/stretchr/testify/assert" +) + +func TestUpdateIntegration(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Seed the database + id := uuid.New() + _, err := client.Integrations.Create(). + SetID(id). + SetName("Old Integration"). + SetType("linear"). + SetConfig(map[string]interface{}{"key": "value"}). + Save(context.Background()) + assert.NoError(t, err) + + // Test case: Valid update + req := integrations.UpdateIntegrationRequest{ + ID: id.String(), + Name: stringPointer("Updated Integration"), + } + var res integrations.GetIntegrationResponse + handler := integrations.UpdateIntegration(client) + err = handler(context.Background(), req, &res) + + assert.NoError(t, err) + assert.NoError(t, err) + assert.Equal(t, "Updated Integration", res.Name) + assert.Equal(t, "linear", res.Type) + assert.Equal(t, "value", res.Config["key"]) + + // Verify database + integration, err := client.Integrations.Get(context.Background(), id) + assert.NoError(t, err) + assert.Equal(t, "Updated Integration", integration.Name) +} + +func TestUpdateIntegration_InvalidUUID(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + req := integrations.UpdateIntegrationRequest{ + ID: "invalid-uuid", + Name: stringPointer("Invalid Update"), + } + var res integrations.GetIntegrationResponse + + handler := integrations.UpdateIntegration(client) + err := handler(context.Background(), req, &res) + + assert.Error(t, err) +} + +func TestUpdateIntegration_NonExistentID(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + req := integrations.UpdateIntegrationRequest{ + ID: uuid.New().String(), + Name: stringPointer("Non-existent ID"), + } + var res integrations.GetIntegrationResponse + + handler := integrations.UpdateIntegration(client) + err := handler(context.Background(), req, &res) + + assert.Error(t, err) +} + +func TestUpdateIntegration_NoChanges(t *testing.T) { + client := enttest.Open(t, "sqlite3", "file:ent?mode=memory&cache=shared&_fk=1") + defer client.Close() + + // Seed the database + id := uuid.New() + _, err := client.Integrations.Create(). + SetID(id). + SetName("Integration"). + SetType("linear"). + SetConfig(map[string]interface{}{"key": "value"}). + Save(context.Background()) + assert.NoError(t, err) + + // Test case: No changes + req := integrations.UpdateIntegrationRequest{ + ID: id.String(), + } + var res integrations.GetIntegrationResponse + + handler := integrations.UpdateIntegration(client) + err = handler(context.Background(), req, &res) + + assert.NoError(t, err) + assert.Equal(t, "Integration", res.Name) +} + +func stringPointer(s string) *string { + return &s +}