woodpecker/remote/gitlab/gitlab_test.go

262 lines
7.4 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"
"net/http"
"testing"
2019-09-14 12:21:16 +00:00
"github.com/franela/goblin"
"github.com/woodpecker-ci/woodpecker/model"
"github.com/woodpecker-ci/woodpecker/remote/gitlab/testdata"
2015-08-22 19:06:56 +00:00
)
func Test_Gitlab(t *testing.T) {
// setup a dummy github server
var server = testdata.NewServer()
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
gitlab := Load(env)
2015-08-22 19:06:56 +00:00
2015-09-30 01:21:17 +00:00
var user = model.User{
2015-08-22 19:06:56 +00:00
Login: "test_user",
Token: "e3b0c44298fc1c149afbf4c8996fb",
}
2015-09-30 01:21:17 +00:00
var repo = model.Repo{
2015-08-22 19:06:56 +00:00
Name: "diaspora-client",
Owner: "diaspora",
}
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() {
gitlab.HideArchives = true
_projects, err := gitlab.Repos(&user)
g.Assert(err == nil).IsTrue()
g.Assert(len(_projects)).Equal(1)
})
g.It("Should return all the projects", func() {
gitlab.HideArchives = false
_projects, err := gitlab.Repos(&user)
g.Assert(err == nil).IsTrue()
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 := gitlab.Repo(&user, "diaspora", "diaspora-client")
g.Assert(err == nil).IsTrue()
g.Assert(_repo.Name).Equal("diaspora-client")
g.Assert(_repo.Owner).Equal("diaspora")
2015-09-30 21:18:09 +00:00
g.Assert(_repo.IsPrivate).Equal(true)
2015-08-22 19:06:56 +00:00
})
g.It("Should return error, when repo not exist", func() {
_, err := gitlab.Repo(&user, "not-existed", "not-existed")
g.Assert(err != nil).IsTrue()
})
})
// Test permissions method
g.Describe("Perm", func() {
g.It("Should return repo permissions", func() {
perm, err := gitlab.Perm(&user, "diaspora", "diaspora-client")
g.Assert(err == nil).IsTrue()
g.Assert(perm.Admin).Equal(true)
g.Assert(perm.Pull).Equal(true)
g.Assert(perm.Push).Equal(true)
})
2016-01-07 14:35:53 +00:00
g.It("Should return repo permissions when user is admin", func() {
perm, err := gitlab.Perm(&user, "brightbox", "puppet")
g.Assert(err == nil).IsTrue()
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 := gitlab.Perm(&user, "not-existed", "not-existed")
g.Assert(err != nil).IsTrue()
})
})
// Test activate method
g.Describe("Activate", func() {
g.It("Should be success", func() {
err := gitlab.Activate(&user, &repo, "http://example.com/api/hook/test/test?access_token=token")
2015-08-22 19:06:56 +00:00
g.Assert(err == nil).IsTrue()
})
g.It("Should be failed, when token not given", func() {
err := gitlab.Activate(&user, &repo, "http://example.com/api/hook/test/test")
2015-08-22 19:06:56 +00:00
g.Assert(err != nil).IsTrue()
})
})
// Test deactivate method
g.Describe("Deactivate", func() {
g.It("Should be success", func() {
err := gitlab.Deactivate(&user, &repo, "http://example.com/api/hook/test/test?access_token=token")
g.Assert(err == nil).IsTrue()
})
})
// Test login method
2015-09-30 01:21:17 +00:00
// g.Describe("Login", func() {
// g.It("Should return user", func() {
// user, err := gitlab.Login("valid_token", "")
2015-08-22 19:06:56 +00:00
2015-09-30 01:21:17 +00:00
// g.Assert(err == nil).IsTrue()
// g.Assert(user == nil).IsFalse()
// })
2015-08-22 19:06:56 +00:00
2015-09-30 01:21:17 +00:00
// g.It("Should return error, when token is invalid", func() {
// _, err := gitlab.Login("invalid_token", "")
2015-08-22 19:06:56 +00:00
2015-09-30 01:21:17 +00:00
// g.Assert(err != nil).IsTrue()
// })
// })
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 hoook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.PushHook),
)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("mike")
g.Assert(repo.Name).Equal("diaspora")
g.Assert(repo.Avatar).Equal("http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg")
g.Assert(repo.Branch).Equal("develop")
g.Assert(build.Ref).Equal("refs/heads/master")
})
g.It("Should parse legacy push hoook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.LegacyPushHook),
)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(repo.Avatar).Equal("")
g.Assert(repo.Branch).Equal("master")
g.Assert(build.Ref).Equal("refs/heads/master")
})
})
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(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.TagHook),
)
2015-08-22 19:06:56 +00:00
2016-02-22 20:43:54 +00:00
repo, build, err := gitlab.Hook(req)
2015-08-22 19:55:08 +00:00
2016-02-22 20:43:54 +00:00
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("jsmith")
g.Assert(repo.Name).Equal("example")
g.Assert(repo.Avatar).Equal("http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg")
g.Assert(repo.Branch).Equal("develop")
g.Assert(build.Ref).Equal("refs/tags/v1.0.0")
2015-08-22 19:55:08 +00:00
2016-02-22 20:43:54 +00:00
})
2015-08-22 19:55:08 +00:00
2016-02-22 20:43:54 +00:00
g.It("Should parse legacy tag push hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.LegacyTagHook),
)
2015-08-22 19:55:08 +00:00
2016-02-22 20:43:54 +00:00
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
g.Assert(build.Ref).Equal("refs/tags/v1.0.0")
2015-08-22 19:06:56 +00:00
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(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.MergeRequestHook),
)
2015-08-22 19:06:56 +00:00
repo, build, err := gitlab.Hook(req)
2015-08-22 19:06:56 +00:00
g.Assert(err == nil).IsTrue()
g.Assert(repo.Avatar).Equal("http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg")
g.Assert(repo.Branch).Equal("develop")
g.Assert(repo.Owner).Equal("awesome_space")
g.Assert(repo.Name).Equal("awesome_project")
2015-08-22 19:06:56 +00:00
g.Assert(build.Title).Equal("MS-Viewport")
})
g.It("Should parse legacy merge request hook", func() {
req, _ := http.NewRequest(
"POST",
"http://example.com/api/hook?owner=diaspora&name=diaspora-client",
bytes.NewReader(testdata.LegacyMergeRequestHook),
)
repo, build, err := gitlab.Hook(req)
g.Assert(err == nil).IsTrue()
g.Assert(repo.Owner).Equal("diaspora")
g.Assert(repo.Name).Equal("diaspora-client")
2015-08-22 19:06:56 +00:00
g.Assert(build.Title).Equal("MS-Viewport")
})
2015-08-22 19:06:56 +00:00
})
})
})
}