github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/auth/grants_test.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package auth
    16  
    17  import (
    18  	"reflect"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/livekit/protocol/livekit"
    25  )
    26  
    27  func TestGrants(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	t.Run("clone default grant", func(t *testing.T) {
    31  		grants := &ClaimGrants{}
    32  		clone := grants.Clone()
    33  		require.NotSame(t, grants, clone)
    34  		require.Same(t, grants.Video, clone.Video)
    35  		require.True(t, reflect.DeepEqual(grants, clone))
    36  		require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
    37  	})
    38  
    39  	t.Run("clone nil video", func(t *testing.T) {
    40  		grants := &ClaimGrants{
    41  			Identity: "identity",
    42  			Name:     "name",
    43  			Sha256:   "sha256",
    44  			Metadata: "metadata",
    45  		}
    46  		clone := grants.Clone()
    47  		require.NotSame(t, grants, clone)
    48  		require.Same(t, grants.Video, clone.Video)
    49  		require.True(t, reflect.DeepEqual(grants, clone))
    50  		require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
    51  	})
    52  
    53  	t.Run("clone with video", func(t *testing.T) {
    54  		tr := true
    55  		fa := false
    56  		video := &VideoGrant{
    57  			RoomCreate:     true,
    58  			RoomList:       false,
    59  			RoomRecord:     true,
    60  			RoomAdmin:      false,
    61  			RoomJoin:       true,
    62  			Room:           "room",
    63  			CanPublish:     &tr,
    64  			CanSubscribe:   &fa,
    65  			CanPublishData: nil,
    66  			Hidden:         true,
    67  			Recorder:       false,
    68  		}
    69  		grants := &ClaimGrants{
    70  			Identity: "identity",
    71  			Name:     "name",
    72  			Kind:     "kind",
    73  			Video:    video,
    74  			Sha256:   "sha256",
    75  			Metadata: "metadata",
    76  		}
    77  		clone := grants.Clone()
    78  		require.NotSame(t, grants, clone)
    79  		require.NotSame(t, grants.Video, clone.Video)
    80  		require.NotSame(t, grants.Video.CanPublish, clone.Video.CanPublish)
    81  		require.NotSame(t, grants.Video.CanSubscribe, clone.Video.CanSubscribe)
    82  		require.Same(t, grants.Video.CanPublishData, clone.Video.CanPublishData)
    83  		require.True(t, reflect.DeepEqual(grants, clone))
    84  		require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
    85  	})
    86  }
    87  
    88  func TestParticipantKind(t *testing.T) {
    89  	const kindMin, kindMax = livekit.ParticipantInfo_STANDARD, livekit.ParticipantInfo_AGENT
    90  	for k := kindMin; k <= kindMax; k++ {
    91  		k := k
    92  		t.Run(k.String(), func(t *testing.T) {
    93  			require.Equal(t, k, kindToProto(kindFromProto(k)))
    94  		})
    95  	}
    96  	const kindNext = kindMax + 1
    97  	if _, err := strconv.Atoi(kindNext.String()); err != nil {
    98  		t.Errorf("Please update kindMax to match protobuf. Missing value: %s", kindNext)
    99  	}
   100  }