95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package jmap_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"mailgoserver/internal/jmap"
|
|
)
|
|
|
|
func TestJMAPIdentityGetReflectsSendAsAliases(t *testing.T) {
|
|
srv, database, _, email, password, mailboxID := newTestJMAPServer(t)
|
|
|
|
mbox, err := database.GetMailboxByID(mailboxID)
|
|
if err != nil || mbox == nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := database.CreateAlias(mailboxID, "sales@example.com", mbox.DomainID, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
resp := doJMAP(t, srv, email, password, jmap.Request{
|
|
MethodCalls: []jmap.Invocation{{Name: "Identity/get", Args: json.RawMessage(`{}`), ID: "c1"}},
|
|
})
|
|
var result struct {
|
|
List []struct {
|
|
ID string `json:"id"`
|
|
Email string `json:"email"`
|
|
} `json:"list"`
|
|
}
|
|
if err := json.Unmarshal(resp.MethodResponses[0].Args, &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
emails := map[string]bool{}
|
|
for _, id := range result.List {
|
|
emails[id.Email] = true
|
|
}
|
|
if !emails[email] {
|
|
t.Errorf("expected the mailbox's own address %q among identities, got %+v", email, result.List)
|
|
}
|
|
if !emails["sales@example.com"] {
|
|
t.Errorf("expected the send-as alias among identities, got %+v", result.List)
|
|
}
|
|
}
|
|
|
|
func TestJMAPIdentitySetUpdatesSignature(t *testing.T) {
|
|
srv, _, _, email, password, _ := newTestJMAPServer(t)
|
|
|
|
getResp := doJMAP(t, srv, email, password, jmap.Request{
|
|
MethodCalls: []jmap.Invocation{{Name: "Identity/get", Args: json.RawMessage(`{}`), ID: "c1"}},
|
|
})
|
|
var getResult struct {
|
|
List []struct{ ID string } `json:"list"`
|
|
}
|
|
if err := json.Unmarshal(getResp.MethodResponses[0].Args, &getResult); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(getResult.List) == 0 {
|
|
t.Fatal("expected at least the primary identity")
|
|
}
|
|
primaryID := getResult.List[0].ID
|
|
|
|
setResp := doJMAP(t, srv, email, password, jmap.Request{
|
|
MethodCalls: []jmap.Invocation{{
|
|
Name: "Identity/set",
|
|
Args: json.RawMessage(`{"update":{"` + primaryID + `":{"htmlSignature":"<p>Best,<br>Alice</p>"}}}`),
|
|
ID: "c1",
|
|
}},
|
|
})
|
|
var setResult struct {
|
|
Updated map[string]any `json:"updated"`
|
|
NotUpdated map[string]any `json:"notUpdated"`
|
|
}
|
|
if err := json.Unmarshal(setResp.MethodResponses[0].Args, &setResult); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(setResult.NotUpdated) != 0 {
|
|
t.Fatalf("expected the signature update to succeed, got notUpdated %+v", setResult.NotUpdated)
|
|
}
|
|
|
|
getResp2 := doJMAP(t, srv, email, password, jmap.Request{
|
|
MethodCalls: []jmap.Invocation{{Name: "Identity/get", Args: json.RawMessage(`{"ids":["` + primaryID + `"]}`), ID: "c2"}},
|
|
})
|
|
var getResult2 struct {
|
|
List []struct {
|
|
HTMLSignature string `json:"htmlSignature"`
|
|
} `json:"list"`
|
|
}
|
|
if err := json.Unmarshal(getResp2.MethodResponses[0].Args, &getResult2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(getResult2.List) != 1 || getResult2.List[0].HTMLSignature != "<p>Best,<br>Alice</p>" {
|
|
t.Fatalf("expected the updated signature to be reflected, got %+v", getResult2.List)
|
|
}
|
|
}
|