-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
198 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package python | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPython_Run(t *testing.T) { | ||
type fields struct { | ||
Bootstrap string | ||
Script string | ||
} | ||
|
||
type args struct { | ||
payload string | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want []byte | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "empty", | ||
fields: fields{ | ||
Script: "pass", | ||
}, | ||
args: args{ | ||
payload: "test", | ||
}, | ||
want: []byte(""), | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "hello world", | ||
fields: fields{ | ||
Script: "print('hello world')", | ||
}, | ||
args: args{ | ||
payload: "test", | ||
}, | ||
want: []byte("hello world\n"), | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "echo", | ||
fields: fields{ | ||
Script: "import sys; print(sys.argv[1])", | ||
}, | ||
args: args{ | ||
payload: "test", | ||
}, | ||
want: []byte("test\n"), | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "error", | ||
fields: fields{ | ||
Script: "import sys; sys.exit(1)", | ||
}, | ||
args: args{ | ||
payload: "test", | ||
}, | ||
want: nil, | ||
wantErr: assert.Error, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
a := &Python{ | ||
Bootstrap: tt.fields.Bootstrap, | ||
Script: tt.fields.Script, | ||
} | ||
got, err := a.Run(ctx, tt.args.payload) | ||
tt.wantErr(t, err) | ||
|
||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package webhook_test | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/sjson" | ||
|
||
"github.com/SecurityBrewery/catalyst/reaction/action/webhook" | ||
catalystTesting "github.com/SecurityBrewery/catalyst/testing" | ||
) | ||
|
||
func TestWebhook_Run(t *testing.T) { | ||
server := catalystTesting.NewRecordingServer() | ||
|
||
go http.ListenAndServe("127.0.0.1:12347", server) //nolint:gosec,errcheck | ||
|
||
type fields struct { | ||
Headers map[string]string | ||
URL string | ||
} | ||
|
||
type args struct { | ||
payload string | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want map[string]any | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "", | ||
fields: fields{ | ||
Headers: map[string]string{}, | ||
URL: "http://127.0.0.1:12347/foo", | ||
}, | ||
args: args{ | ||
payload: "test", | ||
}, | ||
want: map[string]any{ | ||
"statusCode": 200, | ||
"headers": map[string]any{ | ||
"Content-Length": []any{"13"}, | ||
"Content-Type": []any{"text/plain; charset=utf-8"}, | ||
}, | ||
"body": `{"test":true}`, | ||
"isBase64Encoded": false, | ||
}, | ||
wantErr: assert.NoError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
a := &webhook.Webhook{ | ||
Headers: tt.fields.Headers, | ||
URL: tt.fields.URL, | ||
} | ||
got, err := a.Run(ctx, tt.args.payload) | ||
tt.wantErr(t, err) | ||
|
||
want, err := json.Marshal(tt.want) | ||
require.NoError(t, err) | ||
|
||
got, err = sjson.DeleteBytes(got, "headers.Date") | ||
require.NoError(t, err) | ||
|
||
assert.JSONEq(t, string(want), string(got)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package testing | ||
|
||
import "net/http" | ||
|
||
type RecordingServer struct { | ||
Entries []string | ||
} | ||
|
||
func NewRecordingServer() *RecordingServer { | ||
return &RecordingServer{} | ||
} | ||
|
||
func (s *RecordingServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
s.Entries = append(s.Entries, r.URL.Path) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write([]byte(`{"test":true}`)) //nolint:errcheck | ||
} |