summaryrefslogtreecommitdiff
path: root/srv/src/mailinglist/store_test.go
blob: 25eb15026983db321507056df10f4cf7893cf09c (plain)
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
package mailinglist

import (
	"io"
	"io/ioutil"
	"os"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestStore(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mediocre-blog-mailinglist-store-test-")
	if err != nil {
		t.Fatal("Cannot create temporary file", err)
	}
	tmpFilePath := tmpFile.Name()
	tmpFile.Close()

	t.Logf("using temporary sqlite file at %q", tmpFilePath)

	t.Cleanup(func() {
		if err := os.Remove(tmpFilePath); err != nil {
			panic(err)
		}
	})

	store, err := NewStore(tmpFilePath)
	assert.NoError(t, err)

	t.Cleanup(func() {
		assert.NoError(t, store.Close())
	})

	now := func() time.Time {
		return time.Now().Truncate(time.Second)
	}

	assertGet := func(t *testing.T, email Email) {
		t.Helper()

		gotEmail, err := store.Get(email.Email)
		assert.NoError(t, err)
		assert.Equal(t, email, gotEmail)

		gotEmail, err = store.GetBySubToken(email.SubToken)
		assert.NoError(t, err)
		assert.Equal(t, email, gotEmail)

		if email.UnsubToken != "" {
			gotEmail, err = store.GetByUnsubToken(email.UnsubToken)
			assert.NoError(t, err)
			assert.Equal(t, email, gotEmail)
		}
	}

	assertNotFound := func(t *testing.T, email string) {
		t.Helper()
		_, err := store.Get(email)
		assert.ErrorIs(t, err, ErrNotFound)
	}

	// now start actual tests

	// GetAll should not do anything, there's no data
	_, err = store.GetAll()()
	assert.ErrorIs(t, err, io.EOF)

	emailFoo := Email{
		Email:     "foo",
		SubToken:  "subTokenFoo",
		CreatedAt: now(),
	}

	// email isn't stored yet, shouldn't exist
	assertNotFound(t, emailFoo.Email)

	// Set an email, now it should exist
	assert.NoError(t, store.Set(emailFoo))
	assertGet(t, emailFoo)

	// Update the email with an unsub token
	emailFoo.UnsubToken = "unsubTokenFoo"
	emailFoo.VerifiedAt = now()
	assert.NoError(t, store.Set(emailFoo))
	assertGet(t, emailFoo)

	// GetAll should now only return that email
	iter := store.GetAll()
	gotEmail, err := iter()
	assert.NoError(t, err)
	assert.Equal(t, emailFoo, gotEmail)
	_, err = iter()
	assert.ErrorIs(t, err, io.EOF)

	// Delete the email, it should be gone
	assert.NoError(t, store.Delete(emailFoo.Email))
	assertNotFound(t, emailFoo.Email)
	_, err = store.GetAll()()
	assert.ErrorIs(t, err, io.EOF)
}