Skip to content

Commit

Permalink
polish the code and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
littlejiancc committed Sep 14, 2021
1 parent 6227b64 commit bacccf7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
4 changes: 2 additions & 2 deletions modules/gittar/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ func doAuth(c *webcontext.Context, repo *models.Repo, repoName string) {
return
}
logrus.Infof("repo: %s userId: %v, username: %s", repoName, userIdStr, userInfoDto.Username)
//校验通过缓存5分钟结果
//校验失败每次都会请求

// if success, caches the results for 5 minutes
_, validateError := ValidaUserRepoWithCache(c, userIdStr, repo)
if validateError != nil {
logrus.Infof("openapi auth fail repo:%s user:%s", repoName, userInfoDto.Username)
Expand Down
88 changes: 88 additions & 0 deletions modules/gittar/auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed 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 auth

import (
"net/http"
"net/http/httptest"
"testing"

"bou.ke/monkey"
"github.com/labstack/echo"

"github.com/erda-project/erda/modules/gittar/models"
"github.com/erda-project/erda/modules/gittar/pkg/gitmodule"
"github.com/erda-project/erda/modules/gittar/webcontext"
)

func TestIsGitProtocolRequest(t *testing.T) {
e := echo.New()
req1 := httptest.NewRequest(http.MethodGet, "/", nil)
req2 := httptest.NewRequest(http.MethodGet, "/", nil)
req1.Header.Add("Git-Protocol", "version")
res := httptest.NewRecorder()
ctx1 := &webcontext.Context{
EchoContext: e.NewContext(req1, res),
Repository: nil,
User: nil,
Service: nil,
DBClient: nil,
Bundle: nil,
UCAuth: nil,
EtcdClient: nil,
}
ctx2 := &webcontext.Context{
EchoContext: e.NewContext(req2, res),
Repository: nil,
User: nil,
Service: nil,
DBClient: nil,
Bundle: nil,
UCAuth: nil,
EtcdClient: nil,
}

if isGitProtocolRequest(ctx1) != true {
t.Error("fail")
}
if isGitProtocolRequest(ctx2) == true {
t.Error("fail")
}
}

func TestDoAuthWithHttpProtocolWithoutUserID(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
ctx := &webcontext.Context{
EchoContext: e.NewContext(req, res),
Repository: nil,
User: nil,
Service: nil,
DBClient: nil,
Bundle: nil,
UCAuth: nil,
EtcdClient: nil,
}
monkey.Patch(openRepository, func(ctx *webcontext.Context, repo *models.Repo) (*gitmodule.Repository, error) {
return nil, nil
})
defer monkey.UnpatchAll()

doAuth(ctx, nil, "")
if ctx.EchoContext.Response().Status == 500 {
t.Error("fail")
}
}
3 changes: 3 additions & 0 deletions modules/gittar/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func (p *provider) Initialize() error {
// cron task to git gc all repository
go gc.ScheduledExecuteClean()

// start hook task consumer
models.Init(dbClient)

return e.Start(":" + conf.ListenPort())
}

Expand Down
6 changes: 1 addition & 5 deletions modules/gittar/models/web_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ func (svc *Service) CreateHookTask(task *WebHookTask) error {

}

func init() {
db, err := OpenDB()
if err != nil {
panic(err)
}
func Init(db *DBClient) {
for i := 0; i < 5; i += 1 {
go StartHookTaskConsumer(db)
}
Expand Down

0 comments on commit bacccf7

Please sign in to comment.