You've already forked woodpecker
mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-06-12 21:47:35 +02:00
Pass down context.Context (#371)
* pass context down to remote clients * make tests work * add ctx to Refresh() and use it * bitbucketserver * code format * plugin interface: add todo context * solve todo * RM TODO by using context.WithTimeout * refactor & fix * Apply suggestions from code review Co-authored-by: Anbraten <anton@ju60.de> * go fmt * Update server/remote/coding/coding.go Co-authored-by: Anbraten <anton@ju60.de> Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
@ -16,6 +16,7 @@ package coding
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@ -33,6 +34,7 @@ func Test_coding(t *testing.T) {
|
||||
s := httptest.NewServer(fixtures.Handler())
|
||||
c := &Coding{URL: s.URL}
|
||||
|
||||
ctx := context.Background()
|
||||
g := goblin.Goblin(t)
|
||||
g.Describe("Coding", func() {
|
||||
|
||||
@ -67,13 +69,13 @@ func Test_coding(t *testing.T) {
|
||||
g.It("Should redirect to authorize", func() {
|
||||
w := httptest.NewRecorder()
|
||||
r, _ := http.NewRequest("GET", "", nil)
|
||||
_, err := c.Login(w, r)
|
||||
_, err := c.Login(ctx, w, r)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(w.Code).Equal(http.StatusSeeOther)
|
||||
})
|
||||
g.It("Should return authenticated user", func() {
|
||||
r, _ := http.NewRequest("GET", "?code=code", nil)
|
||||
u, err := c.Login(nil, r)
|
||||
u, err := c.Login(ctx, nil, r)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(u.Login).Equal(fakeUser.Login)
|
||||
g.Assert(u.Token).Equal(fakeUser.Token)
|
||||
@ -83,43 +85,33 @@ func Test_coding(t *testing.T) {
|
||||
|
||||
g.Describe("Given an access token", func() {
|
||||
g.It("Should return the anthenticated user", func() {
|
||||
login, err := c.Auth(
|
||||
fakeUser.Token,
|
||||
fakeUser.Secret,
|
||||
)
|
||||
login, err := c.Auth(ctx, fakeUser.Token, fakeUser.Secret)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(login).Equal(fakeUser.Login)
|
||||
})
|
||||
g.It("Should handle a failure to resolve user", func() {
|
||||
_, err := c.Auth(
|
||||
fakeUserNotFound.Token,
|
||||
fakeUserNotFound.Secret,
|
||||
)
|
||||
_, err := c.Auth(ctx, fakeUserNotFound.Token, fakeUserNotFound.Secret)
|
||||
g.Assert(err != nil).IsTrue()
|
||||
})
|
||||
})
|
||||
|
||||
g.Describe("Given a refresh token", func() {
|
||||
g.It("Should return a refresh access token", func() {
|
||||
ok, err := c.Refresh(fakeUserRefresh)
|
||||
ok, err := c.Refresh(ctx, fakeUserRefresh)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(ok).IsTrue()
|
||||
g.Assert(fakeUserRefresh.Token).Equal("VDZupx0usVRV4oOd1FCu4xUxgk8SY0TK")
|
||||
g.Assert(fakeUserRefresh.Secret).Equal("BenBQq7TWZ7Cp0aUM47nQjTz2QHNmTWcPctB609n")
|
||||
})
|
||||
g.It("Should handle an invalid refresh token", func() {
|
||||
ok, _ := c.Refresh(fakeUserRefreshInvalid)
|
||||
ok, _ := c.Refresh(ctx, fakeUserRefreshInvalid)
|
||||
g.Assert(ok).IsFalse()
|
||||
})
|
||||
})
|
||||
|
||||
g.Describe("When requesting a repository", func() {
|
||||
g.It("Should return the details", func() {
|
||||
repo, err := c.Repo(
|
||||
fakeUser,
|
||||
fakeRepo.Owner,
|
||||
fakeRepo.Name,
|
||||
)
|
||||
repo, err := c.Repo(ctx, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(repo.FullName).Equal(fakeRepo.FullName)
|
||||
g.Assert(repo.Avatar).Equal(s.URL + fakeRepo.Avatar)
|
||||
@ -130,57 +122,49 @@ func Test_coding(t *testing.T) {
|
||||
g.Assert(repo.IsPrivate).Equal(fakeRepo.IsPrivate)
|
||||
})
|
||||
g.It("Should handle not found errors", func() {
|
||||
_, err := c.Repo(
|
||||
fakeUser,
|
||||
fakeRepoNotFound.Owner,
|
||||
fakeRepoNotFound.Name,
|
||||
)
|
||||
_, err := c.Repo(ctx, fakeUser, fakeRepoNotFound.Owner, fakeRepoNotFound.Name)
|
||||
g.Assert(err != nil).IsTrue()
|
||||
})
|
||||
})
|
||||
|
||||
g.Describe("When requesting repository permissions", func() {
|
||||
g.It("Should authorize admin access for project owner", func() {
|
||||
perm, err := c.Perm(fakeUser, "demo1", "perm_owner")
|
||||
perm, err := c.Perm(ctx, fakeUser, "demo1", "perm_owner")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(perm.Pull).IsTrue()
|
||||
g.Assert(perm.Push).IsTrue()
|
||||
g.Assert(perm.Admin).IsTrue()
|
||||
})
|
||||
g.It("Should authorize admin access for project admin", func() {
|
||||
perm, err := c.Perm(fakeUser, "demo1", "perm_admin")
|
||||
perm, err := c.Perm(ctx, fakeUser, "demo1", "perm_admin")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(perm.Pull).IsTrue()
|
||||
g.Assert(perm.Push).IsTrue()
|
||||
g.Assert(perm.Admin).IsTrue()
|
||||
})
|
||||
g.It("Should authorize read access for project member", func() {
|
||||
perm, err := c.Perm(fakeUser, "demo1", "perm_member")
|
||||
perm, err := c.Perm(ctx, fakeUser, "demo1", "perm_member")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(perm.Pull).IsTrue()
|
||||
g.Assert(perm.Push).IsTrue()
|
||||
g.Assert(perm.Admin).IsFalse()
|
||||
})
|
||||
g.It("Should authorize no access for project guest", func() {
|
||||
perm, err := c.Perm(fakeUser, "demo1", "perm_guest")
|
||||
perm, err := c.Perm(ctx, fakeUser, "demo1", "perm_guest")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(perm.Pull).IsFalse()
|
||||
g.Assert(perm.Push).IsFalse()
|
||||
g.Assert(perm.Admin).IsFalse()
|
||||
})
|
||||
g.It("Should handle not found errors", func() {
|
||||
_, err := c.Perm(
|
||||
fakeUser,
|
||||
fakeRepoNotFound.Owner,
|
||||
fakeRepoNotFound.Name,
|
||||
)
|
||||
_, err := c.Perm(ctx, fakeUser, fakeRepoNotFound.Owner, fakeRepoNotFound.Name)
|
||||
g.Assert(err != nil).IsTrue()
|
||||
})
|
||||
})
|
||||
|
||||
g.Describe("When downloading a file", func() {
|
||||
g.It("Should return file for specified build", func() {
|
||||
data, err := c.File(fakeUser, fakeRepo, fakeBuild, ".drone.yml")
|
||||
data, err := c.File(ctx, fakeUser, fakeRepo, fakeBuild, ".drone.yml")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(string(data)).Equal("pipeline:\n test:\n image: golang:1.6\n commands:\n - go test\n")
|
||||
})
|
||||
@ -213,22 +197,22 @@ func Test_coding(t *testing.T) {
|
||||
|
||||
g.Describe("When activating a repository", func() {
|
||||
g.It("Should create the hook", func() {
|
||||
err := c.Activate(fakeUser, fakeRepo, "http://127.0.0.1")
|
||||
err := c.Activate(ctx, fakeUser, fakeRepo, "http://127.0.0.1")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
})
|
||||
g.It("Should update the hook when exists", func() {
|
||||
err := c.Activate(fakeUser, fakeRepo, "http://127.0.0.2")
|
||||
err := c.Activate(ctx, fakeUser, fakeRepo, "http://127.0.0.2")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
})
|
||||
})
|
||||
|
||||
g.Describe("When deactivating a repository", func() {
|
||||
g.It("Should successfully remove hook", func() {
|
||||
err := c.Deactivate(fakeUser, fakeRepo, "http://127.0.0.3")
|
||||
err := c.Deactivate(ctx, fakeUser, fakeRepo, "http://127.0.0.3")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
})
|
||||
g.It("Should successfully deactivate when hook already removed", func() {
|
||||
err := c.Deactivate(fakeUser, fakeRepo, "http://127.0.0.4")
|
||||
err := c.Deactivate(ctx, fakeUser, fakeRepo, "http://127.0.0.4")
|
||||
g.Assert(err == nil).IsTrue()
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user