github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/scheme/options_test.go (about)

     1  package scheme
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Scheme"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
     9  )
    10  
    11  func TestSchemeOptions(t *testing.T) {
    12  	{
    13  		opts := []scheme.PermissionsOption{
    14  			scheme.WithClearPermissions(),
    15  			scheme.WithChangeOwner("ow"),
    16  			scheme.WithGrantPermissions(scheme.Permissions{
    17  				Subject:         "grant",
    18  				PermissionNames: []string{"a", "b", "c"},
    19  			}),
    20  			scheme.WithSetPermissions(scheme.Permissions{
    21  				Subject:         "set",
    22  				PermissionNames: []string{"d"},
    23  			}),
    24  			scheme.WithRevokePermissions(scheme.Permissions{
    25  				Subject:         "revoke",
    26  				PermissionNames: []string{"e"},
    27  			}),
    28  		}
    29  
    30  		var desc permissionsDesc
    31  		for _, o := range opts {
    32  			if o != nil {
    33  				o(&desc)
    34  			}
    35  		}
    36  
    37  		if !desc.clear {
    38  			t.Errorf("Clear is not as expected")
    39  		}
    40  
    41  		count := len(desc.actions)
    42  		for _, a := range desc.actions {
    43  			switch a := a.GetAction().(type) {
    44  			case *Ydb_Scheme.PermissionsAction_ChangeOwner:
    45  				count--
    46  				if a.ChangeOwner != "ow" {
    47  					t.Errorf("Owner is not as expected")
    48  				}
    49  			case *Ydb_Scheme.PermissionsAction_Grant:
    50  				count--
    51  				if a.Grant.GetSubject() != "grant" || len(a.Grant.GetPermissionNames()) != 3 {
    52  					t.Errorf("Grant is not as expected")
    53  				}
    54  			case *Ydb_Scheme.PermissionsAction_Set:
    55  				count--
    56  				if a.Set.GetSubject() != "set" || len(a.Set.GetPermissionNames()) != 1 || a.Set.GetPermissionNames()[0] != "d" {
    57  					t.Errorf("Set is not as expected")
    58  				}
    59  			case *Ydb_Scheme.PermissionsAction_Revoke:
    60  				count--
    61  				revokeSubject := a.Revoke.GetSubject()
    62  				permissionNames := a.Revoke.GetPermissionNames()
    63  				if revokeSubject != "revoke" || len(permissionNames) != 1 || permissionNames[0] != "e" {
    64  					t.Errorf("Revoke is not as expected")
    65  				}
    66  			}
    67  		}
    68  
    69  		if count != 0 {
    70  			t.Errorf("Count of permission actions is not as expected")
    71  		}
    72  	}
    73  }