-
Notifications
You must be signed in to change notification settings - Fork 18
/
helper_test.go
90 lines (69 loc) · 2.29 KB
/
helper_test.go
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
package proton_test
import (
"context"
"fmt"
"runtime"
"testing"
"github.com/ProtonMail/gluon/async"
"github.com/ProtonMail/go-proton-api"
"github.com/bradenaw/juniper/iterator"
"github.com/bradenaw/juniper/stream"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func createTestMessages(t *testing.T, c *proton.Client, pass string, count int) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
user, err := c.GetUser(ctx)
require.NoError(t, err)
addr, err := c.GetAddresses(ctx)
require.NoError(t, err)
salt, err := c.GetSalts(ctx)
require.NoError(t, err)
keyPass, err := salt.SaltForKey([]byte(pass), user.Keys.Primary().ID)
require.NoError(t, err)
_, addrKRs, err := proton.Unlock(user, addr, keyPass, async.NoopPanicHandler{})
require.NoError(t, err)
req := iterator.Collect(iterator.Map(iterator.Counter(count), func(i int) proton.ImportReq {
return proton.ImportReq{
Metadata: proton.ImportMetadata{
AddressID: addr[0].ID,
Flags: proton.MessageFlagReceived,
Unread: true,
},
Message: []byte(fmt.Sprintf("From: [email protected]\r\nReceiver: [email protected]\r\nSubject: %v\r\n\r\nHello World!", uuid.New())),
}
}))
str, err := c.ImportMessages(ctx, addrKRs[addr[0].ID], runtime.NumCPU(), runtime.NumCPU(), req...)
require.NoError(t, err)
res, err := stream.Collect(ctx, str)
require.NoError(t, err)
for _, res := range res {
require.Equal(t, proton.SuccessCode, res.Code)
}
}
func importMessage(t *testing.T, c *proton.Client, ctx context.Context, pass string, literal string) (stream.Stream[proton.ImportRes], error) {
t.Helper()
user, err := c.GetUser(ctx)
require.NoError(t, err)
addr, err := c.GetAddresses(ctx)
require.NoError(t, err)
salt, err := c.GetSalts(ctx)
require.NoError(t, err)
keyPass, err := salt.SaltForKey([]byte(pass), user.Keys.Primary().ID)
require.NoError(t, err)
_, addrKRs, err := proton.Unlock(user, addr, keyPass, async.NoopPanicHandler{})
require.NoError(t, err)
req := []proton.ImportReq{
{
Metadata: proton.ImportMetadata{
AddressID: addr[0].ID,
Flags: proton.MessageFlagReceived,
Unread: true,
},
Message: []byte(literal),
},
}
return c.ImportMessages(ctx, addrKRs[addr[0].ID], runtime.NumCPU(), runtime.NumCPU(), req...)
}