From 0b73e5489b6312534eb4e0830bc116fc1d085ea8 Mon Sep 17 00:00:00 2001 From: folex <0xdxdy@gmail.com> Date: Fri, 27 Apr 2018 20:48:54 +0300 Subject: [PATCH] Add tests: - read access - write access - admin access --- remote/bitbucket/bitbucket.go | 24 +++++++++------- remote/bitbucket/bitbucket_test.go | 37 +++++++++++++++++++++--- remote/bitbucket/fixtures/handler.go | 42 ++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 14 deletions(-) diff --git a/remote/bitbucket/bitbucket.go b/remote/bitbucket/bitbucket.go index 1dcaa068..35ec84f2 100644 --- a/remote/bitbucket/bitbucket.go +++ b/remote/bitbucket/bitbucket.go @@ -182,17 +182,21 @@ func (c *config) Perm(u *model.User, owner, name string) (*model.Perm, error) { } perm, err := client.GetPermission(repo.FullName) - - if err == nil { - switch perm.Permission { - case "admin": - perms.Push = true - perms.Admin = true - case "write": - perms.Push = true - } + if err != nil { + return perms, err } - 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 } diff --git a/remote/bitbucket/bitbucket_test.go b/remote/bitbucket/bitbucket_test.go index c22d0e79..91172321 100644 --- a/remote/bitbucket/bitbucket_test.go +++ b/remote/bitbucket/bitbucket_test.go @@ -161,19 +161,30 @@ func Test_bitbucket(t *testing.T) { g.It("Should authorize read access", func() { perm, err := c.Perm( fakeUser, - fakeRepoNoHooks.Owner, - fakeRepoNoHooks.Name, + fakeRepoReadOnly.Owner, + fakeRepoReadOnly.Name, ) g.Assert(err == nil).IsTrue() g.Assert(perm.Pull).IsTrue() g.Assert(perm.Push).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() { perm, err := c.Perm( fakeUser, - fakeRepo.Owner, - fakeRepo.Name, + fakeRepoAdmin.Owner, + fakeRepoAdmin.Name, ) g.Assert(err == nil).IsTrue() g.Assert(perm.Pull).IsTrue() @@ -350,6 +361,24 @@ var ( 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{ Commit: "9ecad50", } diff --git a/remote/bitbucket/fixtures/handler.go b/remote/bitbucket/fixtures/handler.go index ec855a60..f0565995 100644 --- a/remote/bitbucket/fixtures/handler.go +++ b/remote/bitbucket/fixtures/handler.go @@ -15,6 +15,7 @@ package fixtures import ( + "fmt" "net/http" "github.com/gin-gonic/gin" @@ -36,6 +37,7 @@ func Handler() http.Handler { e.GET("/2.0/repositories/:owner", getUserRepos) e.GET("/2.0/teams/", getUserTeams) e.GET("/2.0/user/", getUser) + e.GET("/2.0/user/permissions/repositories", getPermissions) return e } @@ -70,6 +72,8 @@ func getRepo(c *gin.Context) { switch c.Param("name") { case "not_found", "repo_unknown", "repo_not_found": c.String(404, "") + case "permission_read", "permission_write", "permission_admin": + c.String(200, fmt.Sprintf(permissionRepoPayload, c.Param("name"))) default: 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 = ` { "access_token":"2YotnFZFEjr1zCsicMWpAA", @@ -170,6 +192,14 @@ const repoPayload = ` } ` +const permissionRepoPayload = ` +{ + "full_name": "test_name/%s", + "scm": "git", + "is_private": true +} +` + const repoHookPayload = ` { "pagelen": 10, @@ -238,3 +268,15 @@ const userTeamPayload = ` ] } ` + +const permissionPayload = ` +{ + "pagelen": 1, + "values": [ + { + "permission": "%s" + } + ], + "page": 1 +} +`