Skip to content

Commit 8b29494

Browse files
committed
fix: lot of fixes due to integrations tests
1 parent afc2f87 commit 8b29494

18 files changed

Lines changed: 274 additions & 179 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
"dockerComposeFile": "docker-compose.yaml",
66
"service": "workspace",
77
"workspaceFolder": "/workspace",
8-
98
"features": {
109
"ghcr.io/devcontainers/features/common-utils:2": {},
1110
"ghcr.io/devcontainers/features/go:1": {},
1211
"ghcr.io/eitsupi/devcontainer-features/go-task:1": {},
13-
"ghcr.io/dhoeric/features/k6:1": {}
12+
"ghcr.io/dhoeric/features/k6:1": {},
13+
"ghcr.io/devcontainers-extra/features/apt-get-packages": {
14+
"packages": [
15+
"redis-tools",
16+
"postgresql-client"
17+
]
18+
}
1419
},
15-
1620
// Configure tool-specific properties.
1721
"customizations": {
1822
// Configure properties specific to VS Code.
@@ -35,14 +39,15 @@
3539
"files.encoding": "utf8",
3640
"workbench.editor.decorations.badges": true,
3741
"workbench.editor.decorations.colors": true,
38-
"editor.rulers": [80],
42+
"editor.rulers": [
43+
80
44+
],
3945
"search.useGlobalIgnoreFiles": true,
4046
"search.useParentIgnoreFiles": true,
4147
"[yaml]": {
4248
"editor.defaultFormatter": "redhat.vscode-yaml"
4349
}
4450
},
45-
4651
// Add the IDs of extensions you want installed when the container is created.
4752
"extensions": [
4853
"golang.Go",
@@ -54,12 +59,10 @@
5459
]
5560
}
5661
},
57-
5862
// Use 'forwardPorts' to make a list of ports inside the container available locally.
5963
"forwardPorts": [
6064
8080 // webhooked port
6165
],
62-
6366
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
6467
// "remoteUser": "vscode",
6568
"portsAttributes": {

.devcontainer/docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ services:
4343
POSTGRES_USER: postgres
4444
POSTGRES_PASSWORD: postgres
4545
POSTGRES_DB: postgres
46+
POSTGRES_HOST_AUTH_METHOD: trust
4647
ports:
4748
- 5432:5432

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"-p",
2929
"8081",
3030
"--config",
31-
"tests/integrations/webhooked_config.integrations.yaml"
31+
"tests/integrations/extended_config.integrations.yaml"
3232
],
3333
"cwd": "${workspaceFolder}",
3434
"env": {

executor.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88

99
"github.com/42atomys/webhooked/internal/config"
1010
"github.com/42atomys/webhooked/internal/contextutil"
11+
"github.com/42atomys/webhooked/internal/fasthttpz"
1112
"github.com/42atomys/webhooked/storage"
1213
"github.com/rs/zerolog/log"
1314
"github.com/valyala/fasthttp"
1415
)
1516

1617
type Executor interface {
17-
IncomingRequest(ctx context.Context, rctx *fasthttp.RequestCtx) error
18+
IncomingRequest(ctx context.Context, rctx *fasthttpz.RequestCtx) error
1819
}
1920

2021
type DefaultExecutor struct {
@@ -24,7 +25,7 @@ type DefaultExecutor struct {
2425
wgPool sync.Pool
2526
}
2627

27-
type pipelineFn = func(ctx context.Context, rctx *fasthttp.RequestCtx, wh *config.Webhook) (context.Context, error)
28+
type pipelineFn = func(ctx context.Context, rctx *fasthttpz.RequestCtx, wh *config.Webhook) (context.Context, error)
2829

2930
func NewExecutor(config *config.Config) *DefaultExecutor {
3031
return &DefaultExecutor{
@@ -43,7 +44,7 @@ func NewExecutor(config *config.Config) *DefaultExecutor {
4344
}
4445
}
4546

46-
func (e *DefaultExecutor) IncomingRequest(ctx context.Context, rctx *fasthttp.RequestCtx) error {
47+
func (e *DefaultExecutor) IncomingRequest(ctx context.Context, rctx *fasthttpz.RequestCtx) error {
4748
wh, err := e.config.FetchWebhookByPath(rctx.Path())
4849
if errors.Is(err, config.ErrSpecNotFound) {
4950
return ErrHTTPNotFound(rctx, err)
@@ -70,8 +71,8 @@ func (e *DefaultExecutor) pipelineOrder() []pipelineFn {
7071
}
7172
}
7273

73-
func (e *DefaultExecutor) pipelineSecure(ctx context.Context, rctx *fasthttp.RequestCtx, wh *config.Webhook) (context.Context, error) {
74-
if secure, err := wh.Security.IsSecure(rctx); err != nil || !secure {
74+
func (e *DefaultExecutor) pipelineSecure(ctx context.Context, rctx *fasthttpz.RequestCtx, wh *config.Webhook) (context.Context, error) {
75+
if secure, err := wh.Security.IsSecure(ctx, rctx); err != nil || !secure {
7576
if err != nil {
7677
return ctx, ErrHTTPInternalServerError(rctx, fmt.Errorf("error during security validation: %w", err))
7778
}
@@ -80,7 +81,7 @@ func (e *DefaultExecutor) pipelineSecure(ctx context.Context, rctx *fasthttp.Req
8081
return ctx, nil
8182
}
8283

83-
func (e *DefaultExecutor) pipelineStore(ctx context.Context, rctx *fasthttp.RequestCtx, wh *config.Webhook) (context.Context, error) {
84+
func (e *DefaultExecutor) pipelineStore(ctx context.Context, rctx *fasthttpz.RequestCtx, wh *config.Webhook) (context.Context, error) {
8485
wgInterface := e.wgPool.Get()
8586
var wg *sync.WaitGroup
8687
if wgInterface != nil {
@@ -159,7 +160,7 @@ func (e *DefaultExecutor) pipelineStore(ctx context.Context, rctx *fasthttp.Requ
159160
return ctx, nil
160161
}
161162

162-
func (e *DefaultExecutor) pipelineResponse(ctx context.Context, rctx *fasthttp.RequestCtx, wh *config.Webhook) (context.Context, error) {
163+
func (e *DefaultExecutor) pipelineResponse(ctx context.Context, rctx *fasthttpz.RequestCtx, wh *config.Webhook) (context.Context, error) {
163164
if wh.Response.Formatting == nil || !wh.Response.Formatting.HasTemplate() {
164165
rctx.SetStatusCode(fasthttp.StatusNoContent)
165166
return ctx, nil

executor_test.go

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77

88
"github.com/42atomys/webhooked/format"
99
"github.com/42atomys/webhooked/internal/config"
10+
"github.com/42atomys/webhooked/internal/fasthttpz"
1011
"github.com/42atomys/webhooked/security"
1112
securityNoop "github.com/42atomys/webhooked/security/noop"
1213
"github.com/42atomys/webhooked/storage"
1314
storageNoop "github.com/42atomys/webhooked/storage/noop"
1415
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
1517
"github.com/valyala/fasthttp"
1618
)
1719

@@ -22,10 +24,9 @@ func TestNewExecutor(t *testing.T) {
2224
}
2325

2426
func TestDefaultExecutor_IncomingRequest_SpecNotFound(t *testing.T) {
25-
// Setup
2627
executor := NewExecutor(&config.Config{})
2728

28-
ctx := &fasthttp.RequestCtx{}
29+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
2930
ctx.Request.SetRequestURI("/nonexistent/path")
3031

3132
// Execute
@@ -37,12 +38,9 @@ func TestDefaultExecutor_IncomingRequest_SpecNotFound(t *testing.T) {
3738
}
3839

3940
func TestDefaultExecutor_IncomingRequest_Success(t *testing.T) {
40-
// Setup test configuration
41-
setupTestConfig(t)
41+
executor := NewExecutor(setupTestConfig(t))
4242

43-
executor := NewExecutor(&config.Config{})
44-
45-
ctx := &fasthttp.RequestCtx{}
43+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
4644
ctx.Request.SetRequestURI("/webhooks/v1alpha2/test")
4745
ctx.Request.Header.SetMethod("POST")
4846
ctx.Request.SetBody([]byte(`{"test": "data"}`))
@@ -56,12 +54,9 @@ func TestDefaultExecutor_IncomingRequest_Success(t *testing.T) {
5654
}
5755

5856
func TestDefaultExecutor_IncomingRequest_SecurityFailure(t *testing.T) {
59-
// Setup test configuration with security that will fail
60-
setupTestConfigWithFailingSecurity(t)
61-
62-
executor := NewExecutor(&config.Config{})
57+
executor := NewExecutor(setupTestConfigWithFailingSecurity(t))
6358

64-
ctx := &fasthttp.RequestCtx{}
59+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
6560
ctx.Request.SetRequestURI("/webhooks/v1alpha2/secure-test")
6661
ctx.Request.Header.SetMethod("POST")
6762
ctx.Request.SetBody([]byte(`{"test": "data"}`))
@@ -74,6 +69,21 @@ func TestDefaultExecutor_IncomingRequest_SecurityFailure(t *testing.T) {
7469
assert.Equal(t, fasthttp.StatusUnauthorized, ctx.Response.StatusCode())
7570
}
7671

72+
func TestDefaultExecutor_IncomingRequest_SecurityError(t *testing.T) {
73+
executor := NewExecutor(setupTestConfigWithFailingSecurity(t))
74+
75+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
76+
ctx.Request.SetRequestURI("/webhooks/v1alpha2/secure-test-error")
77+
ctx.Request.Header.SetMethod("POST")
78+
ctx.Request.SetBody([]byte(`{"test": "data"}`))
79+
80+
// Execute
81+
err := executor.IncomingRequest(context.Background(), ctx)
82+
83+
// Assert
84+
assert.Error(t, err)
85+
assert.Equal(t, fasthttp.StatusInternalServerError, ctx.Response.StatusCode())
86+
}
7787
func TestDefaultExecutor_pipelineOrder(t *testing.T) {
7888
executor := &DefaultExecutor{}
7989
pipeline := executor.pipelineOrder()
@@ -92,7 +102,7 @@ func TestDefaultExecutor_pipelineSecure_Success(t *testing.T) {
92102
},
93103
}
94104

95-
ctx := &fasthttp.RequestCtx{}
105+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
96106

97107
resultCtx, err := executor.pipelineSecure(context.Background(), ctx, webhook)
98108

@@ -107,7 +117,7 @@ func TestDefaultExecutor_pipelineResponse_NoTemplate(t *testing.T) {
107117
Response: config.Response{},
108118
}
109119

110-
ctx := &fasthttp.RequestCtx{}
120+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
111121

112122
resultCtx, err := executor.pipelineResponse(context.Background(), ctx, webhook)
113123

@@ -130,7 +140,7 @@ func TestDefaultExecutor_pipelineStore_Success(t *testing.T) {
130140
},
131141
}
132142

133-
ctx := &fasthttp.RequestCtx{}
143+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
134144
ctx.Request.SetBody([]byte(`{"test": "data"}`))
135145

136146
resultCtx, err := executor.pipelineStore(context.Background(), ctx, webhook)
@@ -141,22 +151,66 @@ func TestDefaultExecutor_pipelineStore_Success(t *testing.T) {
141151

142152
// Helper functions for test setup
143153

144-
func setupTestConfig(t *testing.T) {
145-
// Since we can't modify the global config easily in tests,
146-
// we'll skip these tests that require global config manipulation
147-
t.Skip("Skipping test that requires global config modification")
154+
func setupTestConfig(t *testing.T) *config.Config {
155+
config := &config.Config{
156+
APIVersion: config.APIVersionV1Alpha2,
157+
Kind: config.KindConfiguration,
158+
Specs: []*config.Spec{
159+
{
160+
Webhooks: []*config.Webhook{
161+
{
162+
Name: "success-test",
163+
EntrypointURL: "/test",
164+
Security: security.Security{
165+
Type: "noop",
166+
Specs: &securityNoop.NoopSecuritySpec{},
167+
},
168+
},
169+
},
170+
},
171+
},
172+
}
173+
174+
require.NoError(t, config.Validate())
175+
return config
148176
}
149177

150-
func setupTestConfigWithFailingSecurity(t *testing.T) {
151-
// Since we can't modify the global config easily in tests,
152-
// we'll skip these tests that require global config manipulation
153-
t.Skip("Skipping test that requires global config modification")
178+
func setupTestConfigWithFailingSecurity(t *testing.T) *config.Config {
179+
config := &config.Config{
180+
APIVersion: config.APIVersionV1Alpha2,
181+
Kind: config.KindConfiguration,
182+
Specs: []*config.Spec{
183+
{
184+
Webhooks: []*config.Webhook{
185+
{
186+
Name: "secure-test",
187+
EntrypointURL: "/secure-test",
188+
Security: security.Security{
189+
Type: "failling",
190+
Specs: &mockFailingSecurity{},
191+
},
192+
},
193+
{
194+
Name: "secure-test-error",
195+
EntrypointURL: "/secure-test-error",
196+
Security: security.Security{
197+
Type: "error",
198+
Specs: &mockErrorSecurity{},
199+
},
200+
},
201+
},
202+
},
203+
},
204+
}
205+
206+
require.NoError(t, config.Validate())
207+
return config
154208
}
155209

156210
// Mock security implementation that always fails
157211
type mockFailingSecurity struct{}
158212

159-
func (m *mockFailingSecurity) IsSecure(ctx *fasthttp.RequestCtx) (bool, error) {
213+
func (m *mockFailingSecurity) IsSecure(ctx context.Context, rctx *fasthttpz.RequestCtx) (bool, error) {
160214
return false, nil
161215
}
162216

@@ -171,7 +225,7 @@ func (m *mockFailingSecurity) Initialize() error {
171225
// Mock security implementation that returns an error
172226
type mockErrorSecurity struct{}
173227

174-
func (m *mockErrorSecurity) IsSecure(ctx *fasthttp.RequestCtx) (bool, error) {
228+
func (m *mockErrorSecurity) IsSecure(ctx context.Context, rctx *fasthttpz.RequestCtx) (bool, error) {
175229
return false, errors.New("security check failed")
176230
}
177231

@@ -193,7 +247,7 @@ func TestDefaultExecutor_pipelineSecure_Error(t *testing.T) {
193247
},
194248
}
195249

196-
ctx := &fasthttp.RequestCtx{}
250+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
197251

198252
resultCtx, err := executor.pipelineSecure(context.Background(), ctx, webhook)
199253

@@ -212,7 +266,7 @@ func TestDefaultExecutor_pipelineSecure_Unauthorized(t *testing.T) {
212266
},
213267
}
214268

215-
ctx := &fasthttp.RequestCtx{}
269+
ctx := &fasthttpz.RequestCtx{RequestCtx: &fasthttp.RequestCtx{}}
216270

217271
resultCtx, err := executor.pipelineSecure(context.Background(), ctx, webhook)
218272

0 commit comments

Comments
 (0)