mirror of
https://github.com/ossf/scorecard.git
synced 2024-09-20 05:27:12 +03:00
🌱 Unit tests for Gitlab status and WebHooks (#3367)
* 🌱 Improve gitlabrepo webhook handler tests
- Add a test for the gitlabrepo webhook handler
- Add a stubTripper for testing the webhook handler
- Add two test cases for the webhook handler: one valid and one invalid
[clients/gitlabrepo/webhook_test.go]
- Add a test for the gitlabrepo webhook handler
- Add a stubTripper for testing the webhook handler
- Add two test cases for the webhook handler: one valid and one invalid
Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
* :seelding: Tests for Gitlab repo status
- Add valid status data for Gitlab repos
- Add tests for the `listStatuses` function
- Add HTTP stubbing to the tests
[clients/gitlabrepo/testdata/valid-status]
- Add a new file with valid status data for Gitlab repos
[clients/gitlabrepo/statuses_test.go]
- Add a new file `clients/gitlabrepo/statuses_test.go`
- Add tests for the `listStatuses` function
- Add HTTP stubbing to the tests
Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
---------
Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
This commit is contained in:
parent
9033d51496
commit
d7841b95ca
86
clients/gitlabrepo/statuses_test.go
Normal file
86
clients/gitlabrepo/statuses_test.go
Normal file
@ -0,0 +1,86 @@
|
||||
// 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"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
|
||||
"github.com/ossf/scorecard/v4/clients"
|
||||
)
|
||||
|
||||
func Test_listStatuses(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
responsePath string
|
||||
want []clients.Status
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid webhook",
|
||||
responsePath: "./testdata/valid-status",
|
||||
want: []clients.Status{
|
||||
{
|
||||
State: "pending",
|
||||
Context: "bundler:audit",
|
||||
URL: "https://gitlab.example.com/janedoe/gitlab-foss/builds/91",
|
||||
TargetURL: "https://gitlab.example.com/janedoe/gitlab-foss/builds/91",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid webhook",
|
||||
responsePath: "./testdata/invalid-status",
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
httpClient := &http.Client{
|
||||
Transport: stubTripper{
|
||||
responsePath: tt.responsePath,
|
||||
},
|
||||
}
|
||||
client, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
|
||||
if err != nil {
|
||||
t.Fatalf("gitlab.NewClient error: %v", err)
|
||||
}
|
||||
handler := &statusesHandler{
|
||||
glClient: client,
|
||||
}
|
||||
|
||||
repoURL := repoURL{
|
||||
owner: "ossf-tests",
|
||||
commitSHA: clients.HeadSHA,
|
||||
}
|
||||
handler.init(&repoURL)
|
||||
got, err := handler.listStatuses("HEAD")
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("listStatuses error: %v, wantedErr: %t", err, tt.wantErr)
|
||||
}
|
||||
if !cmp.Equal(got, tt.want) {
|
||||
t.Errorf("listStatuses() = %v, want %v", got, cmp.Diff(got, tt.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
23
clients/gitlabrepo/testdata/valid-status
vendored
Normal file
23
clients/gitlabrepo/testdata/valid-status
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
[
|
||||
{
|
||||
"status" : "pending",
|
||||
"created_at" : "2016-01-19T08:40:25.934Z",
|
||||
"started_at" : null,
|
||||
"name" : "bundler:audit",
|
||||
"allow_failure" : true,
|
||||
"author" : {
|
||||
"username" : "janedoe",
|
||||
"state" : "active",
|
||||
"web_url" : "https://gitlab.example.com/janedoe",
|
||||
"avatar_url" : "https://gitlab.example.com/uploads/user/avatar/28/jane-doe-400-400.png",
|
||||
"id" : 28,
|
||||
"name" : "Jane Doe"
|
||||
},
|
||||
"description" : null,
|
||||
"sha" : "18f3e63d05582537db6d183d9d557be09e1f90c8",
|
||||
"target_url" : "https://gitlab.example.com/janedoe/gitlab-foss/builds/91",
|
||||
"finished_at" : null,
|
||||
"id" : 91,
|
||||
"ref" : "master"
|
||||
}
|
||||
]
|
27
clients/gitlabrepo/testdata/valid-webhook
vendored
Normal file
27
clients/gitlabrepo/testdata/valid-webhook
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"url": "http://example.com/hook",
|
||||
"project_id": 3,
|
||||
"push_events": true,
|
||||
"push_events_branch_filter": "",
|
||||
"issues_events": true,
|
||||
"confidential_issues_events": true,
|
||||
"merge_requests_events": true,
|
||||
"tag_push_events": true,
|
||||
"note_events": true,
|
||||
"confidential_note_events": true,
|
||||
"job_events": true,
|
||||
"pipeline_events": true,
|
||||
"wiki_page_events": true,
|
||||
"deployment_events": true,
|
||||
"releases_events": true,
|
||||
"enable_ssl_verification": true,
|
||||
"repository_update_events": false,
|
||||
"alert_status": "executable",
|
||||
"disabled_until": null,
|
||||
"url_variables": [ ],
|
||||
"created_at": "2012-10-12T17:04:47Z"
|
||||
}
|
||||
|
||||
]
|
103
clients/gitlabrepo/webhook_test.go
Normal file
103
clients/gitlabrepo/webhook_test.go
Normal file
@ -0,0 +1,103 @@
|
||||
// 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"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
|
||||
"github.com/ossf/scorecard/v4/clients"
|
||||
)
|
||||
|
||||
type stubTripper struct {
|
||||
responsePath string
|
||||
}
|
||||
|
||||
func (s stubTripper) RoundTrip(_ *http.Request) (*http.Response, error) {
|
||||
f, err := os.Open(s.responsePath)
|
||||
if err != nil {
|
||||
//nolint:wrapcheck
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
Status: "200 OK",
|
||||
StatusCode: http.StatusOK,
|
||||
Body: f,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func Test_listWebhooks(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
responsePath string
|
||||
want []clients.Webhook
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid webhook",
|
||||
responsePath: "./testdata/valid-webhook",
|
||||
want: []clients.Webhook{
|
||||
{
|
||||
ID: 1,
|
||||
Path: "http://example.com/hook",
|
||||
UsesAuthSecret: true,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid webhook",
|
||||
responsePath: "./testdata/invalid-webhook",
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
httpClient := &http.Client{
|
||||
Transport: stubTripper{
|
||||
responsePath: tt.responsePath,
|
||||
},
|
||||
}
|
||||
client, err := gitlab.NewClient("", gitlab.WithHTTPClient(httpClient))
|
||||
if err != nil {
|
||||
t.Fatalf("gitlab.NewClient error: %v", err)
|
||||
}
|
||||
handler := &webhookHandler{
|
||||
glClient: client,
|
||||
}
|
||||
|
||||
repoURL := repoURL{
|
||||
owner: "ossf-tests",
|
||||
commitSHA: clients.HeadSHA,
|
||||
}
|
||||
handler.init(&repoURL)
|
||||
got, err := handler.listWebhooks()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("listWebhooks error: %v, wantedErr: %t", err, tt.wantErr)
|
||||
}
|
||||
if !cmp.Equal(got, tt.want) {
|
||||
t.Errorf("listWebhooks() = %v, want %v", got, cmp.Diff(got, tt.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user