File: draft_test.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (149 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tests

import (
	"regexp"
	"testing"
	"time"

	"github.com/ProtonMail/gluon/imap"
	"github.com/stretchr/testify/require"
)

func TestDraftScenario(t *testing.T) {
	// Simulate a draft update issued from the connector, which involves deleting the original message in drafts
	// and replacing it with a new one.
	runOneToOneTestWithAuth(t, defaultServerOptions(t), func(c *testConnection, s *testSession) {
		mailboxID := s.mailboxCreated("user", []string{"Drafts"})

		c.C("A002 SELECT Drafts").OK("A002")

		messageID := s.messageCreated("user", mailboxID, []byte("To: 3@3.pm"), time.Now())

		c.C("A002 NOOP")
		c.S("* 1 EXISTS")
		c.S("* 1 RECENT")
		c.OK("A002")

		c.C("A003 FETCH 1 (BODY.PEEK[HEADER.FIELDS (To)])")
		c.S("* 1 FETCH (BODY[HEADER.FIELDS (TO)] {10}\r\nTo: 3@3.pm)")
		c.OK("A003")

		s.messageDeleted("user", messageID)
		s.messageCreated("user", mailboxID, []byte("To: 4@4.pm"), time.Now())
		s.flush("user")

		c.C("A004 NOOP")
		c.S("* 1 EXPUNGE")
		c.S("* 1 EXISTS")
		c.S("* 1 RECENT")
		c.OK("A004")

		c.C("A005 FETCH 1 (BODY.PEEK[HEADER.FIELDS (To)])")
		c.S("* 1 FETCH (BODY[HEADER.FIELDS (TO)] {10}\r\nTo: 4@4.pm)")
		c.OK("A005")
	})
}

func TestDraftSavedAgain(t *testing.T) {
	// Email client can save same message i.e.: it will delete old draft
	// and append one with same content.
	runOneToOneTestWithAuth(t, defaultServerOptions(t), func(c *testConnection, s *testSession) {
		mailboxID := s.mailboxCreatedWithAttributes("user", []string{"Drafts"}, imap.NewFlagSet(imap.AttrDrafts))

		c.C("A002 SELECT Drafts").OK("A002")

		_ = s.messageCreated("user", mailboxID, []byte(buildRFC5322TestLiteral("To: 3@3.pm")), time.Now())
		s.flush("user")

		c.C("A002 NOOP")
		c.S("* 1 EXISTS")
		c.S("* 1 RECENT")
		c.OK("A002")

		c.C("A003 FETCH 1 (BODY.PEEK[])")
		raw := c.read()

		fetch := regexp.MustCompile(
			regexp.QuoteMeta("* 1 FETCH (BODY[] {134}\r\nX-Pm-Gluon-Id: ") +
				".*" +
				regexp.QuoteMeta("\r\n"+buildRFC5322TestLiteral("To: 3@3.pm")+")\r\n"),
		)

		require.Regexp(t, fetch, string(raw))
		c.OK("A003")

		// Expunge+Append same message
		{
			c.C(`A004 STORE 1 +FLAGS (\Deleted)`).OK("A004")
			c.C(`A005 EXPUNGE`).OK("A005")

			c.doAppend(
				"Drafts",
				string(raw[25:len(raw)-3]),
			).expect("OK")

			c.C("A004 FETCH 1 (BODY.PEEK[])")
			raw := c.read()
			require.Regexp(t, fetch, string(raw))
			c.OK("A004")
		}

		newBody2 := string(raw[25:]) + "\r\nThis is body\r\n"
		fetchUpdated2 := regexp.MustCompile(
			regexp.QuoteMeta("* 1 FETCH (BODY[] {153}\r\nX-Pm-Gluon-Id: ") +
				".*" +
				regexp.QuoteMeta("\r\n"+buildRFC5322TestLiteral("To: 3@3.pm)")+"\r\n\r\nThis is body\r\n)"),
		)
		// Expunge+Append different message
		{
			c.C(`A004 STORE 1 +FLAGS (\Deleted)`).OK("A004")
			c.C(`A005 EXPUNGE`).OK("A005")

			c.doAppend(
				"Drafts",
				newBody2,
			).expect("OK")

			c.C("A004 FETCH 1 (BODY.PEEK[])")
			raw := c.read()
			require.Regexp(t, fetchUpdated2, string(raw))
			c.OK("A004")
		}

		// Append + Expunge different message
		newBody3 := newBody2 + "hello\r\n"
		fetchUpdated3 := regexp.MustCompile(
			regexp.QuoteMeta("* ") +
				"\\d" +
				regexp.QuoteMeta(" FETCH (BODY[] {160}\r\nX-Pm-Gluon-Id: ") +
				".*" +
				regexp.QuoteMeta("\r\n"+buildRFC5322TestLiteral("To: 3@3.pm)")+"\r\n\r\nThis is body\r\nhello\r\n)"),
		)
		{

			c.doAppend(
				"Drafts",
				newBody3,
			).expect("OK")

			c.C("A004 FETCH 1 (BODY.PEEK[])")
			raw := c.read()
			require.Regexp(t, fetchUpdated2, string(raw))
			c.OK("A004")

			c.C("A005 FETCH 2 (BODY.PEEK[])")
			raw = c.read()
			require.Regexp(t, fetchUpdated3, string(raw))
			c.OK("A005")

			c.C(`A004 STORE 1 +FLAGS (\Deleted)`).OK("A004")
			c.C(`A005 EXPUNGE`).OK("A005")

			c.C("A006 FETCH 1 (BODY.PEEK[])")
			raw = c.read()
			require.Regexp(t, fetchUpdated3, string(raw))
			c.OK("A006")

		}
	})
}