Skip to content

Commit

Permalink
test(integration): add custom security test
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Sep 25, 2023
1 parent d84da5c commit 17e2d1e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
22 changes: 21 additions & 1 deletion _testdata/positive/security.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@
}
}
}
},
"/customSecurity": {
"get": {
"operationId": "customSecurity",
"security": [
{
"custom": []
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {
Expand All @@ -86,7 +101,12 @@
"bearerToken": {
"type": "http",
"scheme": "bearer"
},
"custom": {
"type": "http",
"scheme": "digest",
"x-ogen-custom-security": true
}
}
}
}
}
42 changes: 42 additions & 0 deletions internal/integration/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import (
"github.com/ogen-go/ogen/ogenerrors"
)

const customSecurityHeader = "X-Foo-Custom"

type testSecurity struct {
basicAuth api.BasicAuth
bearerToken api.BearerToken
headerKey api.HeaderKey
queryKey api.QueryKey
cookieKey api.CookieKey
custom string
}

func (t *testSecurity) OptionalSecurity(ctx context.Context) error {
Expand All @@ -34,6 +37,10 @@ func (t *testSecurity) IntersectSecurity(ctx context.Context) error {
return nil
}

func (t *testSecurity) CustomSecurity(ctx context.Context) error {
return nil
}

type tokenKey string

func (t *testSecurity) HandleBasicAuth(ctx context.Context, operationName string, v api.BasicAuth) (context.Context, error) {
Expand Down Expand Up @@ -71,12 +78,21 @@ func (t *testSecurity) HandleCookieKey(ctx context.Context, operationName string
return context.WithValue(ctx, tokenKey("CookieKey"), v), nil
}

func (t *testSecurity) HandleCustom(ctx context.Context, operationName string, req *http.Request) (context.Context, error) {
got := req.Header.Get(customSecurityHeader)
if got != t.custom {
return nil, errors.Errorf("invalid custom auth: %q", got)
}
return context.WithValue(ctx, tokenKey("Custom"), got), nil
}

type testSecuritySource struct {
basicAuth *api.BasicAuth
bearerToken *api.BearerToken
headerKey *api.HeaderKey
queryKey *api.QueryKey
cookieKey *api.CookieKey
custom string
}

func (t *testSecuritySource) BasicAuth(ctx context.Context, operationName string) (r api.BasicAuth, _ error) {
Expand Down Expand Up @@ -114,13 +130,22 @@ func (t *testSecuritySource) CookieKey(ctx context.Context, operationName string
return r, ogenerrors.ErrSkipClientSecurity
}

func (t *testSecuritySource) Custom(ctx context.Context, operationName string, req *http.Request) error {
if t.custom == "" {
return ogenerrors.ErrSkipClientSecurity
}
req.Header.Set(customSecurityHeader, t.custom)
return nil
}

func TestSecurity(t *testing.T) {
h := &testSecurity{
basicAuth: api.BasicAuth{Username: "username", Password: "password"},
bearerToken: api.BearerToken{Token: "BearerToken"},
headerKey: api.HeaderKey{APIKey: "HeaderKey"},
queryKey: api.QueryKey{APIKey: "QueryKey"},
cookieKey: api.CookieKey{APIKey: "CookieKey"},
custom: "foobar-custom-token",
}
srv, err := api.NewServer(h, h)
require.NoError(t, err)
Expand All @@ -135,6 +160,7 @@ func TestSecurity(t *testing.T) {
bearerToken: &h.bearerToken,
headerKey: &h.headerKey,
queryKey: &h.queryKey,
custom: h.custom,
}, api.WithClient(s.Client()))
require.NoError(t, err)

Expand Down Expand Up @@ -217,6 +243,22 @@ func TestSecurity(t *testing.T) {

require.NoError(t, client.IntersectSecurity(context.Background()))
})
t.Run("CustomSecurity", func(t *testing.T) {
resp := sendReq(t, "/customSecurity", nil)
require.Equal(t, http.StatusUnauthorized, resp.StatusCode)

resp = sendReq(t, "/customSecurity", func(r *http.Request) {
r.Header.Set(customSecurityHeader, "wrong-token")
})
require.Equal(t, http.StatusUnauthorized, resp.StatusCode)

resp = sendReq(t, "/customSecurity", func(r *http.Request) {
r.Header.Set(customSecurityHeader, h.custom)
})
require.Equal(t, http.StatusOK, resp.StatusCode)

require.NoError(t, client.CustomSecurity(context.Background()))
})
}

func TestSecurityClientCheck(t *testing.T) {
Expand Down

0 comments on commit 17e2d1e

Please sign in to comment.