From ab26dfe90dd16fafbc754254253aa9d52803c6b6 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Fri, 8 Jul 2022 15:11:50 +0200 Subject: [PATCH] Add testcase for initial permissions in a room. --- hub_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/hub_test.go b/hub_test.go index b8502f1..ad25262 100644 --- a/hub_test.go +++ b/hub_test.go @@ -387,7 +387,8 @@ func processRoomRequest(t *testing.T, w http.ResponseWriter, r *http.Request, re RoomId: request.Room.RoomId, }, } - if request.Room.RoomId == "test-room-with-sessiondata" { + switch request.Room.RoomId { + case "test-room-with-sessiondata": data := map[string]string{ "userid": "userid-from-sessiondata", } @@ -396,6 +397,9 @@ func processRoomRequest(t *testing.T, w http.ResponseWriter, r *http.Request, re t.Fatalf("Could not marshal %+v: %s", data, err) } response.Room.Session = (*json.RawMessage)(&tmp) + case "test-room-initial-permissions": + permissions := []Permission{PERMISSION_MAY_PUBLISH_AUDIO} + response.Room.Permissions = &permissions } return response } @@ -2417,7 +2421,7 @@ func TestJoinMultiple(t *testing.T) { } } -func TestJoinMultipleDisplaynamesPermission(t *testing.T) { +func TestJoinDisplaynamesPermission(t *testing.T) { hub, _, _, server := CreateHubForTest(t) client1 := NewTestClient(t, server, hub) @@ -2499,6 +2503,49 @@ func TestJoinMultipleDisplaynamesPermission(t *testing.T) { } } +func TestInitialRoomPermissions(t *testing.T) { + hub, _, _, server := CreateHubForTest(t) + + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) + defer cancel() + + client := NewTestClient(t, server, hub) + defer client.CloseWithBye() + + if err := client.SendHello(testDefaultUserId + "1"); err != nil { + t.Fatal(err) + } + + hello, err := client.RunUntilHello(ctx) + if err != nil { + t.Fatal(err) + } + + // Join room by id. + roomId := "test-room-initial-permissions" + if room, err := client.JoinRoom(ctx, roomId); err != nil { + t.Fatal(err) + } else if room.Room.RoomId != roomId { + t.Fatalf("Expected room %s, got %s", roomId, room.Room.RoomId) + } + + if err := client.RunUntilJoined(ctx, hello.Hello); err != nil { + t.Error(err) + } + + session := hub.GetSessionByPublicId(hello.Hello.SessionId).(*ClientSession) + if session == nil { + t.Fatalf("Session %s does not exist", hello.Hello.SessionId) + } + + if !session.HasPermission(PERMISSION_MAY_PUBLISH_AUDIO) { + t.Errorf("Session %s should have %s, got %+v", session.PublicId(), PERMISSION_MAY_PUBLISH_AUDIO, session.permissions) + } + if session.HasPermission(PERMISSION_MAY_PUBLISH_VIDEO) { + t.Errorf("Session %s should not have %s, got %+v", session.PublicId(), PERMISSION_MAY_PUBLISH_VIDEO, session.permissions) + } +} + func TestJoinRoomSwitchClient(t *testing.T) { hub, _, _, server := CreateHubForTest(t)