github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/auth/grants_test.go (about)

     1  package auth
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestGrants(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	t.Run("clone default grant", func(t *testing.T) {
    14  		grants := &ClaimGrants{}
    15  		clone := grants.Clone()
    16  		require.NotSame(t, grants, clone)
    17  		require.Same(t, grants.Video, clone.Video)
    18  		require.True(t, reflect.DeepEqual(grants, clone))
    19  		require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
    20  	})
    21  
    22  	t.Run("clone nil video", func(t *testing.T) {
    23  		grants := &ClaimGrants{
    24  			Identity: "identity",
    25  			Name:     "name",
    26  			Sha256:   "sha256",
    27  			Metadata: "metadata",
    28  		}
    29  		clone := grants.Clone()
    30  		require.NotSame(t, grants, clone)
    31  		require.Same(t, grants.Video, clone.Video)
    32  		require.True(t, reflect.DeepEqual(grants, clone))
    33  		require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
    34  	})
    35  
    36  	t.Run("clone with video", func(t *testing.T) {
    37  		tr := true
    38  		fa := false
    39  		video := &VideoGrant{
    40  			RoomCreate:     true,
    41  			RoomList:       false,
    42  			RoomRecord:     true,
    43  			RoomAdmin:      false,
    44  			RoomJoin:       true,
    45  			Room:           "room",
    46  			CanPublish:     &tr,
    47  			CanSubscribe:   &fa,
    48  			CanPublishData: nil,
    49  			Hidden:         true,
    50  			Recorder:       false,
    51  		}
    52  		grants := &ClaimGrants{
    53  			Identity: "identity",
    54  			Name:     "name",
    55  			Video:    video,
    56  			Sha256:   "sha256",
    57  			Metadata: "metadata",
    58  		}
    59  		clone := grants.Clone()
    60  		require.NotSame(t, grants, clone)
    61  		require.NotSame(t, grants.Video, clone.Video)
    62  		require.NotSame(t, grants.Video.CanPublish, clone.Video.CanPublish)
    63  		require.NotSame(t, grants.Video.CanSubscribe, clone.Video.CanSubscribe)
    64  		require.Same(t, grants.Video.CanPublishData, clone.Video.CanPublishData)
    65  		require.True(t, reflect.DeepEqual(grants, clone))
    66  		require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
    67  	})
    68  }