package main
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
)
type fakeVerifier struct{}
func (fakeVerifier) Verify(_ context.Context, token string) (string, error) {
if token == "" || token == "bad" {
return "", errors.New("invalid")
}
return token, nil
}
func newTestApp(t *testing.T) *httptest.Server {
t.Helper()
contacts, err := LoadContacts(filepath.Join(t.TempDir(), "contacts.json"))
if err != nil {
t.Fatal(err)
}
app := &App{
verifier: fakeVerifier{},
store: NewStore(),
contacts: contacts,
defaultJitsiURL: "https://meet.jit.si",
}
mux := http.NewServeMux()
app.Routes(mux)
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
return srv
}
func do(t *testing.T, srv *httptest.Server, as, method, path, body string) (int, map[string]any) {
t.Helper()
var r io.Reader
if body != "" {
r = strings.NewReader(body)
}
req, _ := http.NewRequest(method, srv.URL+path, r)
if as != "" {
req.Header.Set("Authorization", "Bearer "+as)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
var out map[string]any
_ = json.NewDecoder(resp.Body).Decode(&out)
return resp.StatusCode, out
}
func TestAuth(t *testing.T) {
srv := newTestApp(t)
if status, _ := do(t, srv, "", "GET", "/me", ""); status != 401 {
t.Errorf("no token: %d", status)
}
if status, _ := do(t, srv, "bad", "GET", "/me", ""); status != 401 {
t.Errorf("bad token: %d", status)
}
status, me := do(t, srv, "alice", "GET", "/me", "")
if status != 200 || me["username"] != "alice" || me["defaultJitsiUrl"] != "https://meet.jit.si" {
t.Errorf("/me: %d %v", status, me)
}
}
func TestCallOnDefaultServer(t *testing.T) {
srv := newTestApp(t)
status, call := do(t, srv, "alice", "POST", "/call", `{"to":"Bob"}`)
if status != 200 || call["jitsiUrl"] != "https://meet.jit.si" || call["meetingId"] == "" {
t.Fatalf("call: %d %v", status, call)
}
_, poll := do(t, srv, "bob", "GET", "/poll", "")
inv := poll["invites"].([]any)[0].(map[string]any)
if inv["from"] != "alice" || inv["jitsiUrl"] != "https://meet.jit.si" || inv["meetingId"] != call["meetingId"] {
t.Fatalf("invite: %v", inv)
}
}
func TestCallOnAnotherServer(t *testing.T) {
srv := newTestApp(t)
status, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob","jitsiUrl":"Jitsi.Example.org/"}`)
if status != 200 || call["jitsiUrl"] != "https://jitsi.example.org" {
t.Fatalf("call: %d %v", status, call)
}
_, poll := do(t, srv, "bob", "GET", "/poll", "")
inv := poll["invites"].([]any)[0].(map[string]any)
if inv["jitsiUrl"] != "https://jitsi.example.org" || inv["meetingId"] != call["meetingId"] {
t.Fatalf("invite: %v", inv)
}
}
func TestCallValidation(t *testing.T) {
srv := newTestApp(t)
for _, tc := range []struct{ body, why string }{
{`{"to":"alice"}`, "self"},
{`{"to":"not a name"}`, "malformed name"},
{`{"to":"bob@example.com"}`, "not a username"},
{`{"to":"bob","jitsiUrl":"javascript:alert(1)"}`, "non-http server"},
{`{"to":"bob","jitsiUrl":"file:///etc/passwd"}`, "file server"},
{`{"to":"bob","jitsiUrl":"https://x.example/?a=b"}`, "query in server"},
{`{"to":"bob","jitsiUrl":"https://user@x.example"}`, "credentials in server"},
} {
if status, _ := do(t, srv, "alice", "POST", "/call", tc.body); status != 400 {
t.Errorf("%s: status %d", tc.why, status)
}
}
}
func TestContacts(t *testing.T) {
srv := newTestApp(t)
if status, _ := do(t, srv, "alice", "POST", "/contacts", `{"username":"Joe"}`); status != 200 {
t.Fatalf("add: %d", status)
}
do(t, srv, "alice", "POST", "/contacts", `{"username":"bob"}`)
if status, _ := do(t, srv, "alice", "POST", "/contacts", `{"username":"alice"}`); status != 400 {
t.Errorf("adding yourself: %d", status)
}
do(t, srv, "joe", "GET", "/poll", "")
_, list := do(t, srv, "alice", "GET", "/contacts", "")
got, _ := json.Marshal(list["contacts"])
want := `[{"online":false,"username":"bob"},{"online":true,"username":"joe"}]`
if string(got) != want {
t.Fatalf("contacts = %s, want %s", got, want)
}
do(t, srv, "alice", "DELETE", "/contacts/bob", "")
_, list = do(t, srv, "alice", "GET", "/contacts", "")
if n := len(list["contacts"].([]any)); n != 1 {
t.Fatalf("after delete: %d contacts", n)
}
_, other := do(t, srv, "bob", "GET", "/contacts", "")
if n := len(other["contacts"].([]any)); n != 0 {
t.Fatalf("bob sees %d of alice's contacts", n)
}
}
func TestCallHeartbeatAndHangUp(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
id := call["meetingId"].(string)
path := "/calls/" + id
status, st := do(t, srv, "alice", "POST", path+"/heartbeat", "")
if status != 200 || st["peer"] != "bob" || st["peerSeenMsAgo"] != nil || st["endedBy"] != "" {
t.Fatalf("caller before answer: %d %v", status, st)
}
if status, st = do(t, srv, "bob", "POST", path+"/heartbeat", ""); status != 200 || st["peer"] != "alice" || st["peerSeenMsAgo"] == nil {
t.Fatalf("callee: %d %v", status, st)
}
if _, st = do(t, srv, "alice", "POST", path+"/heartbeat", ""); st["peerSeenMsAgo"] == nil {
t.Fatalf("caller after answer: %v", st)
}
if status, _ = do(t, srv, "eve", "POST", path+"/heartbeat", ""); status != 403 {
t.Errorf("stranger heartbeat: %d", status)
}
if status, _ = do(t, srv, "eve", "POST", path+"/hangup", ""); status != 403 {
t.Errorf("stranger hangup: %d", status)
}
if status, _ = do(t, srv, "alice", "POST", "/calls/quple-nope/heartbeat", ""); status != 404 {
t.Errorf("unknown call: %d", status)
}
if status, _ = do(t, srv, "alice", "GET", path+"/heartbeat", ""); status != 405 {
t.Errorf("GET heartbeat: %d", status)
}
if status, _ = do(t, srv, "bob", "POST", path+"/hangup", ""); status != 200 {
t.Fatalf("hangup: %d", status)
}
do(t, srv, "alice", "POST", path+"/hangup", "")
if _, st = do(t, srv, "alice", "POST", path+"/heartbeat", ""); st["endedBy"] != "bob" {
t.Fatalf("after hangup: %v", st)
}
}
func TestHeartbeatCountsAsPresence(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
do(t, srv, "bob", "POST", "/calls/"+call["meetingId"].(string)+"/heartbeat", "")
do(t, srv, "alice", "POST", "/contacts", `{"username":"bob"}`)
_, list := do(t, srv, "alice", "GET", "/contacts", "")
if c := list["contacts"].([]any)[0].(map[string]any); c["online"] != true {
t.Fatalf("bob in a call shows offline: %v", c)
}
}
func TestRingingAndCancel(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
path := "/calls/" + call["meetingId"].(string)
if status, st := do(t, srv, "bob", "GET", path, ""); status != 200 || st["peer"] != "alice" || st["endedBy"] != "" {
t.Fatalf("callee status: %d %v", status, st)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", ""); st["peerSeenMsAgo"] != nil {
t.Fatalf("checking the call counted as answering: %v", st)
}
if status, _ := do(t, srv, "eve", "GET", path, ""); status != 403 {
t.Errorf("stranger status: %d", status)
}
do(t, srv, "alice", "POST", path+"/hangup", "")
if _, st := do(t, srv, "bob", "GET", path, ""); st["endedBy"] != "alice" {
t.Fatalf("after cancel: %v", st)
}
}
func TestCancelledCallIsNotDelivered(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
do(t, srv, "alice", "POST", "/calls/"+call["meetingId"].(string)+"/hangup", "")
if _, poll := do(t, srv, "bob", "GET", "/poll", ""); len(poll["invites"].([]any)) != 0 {
t.Fatalf("cancelled call delivered: %v", poll)
}
}
func TestMoveCallBetweenDevices(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
id := call["meetingId"].(string)
path := "/calls/" + id
phone := `{"device":"alice-phone","name":"iPhone","joined":true}`
do(t, srv, "alice", "POST", path+"/heartbeat", phone)
do(t, srv, "bob", "POST", path+"/heartbeat", `{"device":"bob-phone","name":"iPhone","joined":true}`)
_, poll := do(t, srv, "alice", "GET", "/poll?device=alice-mac", "")
calls := poll["calls"].([]any)
if len(calls) != 1 {
t.Fatalf("mac sees %v", poll)
}
if c := calls[0].(map[string]any); c["meetingId"] != id || c["peer"] != "bob" || c["device"] != "iPhone" || c["jitsiUrl"] != "https://meet.jit.si" {
t.Fatalf("active call: %v", c)
}
if _, poll = do(t, srv, "alice", "GET", "/poll?device=alice-phone", ""); len(poll["calls"].([]any)) != 0 {
t.Fatalf("phone sees its own call: %v", poll)
}
if _, poll = do(t, srv, "bob", "GET", "/poll?device=bob-phone", ""); len(poll["calls"].([]any)) != 0 {
t.Fatalf("bob's phone sees its own call: %v", poll)
}
if status, moved := do(t, srv, "alice", "POST", path+"/move", `{"device":"alice-mac","name":"Mac"}`); status != 200 || moved["jitsiUrl"] != "https://meet.jit.si" || moved["peer"] != "bob" {
t.Fatalf("move: %d %v", status, moved)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", phone); st["movedTo"] != "Mac" || st["movedJoined"] == true {
t.Fatalf("phone before the mac joined: %v", st)
}
do(t, srv, "alice", "POST", path+"/heartbeat", `{"device":"alice-mac","name":"Mac","joined":true}`)
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", phone); st["movedTo"] != "Mac" || st["movedJoined"] != true {
t.Fatalf("phone after the mac joined: %v", st)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", `{"device":"alice-mac","name":"Mac","joined":true}`); st["movedTo"] != nil {
t.Fatalf("mac told to move: %v", st)
}
if _, poll = do(t, srv, "alice", "GET", "/poll?device=alice-phone", ""); len(poll["calls"].([]any)) != 1 {
t.Fatalf("phone doesn't see the call on the mac: %v", poll)
}
if status, _ := do(t, srv, "eve", "POST", path+"/move", `{"device":"x","name":"x"}`); status != 403 {
t.Errorf("stranger move: %d", status)
}
if status, _ := do(t, srv, "alice", "POST", path+"/move", `{}`); status != 400 {
t.Errorf("move without a device: %d", status)
}
do(t, srv, "bob", "POST", path+"/hangup", "")
if status, _ := do(t, srv, "alice", "POST", path+"/move", `{"device":"alice-phone","name":"iPhone"}`); status != 409 {
t.Errorf("moving an ended call: %d", status)
}
if _, poll = do(t, srv, "alice", "GET", "/poll?device=alice-phone", ""); len(poll["calls"].([]any)) != 0 {
t.Fatalf("ended call still offered: %v", poll)
}
}
func TestUnansweredCallIsNotOffered(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
do(t, srv, "alice", "POST", "/calls/"+call["meetingId"].(string)+"/heartbeat", `{"device":"alice-phone","name":"iPhone"}`)
if _, poll := do(t, srv, "alice", "GET", "/poll?device=alice-mac", ""); len(poll["calls"].([]any)) != 0 {
t.Fatalf("ringing call offered: %v", poll)
}
if _, poll := do(t, srv, "alice", "GET", "/poll", ""); poll["calls"] != nil {
t.Fatalf("calls without a device: %v", poll)
}
}
func TestEveryDeviceRingsAndHearsWhereItWasAnswered(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob","device":"alice-mac","name":"Mac"}`)
id := call["meetingId"].(string)
for _, device := range []string{"bob-phone", "bob-mac"} {
_, poll := do(t, srv, "bob", "GET", "/poll?device="+device, "")
if invites := poll["invites"].([]any); len(invites) != 1 || invites[0].(map[string]any)["meetingId"] != id {
t.Fatalf("%s: %v", device, poll)
}
if _, poll = do(t, srv, "bob", "GET", "/poll?device="+device, ""); len(poll["invites"].([]any)) != 0 {
t.Fatalf("%s rings twice: %v", device, poll)
}
}
do(t, srv, "bob", "POST", "/calls/"+id+"/heartbeat", `{"device":"bob-phone","name":"iPhone"}`)
if _, st := do(t, srv, "bob", "GET", "/calls/"+id+"?device=bob-mac", ""); st["answeredOn"] != "iPhone" || st["endedBy"] != "" || st["peerEnded"] != false {
t.Fatalf("mac while ringing: %v", st)
}
if _, st := do(t, srv, "bob", "GET", "/calls/"+id+"?device=bob-phone", ""); st["answeredOn"] != nil {
t.Fatalf("phone told it was answered elsewhere: %v", st)
}
if _, st := do(t, srv, "alice", "POST", "/calls/"+id+"/heartbeat", `{"device":"alice-mac","name":"Mac"}`); st["peerSeenMsAgo"] == nil {
t.Fatalf("caller doesn't see the answer: %v", st)
}
}
func TestCallYourself(t *testing.T) {
srv := newTestApp(t)
if status, _ := do(t, srv, "alice", "POST", "/call", `{"to":"alice"}`); status != 400 {
t.Fatalf("self call without a device: %d", status)
}
status, call := do(t, srv, "alice", "POST", "/call", `{"to":"alice","device":"alice-mac","name":"Mac"}`)
if status != 200 {
t.Fatalf("self call: %d %v", status, call)
}
id := call["meetingId"].(string)
path := "/calls/" + id
mac := `{"device":"alice-mac","name":"Mac","joined":true}`
phone := `{"device":"alice-phone","name":"iPhone","joined":true}`
if _, poll := do(t, srv, "alice", "GET", "/poll?device=alice-mac", ""); len(poll["invites"].([]any)) != 0 {
t.Fatalf("calling device rings: %v", poll)
}
if _, poll := do(t, srv, "alice", "GET", "/poll?device=alice-phone", ""); len(poll["invites"].([]any)) != 1 {
t.Fatalf("other device doesn't ring: %v", poll)
}
if _, poll := do(t, srv, "alice", "GET", "/poll?device=alice-ipad", ""); len(poll["calls"].([]any)) != 0 {
t.Fatalf("self call offered to move: %v", poll)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", mac); st["peerSeenMsAgo"] != nil || st["peer"] != "alice" {
t.Fatalf("mac while ringing: %v", st)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", phone); st["peerSeenMsAgo"] == nil || st["movedTo"] != nil {
t.Fatalf("phone after answering: %v", st)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", mac); st["peerSeenMsAgo"] == nil || st["movedTo"] != nil {
t.Fatalf("mac after the answer: %v", st)
}
if status, _ := do(t, srv, "alice", "POST", path+"/move", `{"device":"alice-ipad","name":"iPad"}`); status != 409 {
t.Errorf("moving a self call: %d", status)
}
do(t, srv, "alice", "POST", path+"/hangup", `{"device":"alice-phone"}`)
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", mac); st["peerEnded"] != true || st["endedBy"] != "alice" {
t.Fatalf("mac after the phone hung up: %v", st)
}
if _, st := do(t, srv, "alice", "POST", path+"/heartbeat", phone); st["peerEnded"] != false {
t.Fatalf("phone after hanging up: %v", st)
}
}
func TestHangUpTellsThePeer(t *testing.T) {
srv := newTestApp(t)
_, call := do(t, srv, "alice", "POST", "/call", `{"to":"bob"}`)
id := call["meetingId"].(string)
do(t, srv, "bob", "POST", "/calls/"+id+"/hangup", "")
if _, st := do(t, srv, "alice", "GET", "/calls/"+id, ""); st["peerEnded"] != true || st["endedBy"] != "bob" {
t.Fatalf("caller after bob declined: %v", st)
}
if _, st := do(t, srv, "bob", "GET", "/calls/"+id, ""); st["peerEnded"] != false {
t.Fatalf("bob after declining: %v", st)
}
}
func TestCrossedCallsShareOneRoom(t *testing.T) {
srv := newTestApp(t)
_, first := do(t, srv, "alice", "POST", "/call", `{"to":"bob","jitsiUrl":"https://jitsi.example.org","device":"alice-mac","name":"Mac"}`)
id := first["meetingId"].(string)
do(t, srv, "bob", "GET", "/poll?device=bob-mac", "")
_, second := do(t, srv, "bob", "POST", "/call", `{"to":"alice","device":"bob-mac","name":"Mac"}`)
if second["joined"] != true || second["meetingId"] != id || second["jitsiUrl"] != "https://jitsi.example.org" {
t.Fatalf("crossed call: %v", second)
}
if _, poll := do(t, srv, "alice", "GET", "/poll?device=alice-phone", ""); len(poll["invites"].([]any)) != 0 {
t.Fatalf("alice rings: %v", poll)
}
if _, poll := do(t, srv, "bob", "GET", "/poll?device=bob-phone", ""); len(poll["invites"].([]any)) != 0 {
t.Fatalf("bob's iPhone rings: %v", poll)
}
if _, st := do(t, srv, "alice", "POST", "/calls/"+id+"/heartbeat", `{"device":"alice-mac","name":"Mac"}`); st["peerSeenMsAgo"] == nil {
t.Fatalf("alice doesn't see the answer: %v", st)
}
if _, st := do(t, srv, "bob", "GET", "/calls/"+id+"?device=bob-phone", ""); st["answeredOn"] != "Mac" {
t.Fatalf("bob's iPhone: %v", st)
}
if _, st := do(t, srv, "bob", "POST", "/calls/"+id+"/heartbeat", `{"device":"bob-mac","name":"Mac"}`); st["peer"] != "alice" || st["movedTo"] != nil {
t.Fatalf("bob in the call: %v", st)
}
}
func TestCallingBackAfterAHangUpRings(t *testing.T) {
srv := newTestApp(t)
_, first := do(t, srv, "alice", "POST", "/call", `{"to":"bob","device":"alice-mac","name":"Mac"}`)
id := first["meetingId"].(string)
do(t, srv, "alice", "POST", "/calls/"+id+"/hangup", `{"device":"alice-mac"}`)
_, second := do(t, srv, "bob", "POST", "/call", `{"to":"alice","device":"bob-mac","name":"Mac"}`)
if second["joined"] != nil || second["meetingId"] == id {
t.Fatalf("joined a hung-up call: %v", second)
}
if _, poll := do(t, srv, "alice", "GET", "/poll?device=alice-phone", ""); len(poll["invites"].([]any)) != 1 {
t.Fatalf("alice doesn't ring: %v", poll)
}
}