Add reponse helper function for group permissions

Reponse to extract groups and their permissions was added.

Additional definitons for the avilable Bitbucket Server permission
levels were added to allow for easier usage.

Tests were added and udpated.
This commit is contained in:
Matthias Preu 2020-03-06 13:38:16 +01:00
parent e7592ef714
commit e132e33e07
2 changed files with 141 additions and 1 deletions

View File

@ -105,12 +105,61 @@ type UserWithMetadata struct {
Status string `json:"status"`
}
// PermissionGlobal are global permissions
type PermissionGlobal string
const (
// PermissionGlobalLicensedUser represents the ability to log into the system
PermissionGlobalLicensedUser PermissionGlobal = "LICENSED_USER"
// PermissionGlobalProjectCreate allows project creation
PermissionGlobalProjectCreate PermissionGlobal = "PROJECT_CREATE"
// PermissionGlobalAdmin represents an administrator
PermissionGlobalAdmin PermissionGlobal = "ADMIN"
// PermissionGlobalSysAdmin represents a system administrator
PermissionGlobalSysAdmin PermissionGlobal = "SYS_ADMIN"
)
// PermissionProject are project level permissions
type PermissionProject string
const (
// PermissionProjectAdmin grants admin priviledges
PermissionProjectAdmin PermissionProject = "PROJECT_ADMIN"
// PermissionProjectRead grants read priviledges
PermissionProjectRead PermissionProject = "PROJECT_READ"
// PermissionProjectWrite grants write priviledges
PermissionProjectWrite PermissionProject = "PROJECT_WRITE"
)
// PermissionRepository are repository level permissions
type PermissionRepository string
const (
// PermissionRepositoryAdmin grants admin priviledges
PermissionRepositoryAdmin PermissionRepository = "REPO_ADMIN"
// PermissionRepositoryRead grants read priviledges
PermissionRepositoryRead PermissionRepository = "REPO_READ"
// PermissionRepositoryWrite grants write priviledges
PermissionRepositoryWrite PermissionRepository = "REPO_WRITE"
)
// UserPermission contains a user with its permission
type UserPermission struct {
User User `json:"user"`
Permission string `json:"permission"`
}
// Group represents a user group
type Group struct {
Name string `json:"name"`
}
// GroupPermission contains a group with its permission
type GroupPermission struct {
Group Group `json:"group"`
Permission string `json:"permission"`
}
type MergeResult struct {
Outcome string `json:"outcome"`
Current bool `json:"current"`
@ -323,6 +372,21 @@ type Change struct {
} `json:"new"`
}
// String converts global permission to its string representation
func (p PermissionGlobal) String() string {
return string(p)
}
// String converts project permission to its string representation
func (p PermissionProject) String() string {
return string(p)
}
// String converts repository permission to its string representation
func (p PermissionRepository) String() string {
return string(p)
}
func (k *SSHKey) String() string {
parts := make([]string, 1, 2)
parts[0] = strings.TrimSpace(k.Text)
@ -399,6 +463,13 @@ func GetUsersPermissionResponse(r *APIResponse) ([]UserPermission, error) {
return c, err
}
// GetGroupsPermissionResponse casts group permissions into structure
func GetGroupsPermissionResponse(r *APIResponse) ([]GroupPermission, error) {
var c []GroupPermission
err := mapstructure.Decode(r.Values["values"], &c)
return c, err
}
// GetWebhooksResponse cast Webhooks into structure
func GetWebhooksResponse(r *APIResponse) ([]Webhook, error) {
var h []Webhook

View File

@ -360,7 +360,7 @@ func TestGetUsersPermissionResponse(t *testing.T) {
Slug: "jcitizen",
Type: "NORMAL",
},
Permission: "REPO_ADMIN",
Permission: PermissionRepositoryAdmin.String(),
},
},
wantErr: false,
@ -380,6 +380,75 @@ func TestGetUsersPermissionResponse(t *testing.T) {
}
}
func TestGetGroupsPermissionResponse(t *testing.T) {
type args struct {
r *APIResponse
}
tests := []struct {
name string
args args
want []GroupPermission
wantErr bool
}{
{
name: "Empty list",
args: args{
r: &APIResponse{
Values: map[string]interface{}{"values": []interface{}{}},
},
},
want: []GroupPermission{},
wantErr: false,
},
{
name: "Bad response",
args: args{
r: &APIResponse{
Values: map[string]interface{}{"values": "not an array"},
},
},
want: nil,
wantErr: true,
},
{
name: "Single group permission",
args: args{
r: &APIResponse{
Values: map[string]interface{}{
"values": []interface{}{map[string]interface{}{
"group": map[string]interface{}{
"name": "group1",
},
"permission": "REPO_ADMIN",
}},
},
},
},
want: []GroupPermission{
GroupPermission{
Group: Group{
Name: "group1",
},
Permission: PermissionRepositoryAdmin.String(),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetGroupsPermissionResponse(tt.args.r)
if err != nil && !tt.wantErr {
t.Errorf("GetGroupsPermissionResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetGroupsPermissionResponse() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewAPIResponse(t *testing.T) {
type args struct {
r *http.Response