|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package api |
| 19 | + |
| 20 | +import ( |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + corectx "github.com/apache/incubator-devlake/core/context" |
| 27 | + contextimpl "github.com/apache/incubator-devlake/impls/context" |
| 28 | + "github.com/apache/incubator-devlake/impls/logruslog" |
| 29 | + "github.com/gin-gonic/gin" |
| 30 | + "github.com/spf13/viper" |
| 31 | +) |
| 32 | + |
| 33 | +func newPushTestBasicRes() corectx.BasicRes { |
| 34 | + cfg := viper.New() |
| 35 | + cfg.Set("ENCRYPTION_SECRET", strings.Repeat("a", 32)) |
| 36 | + return contextimpl.NewDefaultBasicRes(cfg, logruslog.Global, nil) |
| 37 | +} |
| 38 | + |
| 39 | +func TestRequirePushAuthenticationRejectsMissingToken(t *testing.T) { |
| 40 | + gin.SetMode(gin.TestMode) |
| 41 | + router := gin.New() |
| 42 | + router.Use(RequirePushAuthentication(newPushTestBasicRes())) |
| 43 | + router.POST("/push/:tableName", func(c *gin.Context) { |
| 44 | + c.Status(http.StatusOK) |
| 45 | + }) |
| 46 | + |
| 47 | + req := httptest.NewRequest(http.MethodPost, "/push/commits", strings.NewReader(`[{}]`)) |
| 48 | + req.Header.Set("Content-Type", "application/json") |
| 49 | + resp := httptest.NewRecorder() |
| 50 | + router.ServeHTTP(resp, req) |
| 51 | + |
| 52 | + if resp.Code != http.StatusUnauthorized { |
| 53 | + t.Fatalf("status = %d, want %d", resp.Code, http.StatusUnauthorized) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestRequirePushAuthenticationRejectsMalformedToken(t *testing.T) { |
| 58 | + gin.SetMode(gin.TestMode) |
| 59 | + router := gin.New() |
| 60 | + router.Use(RequirePushAuthentication(newPushTestBasicRes())) |
| 61 | + router.POST("/push/:tableName", func(c *gin.Context) { |
| 62 | + c.Status(http.StatusOK) |
| 63 | + }) |
| 64 | + |
| 65 | + req := httptest.NewRequest(http.MethodPost, "/push/commits", strings.NewReader(`[{}]`)) |
| 66 | + req.Header.Set("Content-Type", "application/json") |
| 67 | + req.Header.Set("Authorization", "Basic dGVzdDp0ZXN0") |
| 68 | + resp := httptest.NewRecorder() |
| 69 | + router.ServeHTTP(resp, req) |
| 70 | + |
| 71 | + if resp.Code != http.StatusUnauthorized { |
| 72 | + t.Fatalf("status = %d, want %d", resp.Code, http.StatusUnauthorized) |
| 73 | + } |
| 74 | +} |
0 commit comments