9 changed files with 328 additions and 62 deletions
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2014 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
func assertDataError(t *testing.T, err error, code string) { |
||||||
|
if err == nil { |
||||||
|
t.Error("Expected an error, but none was returned") |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
dataError, ok := err.(*DataError) |
||||||
|
if !ok { |
||||||
|
t.Errorf("Expected error %#v to be a *DataError", err) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
if code != dataError.Code { |
||||||
|
t.Errorf("Expected error code to be %v, but was %v", code, dataError.Code) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,124 @@ |
|||||||
|
/* |
||||||
|
* Spreed WebRTC. |
||||||
|
* Copyright (C) 2013-2014 struktur AG |
||||||
|
* |
||||||
|
* This file is part of Spreed WebRTC. |
||||||
|
* |
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU Affero General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU Affero General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Affero General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
testRoomName string = "a-room-name" |
||||||
|
) |
||||||
|
|
||||||
|
func NewTestRoomWorker() RoomWorker { |
||||||
|
worker := NewRoomWorker(&roomManager{}, testRoomName, nil) |
||||||
|
go worker.Start() |
||||||
|
return worker |
||||||
|
} |
||||||
|
|
||||||
|
func NewTestRoomWorkerWithPIN(t *testing.T) (RoomWorker, string) { |
||||||
|
pin := "asdf" |
||||||
|
worker := NewRoomWorker(&roomManager{}, testRoomName, &DataRoomCredentials{PIN: pin}) |
||||||
|
go worker.Start() |
||||||
|
return worker, pin |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Join_SucceedsWhenNoCredentialsAreRequired(t *testing.T) { |
||||||
|
worker := NewTestRoomWorker() |
||||||
|
|
||||||
|
_, err := worker.Join(nil, &Session{}, nil) |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("Unexpected error %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
if userCount := len(worker.GetUsers()); userCount != 1 { |
||||||
|
t.Errorf("Expected join to have been accepted but room contains %d users", userCount) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Join_FailsIfCredentialsAreGivenWhenUnneeded(t *testing.T) { |
||||||
|
worker := NewTestRoomWorker() |
||||||
|
|
||||||
|
_, err := worker.Join(&DataRoomCredentials{}, &Session{}, nil) |
||||||
|
|
||||||
|
assertDataError(t, err, "authorization_not_required") |
||||||
|
if userCount := len(worker.GetUsers()); userCount != 0 { |
||||||
|
t.Errorf("Expected join to have been rejected but room contains %d users", userCount) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Join_FailsIfNoCredentialsAreGiven(t *testing.T) { |
||||||
|
worker, _ := NewTestRoomWorkerWithPIN(t) |
||||||
|
|
||||||
|
_, err := worker.Join(nil, &Session{}, nil) |
||||||
|
|
||||||
|
assertDataError(t, err, "authorization_required") |
||||||
|
if userCount := len(worker.GetUsers()); userCount != 0 { |
||||||
|
t.Errorf("Expected join to have been rejected but room contains %d users", userCount) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Join_FailsIfIncorrectCredentialsAreGiven(t *testing.T) { |
||||||
|
worker, _ := NewTestRoomWorkerWithPIN(t) |
||||||
|
|
||||||
|
_, err := worker.Join(&DataRoomCredentials{PIN: "adfs"}, &Session{}, nil) |
||||||
|
|
||||||
|
assertDataError(t, err, "invalid_credentials") |
||||||
|
if userCount := len(worker.GetUsers()); userCount != 0 { |
||||||
|
t.Errorf("Expected join to have been rejected but room contains %d users", userCount) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Join_SucceedsWhenTheCorrectPINIsGiven(t *testing.T) { |
||||||
|
worker, pin := NewTestRoomWorkerWithPIN(t) |
||||||
|
|
||||||
|
if _, err := worker.Join(&DataRoomCredentials{PIN: pin}, &Session{}, nil); err != nil { |
||||||
|
t.Fatalf("Unexpected error %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
if len(worker.GetUsers()) < 1 { |
||||||
|
t.Error("Expected join to have been accepted but room contains no users") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Update_AllowsClearingCredentials(t *testing.T) { |
||||||
|
worker, _ := NewTestRoomWorkerWithPIN(t) |
||||||
|
|
||||||
|
if err := worker.Update(&DataRoom{Credentials: &DataRoomCredentials{PIN: ""}}); err != nil { |
||||||
|
t.Fatalf("Failed to update room: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
_, err := worker.Join(&DataRoomCredentials{}, &Session{}, nil) |
||||||
|
assertDataError(t, err, "authorization_not_required") |
||||||
|
} |
||||||
|
|
||||||
|
func Test_RoomWorker_Update_RetainsCredentialsWhenOtherPropertiesAreUpdated(t *testing.T) { |
||||||
|
worker, pin := NewTestRoomWorkerWithPIN(t) |
||||||
|
|
||||||
|
if err := worker.Update(&DataRoom{}); err != nil { |
||||||
|
t.Fatalf("Failed to update room: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
if _, err := worker.Join(&DataRoomCredentials{PIN: pin}, &Session{}, nil); err != nil { |
||||||
|
t.Fatalf("Unexpected error joining room %v", err) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue