Skip to content

Commit

Permalink
Merge remote-tracking branch 'BigFiles/master' into fix-complexity-pr…
Browse files Browse the repository at this point in the history
…oblem

# Conflicts:
#	server/server.go
  • Loading branch information
Zherphy committed Nov 8, 2024
2 parents f6545c9 + 10beb8f commit 11ca45e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ jobs:
with:
args: -exclude-dir BigFiles -exclude-dir utils ./...
- name: Unit tests
run: go test -race -count=1 -v ./...
run: go test -race -count=1 -cover -v ./...
- name: cleanup
run: rm -f ~/.netrc
65 changes: 65 additions & 0 deletions auth/gitee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package auth

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

// SuiteGitee used for testing
type SuiteGitee struct {
suite.Suite
Repo string
Owner string
}

// SetupSuite used for testing
func (s *SuiteGitee) SetupSuite() {
s.Repo = "software-package-server"
s.Owner = "src-openeuler"
}

// TearDownSuite used for testing
func (s *SuiteGitee) TearDownSuite() {
}

func (s *SuiteGitee) TestGetToken() {
// getToken fail
token, err := getToken("user", "wrong_pwd")
assert.Equal(s.T(), "", token)
assert.NotNil(s.T(), err.Error())
}

func (s *SuiteGitee) TestCheckRepoOwner() {
// CheckRepoOwner success
userInRepo := UserInRepo{
Repo: s.Repo,
Owner: s.Owner,
}
err := CheckRepoOwner(userInRepo)
assert.Nil(s.T(), err)

// check no_exist repo
userInRepo = UserInRepo{
Repo: "repo",
Owner: "owner",
}
err = CheckRepoOwner(userInRepo)
assert.NotNil(s.T(), err)
}

func (s *SuiteGitee) TestVerifyUser() {
userInRepo := UserInRepo{
Repo: s.Repo,
Owner: s.Owner,
Operation: "download",
}

err := verifyUser(userInRepo)
assert.NotNil(s.T(), err)
}

func TestGitee(t *testing.T) {
suite.Run(t, new(SuiteGitee))
}
47 changes: 47 additions & 0 deletions auth/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package auth

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

// SuiteGithub used for testing
type SuiteGithub struct {
suite.Suite
Username string
Password string
}

// SetupSuite used for testing
func (s *SuiteGithub) SetupSuite() {
s.Username = "username"
s.Password = "password"
}

// TearDownSuite used for testing
func (s *SuiteGithub) TearDownSuite() {
}

func (s *SuiteGithub) TestStatic() {
// Static success
static := Static(s.Username, s.Password)
err := static(s.Username, s.Password)
assert.Nil(s.T(), err)

// Static fail
static = Static(s.Username, s.Password)
err = static(s.Username, "wrong_pwd")
assert.NotNil(s.T(), err)
}

func (s *SuiteGithub) TestGithubOrg() {
githubAuth := GithubOrg("github_org")
err := githubAuth("user", "token")
assert.NotNil(s.T(), err)
}

func TestGithub(t *testing.T) {
suite.Run(t, new(SuiteGithub))
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require github.com/stretchr/testify v1.9.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.24.6+incompatible
Expand Down
48 changes: 20 additions & 28 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,27 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
userInRepo.Owner = chi.URLParam(r, "owner")
userInRepo.Repo = chi.URLParam(r, "repo")
if err = auth.CheckRepoOwner(userInRepo); req.Operation == "upload" || err != nil {
err = s.dealWithAuthError(userInRepo, w, r, err)
if username, password, ok := r.BasicAuth(); ok {
userInRepo.Username = username
userInRepo.Password = password
err = s.isAuthorized(userInRepo)
} else {
err = errors.New("unauthorized: cannot get password")
}
if err != nil {
v := err.Error()
switch {
case strings.HasPrefix(v, "unauthorized") || strings.HasPrefix(v, "not_found"):
w.WriteHeader(401)
case strings.HasPrefix(v, "forbidden"):
w.WriteHeader(403)
default:
w.WriteHeader(500)
}
w.Header().Set("LFS-Authenticate", `Basic realm="Git LFS"`)
must(json.NewEncoder(w).Encode(batch.ErrorResponse{
Message: v,
}))
return
}
}
Expand Down Expand Up @@ -165,33 +184,6 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
must(json.NewEncoder(w).Encode(resp))
}

func (s *server) dealWithAuthError(userInRepo auth.UserInRepo, w http.ResponseWriter, r *http.Request, err error) error {
if username, password, ok := r.BasicAuth(); ok {
userInRepo.Username = username
userInRepo.Password = password
err = s.isAuthorized(userInRepo)
} else {
err = errors.New("unauthorized: cannot get password")
}
if err != nil {
v := err.Error()
switch {
case strings.HasPrefix(v, "unauthorized") || strings.HasPrefix(v, "not_found"):
w.WriteHeader(401)
case strings.HasPrefix(v, "forbidden"):
w.WriteHeader(403)
default:
w.WriteHeader(500)
}
w.Header().Set("LFS-Authenticate", `Basic realm="Git LFS"`)
must(json.NewEncoder(w).Encode(batch.ErrorResponse{
Message: v,
}))
return err
}
return nil
}

func (s *server) downloadObject(in *batch.RequestObject, out *batch.Object) {
getObjectMetadataInput := &obs.GetObjectMetadataInput{
Bucket: s.bucket,
Expand Down

0 comments on commit 11ca45e

Please sign in to comment.