80 lines
2.7 KiB
Go
80 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"github.com/google/uuid"
|
|
"net/http/httptest"
|
|
"pageswiki/internal/store"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestContactsPreserveImportAndIsolation(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
raw := "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:urn:test:person\r\nFN:田中\r\nEMAIL;TYPE=work:tanaka@example.invalid\r\nTEL;TYPE=cell:1234\r\nX-PRIVATE:keep\\,this\r\nPHOTO:https://example.invalid/do-not-fetch\r\nEND:VCARD\r\n"
|
|
c, e := s.store.SaveContact(ctx, domain, actor, store.Contact{UUID: uuid.NewString(), VCard: raw}, true)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
repeat, e := s.store.SaveContact(ctx, domain, actor, store.Contact{UUID: uuid.NewString(), VCard: raw}, true)
|
|
if e != nil || repeat.UUID != c.UUID {
|
|
t.Fatal("not idempotent", e)
|
|
}
|
|
c.Name = "変更"
|
|
c, e = s.store.SaveContact(ctx, domain, actor, c, false)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, preserved := range []string{"EMAIL;TYPE=work:", "TEL;TYPE=cell:", "X-PRIVATE:keep\\,this", "PHOTO:https:"} {
|
|
if !strings.Contains(c.VCard, preserved) {
|
|
t.Fatal("lost", preserved)
|
|
}
|
|
}
|
|
old := c
|
|
old.Version--
|
|
if _, e = s.store.SaveContact(ctx, domain, actor, old, false); e != store.ErrTaskConflict {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.Contact(ctx, domain, uuid.NewString(), c.UUID); e == nil {
|
|
t.Fatal("other user read")
|
|
}
|
|
req := httptest.NewRequest("GET", "http://localhost"+contactsAPI+"items?q=tanaka", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
s.handleContacts(w, req)
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "変更") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
c.Deleted = true
|
|
if _, e = s.store.SaveContact(ctx, domain, actor, c, false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
items, _, e := s.store.Contacts(ctx, domain, actor, "", "")
|
|
if e != nil || len(items) != 0 {
|
|
t.Fatal(items, e)
|
|
}
|
|
if e = s.setSystemExtensionEnabled(domain, "organizer.contacts", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.store.Contact(ctx, domain, actor, c.UUID); e != store.ErrAccessDenied {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
func TestContactFormatValidation(t *testing.T) {
|
|
for _, raw := range []string{"BEGIN:VCARD\nVERSION:2.1\nFN:x\nEND:VCARD\n", "BEGIN:VCARD\nVERSION:4.0\nFN;ENCODING=QUOTED-PRINTABLE:x\nEND:VCARD\n", "BEGIN:VCARD\nVERSION:4.0\nFN:x\nmalformed\nEND:VCARD\n"} {
|
|
if _, e := store.ParseContact(raw); e == nil {
|
|
t.Fatal("accepted malformed/legacy")
|
|
}
|
|
}
|
|
raw := "BEGIN:VCARD\r\nVERSION:4.0\r\nFN:" + strings.Repeat("名", 90) + "\r\nNOTE:one\\ntwo\r\nEND:VCARD\r\n"
|
|
c, e := store.ParseContact(raw)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
again, e := store.ParseContact(c.VCard)
|
|
if e != nil || again.Name != c.Name || again.Notes != "one\ntwo" {
|
|
t.Fatal(again, e)
|
|
}
|
|
}
|