File: account_user_grants_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (101 lines) | stat: -rw-r--r-- 2,649 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package integration

import (
	"context"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/linode/linodego"
)

func TestUserGrants_Update(t *testing.T) {
	username := usernamePrefix + "updateusergrants"

	client, _, teardown := setupUser(t, []userModifier{
		func(createOpts *linodego.UserCreateOptions) {
			createOpts.Username = username
			createOpts.Email = usernamePrefix + "updateusergrants@example.com"
			createOpts.Restricted = true
		},
	}, "fixtures/TestUserGrants_Update")
	defer teardown()

	accessLevel := linodego.AccessLevelReadOnly

	globalGrants := linodego.GlobalUserGrants{
		AccountAccess:      &accessLevel,
		AddDomains:         false,
		AddDatabases:       true,
		AddFirewalls:       true,
		AddImages:          true,
		AddLinodes:         false,
		AddLongview:        true,
		AddNodeBalancers:   false,
		AddPlacementGroups: false,
		AddStackScripts:    true,
		AddVolumes:         true,
		AddVPCs:            true,
		CancelAccount:      false,
	}

	grants, err := client.UpdateUserGrants(context.TODO(), username, linodego.UserGrantsUpdateOptions{
		Global: globalGrants,
	})
	if err != nil {
		t.Fatalf("failed to get user grants: %s", err)
	}

	if !cmp.Equal(grants.Global, globalGrants) {
		t.Errorf("expected rules to match test rules, but got diff: %s", cmp.Diff(grants.Global, globalGrants))
	}
}

func TestUserGrants_UpdateNoAccess(t *testing.T) {
	username := usernamePrefix + "updateusergrantsna"

	client, _, teardown := setupUser(t, []userModifier{
		func(createOpts *linodego.UserCreateOptions) {
			createOpts.Username = username
			createOpts.Email = usernamePrefix + "updateusergrants@example.com"
			createOpts.Restricted = true
		},
	}, "fixtures/TestUserGrants_UpdateNoAccess")
	defer teardown()

	globalGrants := linodego.GlobalUserGrants{
		AccountAccess: nil,
	}

	grants, err := client.UpdateUserGrants(context.TODO(), username, linodego.UserGrantsUpdateOptions{
		Global: globalGrants,
	})
	if err != nil {
		t.Fatalf("failed to get user grants: %s", err)
	}

	if !cmp.Equal(grants.Global, globalGrants) {
		t.Errorf("expected rules to match test rules, but got diff: %s", cmp.Diff(grants.Global, globalGrants))
	}

	// Ensure all grants are no access
	grantFields := [][]linodego.GrantedEntity{
		grants.Domain,
		grants.Firewall,
		grants.Image,
		grants.Linode,
		grants.Longview,
		grants.NodeBalancer,
		grants.PlacementGroup,
		grants.StackScript,
		grants.Volume,
		grants.VPC,
	}

	for _, grantField := range grantFields {
		for _, grant := range grantField {
			if grant.Permissions != "" {
				t.Errorf("expected permissions to be nil, but got %s", grant.Permissions)
			}
		}
	}
}