scorecard/clients/gitlabrepo/branches_test.go
jimrobison fa42daff71
🐛 Gitlab status updates (#3052)
* doc: Updating gitlab support validation status

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* bug: Updated  logic for gitlab to prevent exceptions based on releases

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* test: Added initial tests for gitlab branches

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* doc: Updated general README

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* refactor: Cleaned up the query for pipelines to be focused on the commitID

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* feat: Allowed for a non-graphql method of retrieving MRs associated to a commit

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* doc: Updated status for the CI-Tests

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

* bug: Updated the host url for graphql querying. This enabled the removal of the code added for handling empty returns when executing against a non-gitlab.com repository.

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>

---------

Signed-off-by: Robison, Jim B <jim.b.robison@lmco.com>
Co-authored-by: Raghav Kaul <8695110+raghavkaul@users.noreply.github.com>
2023-05-26 16:45:46 +00:00

134 lines
3.6 KiB
Go

// Copyright 2023 OpenSSF Scorecard Authors
//
// 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 gitlabrepo
import (
"net/http"
"sync"
"testing"
"github.com/xanzy/go-gitlab"
)
func TestGetBranches(t *testing.T) {
t.Parallel()
tests := []struct {
returnStatus *gitlab.Response
queryBranchReturn *gitlab.Branch
branchReturn *gitlab.ProtectedBranch
apprvlReturn *gitlab.ProjectApprovals
name string
branchName string
expectedBranchName string
projectID string
projectChecksReturn []*gitlab.ProjectStatusCheck
}{
{
name: "TargetCommittishFromRelease",
branchName: "/myproject/-/commit/magiccommitid",
expectedBranchName: "",
projectID: "",
},
{
name: "Existing Protected Branch",
branchName: "branchName",
expectedBranchName: "branchName",
queryBranchReturn: &gitlab.Branch{
Name: "branchName",
Protected: true,
},
returnStatus: &gitlab.Response{
Response: &http.Response{
StatusCode: 200,
},
},
branchReturn: &gitlab.ProtectedBranch{},
projectChecksReturn: []*gitlab.ProjectStatusCheck{},
apprvlReturn: &gitlab.ProjectApprovals{},
},
{
name: "Existing UnProtected Branch",
branchName: "branchName",
expectedBranchName: "branchName",
queryBranchReturn: &gitlab.Branch{
Name: "branchName",
Protected: false,
},
returnStatus: &gitlab.Response{
Response: &http.Response{
StatusCode: 200,
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
handler := branchesHandler{
once: new(sync.Once),
repourl: &repoURL{
projectID: "5000",
},
queryBranch: func(pid interface{}, branch string,
options ...gitlab.RequestOptionFunc,
) (*gitlab.Branch, *gitlab.Response, error) {
return tt.queryBranchReturn, tt.returnStatus, nil
},
getProtectedBranch: func(pid interface{}, branch string,
options ...gitlab.RequestOptionFunc,
) (*gitlab.ProtectedBranch, *gitlab.Response, error) {
return tt.branchReturn, tt.returnStatus, nil
},
getProjectChecks: func(pid interface{}, opt *gitlab.ListOptions,
options ...gitlab.RequestOptionFunc,
) ([]*gitlab.ProjectStatusCheck, *gitlab.Response, error) {
return tt.projectChecksReturn, tt.returnStatus, nil
},
getApprovalConfiguration: func(pid interface{}, options ...gitlab.RequestOptionFunc) (
*gitlab.ProjectApprovals, *gitlab.Response, error,
) {
return tt.apprvlReturn, tt.returnStatus, nil
},
}
handler.once.Do(func() {})
// nolint: errcheck
br, _ := handler.getBranch(tt.branchName)
// nolint: unconvert
if string(*br.Name) != tt.expectedBranchName {
t.Errorf("Branch Name (%s) didn't match expected value (%s)", *br.Name, tt.expectedBranchName)
}
if tt.queryBranchReturn == nil {
return
}
actualBool := *br.Protected
expectedBool := tt.queryBranchReturn.Protected
if actualBool != expectedBool {
t.Errorf("Branch Protection didn't match expectation")
}
})
}
}