From cc43c8e3127989e3c4406b3ed31bea03e674725f Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 17 Jan 2025 17:44:28 +0000 Subject: [PATCH] A few simple test case to get us started and run them in CI --- .github/workflows/test.yaml | 21 ++++++++ main.go | 14 +++-- main_test.go | 104 ++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/test.yaml create mode 100644 main_test.go diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..bb55eec --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,21 @@ +name: Test + +on: + pull_request: + push: + branches: [main] + +jobs: + test: + name: Testing + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v4 + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - name: Test + run: go test -timeout 30s diff --git a/main.go b/main.go index a2a1924..7242a9d 100644 --- a/main.go +++ b/main.go @@ -170,6 +170,15 @@ func (h *Handler) handle(w http.ResponseWriter, r *http.Request) { } } +func (h *Handler) prepareMux() (*http.ServeMux) { + + mux := http.NewServeMux() + mux.HandleFunc("/sfu/get", h.handle) + mux.HandleFunc("/healthz", h.healthcheck) + + return mux +} + func main() { skipVerifyTLS := os.Getenv("LIVEKIT_INSECURE_SKIP_VERIFY_TLS") == "YES_I_KNOW_WHAT_I_AM_DOING" if skipVerifyTLS { @@ -203,10 +212,7 @@ func main() { skipVerifyTLS: skipVerifyTLS, } - http.HandleFunc("/sfu/get", handler.handle) - http.HandleFunc("/healthz", handler.healthcheck) - - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", lk_jwt_port), nil)) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", lk_jwt_port), handler.prepareMux())) } func getJoinToken(apiKey, apiSecret, room, identity string) (string, error) { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..f73df1f --- /dev/null +++ b/main_test.go @@ -0,0 +1,104 @@ +// Copyright 2025 New Vector Ltd + +// 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 . + +package main + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/matrix-org/gomatrix" +) + +func TestHealthcheck(t *testing.T) { + handler := &Handler{} + req, err := http.NewRequest("GET", "/healthz", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler.prepareMux().ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) + } +} + +func TestHandleOptions(t *testing.T) { + handler := &Handler{} + req, err := http.NewRequest("OPTIONS", "/sfu/get", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler.prepareMux().ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code for OPTIONS: got %v want %v", status, http.StatusOK) + } +} + +func TestHandlePostMissingRoom(t *testing.T) { + handler := &Handler{} + body := SFURequest{ + Room: "", + OpenIDToken: OpenIDTokenType{AccessToken: "token", MatrixServerName: "server"}, + DeviceID: "device", + } + jsonBody, _ := json.Marshal(body) + + req, err := http.NewRequest("POST", "/sfu/get", bytes.NewBuffer(jsonBody)) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler.prepareMux().ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusBadRequest { + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest) + } + + var resp gomatrix.RespError + err = json.NewDecoder(rr.Body).Decode(&resp) + if err != nil { + t.Errorf("failed to decode response body %v", err) + } + + if resp.ErrCode != "M_BAD_JSON" { + t.Errorf("unexpected error code: got %v want %v", resp.ErrCode, "M_BAD_JSON") + } +} + +func TestGetJoinToken(t *testing.T) { + apiKey := "testKey" + apiSecret := "testSecret" + room := "testRoom" + identity := "testIdentity@example.com" + + token, err := getJoinToken(apiKey, apiSecret, room, identity) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if token == "" { + t.Error("expected token to be non-empty") + } +}