From adf825f5a8b1f347620dd98727083900aa1a39f0 Mon Sep 17 00:00:00 2001 From: chyroc Date: Thu, 31 Oct 2019 13:24:41 +0800 Subject: [PATCH] fix --- gerrit_test.go | 11 +++++ projects_access.go | 6 +-- projects_test.go | 103 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 3 deletions(-) diff --git a/gerrit_test.go b/gerrit_test.go index 30a00d7..8dc4ba4 100644 --- a/gerrit_test.go +++ b/gerrit_test.go @@ -101,6 +101,17 @@ func testFormValues(t *testing.T, r *http.Request, values testValues) { } } +func testQueryValues(t *testing.T, r *http.Request, values testValues) { + want := url.Values{} + for k, v := range values { + want.Add(k, v) + } + + if got := r.URL.Query(); !reflect.DeepEqual(got, want) { + t.Errorf("Request parameters: %v, want %v", got, want) + } +} + func TestNewClient_NoGerritInstance(t *testing.T) { mockData := []string{"", "://not-existing"} for _, data := range mockData { diff --git a/projects_access.go b/projects_access.go index 965b2be..9be2f06 100644 --- a/projects_access.go +++ b/projects_access.go @@ -36,13 +36,13 @@ type AccessCheckInfo struct { // CheckAccessOptions is options for check access type CheckAccessOptions struct { // The account for which to check access. Mandatory. - Account string `json:"account"` + Account string `url:"account,omitempty"` // The ref permission for which to check access. If not specified, read access to at least branch is checked. - Permission string `json:"perm"` + Permission string `url:"perm,omitempty"` // The branch for which to check access. This must be given if perm is specified. - Ref string `json:"ref"` + Ref string `url:"ref,omitempty"` } // ListAccessRights lists the access rights for a single project diff --git a/projects_test.go b/projects_test.go index 6077357..f943a4a 100644 --- a/projects_test.go +++ b/projects_test.go @@ -264,3 +264,106 @@ func TestProjectsService_GetBranch(t *testing.T) { } } } + +func TestProjectsService_ListAccessRights(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/projects/MyProject/access", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + + // from: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-access + resp := `{"can_add":true,"can_add_tags":true,"can_upload":true,"config_visible":true,"groups":{"c2ce4749a32ceb82cd6adcce65b8216e12afb41c":{"created_on":"2009-06-08 23:31:00.000000000","description":"Users who perform batch actions on Gerrit","group_id":2,"name":"Non-Interactive Users","options":{},"owner":"Administrators","owner_id":"d5b7124af4de52924ed397913e2c3b37bf186948","url":"#/admin/groups/uuid-c2ce4749a32ceb82cd6adcce65b8216e12afb41c"},"global:Anonymous-Users":{"name":"Anonymous Users","options":{}}},"inherits_from":{"description":"Access inherited by all other projects.","id":"All-Projects","name":"All-Projects"},"is_owner":true,"local":{"refs/*":{"permissions":{"read":{"rules":{"c2ce4749a32ceb82cd6adcce65b8216e12afb41c":{"action":"ALLOW","force":false},"global:Anonymous-Users":{"action":"ALLOW","force":false}}}}}},"owner_of":["refs/*"],"revision":"61157ed63e14d261b6dca40650472a9b0bd88474"}` + _, _ = fmt.Fprint(w, `)]}'`+"\n"+resp) + }) + + projectAccessRight, _, err := testClient.Projects.ListAccessRights("MyProject") + if err != nil { + t.Errorf("project: list access rights error: %s", err) + } + + fmt.Printf("project access rights: %v\n", projectAccessRight) +} + +func TestProjectsService_AddUpdateDeleteAccessRights(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/projects/MyProject/access", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + + // from: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#set-access + resp := `{"revision":"61157ed63e14d261b6dca40650472a9b0bd88474","inherits_from":{"id":"All-Projects","name":"All-Projects","description":"Accessinheritedbyallotherprojects."},"local":{"refs/*":{"permissions":{"read":{"rules":{"global:Anonymous-Users":{"action":"ALLOW","force":false}}}}}},"is_owner":true,"owner_of":["refs/*"],"can_upload":true,"can_add":true,"can_add_tags":true,"config_visible":true,"groups":{"global:Anonymous-Users":{"options":{},"name":"AnonymousUsers"}}}` + _, _ = fmt.Fprint(w, `)]}'`+"\n"+resp) + }) + + var data = `{"remove":{"refs/*":{"permissions":{"read":{"rules":{"c2ce4749a32ceb82cd6adcce65b8216e12afb41c":{"action":"ALLOW"}}}}}}}` + var req = new(gerrit.ProjectAccessInput) + if err := json.Unmarshal([]byte(data), &req); err != nil { + t.Errorf("project: add/update/delete access right request params error: %s", err) + } + + projectAccessRight, _, err := testClient.Projects.AddUpdateDeleteAccessRights("MyProject", req) + if err != nil { + t.Errorf("project: add/update/delete access right error: %s", err) + } + + fmt.Printf("project access rights: %v\n", projectAccessRight) +} + +func TestProjectsService_AccessCheck(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/projects/MyProject/check.access", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + + testQueryValues(t, r, testValues{ + "account": "1000098", + "ref": "refs/heads/secret/bla", + }) + + // from: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#set-access + resp := `{"message": "user Kristen Burns \u003cKristen.Burns@gerritcodereview.com\u003e (1000098) cannot see ref refs/heads/secret/bla in project MyProject","status":403}` + _, _ = fmt.Fprint(w, `)]}'`+"\n"+resp) + }) + + var data = `{"account":"1000098","ref":"refs/heads/secret/bla"}` + var req = new(gerrit.CheckAccessOptions) + if err := json.Unmarshal([]byte(data), &req); err != nil { + t.Errorf("project: access check request params error: %s", err) + } + + accessCheckInfo, _, err := testClient.Projects.CheckAccess("MyProject", req) + if err != nil { + t.Errorf("project: access check error: %s", err) + } + + fmt.Printf("project access check info: %v\n", accessCheckInfo) +} + +func TestProjectsService_CreateAccessChange(t *testing.T) { + setup() + defer teardown() + + testMux.HandleFunc("/projects/MyProject/access:review", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + + // from: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#create-access-change + resp := `{"id":"testproj~refs%2Fmeta%2Fconfig~Ieaf185bf90a1fc3b58461e399385e158a20b31a2","project":"testproj","branch":"refs/meta/config","hashtags":[],"change_id":"Ieaf185bf90a1fc3b58461e399385e158a20b31a2","subject":"Reviewaccesschange","status":"NEW","created":"2017-09-07 14:31:11.852000000","updated":"2017-09-07 14:31:11.852000000","submit_type":"CHERRY_PICK","mergeable":true,"insertions":2,"deletions":0,"unresolved_comment_count":0,"has_review_started":true,"_number":7,"owner":{"_account_id":1000000}}` + _, _ = fmt.Fprint(w, `)]}'`+"\n"+resp) + }) + + var data = `{"add":{"refs/heads/*":{"permissions":{"read":{"rules":{"global:Anonymous-Users":{"action":"DENY","force":false}}}}}}}` + var req = new(gerrit.ProjectAccessInput) + if err := json.Unmarshal([]byte(data), &req); err != nil { + t.Errorf("project: create access change request params error: %s", err) + } + + changeInfo, _, err := testClient.Projects.CreateAccessRightChange("MyProject", req) + if err != nil { + t.Errorf("project: create access change error: %s", err) + } + + fmt.Printf("project create access change info: %v\n", changeInfo) +}