-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathmail_unpack_real_e2e_test.go
More file actions
165 lines (154 loc) · 5.76 KB
/
Copy pathmail_unpack_real_e2e_test.go
File metadata and controls
165 lines (154 loc) · 5.76 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"testing"
"time"
"github.com/daptin/daptin/server/auth"
)
const mailUnpackE2ESchema = `
Actions:
- Name: unpack_e2e
Label: Unpack mail E2E
OnType: mail
InstanceOptional: false
RequestSubjectRelations:
- mail
Permission: 2097152
OutFields:
- Type: mail.unpack
Method: EXECUTE
Reference: unpacked
SkipInResponse: true
Attributes:
mail: "~subject.mail"
- Type: mail.unpack
Method: ACTIONRESPONSE
Attributes:
preferred_text_body: "~unpacked.preferred_text_body"
optional_html_body: "~unpacked.optional_html_body"
parts: "~unpacked.parts"
`
const mailUnpackCloudStoreE2ESchema = mailUnpackE2ESchema + `
Tables:
- TableName: mail
Columns:
- Name: mail
ColumnName: mail
DataType: blob
ColumnType: gzip
IsForeignKey: true
ForeignKeyData:
DataSource: cloud_store
Namespace: localstore
KeyName: mail-messages
`
func TestMailUnpackActionCompositionRealE2E(t *testing.T) {
if os.Getenv("DAPTIN_REAL_E2E") != "1" {
t.Skip("set DAPTIN_REAL_E2E=1 to run the mail.unpack action composition e2e")
}
t.Run("database-backed mail", func(t *testing.T) {
testMailUnpackActionCompositionRealE2E(t, mailUnpackE2ESchema, false, true)
})
t.Run("cloud-store-backed mail", func(t *testing.T) {
testMailUnpackActionCompositionRealE2E(t, mailUnpackCloudStoreE2ESchema, true, false)
})
}
func testMailUnpackActionCompositionRealE2E(t *testing.T, schema string, cloudStore bool, verifyDenial bool) {
t.Helper()
usedPorts := make(map[int]bool, 2)
port := freeTransportE2EPort(t, usedPorts)
httpsPort := freeTransportE2EPort(t, usedPorts)
baseURL := fmt.Sprintf("http://127.0.0.1:%d", port)
daptinProcess := startTransportE2EDaptin(t, port, httpsPort, baseURL, transportE2EDaptinOptions{schema: schema})
defer daptinProcess.stopProcess()
client := &http.Client{Timeout: 20 * time.Second}
adminToken := accessGroupsE2ESignupSigninAdmin(t, client, baseURL)
var otherToken string
if verifyDenial {
otherToken = accessGroupsE2ESignupSigninUser(t, client, baseURL, adminToken, "mail-unpack-other")
}
mailWorldID := accessGroupsE2EFindResourceID(t, client, baseURL, adminToken, "world", "table_name", "mail")
accessGroupsE2EAssertStatus(t, client, http.MethodPatch, baseURL+"/api/world/"+mailWorldID, adminToken,
accessGroupsE2ERecordPayload("world", mailWorldID, map[string]interface{}{
"permission": int64(auth.AuthenticatedExecute),
}), http.StatusOK)
mailServerID := accessGroupsE2ECreateRecord(t, client, baseURL, adminToken, "mail_server", map[string]interface{}{
"hostname": "mail-unpack-e2e.test",
"is_enabled": false,
"listen_interface": "127.0.0.1:0",
"max_size": 10000,
"max_clients": 1,
"xclient_on": false,
"always_on_tls": false,
"authentication_required": true,
})
mailAccountID := accessGroupsE2ECreateRecord(t, client, baseURL, adminToken, "mail_account", map[string]interface{}{
"username": "mail-unpack@mail-unpack-e2e.test",
"password": "testpass123",
"password_md5": "testpass123",
"mail_server_id": mailServerID,
})
mailBoxID := accessGroupsE2ECreateRecord(t, client, baseURL, adminToken, "mail_box", map[string]interface{}{
"name": "INBOX",
"subscribed": true,
"uidvalidity": 1,
"nextuid": 2,
"attributes": "",
"flags": "\\Recent",
"permanent_flags": "\\Seen",
"mail_account_id": mailAccountID,
})
raw := []byte("From: sender@example.com\r\nContent-Type: text/plain; charset=utf-8\r\n\r\ncomposed mail body")
mailValue := interface{}(base64.StdEncoding.EncodeToString(raw))
if cloudStore {
mailValue = []interface{}{map[string]interface{}{
"name": "message.eml",
"file": "data:message/rfc822;base64," + base64.StdEncoding.EncodeToString(raw),
"type": "message/rfc822",
}}
}
mailID := accessGroupsE2ECreateRecord(t, client, baseURL, adminToken, "mail", map[string]interface{}{
"message_id": "mail-unpack-e2e@test.local",
"mail_id": "mail-unpack-e2e",
"from_address": "sender@example.com",
"internal_date": time.Now().UTC().Format(time.RFC3339),
"to_address": "receiver@example.com",
"reply_to_address": "sender@example.com",
"sender_address": "sender@example.com",
"subject": "Mail unpack E2E",
"body": "composed mail body",
"mail": mailValue,
"spam_score": 0,
"hash": "mail-unpack-e2e",
"content_type": "text/plain; charset=utf-8",
"recipient": "receiver@example.com",
"has_attachment": false,
"ip_addr": "127.0.0.1",
"return_path": "sender@example.com",
"is_tls": false,
"seen": false,
"recent": true,
"deleted": false,
"uid": 1,
"spam": false,
"size": len(raw),
"flags": "\\Recent",
"mail_box_id": mailBoxID,
"permission": int64(auth.UserRead | auth.UserExecute),
})
response := accessGroupsE2ERequestJSON(t, client, http.MethodPost,
baseURL+"/action/mail/unpack_e2e?mail_id="+mailID, adminToken,
map[string]interface{}{"attributes": map[string]interface{}{}}, http.StatusOK)
body, ok := accessGroupsE2EFindString(response, "preferred_text_body")
if !ok || body != "composed mail body" {
t.Fatalf("mail.unpack response body = %q, found=%v: %#v", body, ok, response)
}
if verifyDenial {
accessGroupsE2EAssertStatus(t, client, http.MethodPost,
baseURL+"/action/mail/unpack_e2e?mail_id="+mailID, otherToken,
map[string]interface{}{"attributes": map[string]interface{}{}}, http.StatusBadRequest)
}
}