graphql-engine/cli/internal/metadataobject/api_limits/api_limits_test.go
Kali Vara Purushotham Santhati 4cf13762e2 cli: add support for network metadata object
Issue: https://github.com/hasura/graphql-engine/issues/7520

Problem: add support for network metadata object

Solution: add new network.yaml which consists of network metadata and in code add new network metadata object

Metadata.json
```
{
  "resource_version": 6,
  "metadata": {
    "version": 3,
    "sources": [
      {
        "name": "default",
        "kind": "postgres",
        "tables": [],
        "configuration": {
          "connection_info": {
            "use_prepared_statements": true,
            "database_url": {
              "from_env": "HASURA_GRAPHQL_DATABASE_URL"
            },
            "isolation_level": "read-committed",
            "pool_settings": {
              "connection_lifetime": 600,
              "retries": 1,
              "idle_timeout": 180,
              "max_connections": 50
            }
          }
        }
      }
    ],
    "network": {
      "tls_allowlist": [
        {
          "suffix": null,
          "permissions": [
            "self-signed"
          ],
          "host": "www.google.com"
        },
        {
          "suffix": null,
          "permissions": [
            "self-signed"
          ],
          "host": "play.golang.com"
        },
        {
          "suffix": null,
          "permissions": [
            "self-signed"
          ],
          "host": "hasura.io"
        }
      ]
    }
  }
}
```
network.yaml
```
tls_allowlist:
- suffix: null
  permissions:
  - self-signed
  host: www.google.com
- suffix: null
  permissions:
  - self-signed
  host: play.golang.com
- suffix: null
  permissions:
  - self-signed
  host: hasura.io

```

https://github.com/hasura/graphql-engine-mono/pull/2343

GitOrigin-RevId: ef508ae946dc5c7c5bcb4a9a3ccb982394ae8639
2021-09-22 08:12:46 +00:00

137 lines
2.7 KiB
Go

package apilimits
import (
"io/ioutil"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func TestMetadataObject_Build(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
type args struct {
metadata *yaml.MapSlice
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
"can build from file",
fields{
MetadataDir: "testdata/metadata",
logger: logrus.New(),
},
args{
metadata: new(yaml.MapSlice),
},
`api_limits:
disabled: false
rate_limit:
per_role: {}
global:
unique_params: IP
max_reqs_per_min: 1
`,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &MetadataObject{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
err := m.Build(tt.args.metadata)
if tt.wantErr {
require.Error(t, err)
} else {
b, err := yaml.Marshal(tt.args.metadata)
assert.NoError(t, err)
assert.Equal(t, tt.want, string(b))
}
})
}
}
func TestMetadataObject_Export(t *testing.T) {
type fields struct {
MetadataDir string
logger *logrus.Logger
}
type args struct {
metadata yaml.MapSlice
}
tests := []struct {
name string
fields fields
args args
want map[string][]byte
wantErr bool
}{
{
"can export metadata with api_limits",
fields{
MetadataDir: "testdata/metadata",
logger: logrus.New(),
},
args{
metadata: func() yaml.MapSlice {
var metadata yaml.MapSlice
jsonb, err := ioutil.ReadFile("testdata/metadata.json")
assert.NoError(t, err)
assert.NoError(t, yaml.Unmarshal(jsonb, &metadata))
return metadata
}(),
},
map[string][]byte{
"testdata/metadata/api_limits.yaml": []byte(`disabled: false
rate_limit:
per_role: {}
global:
unique_params: IP
max_reqs_per_min: 1
`),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obj := &MetadataObject{
MetadataDir: tt.fields.MetadataDir,
logger: tt.fields.logger,
}
got, err := obj.Export(tt.args.metadata)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
var wantContent = map[string]string{}
var gotContent = map[string]string{}
for k, v := range got {
gotContent[k] = string(v)
}
for k, v := range tt.want {
wantContent[k] = string(v)
}
assert.NoError(t, err)
if diff := cmp.Diff(wantContent, gotContent); diff != "" {
t.Errorf("Export() mismatch (-want +got):\n%s", diff)
}
}
})
}
}