Skip to content

Commit

Permalink
🌱 Fix failing GitLab e2e tests (#3393)
Browse files Browse the repository at this point in the history
* Fix branch protection test type mismatch.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* add unit test for gitlab listissues.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Delete gitlab issues e2e test.

It's been replaced with a unit test.

Signed-off-by: Spencer Schrock <sschrock@google.com>

---------

Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock committed Aug 15, 2023
1 parent accaffd commit 1a32607
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 43 deletions.
4 changes: 2 additions & 2 deletions clients/gitlabrepo/branches_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var _ = Describe("E2E TEST: gitlabrepo.Branches", func() {
Context("Test Default branch- GitLab", func() {
It("returns branches for the repo", func() {
It("returns default branch for the repo", func() {
skipIfTokenIsNot(gitlabPATTokenType, "GitLab only")
repo, err := MakeGitlabRepo("https://gitlab.com/ossf-test/scorecard")
Expect(err).Should(BeNil())
Expand All @@ -35,7 +35,7 @@ var _ = Describe("E2E TEST: gitlabrepo.Branches", func() {
Expect(err).Should(BeNil())
branch, err := client.GetDefaultBranch()
Expect(err).Should(BeNil())
Expect(branch.Name).Should(Equal("main"))
Expect(*branch.Name).Should(Equal("main"))
})
})
})
41 changes: 0 additions & 41 deletions clients/gitlabrepo/issues_e2e_test.go

This file was deleted.

131 changes: 131 additions & 0 deletions clients/gitlabrepo/issues_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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"
"os"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/xanzy/go-gitlab"

"github.com/ossf/scorecard/v4/clients"
)

// suffix may not be the best term, but maps the final part of a path to a response file.
// this is helpful when multiple API calls need to be made.
// e.g. a call to /foo/bar/some/endpoint would have "endpoint" as a suffix.
type suffixStubTripper struct {
// key is suffix, value is response file.
responsePaths map[string]string
}

func (s suffixStubTripper) RoundTrip(r *http.Request) (*http.Response, error) {
pathParts := strings.Split(r.URL.Path, "/")
suffix := pathParts[len(pathParts)-1]
f, err := os.Open(s.responsePaths[suffix])
if err != nil {
//nolint:wrapcheck
return nil, err
}
return &http.Response{
Status: "200 OK",
StatusCode: http.StatusOK,
Body: f,
}, nil
}

func strptr(s string) *string {
return &s
}

func associationptr(r clients.RepoAssociation) *clients.RepoAssociation {
return &r
}

func timeptr(t time.Time) *time.Time {
return &t
}

func Test_listIssues(t *testing.T) {
t.Parallel()
tests := []struct {
name string
issuePath string
memberPath string
want []clients.Issue
wantErr bool
}{
{
name: "issue with maintainer as author",
issuePath: "./testdata/valid-issues",
memberPath: "./testdata/valid-repo-members",
want: []clients.Issue{
{
URI: strptr("131356518"),
CreatedAt: timeptr(time.Date(2023, time.July, 26, 14, 22, 52, 0, time.UTC)),
Author: &clients.User{
ID: 1355794,
},
AuthorAssociation: associationptr(clients.RepoAssociationMaintainer),
},
},
wantErr: false,
},
{
name: "failure fetching issues",
issuePath: "./testdata/invalid-issues",
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
httpClient := &http.Client{
Transport: suffixStubTripper{
responsePaths: map[string]string{
"issues": tt.issuePath, // corresponds to projects/<id>/issues
"all": tt.memberPath, // corresponds to projects/<id>/members/all
},
},
}
client, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
if err != nil {
t.Fatalf("gitlab.NewClient error: %v", err)
}
handler := &issuesHandler{
glClient: client,
}

repoURL := repoURL{
owner: "ossf-tests",
commitSHA: clients.HeadSHA,
}
handler.init(&repoURL)
got, err := handler.listIssues()
if (err != nil) != tt.wantErr {
t.Fatalf("listIssues error: %v, wantedErr: %t", err, tt.wantErr)
}
if !cmp.Equal(got, tt.want) {
t.Errorf("listIssues() = %v, want %v", got, cmp.Diff(got, tt.want))
}
})
}
}
64 changes: 64 additions & 0 deletions clients/gitlabrepo/testdata/valid-issues
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"id": 131356518,
"iid": 1,
"project_id": 48033865,
"title": "test issue",
"description": "",
"state": "opened",
"created_at": "2023-07-26T14:22:52.000Z",
"updated_at": "2023-07-26T14:22:52.000Z",
"closed_at": null,
"closed_by": null,
"labels": [],
"milestone": null,
"assignees": [],
"author": {
"id": 1355794,
"username": "maintainer1",
"name": "Bob",
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/5eefb3063e3a44f456e4c1ca5740105b?s=80&d=identicon",
"web_url": "https://gitlab.com/maintainer1"
},
"type": "ISSUE",
"assignee": null,
"user_notes_count": 0,
"merge_requests_count": 0,
"upvotes": 0,
"downvotes": 0,
"due_date": null,
"confidential": false,
"discussion_locked": null,
"issue_type": "issue",
"web_url": "https://gitlab.com/ossf-test/e2e-issues/-/issues/1",
"time_stats": {
"time_estimate": 0,
"total_time_spent": 0,
"human_time_estimate": null,
"human_total_time_spent": null
},
"task_completion_status": {
"count": 0,
"completed_count": 0
},
"blocking_issues_count": 0,
"has_tasks": true,
"task_status": "",
"_links": {
"self": "https://gitlab.com/api/v4/projects/48033865/issues/1",
"notes": "https://gitlab.com/api/v4/projects/48033865/issues/1/notes",
"award_emoji": "https://gitlab.com/api/v4/projects/48033865/issues/1/award_emoji",
"project": "https://gitlab.com/api/v4/projects/48033865",
"closed_as_duplicate_of": null
},
"references": {
"short": "#1",
"relative": "#1",
"full": "ossf-test/e2e-issues#1"
},
"severity": "UNKNOWN",
"moved_to_id": null,
"service_desk_reply_to": null
}
]
34 changes: 34 additions & 0 deletions clients/gitlabrepo/testdata/valid-repo-members
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"access_level": 50,
"created_at": "2023-02-21T18:24:39.599Z",
"expires_at": null,
"id": 3332021,
"username": "owner1",
"name": "Alice",
"state": "active",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/3332021/avatar.png",
"web_url": "https://gitlab.com/owner1",
"membership_state": "active"
},
{
"access_level": 40,
"created_at": "2023-02-28T20:36:17.805Z",
"created_by": {
"id": 3332021,
"username": "owner1",
"name": "Alice",
"state": "active",
"avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/3332021/avatar.png",
"web_url": "https://gitlab.com/owner1"
},
"expires_at": null,
"id": 1355794,
"username": "maintainer1",
"name": "Bob",
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/5eefb3063e3a44f456e4c1ca5740105b?s=80&d=identicon",
"web_url": "https://gitlab.com/maintainer1",
"membership_state": "active"
}
]

0 comments on commit 1a32607

Please sign in to comment.