postgresdb_test.go 2.86 KB
Newer Older
1
2
//+build !local_tests

3
package keyshareserver
4
5
6
7
8
9
10

import (
	"database/sql"
	"io/ioutil"
	"testing"
	"time"

11
	"github.com/privacybydesign/irmago/internal/common"
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
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const postgresTestUrl = "postgresql://localhost:5432/test"

func TestPostgresDBUserManagement(t *testing.T) {
	SetupDatabase(t)
	defer TeardownDatabase(t)

	db, err := NewPostgresDatabase(postgresTestUrl)
	require.NoError(t, err)

	user, err := db.NewUser(KeyshareUserData{Username: "testuser"})
	require.NoError(t, err)
	assert.Equal(t, "testuser", user.Data().Username)

	nuser, err := db.User("testuser")
	require.NoError(t, err)
	assert.Equal(t, "testuser", nuser.Data().Username)

	_, err = db.User("notexist")
	assert.Error(t, err)

	err = db.UpdateUser(nuser)
	assert.NoError(t, err)

	_, err = db.NewUser(KeyshareUserData{Username: "testuser"})
	assert.Error(t, err)

	err = db.AddLog(nuser, PinCheckFailed, 15)
	assert.NoError(t, err)

	err = db.AddEmailVerification(nuser, "test@example.com", "testtoken")
	assert.NoError(t, err)

	err = db.SetSeen(nuser)
	assert.NoError(t, err)
}

func TestPostgresDBPinReservation(t *testing.T) {
	SetupDatabase(t)
	defer TeardownDatabase(t)

	db, err := NewPostgresDatabase(postgresTestUrl)
	require.NoError(t, err)

	user, err := db.NewUser(KeyshareUserData{Username: "testuser"})
	require.NoError(t, err)

	ok, tries, wait, err := db.ReservePincheck(user)
	require.NoError(t, err)
	assert.True(t, ok)
	assert.True(t, tries > 0)
	assert.Equal(t, int64(0), wait)
	for tries != 0 {
		ok, tries, wait, err = db.ReservePincheck(user)
		require.NoError(t, err)
		assert.True(t, ok)
	}

	time.Sleep(time.Duration(wait-5) * time.Second)

	ok, tries, wait, err = db.ReservePincheck(user)
	assert.NoError(t, err)
	assert.False(t, ok)
	assert.Equal(t, 0, tries)
	assert.True(t, wait > 0)

	time.Sleep(time.Duration(10 * time.Second))

	ok, tries, wait, err = db.ReservePincheck(user)
	assert.NoError(t, err)
	assert.True(t, ok)
	assert.Equal(t, 0, tries)
	assert.True(t, wait > 0)

	ok, tries, wait, err = db.ReservePincheck(user)
	assert.NoError(t, err)
	assert.False(t, ok)
	assert.Equal(t, 0, tries)
	assert.True(t, wait > 0)

	err = db.ClearPincheck(user)
	assert.NoError(t, err)

	ok, tries, wait, err = db.ReservePincheck(user)
	assert.NoError(t, err)
	assert.True(t, ok)
	assert.True(t, tries > 0)
	assert.Equal(t, int64(0), wait)
}

105
func RunScriptOnDB(t *testing.T, filename string, allowErr bool) {
106
107
	db, err := sql.Open("pgx", postgresTestUrl)
	require.NoError(t, err)
108
	defer common.Close(db)
David Venhoek's avatar
David Venhoek committed
109
	scriptData, err := ioutil.ReadFile(filename)
110
111
	require.NoError(t, err)
	_, err = db.Exec(string(scriptData))
112
113
114
	if !allowErr {
		require.NoError(t, err)
	}
115
116
117
}

func SetupDatabase(t *testing.T) {
118
119
	RunScriptOnDB(t, "cleanup.sql", true)
	RunScriptOnDB(t, "schema.sql", false)
120
121
122
}

func TeardownDatabase(t *testing.T) {
123
	RunScriptOnDB(t, "cleanup.sql", false)
124
}