2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-11-11 21:42:31 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2016-05-03 20:01:16 +00:00
|
|
|
"net/http/httptest"
|
2015-11-11 21:42:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-05-03 20:01:16 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-12-30 06:08:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2024-12-22 09:44:34 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/forge/github/fixtures"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/model"
|
2015-11-11 21:42:31 +00:00
|
|
|
)
|
|
|
|
|
2024-12-30 06:08:53 +00:00
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
forge, _ := New(Opts{
|
|
|
|
URL: "http://localhost:8080/",
|
|
|
|
Client: "0ZXh0IjoiI",
|
|
|
|
Secret: "I1NiIsInR5",
|
|
|
|
SkipVerify: true,
|
|
|
|
})
|
|
|
|
f, _ := forge.(*client)
|
|
|
|
assert.Equal(t, "http://localhost:8080", f.url)
|
|
|
|
assert.Equal(t, "http://localhost:8080/api/v3/", f.API)
|
|
|
|
assert.Equal(t, "0ZXh0IjoiI", f.Client)
|
|
|
|
assert.Equal(t, "I1NiIsInR5", f.Secret)
|
|
|
|
assert.True(t, f.SkipVerify)
|
|
|
|
}
|
|
|
|
|
2016-05-03 20:01:16 +00:00
|
|
|
func Test_github(t *testing.T) {
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
|
|
|
s := httptest.NewServer(fixtures.Handler())
|
|
|
|
c, _ := New(Opts{
|
|
|
|
URL: s.URL,
|
|
|
|
SkipVerify: true,
|
|
|
|
})
|
2015-11-11 21:42:31 +00:00
|
|
|
|
2024-12-30 06:08:53 +00:00
|
|
|
defer s.Close()
|
2016-05-03 20:01:16 +00:00
|
|
|
|
2024-12-30 06:08:53 +00:00
|
|
|
ctx := context.Background()
|
2016-05-03 20:01:16 +00:00
|
|
|
|
2024-12-30 06:08:53 +00:00
|
|
|
t.Run("netrc with user token", func(t *testing.T) {
|
|
|
|
forge, _ := New(Opts{})
|
|
|
|
netrc, _ := forge.Netrc(fakeUser, fakeRepo)
|
|
|
|
assert.Equal(t, "github.com", netrc.Machine)
|
|
|
|
assert.Equal(t, fakeUser.AccessToken, netrc.Login)
|
|
|
|
assert.Equal(t, "x-oauth-basic", netrc.Password)
|
|
|
|
})
|
|
|
|
t.Run("netrc with machine account", func(t *testing.T) {
|
|
|
|
forge, _ := New(Opts{})
|
|
|
|
netrc, _ := forge.Netrc(nil, fakeRepo)
|
|
|
|
assert.Equal(t, "github.com", netrc.Machine)
|
|
|
|
assert.Empty(t, netrc.Login)
|
|
|
|
assert.Empty(t, netrc.Password)
|
|
|
|
})
|
2016-05-03 20:01:16 +00:00
|
|
|
|
2024-12-30 06:08:53 +00:00
|
|
|
t.Run("Should return the repository details", func(t *testing.T) {
|
|
|
|
repo, err := c.Repo(ctx, fakeUser, fakeRepo.ForgeRemoteID, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fakeRepo.ForgeRemoteID, repo.ForgeRemoteID)
|
|
|
|
assert.Equal(t, fakeRepo.Owner, repo.Owner)
|
|
|
|
assert.Equal(t, fakeRepo.Name, repo.Name)
|
|
|
|
assert.Equal(t, fakeRepo.FullName, repo.FullName)
|
|
|
|
assert.True(t, repo.IsSCMPrivate)
|
|
|
|
assert.Equal(t, fakeRepo.Clone, repo.Clone)
|
|
|
|
assert.Equal(t, fakeRepo.ForgeURL, repo.ForgeURL)
|
|
|
|
})
|
|
|
|
t.Run("repo not found error", func(t *testing.T) {
|
|
|
|
_, err := c.Repo(ctx, fakeUser, "0", fakeRepoNotFound.Owner, fakeRepoNotFound.Name)
|
|
|
|
assert.Error(t, err)
|
2015-11-11 21:42:31 +00:00
|
|
|
})
|
|
|
|
}
|
2016-03-10 19:30:14 +00:00
|
|
|
|
2016-05-03 20:01:16 +00:00
|
|
|
var (
|
|
|
|
fakeUser = &model.User{
|
2024-11-22 18:12:43 +00:00
|
|
|
Login: "octocat",
|
|
|
|
AccessToken: "cfcd2084",
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fakeRepo = &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: "5",
|
|
|
|
Owner: "octocat",
|
|
|
|
Name: "Hello-World",
|
|
|
|
FullName: "octocat/Hello-World",
|
|
|
|
Avatar: "https://github.com/images/error/octocat_happy.gif",
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: "https://github.com/octocat/Hello-World",
|
2022-11-15 14:01:23 +00:00
|
|
|
Clone: "https://github.com/octocat/Hello-World.git",
|
|
|
|
IsSCMPrivate: true,
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fakeRepoNotFound = &model.Repo{
|
|
|
|
Owner: "test_name",
|
|
|
|
Name: "repo_not_found",
|
|
|
|
FullName: "test_name/repo_not_found",
|
|
|
|
}
|
|
|
|
)
|