Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Improve tests suite #76

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
go tool cover -func coverage.out
- name: Quality Gate - Test coverage shall be above threshold
env:
TESTCOVERAGE_THRESHOLD: 80
TESTCOVERAGE_THRESHOLD: 90
run: |
echo "Quality Gate: checking test coverage is above threshold ..."
echo "Threshold : $TESTCOVERAGE_THRESHOLD %"
Expand Down
5 changes: 4 additions & 1 deletion internal/server/v1alpha1/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Server struct {
// errSecurityFailed is returned when security check failed for a webhook call
var errSecurityFailed = errors.New("security check failed")

// errRequestBodyMissing is returned when the request body is missing
var errRequestBodyMissing = errors.New("request body is missing")

// NewServer creates a new server instance for the v1alpha1 version
func NewServer() *Server {
var s = &Server{
Expand Down Expand Up @@ -89,7 +92,7 @@ func webhookService(s *Server, spec *config.WebhookSpec, r *http.Request) (err e
}

if r.Body == nil {
return errors.New("request don't have body")
return errRequestBodyMissing
}
data, err := io.ReadAll(r.Body)
if err != nil {
Expand Down
108 changes: 94 additions & 14 deletions internal/server/v1alpha1/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"atomys.codes/webhooked/internal/config"
"atomys.codes/webhooked/internal/valuable"
"atomys.codes/webhooked/pkg/factory"
"atomys.codes/webhooked/pkg/storage"
)

func TestNewServer(t *testing.T) {
Expand Down Expand Up @@ -125,43 +126,122 @@ func Test_webhookService(t *testing.T) {

headerFactory, _ := factory.GetFactoryByName("header")
compareFactory, _ := factory.GetFactoryByName("compare")
validPipeline := factory.NewPipeline().AddFactory(headerFactory).AddFactory(compareFactory)

req := httptest.NewRequest("POST", "/v1alpha1/test", strings.NewReader("{}"))
req.Header.Set("X-Token", "test")

invalidReq := httptest.NewRequest("POST", "/v1alpha1/test", nil)
invalidReq.Body = nil

validPipeline := factory.NewPipeline().AddFactory(headerFactory).AddFactory(compareFactory)
validPipeline.Inputs["request"] = req
validPipeline.Inputs["headerName"] = &factory.InputConfig{Name: "headerName", Valuable: valuable.Valuable{Values: []string{"X-Token"}}}
validPipeline.Inputs["first"] = &factory.InputConfig{Name: "headerName", Valuable: valuable.Valuable{Values: []string{"{{ .Outputs.header.value }}"}}}
validPipeline.Inputs["second"] = &factory.InputConfig{Name: "headerName", Valuable: valuable.Valuable{Values: []string{"test"}}}

invalidPipeline := factory.NewPipeline().AddFactory(headerFactory).AddFactory(compareFactory)
invalidPipeline.Inputs["request"] = req
invalidPipeline.Inputs["headerName"] = &factory.InputConfig{Name: "headerName", Valuable: valuable.Valuable{Values: []string{"X-Token"}}}
invalidPipeline.Inputs["first"] = &factory.InputConfig{Name: "headerName", Valuable: valuable.Valuable{Values: []string{"{{ .Outputs.header.value }}"}}}
invalidPipeline.Inputs["second"] = &factory.InputConfig{Name: "headerName", Valuable: valuable.Valuable{Values: []string{"INVALID"}}}

type input struct {
spec *config.WebhookSpec
req *http.Request
}

var tests = []struct {
name string
input *config.WebhookSpec
wantErr bool
name string
input *input
wantErr bool
matchErr error
}{
{"no spec", nil, true},
{"no security", &config.WebhookSpec{
Security: nil,
}, false},
{"empty security", &config.WebhookSpec{
{"no spec", &input{nil, req}, true, config.ErrSpecNotFound},
{"no security", &input{&config.WebhookSpec{Security: nil}, req}, false, nil},
{"empty security", &input{&config.WebhookSpec{
SecurityPipeline: factory.NewPipeline(),
}, false},
}, req}, false, nil},

{"valid security", &config.WebhookSpec{
{"valid security", &input{&config.WebhookSpec{
SecurityPipeline: validPipeline,
}, false},
}, req}, false, nil},
{"invalid security", &input{&config.WebhookSpec{
SecurityPipeline: invalidPipeline,
}, req}, true, errSecurityFailed},
{"invalid body payload", &input{&config.WebhookSpec{
SecurityPipeline: validPipeline,
}, invalidReq}, true, errRequestBodyMissing},
}

for _, test := range tests {
got := webhookService(&Server{}, test.input, req)
log.Warn().Msgf("body %+v", test.input.req.Body)
got := webhookService(&Server{}, test.input.spec, test.input.req)
if test.wantErr {
assert.Error(got, "input: %s", test.name)
assert.ErrorIs(got, test.matchErr, "input: %s", test.name)
} else {
assert.NoError(got, "input: %s", test.name)
}
}
}

func TestServer_webhokServiceStorage(t *testing.T) {
if testing.Short() {
t.Skip("TestServer_webhokServiceStorage testing is skiped in short version of test")
return
}

pusher, err := storage.Load("redis", map[string]interface{}{
"host": "127.0.0.1",
"port": "6379",
"database": 0,
"key": "testKey",
})
assert.NoError(t, err)

var tests = []struct {
name string
req *http.Request
templateString string
wantErr bool
}{
{
"basic",
httptest.NewRequest("POST", "/v1alpha1/test", strings.NewReader("{}")),
"{{ .Payload }}",
false,
},
{
"invalid template",
httptest.NewRequest("POST", "/v1alpha1/test", strings.NewReader("{}")),
"{{ ",
true,
},
}

for _, test := range tests {
spec := &config.WebhookSpec{
Security: nil,
Storage: []*config.StorageSpec{
{
Type: "redis",
Formatting: &config.FormattingSpec{
Template: test.templateString,
},
Client: pusher,
},
},
}

got := webhookService(&Server{}, spec, test.req)
if test.wantErr {
assert.Error(t, got, "input: %s", test.name)
} else {
assert.NoError(t, got, "input: %s", test.name)
}
}

}

func TestServer_runSecurity(t *testing.T) {
assert := assert.New(t)
var s = &Server{}
Expand Down
5 changes: 5 additions & 0 deletions pkg/storage/rabbitmq/rabbitmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (suite *RabbitMQSetupTestSuite) TestRabbitMQNewStorage() {
"immediate": false,
})
assert.NoError(suite.T(), err)

_, err = NewStorage(map[string]interface{}{
"databaseUrl": "amqp://user:",
})
assert.Error(suite.T(), err)
}

func (suite *RabbitMQSetupTestSuite) TestRabbitMQPush() {
Expand Down