Skip to content

Commit

Permalink
feat(max-aliases): Add integration test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ldebruijn committed Oct 19, 2023
1 parent 54fa461 commit 73c2aad
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
78 changes: 78 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,65 @@ func TestHttpServerIntegration(t *testing.T) {
assert.Equal(t, string(ex), string(actual))
},
},
{
name: "blocks requests with too many aliases",
args: args{
request: func() *http.Request {
body := map[string]interface{}{
"query": `
query Foo {
a1: uploadImage(image: $image)
a2: uploadImage(image: $image)
a3: uploadImage(image: $image)
a4: uploadImage(image: $image)
a5: uploadImage(image: $image)
a6: uploadImage(image: $image)
a7: uploadImage(image: $image)
a8: uploadImage(image: $image)
a9: uploadImage(image: $image)
a10: uploadImage(image: $image)
}`,
}

bts, _ := json.Marshal(body)
r := httptest.NewRequest("POST", "/graphql", bytes.NewBuffer(bts))
return r
}(),
cfgOverrides: func(cfg *config.Config) *config.Config {
cfg.MaxAliases.Enabled = true
cfg.MaxAliases.Max = 3
return cfg
},
mockResponse: map[string]interface{}{
"data": map[string]interface{}{
"a1": "Yes",
"a2": "Yes",
"a3": "Yes",
"a4": "Yes",
"a5": "Yes",
"a6": "Yes",
"a7": "Yes",
"a8": "Yes",
"a9": "Yes",
"a10": "Yes",
},
},
},
want: func(t *testing.T, response *http.Response) {
expected := map[string]interface{}{
"errors": []map[string]interface{}{
{
"message": "syntax error: Aliases limit of 3 exceeded, found 10",
},
},
}
_, _ = json.Marshal(expected)
actual, err := io.ReadAll(response.Body)
assert.NoError(t, err)
// perform string comparisons as map[string]interface seems incomparable
assert.True(t, errorsContainsMessage("syntax error: Aliases limit of 3 exceeded, found 10", actual))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -207,3 +266,22 @@ func TestHttpServerIntegration(t *testing.T) {
})
}
}

func errorsContainsMessage(msg string, bytes []byte) bool {
var payload map[string]interface{}
err := json.Unmarshal(bytes, &payload)
if err != nil {
return false
}

if errors, ok := payload["errors"]; ok {
for _, err := range errors.([]interface{}) {
if errMsg, ok := err.(map[string]interface{})["message"]; ok {
if msg == errMsg {
return true
}
}
}
}
return false
}
6 changes: 5 additions & 1 deletion internal/business/persisted_operations/dir_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func NewLocalDirLoader(cfg Config) *DirLoader {
func (d *DirLoader) Load(ctx context.Context) (map[string]string, error) {
files, err := os.ReadDir(d.path)
if err != nil {
return nil, err
// if we can't read the dir, try creating it
err := os.Mkdir(d.path, 0750)
if err != nil {
return nil, err
}
}

var result map[string]string
Expand Down
16 changes: 9 additions & 7 deletions internal/business/persisted_operations/persisted_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ func NewPersistedOperations(log *slog.Logger, cfg Config, loader LocalLoader, re
lock: sync.RWMutex{},
}

poh.reloadFromRemote()
err := poh.reloadFromLocalDir()
if err != nil {
return nil, err
}
if cfg.Enabled {
poh.reloadFromRemote()
err := poh.reloadFromLocalDir()
if err != nil {
return nil, err
}

// start reloader
poh.reload()
// start reloader
poh.reload()
}

return poh, nil
}
Expand Down

0 comments on commit 73c2aad

Please sign in to comment.