Skip to content

Commit

Permalink
HOME-1781: added new permission related API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
VSevostianov committed Sep 27, 2024
1 parent 17997bb commit b6d5cbf
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 7 deletions.
98 changes: 97 additions & 1 deletion svc_contributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,101 @@ func TestContributorService_Create(t *testing.T) {
}
}

func TestContributorService_CreateWithRoleId(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc(
fmt.Sprintf("/projects/%s/contributors", testProjectID),
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
testMethod(t, r, "POST")
testHeader(t, r, apiTokenHeader, testApiToken)
data := `{
"contributors": [
{
"email": "[email protected]",
"fullname": "Mr. Translator",
"is_admin": false,
"is_reviewer": true,
"languages": [
{
"lang_iso": "en",
"is_writable": false
}
],
"role_id": 2
}
]
}`

req := new(bytes.Buffer)
_ = json.Compact(req, []byte(data))

testBody(t, r, req.String())

_, _ = fmt.Fprint(w, `{
"project_id": "`+testProjectID+`",
"contributors": [{
"user_id": 421,
"email": "[email protected]",
"fullname": "Mr. Translator",
"is_admin": false,
"is_reviewer": true,
"languages": [
{
"lang_iso": "en",
"is_writable": false
}
],
"role_id": 2
}]
}`)
})

r, err := client.Contributors().Create(testProjectID, []NewContributor{
{
Email: "[email protected]",
Fullname: "Mr. Translator",
Permission: Permission{
IsAdmin: false,
IsReviewer: true,
Languages: []Language{{
LangISO: "en",
IsWritable: false,
}},
AdminRights: nil,
RoleId: 2,
},
},
})
if err != nil {
t.Errorf("Contributors.Create returned error: %v", err)
}

want := []Contributor{
{
WithUserID: WithUserID{UserID: 421},
Email: "[email protected]",
Fullname: "Mr. Translator",
Permission: Permission{
IsAdmin: false,
IsReviewer: true,
Languages: []Language{{
LangISO: "en",
IsWritable: false,
}},
AdminRights: nil,
RoleId: 2,
},
},
}

if !reflect.DeepEqual(r.Contributors, want) {
t.Errorf("Contributors.Create returned %+v, want %+v", r.Contributors, want)
}
}

func TestContributorService_Delete(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down Expand Up @@ -217,7 +312,8 @@ func TestContributorService_Update(t *testing.T) {
testHeader(t, r, apiTokenHeader, testApiToken)
data := `{
"is_admin": true,
"is_reviewer":false
"is_reviewer":false,
"role_id":null
}`

req := new(bytes.Buffer)
Expand Down
30 changes: 30 additions & 0 deletions svc_team.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lokalise

import "fmt"

const (
pathTeams = "teams"
)
Expand Down Expand Up @@ -30,6 +32,20 @@ type Quota struct {
MAU int64 `json:"mau"`
}

type PermissionRole struct {
ID int `json:"id"`
Role string `json:"role"`
Permissions []string `json:"permissions"`
Description string `json:"description"`
Tag string `json:"tag"`
TagColor string `json:"tagColor"`
DoesEnableAllReadOnlyLanguages bool `json:"doesEnableAllReadOnlyLanguages"`
}

type PermissionRoleResponse struct {
Roles []PermissionRole `json:"roles"`
}

// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Service request/response objects
// _____________________________________________________________________________________________________________________
Expand All @@ -53,3 +69,17 @@ func (c *TeamService) List() (r TeamsResponse, err error) {
applyPaged(resp, &r.Paged)
return r, apiError(resp)
}

// List all possible permission roles
func (c *TeamService) ListPermissionRoles(teamID int64) (r PermissionRoleResponse, err error) {
resp, err := c.getWithOptions(c.Ctx(), pathPermissionRoles(teamID), &r, c.PageOpts())

if err != nil {
return r, err
}
return r, apiError(resp)
}

func pathPermissionRoles(teamID int64) string {
return fmt.Sprintf("teams/%d/roles", teamID)
}
80 changes: 80 additions & 0 deletions svc_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,83 @@ func TestTeamService_List(t *testing.T) {
t.Errorf("Screenshots.List returned %+v, want %+v", r.Teams, want)
}
}

func TestTeamPermissionRoles_List(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc(
"/teams/1/roles",
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
testMethod(t, r, "GET")
testHeader(t, r, apiTokenHeader, testApiToken)

_, _ = fmt.Fprint(w, `{
"roles": [
{
"id": 1,
"role": "Localisation management",
"permissions": [
"activity",
"branches_main_modify"
],
"description": "Manage project settings, contributors and tasks",
"tag": "Full access",
"tagColor": "green",
"doesEnableAllReadOnlyLanguages": true
},
{
"id": 2,
"role": "Developer",
"permissions": [
"download",
"upload"
],
"description": "Create keys, upload and download content",
"tag": "Advanced",
"tagColor": "cyan",
"doesEnableAllReadOnlyLanguages": true
}
]
}`)
})

r, err := client.Teams().ListPermissionRoles(1)
if err != nil {
t.Errorf("Teams.List returned error: %v", err)
}

want := PermissionRoleResponse{
Roles: []PermissionRole{
{
ID: 1,
Role: "Localisation management",
Permissions: []string{
"activity",
"branches_main_modify",
},
Description: "Manage project settings, contributors and tasks",
Tag: "Full access",
TagColor: "green",
DoesEnableAllReadOnlyLanguages: true,
},
{
ID: 2,
Role: "Developer",
Permissions: []string{
"download",
"upload",
},
Description: "Create keys, upload and download content",
Tag: "Advanced",
TagColor: "cyan",
DoesEnableAllReadOnlyLanguages: true,
},
},
}

if !reflect.DeepEqual(r, want) {
t.Errorf("Team.PermissionRoles.List returned %+v, want %+v", r, want)
}
}
18 changes: 12 additions & 6 deletions svc_teamusergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ type TeamUserGroup struct {
}

type Permission struct {
IsAdmin bool `json:"is_admin"`
// IsAdmin is deprecated, and will be removed in next release. Use AdminRights for more granular control.
IsAdmin bool `json:"is_admin"`
// IsReviewer is deprecated, and will be removed in next release. Use the appropriate permissions in AdminRights instead.
IsReviewer bool `json:"is_reviewer"`
Languages []Language `json:"languages,omitempty"`

// Possible values are upload, activity, download, settings, statistics, keys, screenshots, contributors, languages
// Possible values are activity, branches_main_modify, branches_create, branches_merge, statistics, tasks, contributors, settings, manage_languages, download, upload, glossary_delete, glossary_edit, manage_keys, screenshots, custom_status_modify, review
AdminRights []string `json:"admin_rights,omitempty"` // todo make admin rights as constants available in the lib
RoleId int64 `json:"role_id,omitempty"`
}

// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
Expand All @@ -43,9 +45,13 @@ type NewGroupLanguages struct {
}

type NewGroup struct {
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
IsReviewer bool `json:"is_reviewer"`
Name string `json:"name"`
// IsAdmin is deprecated, and will be removed in next release. Use AdminRights for more granular control.
IsAdmin bool `json:"is_admin"`
// IsReviewer is deprecated, and will be removed in next release. Use the appropriate permissions in AdminRights instead.
IsReviewer bool `json:"is_reviewer"`

// Possible values are activity, branches_main_modify, branches_create, branches_merge, statistics, tasks, contributors, settings, manage_languages, download, upload, glossary_delete, glossary_edit, manage_keys, screenshots, custom_status_modify, review
AdminRights []string `json:"admin_rights,omitempty"`
Languages NewGroupLanguages `json:"languages,omitempty"`
}
Expand Down

0 comments on commit b6d5cbf

Please sign in to comment.