Skip to content

Commit

Permalink
HOME-1781: extracted templates separately, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VSevostianov committed Sep 30, 2024
1 parent b6d5cbf commit 6a75066
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 110 deletions.
2 changes: 2 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Api struct {
Teams func() *TeamService
TeamUserGroups func() *TeamUserGroupService
TeamUsers func() *TeamUserService
PermissionTemplates func() *PermissionTemplateService
TranslationProviders func() *TranslationProviderService
Translations func() *TranslationService
TranslationStatuses func() *TranslationStatusService
Expand Down Expand Up @@ -57,6 +58,7 @@ func New(apiToken string, options ...ClientOption) (*Api, error) {
c.Projects = func() *ProjectService { return &ProjectService{BaseService: bs, opts: prjOpts} }
c.Branches = func() *BranchService { return &BranchService{bs} }
c.Teams = func() *TeamService { return &TeamService{bs} }
c.PermissionTemplates = func() *PermissionTemplateService { return &PermissionTemplateService{bs} }
c.TeamUsers = func() *TeamUserService { return &TeamUserService{bs} }
c.TeamUserGroups = func() *TeamUserGroupService { return &TeamUserGroupService{bs} }

Expand Down
41 changes: 41 additions & 0 deletions svc_permissiontemplates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lokalise

import "fmt"

const (
pathTemplates = "teams/%d/roles"
)

// The PermissionTemplate service
type PermissionTemplateService struct {
BaseService
}

type PermissionTemplate 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"`
TagInfo string `json:"tagInfo"`
DoesEnableAllReadOnlyLanguages bool `json:"doesEnableAllReadOnlyLanguages"`
}

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

// List all possible permission roles
func (c *PermissionTemplateService) 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(pathTemplates, teamID)
}
88 changes: 88 additions & 0 deletions svc_permissiontemplates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package lokalise

import (
"fmt"
"net/http"
"reflect"
"testing"
)

func TestTeamPermissionTemplates_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.PermissionTemplates().ListPermissionRoles(1)
if err != nil {
t.Errorf("Teams.List returned error: %v", err)
}

want := PermissionRoleResponse{
Roles: []PermissionTemplate{
{
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)
}
}
30 changes: 0 additions & 30 deletions svc_team.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package lokalise

import "fmt"

const (
pathTeams = "teams"
)
Expand Down Expand Up @@ -32,20 +30,6 @@ 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 @@ -69,17 +53,3 @@ 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: 0 additions & 80 deletions svc_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,83 +78,3 @@ 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)
}
}

0 comments on commit 6a75066

Please sign in to comment.