mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-04 18:21:06 +02:00
Add tests:
- read access - write access - admin access
This commit is contained in:
parent
764c36f736
commit
0b73e5489b
@ -182,17 +182,21 @@ func (c *config) Perm(u *model.User, owner, name string) (*model.Perm, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
perm, err := client.GetPermission(repo.FullName)
|
perm, err := client.GetPermission(repo.FullName)
|
||||||
|
if err != nil {
|
||||||
if err == nil {
|
return perms, err
|
||||||
switch perm.Permission {
|
|
||||||
case "admin":
|
|
||||||
perms.Push = true
|
|
||||||
perms.Admin = true
|
|
||||||
case "write":
|
|
||||||
perms.Push = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
perms.Pull = true
|
|
||||||
|
switch perm.Permission {
|
||||||
|
case "admin":
|
||||||
|
perms.Admin = true
|
||||||
|
fallthrough
|
||||||
|
case "write":
|
||||||
|
perms.Push = true
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
perms.Pull = true
|
||||||
|
}
|
||||||
|
|
||||||
return perms, nil
|
return perms, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,19 +161,30 @@ func Test_bitbucket(t *testing.T) {
|
|||||||
g.It("Should authorize read access", func() {
|
g.It("Should authorize read access", func() {
|
||||||
perm, err := c.Perm(
|
perm, err := c.Perm(
|
||||||
fakeUser,
|
fakeUser,
|
||||||
fakeRepoNoHooks.Owner,
|
fakeRepoReadOnly.Owner,
|
||||||
fakeRepoNoHooks.Name,
|
fakeRepoReadOnly.Name,
|
||||||
)
|
)
|
||||||
g.Assert(err == nil).IsTrue()
|
g.Assert(err == nil).IsTrue()
|
||||||
g.Assert(perm.Pull).IsTrue()
|
g.Assert(perm.Pull).IsTrue()
|
||||||
g.Assert(perm.Push).IsFalse()
|
g.Assert(perm.Push).IsFalse()
|
||||||
g.Assert(perm.Admin).IsFalse()
|
g.Assert(perm.Admin).IsFalse()
|
||||||
})
|
})
|
||||||
|
g.It("Should authorize write access", func() {
|
||||||
|
perm, err := c.Perm(
|
||||||
|
fakeUser,
|
||||||
|
fakeRepoWriteOnly.Owner,
|
||||||
|
fakeRepoWriteOnly.Name,
|
||||||
|
)
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(perm.Pull).IsTrue()
|
||||||
|
g.Assert(perm.Push).IsTrue()
|
||||||
|
g.Assert(perm.Admin).IsFalse()
|
||||||
|
})
|
||||||
g.It("Should authorize admin access", func() {
|
g.It("Should authorize admin access", func() {
|
||||||
perm, err := c.Perm(
|
perm, err := c.Perm(
|
||||||
fakeUser,
|
fakeUser,
|
||||||
fakeRepo.Owner,
|
fakeRepoAdmin.Owner,
|
||||||
fakeRepo.Name,
|
fakeRepoAdmin.Name,
|
||||||
)
|
)
|
||||||
g.Assert(err == nil).IsTrue()
|
g.Assert(err == nil).IsTrue()
|
||||||
g.Assert(perm.Pull).IsTrue()
|
g.Assert(perm.Pull).IsTrue()
|
||||||
@ -350,6 +361,24 @@ var (
|
|||||||
FullName: "test_name/hook_empty",
|
FullName: "test_name/hook_empty",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fakeRepoReadOnly = &model.Repo{
|
||||||
|
Owner: "test_name",
|
||||||
|
Name: "permission_read",
|
||||||
|
FullName: "test_name/permission_read",
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeRepoWriteOnly = &model.Repo{
|
||||||
|
Owner: "test_name",
|
||||||
|
Name: "permission_write",
|
||||||
|
FullName: "test_name/permission_write",
|
||||||
|
}
|
||||||
|
|
||||||
|
fakeRepoAdmin = &model.Repo{
|
||||||
|
Owner: "test_name",
|
||||||
|
Name: "permission_admin",
|
||||||
|
FullName: "test_name/permission_admin",
|
||||||
|
}
|
||||||
|
|
||||||
fakeBuild = &model.Build{
|
fakeBuild = &model.Build{
|
||||||
Commit: "9ecad50",
|
Commit: "9ecad50",
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
package fixtures
|
package fixtures
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -36,6 +37,7 @@ func Handler() http.Handler {
|
|||||||
e.GET("/2.0/repositories/:owner", getUserRepos)
|
e.GET("/2.0/repositories/:owner", getUserRepos)
|
||||||
e.GET("/2.0/teams/", getUserTeams)
|
e.GET("/2.0/teams/", getUserTeams)
|
||||||
e.GET("/2.0/user/", getUser)
|
e.GET("/2.0/user/", getUser)
|
||||||
|
e.GET("/2.0/user/permissions/repositories", getPermissions)
|
||||||
|
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
@ -70,6 +72,8 @@ func getRepo(c *gin.Context) {
|
|||||||
switch c.Param("name") {
|
switch c.Param("name") {
|
||||||
case "not_found", "repo_unknown", "repo_not_found":
|
case "not_found", "repo_unknown", "repo_not_found":
|
||||||
c.String(404, "")
|
c.String(404, "")
|
||||||
|
case "permission_read", "permission_write", "permission_admin":
|
||||||
|
c.String(200, fmt.Sprintf(permissionRepoPayload, c.Param("name")))
|
||||||
default:
|
default:
|
||||||
c.String(200, repoPayload)
|
c.String(200, repoPayload)
|
||||||
}
|
}
|
||||||
@ -144,6 +148,24 @@ func getUserRepos(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func permission(p string) string {
|
||||||
|
return fmt.Sprintf(permissionPayload, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPermissions(c *gin.Context) {
|
||||||
|
query := c.Request.URL.Query()["q"][0]
|
||||||
|
switch query {
|
||||||
|
case `repository.full_name="test_name/permission_read"`:
|
||||||
|
c.String(200, permission("read"))
|
||||||
|
case `repository.full_name="test_name/permission_write"`:
|
||||||
|
c.String(200, permission("write"))
|
||||||
|
case `repository.full_name="test_name/permission_admin"`:
|
||||||
|
c.String(200, permission("admin"))
|
||||||
|
default:
|
||||||
|
c.String(200, permission("read"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const tokenPayload = `
|
const tokenPayload = `
|
||||||
{
|
{
|
||||||
"access_token":"2YotnFZFEjr1zCsicMWpAA",
|
"access_token":"2YotnFZFEjr1zCsicMWpAA",
|
||||||
@ -170,6 +192,14 @@ const repoPayload = `
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const permissionRepoPayload = `
|
||||||
|
{
|
||||||
|
"full_name": "test_name/%s",
|
||||||
|
"scm": "git",
|
||||||
|
"is_private": true
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
const repoHookPayload = `
|
const repoHookPayload = `
|
||||||
{
|
{
|
||||||
"pagelen": 10,
|
"pagelen": 10,
|
||||||
@ -238,3 +268,15 @@ const userTeamPayload = `
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const permissionPayload = `
|
||||||
|
{
|
||||||
|
"pagelen": 1,
|
||||||
|
"values": [
|
||||||
|
{
|
||||||
|
"permission": "%s"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"page": 1
|
||||||
|
}
|
||||||
|
`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user