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 y")
36 tc2.transactf("ok", "noop")
37 tc2.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "y", OldName: "x"})
38
39 // Rename to a mailbox that only exists in database as subscribed.
40 tc.transactf("ok", "rename y sub")
41 tc2.transactf("ok", "noop")
42 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "sub", OldName: "y"})
43
44 // Cannot rename a child to a parent. It already exists.
45 tc.transactf("no", "rename a/b/c a/b")
46 tc.xcode("ALREADYEXISTS")
47 tc.transactf("no", "rename a/b a")
48 tc.xcode("ALREADYEXISTS")
49
50 tc2.transactf("ok", "noop") // Drain.
51 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.
52 tc2.transactf("ok", "noop")
53 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"})
54
55 tc.client.Create("k/l")
56 tc.transactf("ok", "rename k/l k/l/m") // With "l" renamed, a new "k" will be created.
57 tc.transactf("ok", `list "" "k*" return (subscribed)`)
58 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"})
59
60 // Similar, but with missing parent not subscribed.
61 tc.transactf("ok", "rename k/l/m k/ll")
62 tc.transactf("ok", "delete k/l")
63 tc.transactf("ok", "rename k/ll k/l") // Restored to previous mailboxes now.
64 tc.client.Unsubscribe("k")
65 tc.transactf("ok", "rename k/l k/l/m") // With "l" renamed, a new "k" will be created.
66 tc.transactf("ok", `list "" "k*" return (subscribed)`)
67 tc.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "k"}, imapclient.UntaggedList{Flags: []string{"\\Subscribed"}, Separator: '/', Mailbox: "k/l"}, imapclient.UntaggedList{Separator: '/', Mailbox: "k/l/m"})
68
69 // Renaming inbox keeps inbox in existence, moves messages, and does not rename children.
70 tc.transactf("ok", "create inbox/a")
71 // To check if UIDs are renumbered properly, we add UIDs 1 and 2. Expunge 1,
72 // keeping only 2. Then rename the inbox, which should renumber UID 2 in the old
73 // inbox to UID 1 in the newly created mailbox.
74 tc.transactf("ok", "append inbox (\\deleted) \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
75 tc.transactf("ok", "append inbox (label1) \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
76 tc.transactf("ok", `select inbox`)
77 tc.transactf("ok", "expunge")
78 tc.transactf("ok", "rename inbox minbox")
79 tc.transactf("ok", `list "" (inbox inbox/a minbox)`)
80 tc.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "Inbox"}, imapclient.UntaggedList{Separator: '/', Mailbox: "Inbox/a"}, imapclient.UntaggedList{Separator: '/', Mailbox: "minbox"})
81 tc.transactf("ok", `select minbox`)
82 tc.transactf("ok", `uid fetch 1:* flags`)
83 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags{"label1"}}})
84
85 // Renaming to new hiearchy that does not have any subscribes.
86 tc.transactf("ok", "rename minbox w/w")
87 tc.transactf("ok", `list "" "w*"`)
88 tc.xuntagged(imapclient.UntaggedList{Separator: '/', Mailbox: "w"}, imapclient.UntaggedList{Separator: '/', Mailbox: "w/w"})
89
90 // todo: test create+delete+rename of/to a name results in a higher uidvalidity.
91}
92