| /* |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| package api |
| |
| import ( |
| "net/http" |
| "net/http/httptest" |
| "strings" |
| "testing" |
| |
| corectx "github.com/apache/devlake/core/context" |
| contextimpl "github.com/apache/devlake/impls/context" |
| "github.com/apache/devlake/impls/logruslog" |
| "github.com/gin-gonic/gin" |
| "github.com/spf13/viper" |
| ) |
| |
| func newPushTestBasicRes() corectx.BasicRes { |
| cfg := viper.New() |
| cfg.Set("ENCRYPTION_SECRET", strings.Repeat("a", 32)) |
| return contextimpl.NewDefaultBasicRes(cfg, logruslog.Global, nil) |
| } |
| |
| func TestRequirePushAuthenticationRejectsMissingToken(t *testing.T) { |
| gin.SetMode(gin.TestMode) |
| router := gin.New() |
| router.Use(RequirePushAuthentication(newPushTestBasicRes())) |
| router.POST("/push/:tableName", func(c *gin.Context) { |
| c.Status(http.StatusOK) |
| }) |
| |
| req := httptest.NewRequest(http.MethodPost, "/push/commits", strings.NewReader(`[{}]`)) |
| req.Header.Set("Content-Type", "application/json") |
| resp := httptest.NewRecorder() |
| router.ServeHTTP(resp, req) |
| |
| if resp.Code != http.StatusUnauthorized { |
| t.Fatalf("status = %d, want %d", resp.Code, http.StatusUnauthorized) |
| } |
| } |
| |
| func TestRequirePushAuthenticationRejectsMalformedToken(t *testing.T) { |
| gin.SetMode(gin.TestMode) |
| router := gin.New() |
| router.Use(RequirePushAuthentication(newPushTestBasicRes())) |
| router.POST("/push/:tableName", func(c *gin.Context) { |
| c.Status(http.StatusOK) |
| }) |
| |
| req := httptest.NewRequest(http.MethodPost, "/push/commits", strings.NewReader(`[{}]`)) |
| req.Header.Set("Content-Type", "application/json") |
| req.Header.Set("Authorization", "Basic dGVzdDp0ZXN0") |
| resp := httptest.NewRecorder() |
| router.ServeHTTP(resp, req) |
| |
| if resp.Code != http.StatusUnauthorized { |
| t.Fatalf("status = %d, want %d", resp.Code, http.StatusUnauthorized) |
| } |
| } |