|
| 1 | +package echo_middleware_path_auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/labstack/echo/v4" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func testKeyValidator(key string, _ echo.Context) (bool, error) { |
| 14 | + switch key { |
| 15 | + case "valid-key": |
| 16 | + return true, nil |
| 17 | + case "error-key": |
| 18 | + return false, errors.New("some user defined error") |
| 19 | + default: |
| 20 | + return false, nil |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestKeyAuth(t *testing.T) { |
| 25 | + t.Run("auth ok", func(t *testing.T) { |
| 26 | + handlerCalled := false |
| 27 | + handler := func(c echo.Context) error { |
| 28 | + handlerCalled = true |
| 29 | + return c.String(http.StatusOK, "test") |
| 30 | + } |
| 31 | + middlewareChain := PathAuth("apikey", testKeyValidator)(handler) |
| 32 | + |
| 33 | + e := echo.New() |
| 34 | + e.GET("/:apikey", middlewareChain) |
| 35 | + |
| 36 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 37 | + rec := httptest.NewRecorder() |
| 38 | + |
| 39 | + c := e.NewContext(req, rec) |
| 40 | + e.Router().Find(http.MethodGet, "/valid-key", c) |
| 41 | + err := middlewareChain(c) |
| 42 | + |
| 43 | + assert.NoError(t, err) |
| 44 | + assert.True(t, handlerCalled) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("auth nok", func(t *testing.T) { |
| 48 | + handlerCalled := false |
| 49 | + handler := func(c echo.Context) error { |
| 50 | + handlerCalled = true |
| 51 | + return c.String(http.StatusOK, "test") |
| 52 | + } |
| 53 | + middlewareChain := PathAuth("apikey", testKeyValidator)(handler) |
| 54 | + |
| 55 | + e := echo.New() |
| 56 | + e.GET("/:apikey", middlewareChain) |
| 57 | + |
| 58 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 59 | + rec := httptest.NewRecorder() |
| 60 | + |
| 61 | + c := e.NewContext(req, rec) |
| 62 | + e.Router().Find(http.MethodGet, "/error-key", c) |
| 63 | + err := middlewareChain(c) |
| 64 | + |
| 65 | + assert.Error(t, err) |
| 66 | + assert.False(t, handlerCalled) |
| 67 | + }) |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +func TestPathAuthWithConfig(t *testing.T) { |
| 72 | + var testCases = []struct { |
| 73 | + name string |
| 74 | + givenRequestFunc func() *http.Request |
| 75 | + givenRequest func(req *http.Request) |
| 76 | + whenConfig func(conf *PathAuthConfig) |
| 77 | + pathName string |
| 78 | + expectHandlerCalled bool |
| 79 | + expectError string |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "ok, default config", |
| 83 | + givenRequestFunc: func() *http.Request { |
| 84 | + req := httptest.NewRequest(http.MethodPost, "/valid-key", nil) |
| 85 | + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm) |
| 86 | + return req |
| 87 | + }, |
| 88 | + expectHandlerCalled: true, |
| 89 | + expectError: "", |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tc := range testCases { |
| 94 | + t.Run(tc.name, func(t *testing.T) { |
| 95 | + handlerCalled := false |
| 96 | + handler := func(c echo.Context) error { |
| 97 | + handlerCalled = true |
| 98 | + return c.String(http.StatusOK, "test") |
| 99 | + } |
| 100 | + config := PathAuthConfig{ |
| 101 | + Validator: testKeyValidator, |
| 102 | + Param: "apikey", |
| 103 | + } |
| 104 | + if tc.whenConfig != nil { |
| 105 | + tc.whenConfig(&config) |
| 106 | + } |
| 107 | + middlewareChain := PathAuthWithConfig(config)(handler) |
| 108 | + |
| 109 | + e := echo.New() |
| 110 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 111 | + if tc.givenRequestFunc != nil { |
| 112 | + req = tc.givenRequestFunc() |
| 113 | + } |
| 114 | + if tc.givenRequest != nil { |
| 115 | + tc.givenRequest(req) |
| 116 | + } |
| 117 | + e.GET("/:apikey", middlewareChain) |
| 118 | + rec := httptest.NewRecorder() |
| 119 | + c := e.NewContext(req, rec) |
| 120 | + e.Router().Find(http.MethodGet, "/valid-key", c) |
| 121 | + err := middlewareChain(c) |
| 122 | + |
| 123 | + assert.Equal(t, tc.expectHandlerCalled, handlerCalled) |
| 124 | + if tc.expectError != "" { |
| 125 | + assert.EqualError(t, err, tc.expectError) |
| 126 | + } else { |
| 127 | + assert.NoError(t, err) |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestPathAuthWithConfig_panicsOnEmptyValidator(t *testing.T) { |
| 134 | + assert.PanicsWithValue( |
| 135 | + t, |
| 136 | + "PathAuth: requires a validator function", |
| 137 | + func() { |
| 138 | + handler := func(c echo.Context) error { |
| 139 | + return c.String(http.StatusOK, "test") |
| 140 | + } |
| 141 | + PathAuthWithConfig(PathAuthConfig{ |
| 142 | + Validator: nil, |
| 143 | + })(handler) |
| 144 | + }, |
| 145 | + ) |
| 146 | +} |
| 147 | + |
| 148 | +func TestPathAuthWithConfig_panicsOnEmptyParam(t *testing.T) { |
| 149 | + assert.PanicsWithValue( |
| 150 | + t, |
| 151 | + "PathAuth: requires a param", |
| 152 | + func() { |
| 153 | + handler := func(c echo.Context) error { |
| 154 | + return c.String(http.StatusOK, "test") |
| 155 | + } |
| 156 | + PathAuthWithConfig(PathAuthConfig{ |
| 157 | + Validator: func(auth string, c echo.Context) (bool, error) { |
| 158 | + return true, nil |
| 159 | + }, |
| 160 | + Param: "", |
| 161 | + })(handler) |
| 162 | + }, |
| 163 | + ) |
| 164 | + |
| 165 | + assert.PanicsWithValue( |
| 166 | + t, |
| 167 | + "PathAuth: requires a param", |
| 168 | + func() { |
| 169 | + handler := func(c echo.Context) error { |
| 170 | + return c.String(http.StatusOK, "test") |
| 171 | + } |
| 172 | + PathAuth("", func(auth string, c echo.Context) (bool, error) { |
| 173 | + return true, nil |
| 174 | + })(handler) |
| 175 | + }, |
| 176 | + ) |
| 177 | +} |
0 commit comments