github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/access_control_test.go (about)

     1  //go:build functional || vapp || catalog || ALL
     2  
     3  /*
     4   * Copyright 2020 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  
    13  	"github.com/kr/pretty"
    14  	. "gopkg.in/check.v1"
    15  
    16  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    17  )
    18  
    19  // accessControlType is an interface used to test access control for all entities that support it
    20  type accessControlType interface {
    21  	GetAccessControl(useTenantContext bool) (*types.ControlAccessParams, error)
    22  	SetAccessControl(params *types.ControlAccessParams, useTenantContext bool) error
    23  	RemoveAccessControl(useTenantContext bool) error
    24  	IsShared(useTenantContext bool) (bool, error)
    25  	GetId() string
    26  }
    27  
    28  type accessSettingMap map[string]*types.AccessSetting
    29  
    30  func accessSettingsToMap(params types.ControlAccessParams) accessSettingMap {
    31  	if params.AccessSettings == nil {
    32  		return nil
    33  	}
    34  	var result = make(accessSettingMap)
    35  
    36  	for _, setting := range params.AccessSettings.AccessSetting {
    37  
    38  		result[setting.Subject.Name] = setting
    39  	}
    40  
    41  	return result
    42  }
    43  
    44  // testAccessControl runs an access control test on a target type, identified as an interface.
    45  // * label is a test identifier
    46  // * accessible is the entity being tested (such as a vApp or a catalog)
    47  // * params are the access control parameters to be set
    48  // * expected are the result parameters that we should find in the end result
    49  // * wantShared is whether the final settings should result in the entity being shared
    50  func testAccessControl(label string, accessible accessControlType, params types.ControlAccessParams, expected types.ControlAccessParams, wantShared bool, useTenantContext bool, check *C) error {
    51  
    52  	if testVerbose {
    53  		fmt.Printf("-- %s\n", label)
    54  	}
    55  	err := accessible.SetAccessControl(&params, useTenantContext)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	foundParams, err := accessible.GetAccessControl(useTenantContext)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if testVerbose {
    66  		text, err := json.MarshalIndent(foundParams, " ", " ")
    67  		if err == nil {
    68  			fmt.Printf("%s %s\n", label, text)
    69  		}
    70  	}
    71  	if foundParams.IsSharedToEveryone != expected.IsSharedToEveryone {
    72  		fmt.Printf("label    %s\n", label)
    73  		fmt.Printf("found    %# v\n", pretty.Formatter(foundParams))
    74  		fmt.Printf("expected %# v\n", pretty.Formatter(expected))
    75  	}
    76  	check.Assert(foundParams.IsSharedToEveryone, Equals, expected.IsSharedToEveryone)
    77  	if expected.EveryoneAccessLevel != nil {
    78  		check.Assert(foundParams.EveryoneAccessLevel, NotNil)
    79  		check.Assert(*foundParams.EveryoneAccessLevel, Equals, *expected.EveryoneAccessLevel)
    80  	}
    81  	if expected.AccessSettings != nil {
    82  		expectedMap := accessSettingsToMap(expected)
    83  		foundMap := accessSettingsToMap(expected)
    84  
    85  		check.Assert(foundParams.AccessSettings, NotNil)
    86  		check.Assert(len(foundParams.AccessSettings.AccessSetting), Equals, len(expected.AccessSettings.AccessSetting))
    87  
    88  		for k, v := range expectedMap {
    89  			found, exists := foundMap[k]
    90  			check.Assert(exists, Equals, true)
    91  			if v.Subject.Name != "" {
    92  				check.Assert(v.Subject.Name, Equals, found.Subject.Name)
    93  			}
    94  			check.Assert(v.Subject.Type, Not(Equals), "")
    95  			check.Assert(v.Subject.HREF, Not(Equals), "")
    96  			check.Assert(v.Subject.HREF, Equals, found.Subject.HREF)
    97  			check.Assert(v.AccessLevel, Equals, found.AccessLevel)
    98  		}
    99  	}
   100  
   101  	shared, err := accessible.IsShared(useTenantContext)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	check.Assert(shared, Equals, wantShared)
   106  
   107  	return nil
   108  }