1package imapserver
2
3import (
4 "testing"
5
6 "github.com/mjl-/mox/imapclient"
7)
8
9// todo: check that UIDValidity is indeed updated properly.
10func TestRename(t *testing.T) {
11 tc := start(t)
12 defer tc.close()
13
14 tc2 := startNoSwitchboard(t)
15 defer tc2.close()
16
17 tc.client.Login("mjl@mox.example", "testtest")
18 tc2.client.Login("mjl@mox.example", "testtest")
19
20 tc.transactf("bad", "rename") // Missing parameters.
21 tc.transactf("bad", "rename x") // Missing destination.
22 tc.transactf("bad", "rename x y ") // Leftover data.
23
24 tc.transactf("no", "rename doesnotexist newbox") // Does not exist.
25 tc.xcode("NONEXISTENT") // ../rfc/9051:5140
26 tc.transactf("no", `rename "Sent" "Trash"`) // Already exists.
27 tc.xcode("ALREADYEXISTS")
28
29 tc.client.Create("x")
30 tc.client.Subscribe("sub")
31 tc.client.Create("a/b/c")
32 tc.client.Subscribe("x/y/c") // For later rename, but not affected by rename of x.
33 tc2.transactf("ok", "noop") // Drain.
34
35 tc.transactf("ok", "rename x z")
36 tc2.transactf("ok", "noop")
37 tc2.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "z"})
38
39 // OldName is only set for IMAP4rev2 or NOTIFY.
40 tc2.client.Enable("IMAP4rev2")
41 tc.transactf("ok", "rename z y")
42 tc2.transactf("ok", "noop")
43 tc2.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "y", OldName: "z"})
44
45 // Rename to a mailbox that only exists in database as subscribed.
46 tc.transactf("ok", "rename y sub")
47 tc2.transactf("ok", "noop")
48 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "sub", OldName: "y"})
49
50 // Cannot rename a child to a parent. It already exists.
51 tc.transactf("no", "rename a/b/c a/b")
52 tc.xcode("ALREADYEXISTS")
53 tc.transactf("no", "rename a/b a")
54 tc.xcode("ALREADYEXISTS")
55
56 tc2.transactf("ok", "noop") // Drain.
57 tc.transactf("ok", "rename a/b x/y") // This will cause new parent "x" to be created, and a/b and a/b/c to be renamed.
58 tc2.transactf("ok", "noop")
59 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "x"}, imapclient.UntaggedList{Separator: '/', Mailbox: "x/y", OldName: "a/b"}, imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "x/y/c", OldName: "a/b/c"})
60
61 tc.client.Create("k/l")
62 tc.transactf("ok", "rename k/l k/l/m") // With "l" renamed, a new "k" will be created.
63 tc.transactf("ok", `list "" "k*" return (subscribed)`)
64 tc.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "k"}, imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "k/l"}, imapclient.UntaggedList{Separator: '/', Mailbox: "k/l/m"})
65
66 // Similar, but with missing parent not subscribed.
67 tc.transactf("ok", "rename k/l/m k/ll")
68 tc.transactf("ok", "delete k/l")
69 tc.transactf("ok", "rename k/ll k/l") // Restored to previous mailboxes now.
70 tc.client.Unsubscribe("k")
71 tc.transactf("ok", "rename k/l k/l/m") // With "l" renamed, a new "k" will be created.
72 tc.transactf("ok", `list "" "k*" return (subscribed)`)
73 tc.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "k"}, imapclient.UntaggedList{Flags: []string{"\\Subscribed"}, Separator: '/', Mailbox: "k/l"}, imapclient.UntaggedList{Separator: '/', Mailbox: "k/l/m"})
74
75 // Renaming inbox keeps inbox in existence, moves messages, and does not rename children.
76 tc.transactf("ok", "create inbox/a")
77 // To check if UIDs are renumbered properly, we add UIDs 1 and 2. Expunge 1,
78 // keeping only 2. Then rename the inbox, which should renumber UID 2 in the old
79 // inbox to UID 1 in the newly created mailbox.
80 tc.transactf("ok", "append inbox (\\deleted) \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
81 tc.transactf("ok", "append inbox (label1) \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
82 tc.transactf("ok", `select inbox`)
83 tc.transactf("ok", "expunge")
84 tc.transactf("ok", "rename inbox minbox")
85 tc.transactf("ok", `list "" (inbox inbox/a minbox)`)
86 tc.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "Inbox"}, imapclient.UntaggedList{Separator: '/', Mailbox: "Inbox/a"}, imapclient.UntaggedList{Separator: '/', Mailbox: "minbox"})
87 tc.transactf("ok", `select minbox`)
88 tc.transactf("ok", `uid fetch 1:* flags`)
89 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags{"label1"}}})
90
91 // Renaming to new hiearchy that does not have any subscribes.
92 tc.transactf("ok", "rename minbox w/w")
93 tc.transactf("ok", `list "" "w*"`)
94 tc.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "w"}, imapclient.UntaggedList{Separator: '/', Mailbox: "w/w"})
95
96 // todo: test create+delete+rename of/to a name results in a higher uidvalidity.
97}
98