Skip to content

Commit

Permalink
Add permissions displayers
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorAvelar committed Mar 4, 2021
1 parent f5cd2a1 commit 79772a0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
43 changes: 43 additions & 0 deletions commands/displayers/permissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package displayers

import "github.com/VictorAvelar/mollie-api-go/v2/mollie"

// MolliePermissionList is wrapper for displaying.
type MolliePermissionList struct {
*mollie.PermissionsList
}

// KV is a displayable group of key value
func (mp *MolliePermissionList) KV() []map[string]interface{} {
var out []map[string]interface{}

for _, p := range mp.Embedded.Permissions {
x := buildXPermission(p)

out = append(out, x)
}

return out
}

// MolliePermission is wrapper for displaying.
type MolliePermission struct {
*mollie.Permission
}

// KV is a displayable group of key value
func (p *MolliePermission) KV() []map[string]interface{} {
var out []map[string]interface{}
x := buildXPermission(p.Permission)
out = append(out, x)
return out
}

func buildXPermission(mp *mollie.Permission) map[string]interface{} {
return map[string]interface{}{
"RESOURCE": mp.Resource,
"ID": mp.ID,
"DESCRIPTION": mp.Description,
"GRANTED": mp.Granted,
}
}
57 changes: 57 additions & 0 deletions commands/displayers/permissions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package displayers

import (
"testing"

"github.com/VictorAvelar/mollie-api-go/v2/mollie"
"github.com/stretchr/testify/assert"
)

func TestMolliePermission_KV(t *testing.T) {
perm := mollie.Permission{
Description: "random desc",
Granted: true,
ID: "random.test",
Resource: "test_resource",
}

disp := MolliePermission{
Permission: &perm,
}

out := []map[string]interface{}{}
out = append(out, buildXPermission(&perm))

assert.Len(t, disp.KV(), 1)
assert.Equal(t, out, disp.KV())
}

func TestMolliePermissionList_KV(t *testing.T) {
perm := mollie.Permission{
Description: "random desc",
Granted: true,
ID: "random.test",
Resource: "test_resource",
}
permList := MolliePermissionList{
PermissionsList: &mollie.PermissionsList{
Count: 2,
Embedded: struct {
Permissions []*mollie.Permission "json:\"permissions,omitempty\""
}{
[]*mollie.Permission{&perm, &perm},
},
Links: mollie.PermissionLinks{
Documentation: &mollie.URL{Href: "https://example.com", Type: "text/html"},
Self: &mollie.URL{Href: "https://example.com", Type: "text/html"},
},
},
}

out := []map[string]interface{}{}
out = append(out, buildXPermission(&perm), buildXPermission(&perm))

assert.Len(t, permList.KV(), 2)
assert.Equal(t, out, permList.KV())

}

0 comments on commit 79772a0

Please sign in to comment.