From 79772a027966391bad146086f6391102538707ae Mon Sep 17 00:00:00 2001 From: "Victor H. Avelar" Date: Thu, 4 Mar 2021 20:37:02 +0100 Subject: [PATCH] Add permissions displayers --- commands/displayers/permissions.go | 43 +++++++++++++++++++ commands/displayers/permissions_test.go | 57 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 commands/displayers/permissions.go create mode 100644 commands/displayers/permissions_test.go diff --git a/commands/displayers/permissions.go b/commands/displayers/permissions.go new file mode 100644 index 0000000..e337e5c --- /dev/null +++ b/commands/displayers/permissions.go @@ -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, + } +} diff --git a/commands/displayers/permissions_test.go b/commands/displayers/permissions_test.go new file mode 100644 index 0000000..b63fe42 --- /dev/null +++ b/commands/displayers/permissions_test.go @@ -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()) + +}