Skip to content

Commit

Permalink
chore: provide go test cases for webhook sources (#3549)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvrach authored Jul 23, 2024
1 parent 20aa7f3 commit 39d30d3
Show file tree
Hide file tree
Showing 43 changed files with 4,506 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go.work
go.work.sum
23 changes: 23 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# GO libraries

## webhook/testdata

To generate the test files use:

```bash
go generate ./...
```

Work against local rudder-server:

```bash
go work init
go work use .
go work use ../../rudder-server
```

Then run the webhook tests:

```bash
go test github.com/rudderlabs/rudder-server/gateway/webhook -count 1 -timeout 2m
```
11 changes: 11 additions & 0 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/rudderlabs/rudder-transformer/go

go 1.22.4

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
99 changes: 99 additions & 0 deletions go/webhook/testcases/testcases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package testcases

import (
"embed"
"encoding/json"
"io/fs"
"testing"
"time"

"github.com/stretchr/testify/require"
)

type Setup struct {
Context Context
Cases []Case
}

type Context struct {
Now time.Time
RequestIP string `json:"request_ip"`
}

type Case struct {
Name string
Description string
Skip string
Input Input
Output Output
}

type Input struct {
Request Request
}
type Request struct {
Method string
RawQuery string `json:"query"`
Headers map[string]string
Body json.RawMessage
}

type Output struct {
Response Response
Queue []json.RawMessage
ErrQueue []json.RawMessage `json:"err_queue"`
}

type Response struct {
Body json.RawMessage
StatusCode int `json:"status"`
}

//go:generate npx ts-node ../../../test/scripts/generateJson.ts sources ./testdata/testcases
//go:generate npx prettier --write ./testdata/**/*.json

//go:embed testdata/context.json
var contextData []byte

//go:embed testdata/testcases/**/*.json
var testdata embed.FS

func Load(t *testing.T) Setup {
t.Helper()

var tc Context
err := json.Unmarshal(contextData, &tc)
require.NoError(t, err)

var tcs []Case
err = fs.WalkDir(testdata, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

f, err := testdata.Open(path)
if err != nil {
return err
}
defer f.Close()

var tc Case
err = json.NewDecoder(f).Decode(&tc)
if err != nil {
return err
}
tcs = append(tcs, tc)

return nil
})
require.NoError(t, err)

return Setup{
Context: tc,
Cases: tcs,
}
}
5 changes: 5 additions & 0 deletions go/webhook/testcases/testdata/context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"request_ip_comment": "192.0.2.x/24 - This block is assigned as \"TEST-NET\" for use in documentation and example code.",
"request_ip": "192.0.2.30",
"now": "2024-03-03T04:48:29.000Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "adjust",
"description": "Simple track call",
"input": {
"request": {
"body": {
"id": "adjust",
"query_parameters": {
"gps_adid": ["38400000-8cf0-11bd-b23e-10b96e40000d"],
"adid": ["18546f6171f67e29d1cb983322ad1329"],
"tracker_token": ["abc"],
"custom": ["custom"],
"tracker_name": ["dummy"],
"created_at": ["1404214665"],
"event_name": ["Click"]
},
"updated_at": "2023-02-10T12:16:07.251Z",
"created_at": "2023-02-10T12:05:04.402Z"
},
"headers": {
"Content-Type": "application/json"
}
}
},
"output": {
"response": {
"status": 200,
"body": "OK"
},
"queue": [
{
"context": {
"library": {
"name": "unknown",
"version": "unknown"
},
"integration": {
"name": "Adjust"
},
"device": {
"id ": "18546f6171f67e29d1cb983322ad1329"
}
},
"integrations": {
"Adjust": false
},
"type": "track",
"event": "Click",
"originalTimestamp": "2014-07-01T11:37:45.000Z",
"timestamp": "2014-07-01T11:37:45.000Z",
"properties": {
"gps_adid": "38400000-8cf0-11bd-b23e-10b96e40000d",
"tracker_token": "abc",
"custom": "custom",
"tracker_name": "dummy"
},
"anonymousId": "97fcd7b2-cc24-47d7-b776-057b7b199513",
"receivedAt": "2024-03-03T04:48:29.000Z",
"request_ip": "192.0.2.30",
"messageId": "00000000-0000-0000-0000-000000000000"
}
],
"errQueue": []
},
"skip": "FIXME"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "adjust",
"description": "Simple track call with no query parameters",
"input": {
"request": {
"body": {
"id": "adjust",
"updated_at": "2023-02-10T12:16:07.251Z",
"created_at": "2023-02-10T12:05:04.402Z"
},
"headers": {
"Content-Type": "application/json"
}
}
},
"output": {
"response": {
"status": 400,
"body": "Query_parameters is missing"
},
"queue": [],
"errQueue": [null]
},
"skip": "FIXME"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"name": "auth0",
"description": "add member to an organization",
"input": {
"request": {
"body": {
"log_id": "90020221031061004280169676882609459981150114445973782546",
"data": {
"date": "2022-10-31T06:09:59.135Z",
"type": "sapi",
"description": "Add members to an organization",
"client_id": "vQcJNDTxsM1W72eHFonRJdzyOvawlwIt",
"client_name": "",
"ip": "35.167.74.121",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"details": {
"request": {
"ip": "35.167.74.121",
"auth": {
"user": {
"name": "rudder test",
"email": "[email protected]",
"user_id": "google-oauth2|123456"
},
"strategy": "jwt",
"credentials": {
"jti": "571921bf7833a97efabf08d765a0ec8f"
}
},
"body": {
"members": ["auth0|123456"]
},
"path": "/api/v2/organizations/org_eoe8p2atZ7furBxg/members",
"query": {},
"method": "post",
"channel": "https://manage.auth0.com/",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
},
"response": {
"body": {},
"statusCode": 204
}
},
"user_id": "google-oauth2|123456",
"log_id": "90020221031061004280169676882609459981150114445973782546"
}
},
"headers": {
"Content-Type": "application/json"
}
}
},
"output": {
"response": {
"status": 200,
"body": "OK"
},
"queue": [
{
"type": "group",
"sentAt": "2022-10-31T06:09:59.135Z",
"userId": "google-oauth2|123456",
"anonymousId": "97fcd7b2-cc24-47d7-b776-057b7b199513",
"context": {
"library": {
"name": "unknown",
"version": "unknown"
},
"traits": {
"userId": "google-oauth2|123456"
},
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"request_ip": "35.167.74.121",
"integration": {
"name": "Auth0"
}
},
"groupId": "org_eoe8p2atZ7furBxg",
"properties": {
"log_id": "90020221031061004280169676882609459981150114445973782546",
"details": {
"request": {
"ip": "35.167.74.121",
"auth": {
"user": {
"name": "rudder test",
"email": "[email protected]",
"user_id": "google-oauth2|123456"
},
"strategy": "jwt",
"credentials": {
"jti": "571921bf7833a97efabf08d765a0ec8f"
}
},
"body": {
"members": ["auth0|123456"]
},
"path": "/api/v2/organizations/org_eoe8p2atZ7furBxg/members",
"query": {},
"method": "post",
"channel": "https://manage.auth0.com/",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
},
"response": {
"body": {},
"statusCode": 204
}
},
"client_id": "vQcJNDTxsM1W72eHFonRJdzyOvawlwIt",
"client_name": "",
"description": "Add members to an organization",
"source_type": "sapi"
},
"integrations": {
"Auth0": false
},
"originalTimestamp": "2022-10-31T06:09:59.135Z",
"receivedAt": "2024-03-03T04:48:29.000Z",
"request_ip": "192.0.2.30",
"messageId": "00000000-0000-0000-0000-000000000000"
}
],
"errQueue": []
},
"skip": "dynamic anonymousId"
}
21 changes: 21 additions & 0 deletions go/webhook/testcases/testdata/testcases/auth0/empty_batch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "auth0",
"description": "empty batch",
"input": {
"request": {
"body": [],
"headers": {
"Content-Type": "application/json"
}
}
},
"output": {
"response": {
"status": 200,
"body": "OK"
},
"queue": [],
"errQueue": []
},
"skip": "dynamic anonymousId"
}
Loading

0 comments on commit 39d30d3

Please sign in to comment.