2024-05-09 20:29:48 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"mchess_server/api"
|
|
|
|
"mchess_server/utils"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_GetLobbyFromPassphraseHandler(t *testing.T) {
|
|
|
|
var passphraseURLParameter string
|
2024-05-20 13:36:13 +00:00
|
|
|
var receivedPhrase string
|
2024-05-09 20:29:48 +00:00
|
|
|
|
|
|
|
t.Run("host a lobby", func(t *testing.T) {
|
|
|
|
r1 := httptest.NewRecorder()
|
|
|
|
ctx1, e1 := gin.CreateTestContext(r1)
|
2024-05-12 13:36:30 +00:00
|
|
|
e1.GET("/api/hostPrivate", HostGameHandler)
|
2024-05-09 20:29:48 +00:00
|
|
|
hostGameRequest, _ := http.NewRequest("GET", "/api/hostPrivate", nil)
|
|
|
|
ctx1.Request = hostGameRequest
|
|
|
|
|
|
|
|
e1.ServeHTTP(r1, hostGameRequest)
|
|
|
|
|
|
|
|
playerInfo := api.PlayerInfo{}
|
|
|
|
err := json.Unmarshal(r1.Body.Bytes(), &playerInfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-05-20 13:36:13 +00:00
|
|
|
receivedPhrase = *playerInfo.Passphrase
|
2024-05-09 20:29:48 +00:00
|
|
|
|
|
|
|
passphrase := utils.NewPassphraseFromString(receivedPhrase)
|
|
|
|
passphraseURLParameter = passphrase.AsURLParam()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("see if the lobby can be fetched by using the passphrase", func(t *testing.T) {
|
|
|
|
r2 := httptest.NewRecorder()
|
|
|
|
ctx2, engine := gin.CreateTestContext(r2)
|
|
|
|
engine.GET("/api/getLobbyForPassphrase/:phrase", GetLobbyForPassphraseHandler)
|
|
|
|
|
|
|
|
url := "/api/getLobbyForPassphrase/" + passphraseURLParameter
|
|
|
|
getLobbyRequest, _ := http.NewRequest("GET", url, nil)
|
|
|
|
ctx2.Request = getLobbyRequest
|
|
|
|
|
|
|
|
engine.ServeHTTP(r2, getLobbyRequest)
|
|
|
|
|
2024-05-20 13:36:13 +00:00
|
|
|
passhrase := api.Passphrase{}
|
|
|
|
err := json.Unmarshal(r2.Body.Bytes(), &passhrase)
|
2024-05-09 20:29:48 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-05-20 13:36:13 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r2.Code)
|
2024-05-09 20:29:48 +00:00
|
|
|
})
|
|
|
|
}
|