File: run_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 (139 lines) | stat: -rw-r--r-- 4,945 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
// Package tests defines "integration" tests of the library that run on top of a dummy remote.
package tests

import (
	"testing"

	"github.com/ProtonMail/gluon/imap"
	"github.com/emersion/go-imap/client"
	_ "github.com/mattn/go-sqlite3"
	"github.com/stretchr/testify/require"
)

// runOneToOneTest runs a test with one account and one connection.
func runOneToOneTest(tb testing.TB, options *serverOptions, tests func(*testConnection, *testSession)) {
	runTest(tb, options, []int{1}, func(c map[int]*testConnection, s *testSession) {
		tests(c[1], s)
	})
}

// runOneToOneTestWithAuth runs a test with one account and one connection. The connection is logged in.
func runOneToOneTestWithAuth(tb testing.TB, options *serverOptions, tests func(*testConnection, *testSession)) {
	runOneToOneTest(tb, options, func(c *testConnection, s *testSession) {
		tests(c.Login(options.defaultUsername(), options.defaultPassword()), s)
	})
}

// runOneToOneTestWithData runs a test with one account and one connection. A mailbox is created with test data.
func runOneToOneTestWithData(
	tb testing.TB,
	options *serverOptions,
	tests func(*testConnection, *testSession, string, imap.MailboxID),
) {
	runOneToOneTestWithAuth(tb, options, func(c *testConnection, s *testSession) {
		withData(s, options.defaultUsername(), func(mbox string, mboxID imap.MailboxID) {
			withTag(func(tag string) { c.Cf("%v select %v", tag, mbox).OK(tag) })
			tests(c, s, mbox, mboxID)
		})
	})
}

// runManyToOneTest runs a test with one account and multiple connections.
func runManyToOneTest(
	tb testing.TB,
	options *serverOptions,
	connIDs []int,
	tests func(map[int]*testConnection, *testSession),
) {
	runTest(tb, options, connIDs, func(c map[int]*testConnection, s *testSession) {
		tests(c, s)
	})
}

// runManyToOneTestWithAuth runs a test with one account and multiple connections. Each connection is logged in.
func runManyToOneTestWithAuth(
	tb testing.TB,
	options *serverOptions,
	connIDs []int, tests func(map[int]*testConnection, *testSession),
) {
	runManyToOneTest(tb, options, connIDs, func(c map[int]*testConnection, s *testSession) {
		for _, c := range c {
			c.Login(options.defaultUsername(), options.defaultPassword())
		}

		tests(c, s)
	})
}

// runManyToOneTestWithData runs a test with one account and multiple connections. Apply mailbox is created with test data.
func runManyToOneTestWithData(
	tb testing.TB,
	options *serverOptions,
	connIDs []int,
	tests func(map[int]*testConnection, *testSession, string, imap.MailboxID),
) {
	runManyToOneTestWithAuth(tb, options, connIDs, func(c map[int]*testConnection, s *testSession) {
		withData(s, options.defaultUsername(), func(mbox string, mboxID imap.MailboxID) {
			for _, c := range c {
				withTag(func(tag string) { c.Cf("%v select %v", tag, mbox).OK(tag) })
			}

			tests(c, s, mbox, mboxID)
		})
	})
}

// runTest runs the mailserver and creates test connections to it.
func runTest(tb testing.TB, options *serverOptions, connIDs []int, tests func(map[int]*testConnection, *testSession)) {
	runServer(tb, options, func(s *testSession) {
		withConnections(tb, s, connIDs, func(c map[int]*testConnection) {
			tests(c, s)
		})
	})
}

// -- IMAP client test helpers

// runTestClient runs the mailserver and creates test connections to it using an imap client.
func runTestClient(
	tb testing.TB,
	options *serverOptions,
	connIDs []int,
	tests func(map[int]*client.Client, *testSession),
) {
	runServer(tb, options, func(s *testSession) {
		withClients(tb, s, connIDs, func(clientMap map[int]*client.Client) {
			tests(clientMap, s)
		})
	})
}

// runOneToOneTestClient runs a test with one account and one connection using an imap client.
func runOneToOneTestClient(tb testing.TB, options *serverOptions, test func(*client.Client, *testSession)) {
	runTestClient(tb, options, []int{1}, func(c map[int]*client.Client, s *testSession) {
		test(c[1], s)
	})
}

// runOneToOneTestClientWithAuth runs a test with one account and one connection using an imap client. The connection is logged in.
func runOneToOneTestClientWithAuth(tb testing.TB, options *serverOptions, test func(*client.Client, *testSession)) {
	runOneToOneTestClient(tb, options, func(client *client.Client, s *testSession) {
		require.NoError(tb, client.Login(options.defaultUsername(), string(options.defaultPassword())))
		test(client, s)
	})
}

// runOneToOneTestClientWithData runs a test with one account and one connection using an imap client. Apply mailbox is created with test data.
func runOneToOneTestClientWithData(
	tb testing.TB,
	options *serverOptions,
	test func(*client.Client, *testSession, string, imap.MailboxID),
) {
	runOneToOneTestClientWithAuth(tb, options, func(client *client.Client, s *testSession) {
		withData(s, options.defaultUsername(), func(mbox string, mboxID imap.MailboxID) {
			_, err := client.Select(mbox, false)
			require.NoError(s.tb, err)
			test(client, s, mbox, mboxID)
		})
	})
}