internal/imap/search_test.go (view raw)
1package imap
2
3import (
4 "database/sql"
5 "testing"
6 "time"
7
8 "postern/internal/model"
9
10 "github.com/emersion/go-imap/v2"
11)
12
13func TestSearchDate(t *testing.T) {
14 intDate := time.Now().UTC()
15 envDate := time.Now().UTC()
16 m := model.Message{
17 UID: 123,
18 InternalDate: intDate.Format(time.RFC3339),
19 EnvelopeDate: sql.NullString{
20 String: envDate.Format(time.RFC3339),
21 Valid: true,
22 },
23 }
24
25 // internal
26 criteria := &imap.SearchCriteria{
27 Before: intDate.Add(-time.Hour * 24),
28 }
29
30 if evalSearchDates(criteria, m) {
31 t.Error("expected BEFORE not to find given message")
32 }
33
34 criteria.Before = criteria.Before.Add(time.Hour * 24 * 2)
35 if !evalSearchDates(criteria, m) {
36 t.Error("expected BEFORE to find given message")
37 }
38
39 criteria = &imap.SearchCriteria{Since: time.Now().UTC()}
40 if !evalSearchDates(criteria, m) {
41 t.Error("expected SINCE to find given message")
42 }
43
44 criteria.Since = criteria.Since.Add(time.Hour * 24)
45 if evalSearchDates(criteria, m) {
46 t.Error("expected SINCE not to find given message")
47 }
48
49 // envelope date
50 criteria = &imap.SearchCriteria{
51 SentBefore: intDate.Add(-time.Hour * 24),
52 }
53 if evalSearchDates(criteria, m) {
54 t.Error("expected SENTBEFORE not to find given message")
55 }
56
57 criteria.SentBefore = criteria.SentBefore.Add(time.Hour * 24 * 2)
58 if !evalSearchDates(criteria, m) {
59 t.Error("expected SENTBEFORE to find given message")
60 }
61
62 criteria = &imap.SearchCriteria{
63 SentSince: time.Now().UTC(),
64 }
65 if !evalSearchDates(criteria, m) {
66 t.Error("expected SENTSINCE to find given message")
67 }
68 criteria.SentSince = time.Now().UTC().Add(time.Hour * 24)
69 if evalSearchDates(criteria, m) {
70 t.Error("expected SENTSINCE not to find given message")
71 }
72
73 // missing envelope date will not result in any findings
74 m = model.Message{
75 UID: 123,
76 InternalDate: intDate.Format(time.RFC3339),
77 EnvelopeDate: sql.NullString{},
78 }
79 criteria = &imap.SearchCriteria{
80 SentBefore: intDate.Add(time.Hour * 24),
81 }
82 if evalSearchDates(criteria, m) {
83 t.Error("expected SENTBEFORE not to find given message with missing envelope Date")
84 }
85}