woodpecker/server/remote/gitlab/gitlab_test.go

233 lines
7 KiB
Go
Raw Normal View History

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-08-22 19:06:56 +00:00
package gitlab
import (
"bytes"
"context"
2015-08-22 19:06:56 +00:00
"net/http"
"net/url"
"strconv"
2015-08-22 19:06:56 +00:00
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/remote/gitlab/testdata"
2015-08-22 19:06:56 +00:00
)
func load(t *testing.T, config string) *Gitlab {
_url, err := url.Parse(config)
if err != nil {
t.FailNow()
}
params := _url.Query()
_url.RawQuery = ""
gitlab := Gitlab{}
gitlab.URL = _url.String()
gitlab.ClientID = params.Get("client_id")
gitlab.ClientSecret = params.Get("client_secret")
gitlab.SkipVerify, _ = strconv.ParseBool(params.Get("skip_verify"))
gitlab.HideArchives, _ = strconv.ParseBool(params.Get("hide_archives"))
// this is a temp workaround
gitlab.Search, _ = strconv.ParseBool(params.Get("search"))
return &gitlab
}
2015-08-22 19:06:56 +00:00
func Test_Gitlab(t *testing.T) {
// setup a dummy gitlab server
2022-01-05 20:50:23 +00:00
server := testdata.NewServer(t)
2015-08-22 19:06:56 +00:00
defer server.Close()
2016-04-12 20:22:23 +00:00
env := server.URL + "?client_id=test&client_secret=test"
2015-09-30 01:21:17 +00:00
client := load(t, env)
2015-08-22 19:06:56 +00:00
2022-01-05 20:50:23 +00:00
user := model.User{
2015-08-22 19:06:56 +00:00
Login: "test_user",
Token: "e3b0c44298fc1c149afbf4c8996fb",
}
2022-01-05 20:50:23 +00:00
repo := model.Repo{
2015-08-22 19:06:56 +00:00
Name: "diaspora-client",
Owner: "diaspora",
}
ctx := context.Background()
2015-08-22 19:06:56 +00:00
g := goblin.Goblin(t)
g.Describe("Gitlab Plugin", func() {
// Test projects method
g.Describe("AllProjects", func() {
g.It("Should return only non-archived projects is hidden", func() {
client.HideArchives = true
_projects, err := client.Repos(ctx, &user)
assert.NoError(t, err)
assert.Len(t, _projects, 1)
})
g.It("Should return all the projects", func() {
client.HideArchives = false
_projects, err := client.Repos(ctx, &user)
g.Assert(err).IsNil()
g.Assert(len(_projects)).Equal(2)
})
})
2015-08-22 19:06:56 +00:00
// Test repository method
g.Describe("Repo", func() {
g.It("Should return valid repo", func() {
_repo, err := client.Repo(ctx, &user, "0", "diaspora", "diaspora-client")
assert.NoError(t, err)
assert.Equal(t, "diaspora-client", _repo.Name)
assert.Equal(t, "diaspora", _repo.Owner)
assert.True(t, _repo.IsSCMPrivate)
2015-08-22 19:06:56 +00:00
})
g.It("Should return error, when repo not exist", func() {
_, err := client.Repo(ctx, &user, "0", "not-existed", "not-existed")
assert.Error(t, err)
2015-08-22 19:06:56 +00:00
})
})
// Test permissions method
g.Describe("Perm", func() {
g.It("Should return repo permissions", func() {
perm, err := client.Perm(ctx, &user, &repo)
assert.NoError(t, err)
assert.True(t, perm.Admin)
assert.True(t, perm.Pull)
assert.True(t, perm.Push)
2015-08-22 19:06:56 +00:00
})
2016-01-07 14:35:53 +00:00
g.It("Should return repo permissions when user is admin", func() {
perm, err := client.Perm(ctx, &user, &model.Repo{
Owner: "brightbox",
Name: "puppet",
})
assert.NoError(t, err)
2016-01-07 14:35:53 +00:00
g.Assert(perm.Admin).Equal(true)
g.Assert(perm.Pull).Equal(true)
g.Assert(perm.Push).Equal(true)
})
2015-08-22 19:06:56 +00:00
g.It("Should return error, when repo is not exist", func() {
_, err := client.Perm(ctx, &user, &model.Repo{
Owner: "not-existed",
Name: "not-existed",
})
2015-08-22 19:06:56 +00:00
g.Assert(err).IsNotNil()
2015-08-22 19:06:56 +00:00
})
})
// Test activate method
g.Describe("Activate", func() {
g.It("Should be success", func() {
err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook/test/test?access_token=token")
assert.NoError(t, err)
2015-08-22 19:06:56 +00:00
})
g.It("Should be failed, when token not given", func() {
err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook/test/test")
2015-08-22 19:06:56 +00:00
g.Assert(err).IsNotNil()
2015-08-22 19:06:56 +00:00
})
})
// Test deactivate method
g.Describe("Deactivate", func() {
g.It("Should be success", func() {
err := client.Deactivate(ctx, &user, &repo, "http://example.com/api/hook/test/test?access_token=token")
2015-08-22 19:06:56 +00:00
g.Assert(err).IsNil()
2015-08-22 19:06:56 +00:00
})
})
// Test hook method
g.Describe("Hook", func() {
2016-02-22 20:43:54 +00:00
g.Describe("Push hook", func() {
g.It("Should parse actual push hook", func() {
2016-02-22 20:43:54 +00:00
req, _ := http.NewRequest(
testdata.ServiceHookMethod,
testdata.ServiceHookURL.String(),
bytes.NewReader(testdata.ServiceHookPushBody),
2016-02-22 20:43:54 +00:00
)
req.Header = testdata.ServiceHookHeaders
hookRepo, build, err := client.Hook(ctx, req)
assert.NoError(t, err)
if assert.NotNil(t, hookRepo) && assert.NotNil(t, build) {
assert.Equal(t, build.Event, model.EventPush)
assert.Equal(t, "test", hookRepo.Owner)
assert.Equal(t, "woodpecker", hookRepo.Name)
assert.Equal(t, "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg", hookRepo.Avatar)
assert.Equal(t, "develop", hookRepo.Branch)
assert.Equal(t, "refs/heads/master", build.Ref)
assert.Equal(t, []string{"cmd/cli/main.go"}, build.ChangedFiles)
}
2016-02-22 20:43:54 +00:00
})
})
2015-08-22 19:06:56 +00:00
2016-02-22 20:43:54 +00:00
g.Describe("Tag push hook", func() {
g.It("Should parse tag push hook", func() {
req, _ := http.NewRequest(
testdata.ServiceHookMethod,
testdata.ServiceHookURL.String(),
bytes.NewReader(testdata.ServiceHookTagPushBody),
2016-02-22 20:43:54 +00:00
)
req.Header = testdata.ServiceHookHeaders
hookRepo, build, err := client.Hook(ctx, req)
assert.NoError(t, err)
if assert.NotNil(t, hookRepo) && assert.NotNil(t, build) {
assert.Equal(t, "test", hookRepo.Owner)
assert.Equal(t, "woodpecker", hookRepo.Name)
assert.Equal(t, "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg", hookRepo.Avatar)
assert.Equal(t, "develop", hookRepo.Branch)
assert.Equal(t, "refs/tags/v22", build.Ref)
assert.Len(t, build.ChangedFiles, 0)
}
2016-02-22 20:43:54 +00:00
})
2015-08-22 19:06:56 +00:00
})
g.Describe("Merge request hook", func() {
g.It("Should parse merge request hook", func() {
req, _ := http.NewRequest(
testdata.ServiceHookMethod,
testdata.ServiceHookURL.String(),
bytes.NewReader(testdata.WebhookMergeRequestBody),
)
req.Header = testdata.ServiceHookHeaders
// TODO: insert fake store into context to retrieve user & repo, this will activate fetching of ChangedFiles
hookRepo, build, err := client.Hook(ctx, req)
assert.NoError(t, err)
if assert.NotNil(t, hookRepo) && assert.NotNil(t, build) {
assert.Equal(t, "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg", hookRepo.Avatar)
assert.Equal(t, "main", hookRepo.Branch)
assert.Equal(t, "anbraten", hookRepo.Owner)
assert.Equal(t, "woodpecker", hookRepo.Name)
assert.Equal(t, "Update client.go 🎉", build.Title)
assert.Len(t, build.ChangedFiles, 0) // see L217
}
})
2015-08-22 19:06:56 +00:00
})
})
})
}