Skip to content

Commit

Permalink
test: added integration test for degraphql routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashansa-K committed Jan 23, 2025
1 parent 8946072 commit 435ebd8
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,15 @@ var (
}}
)

const complexQueryForDegraphqlRoute = `query SearchPosts($filters: PostsFilters) {
posts(filter: $filters) {
id
title
author
}
}
`

// test scope:
// - 1.4.3
func Test_Sync_ServicesRoutes_Till_1_4_3(t *testing.T) {
Expand Down Expand Up @@ -7567,3 +7576,87 @@ func Test_Sync_Scoped_Plugins_3x(t *testing.T) {
})
}
}

func Test_Sync_DegraphqlRoutes(t *testing.T) {
client, err := getTestClient()
if err != nil {
t.Fatalf(err.Error())
}

ctx := context.Background()
dumpConfig := deckDump.Config{CustomEntityTypes: []string{"degraphql_routes"}}

runWhen(t, "enterprise", ">=3.0.0")

t.Run("create degraphql route", func(t *testing.T) {
currentState, err := fetchCurrentState(ctx, client, dumpConfig)
require.NoError(t, err)

targetState := stateFromFile(ctx, t, "testdata/sync/036-degraphql-routes/kong.yaml", client, dumpConfig)
syncer, err := deckDiff.NewSyncer(deckDiff.SyncerOpts{
CurrentState: currentState,
TargetState: targetState,

KongClient: client,
})
require.NoError(t, err)

stats, errs, changes := syncer.Solve(ctx, 1, false, true)
require.Len(t, errs, 0, "Should have no errors in syncing")
logEntityChanges(t, stats, changes)

newState, err := fetchCurrentState(ctx, client, dumpConfig)
require.NoError(t, err)

degraphqlRoutes, err := newState.DegraphqlRoutes.GetAll()
require.NoError(t, err)

assert.Equal(t, 1, len(degraphqlRoutes))
assert.Equal(t, "/foo", *degraphqlRoutes[0].URI)
assert.Equal(t, "query{ foo { bar } }", *degraphqlRoutes[0].Query)

expectedMethods := kong.StringSlice("GET")
assert.Equal(t, expectedMethods, degraphqlRoutes[0].Methods)

t.Cleanup(func() {
mustResetKongState(ctx, t, client, dumpConfig)
})
})

t.Run("create degraphql route - complex query", func(t *testing.T) {
currentState, err := fetchCurrentState(ctx, client, dumpConfig)
require.NoError(t, err)

targetState := stateFromFile(ctx, t, "testdata/sync/036-degraphql-routes/kong-complex-query.yaml", client, dumpConfig)
syncer, err := deckDiff.NewSyncer(deckDiff.SyncerOpts{
CurrentState: currentState,
TargetState: targetState,

KongClient: client,
})
require.NoError(t, err)

stats, errs, changes := syncer.Solve(ctx, 1, false, true)
require.Len(t, errs, 0, "Should have no errors in syncing")
logEntityChanges(t, stats, changes)

newState, err := fetchCurrentState(ctx, client, dumpConfig)
require.NoError(t, err)

degraphqlRoutes, err := newState.DegraphqlRoutes.GetAll()
require.NoError(t, err)

assert.Equal(t, 1, len(degraphqlRoutes))
assert.Equal(t, "/search/posts", *degraphqlRoutes[0].URI)

expectedQuery := kong.String(complexQueryForDegraphqlRoute)
assert.Equal(t, expectedQuery, degraphqlRoutes[0].Query)

expectedMethods := kong.StringSlice("POST")
assert.Equal(t, expectedMethods, degraphqlRoutes[0].Methods)

t.Cleanup(func() {
mustResetKongState(ctx, t, client, dumpConfig)
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
_format_version: "3.0"
plugins:
- config:
graphql_server_path: /graphql
enabled: true
name: degraphql
protocols:
- grpc
- grpcs
- http
- https
services:
- connect_timeout: 60000
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
write_timeout: 60000
plugin_entities:
- type: degraphql_routes
fields:
uri: "/search/posts"
methods:
- "POST"
query: |
query SearchPosts($filters: PostsFilters) {
posts(filter: $filters) {
id
title
author
}
}
service: svc1
26 changes: 26 additions & 0 deletions tests/integration/testdata/sync/036-degraphql-routes/kong.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
_format_version: "3.0"
plugins:
- config:
graphql_server_path: /graphql
enabled: true
name: degraphql
protocols:
- grpc
- grpcs
- http
- https
services:
- connect_timeout: 60000
host: mockbin.org
name: svc1
port: 80
protocol: http
read_timeout: 60000
retries: 5
write_timeout: 60000
plugin_entities:
- type: degraphql_routes
fields:
uri: "/foo"
query: "query{ foo { bar } }"
service: svc1

0 comments on commit 435ebd8

Please sign in to comment.