Blob


1 package apub
3 import (
4 "bytes"
5 "net/mail"
6 "os"
7 "testing"
8 )
10 func TestMailAddress(t *testing.T) {
11 tests := []struct {
12 name string
13 from string
14 followers string
15 }{
16 {
17 "testdata/actor/mastodon.json",
18 `"Oliver Lowe" <otl@hachyderm.io>`,
19 `"Oliver Lowe (followers)" <otl+followers@hachyderm.io>`,
20 },
21 {
22 "testdata/actor/akkoma.json",
23 `"Kari'boka" <kariboka@social.harpia.red>`,
24 `"Kari'boka (followers)" <kariboka+followers@social.harpia.red>`,
25 },
26 {
27 "testdata/actor/lemmy.json",
28 "<Spotlight7573@lemmy.world>",
29 "<@>", // empty mail.Address
30 },
31 }
32 for _, tt := range tests {
33 f, err := os.Open(tt.name)
34 if err != nil {
35 t.Error(err)
36 continue
37 }
38 defer f.Close()
39 actor, err := DecodeActor(f)
40 if err != nil {
41 t.Errorf("%s: decode actor: %v", tt.name, err)
42 continue
43 }
44 if actor.Address().String() != tt.from {
45 t.Errorf("%s: from address: want %s, got %s", tt.name, tt.from, actor.Address().String())
46 }
47 got := actor.FollowersAddress().String()
48 if got != tt.followers {
49 t.Errorf("%s: followers address: want %s, got %s", tt.name, tt.followers, got)
50 }
51 }
52 }
54 func TestMarshalMail(t *testing.T) {
55 var notes []string = []string{
56 "testdata/note/akkoma.json",
57 "testdata/note/lemmy.json",
58 "testdata/note/mastodon.json",
59 "testdata/page.json",
60 }
61 for _, name := range notes {
62 f, err := os.Open(name)
63 if err != nil {
64 t.Error(err)
65 continue
66 }
67 defer f.Close()
68 a, err := Decode(f)
69 if err != nil {
70 t.Errorf("%s: decode activity: %v", name, err)
71 continue
72 }
73 b, err := MarshalMail(a)
74 if err != nil {
75 t.Errorf("%s: marshal to mail message: %v", name, err)
76 continue
77 }
78 if _, err := mail.ReadMessage(bytes.NewReader(b)); err != nil {
79 t.Errorf("%s: read back message from marshalled activity: %v", name, err)
80 }
81 }
82 }
84 func TestUnmarshalMail(t *testing.T) {
85 f, err := os.Open("testdata/outbound.eml")
86 if err != nil {
87 t.Fatal(err)
88 }
89 defer f.Close()
90 msg, err := mail.ReadMessage(f)
91 if err != nil {
92 t.Fatal(err)
93 }
94 if testing.Short() {
95 t.Skip("skipping network calls to unmarshal mail message to Activity")
96 }
97 if _, err := UnmarshalMail(msg); err != nil {
98 t.Fatal(err)
99 }