Test: room controller with password

This commit is contained in:
2020-03-27 16:00:37 +08:00
parent 345df1ee90
commit b79374c9cc
2 changed files with 35 additions and 13 deletions
+8 -5
View File
@@ -24,16 +24,19 @@ func TestHall_CreateRoom(t *testing.T) {
func TestHall_JoinRoom(t *testing.T) {
hall := GetHall()
roomID := hall.CreateRoom(0002, "test room", "ppp")
if !hall.JoinRoom(0003, roomID) {
if !hall.JoinRoom(0003, roomID, "ppp") {
t.Errorf("2nd player joined failed")
}
if !hall.JoinRoom(0004, roomID) {
if !hall.JoinRoom(0004, roomID, "ppp") {
t.Errorf("3rd player joined failed")
}
if !hall.JoinRoom(0005, roomID) {
if hall.JoinRoom(0005, roomID, "pppaaa") {
t.Errorf("joined room via wrong password")
}
if !hall.JoinRoom(0005, roomID, "ppp") {
t.Errorf("4th player joined failed")
}
if hall.JoinRoom(0006, roomID) {
if hall.JoinRoom(0006, roomID, "ppp") {
t.Errorf("5th player joined room")
}
}
@@ -47,7 +50,7 @@ func TestHall_GetRoomID(t *testing.T) {
if hall.GetRoomID(8) >= 0 {
t.Errorf("get ghost roomID")
}
hall.JoinRoom(8, roomID)
hall.JoinRoom(8, roomID, "ppp")
if hall.GetRoomID(8) != roomID {
t.Errorf("get wrong roomID")
}
+27 -8
View File
@@ -31,10 +31,11 @@ func TestRoomCreateHandler(t *testing.T) {
}
func TestRoomJoinHandler(t *testing.T) {
container.GetHall().CreateRoom(003, "test", "pp")
roomID := container.GetHall().CreateRoom(003, "test", "pp")
body, _ := json.Marshal(joinRoomMsg{
UserID: 004,
RoomID: 0,
UserID: 004,
RoomID: roomID,
Password: "pp",
})
req, err := http.NewRequest("PUT", "/room", bytes.NewReader(body))
if err != nil {
@@ -52,7 +53,23 @@ func TestRoomJoinHandler(t *testing.T) {
body, _ = json.Marshal(joinRoomMsg{
UserID: 005,
RoomID: 0,
RoomID: roomID,
})
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
rr = httptest.NewRecorder()
handler = http.HandlerFunc(RoomJoinHandler)
handler.ServeHTTP(rr, req)
err = json.Unmarshal([]byte(rr.Body.String()), &msg)
if msg.Success == true {
t.Errorf("join room with wrong password")
}
body, _ = json.Marshal(joinRoomMsg{
UserID: 005,
RoomID: roomID,
Password: "pp",
})
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
if err != nil {
@@ -66,8 +83,9 @@ func TestRoomJoinHandler(t *testing.T) {
t.Errorf("failed to join room as 3rd player")
}
body, _ = json.Marshal(joinRoomMsg{
UserID: 006,
RoomID: 0,
UserID: 006,
RoomID: roomID,
Password: "pp",
})
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
if err != nil {
@@ -81,8 +99,9 @@ func TestRoomJoinHandler(t *testing.T) {
t.Errorf("failed to join room as 4th player")
}
body, _ = json.Marshal(joinRoomMsg{
UserID: 006,
RoomID: 0,
UserID: 006,
RoomID: roomID,
Password: "pp",
})
req, err = http.NewRequest("PUT", "/room", bytes.NewReader(body))
if err != nil {