summaryrefslogtreecommitdiff
path: root/tests/test_canarydb.py
blob: 5a0fc237145438a3859a0cc36bff3cf56fef2b09 (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
import unittest
import tempfile, shutil
import datetime

from emailcanary import canarydb

class TestCanaryDB(unittest.TestCase):
	def setUp(self):
		self.tempdir = tempfile.mkdtemp()
		self.db = canarydb.CanaryDB(self.tempdir + "canary.db")

	def tearDown(self):
		self.db.close()
		shutil.rmtree(self.tempdir)

	def testPingCheckPong(self):
		address = "test@example.com"
		time = datetime.datetime(2015, 10, 24, 9, 00)
		uuid = "1234"
		expectedDelta = datetime.datetime.now() - time

		# Record a Ping
		self.db.ping(address, time, uuid)

		# Check for missing pongs
		missing = self.db.get_missing_pongs()

		self.assertEqual(1, len(missing))
		firstMissing = missing[0]
		self.assertEqual(3, len(firstMissing))
		self.assertEqual(uuid, firstMissing[0])
		self.assertEqual(address, firstMissing[1])
		delta = firstMissing[2].total_seconds() - expectedDelta.total_seconds()
		self.assertTrue(delta <= 10)

		# Record a pong
		pongtime = datetime.datetime(2015, 10, 24, 9, 05)
		self.db.pong(address, pongtime, uuid)

		# Check for missing pongs
		missing = self.db.get_missing_pongs()
		self.assertEqual(0, len(missing))

	def testCloseReopen(self):
		address = "test@example.com"
		time = datetime.datetime(2015, 10, 24, 9, 00)
		uuid = "1234"
		expectedDelta = datetime.datetime.now() - time

		# Record a Ping
		self.db.ping(address, time, uuid)

		# Close, Reopen
		self.db.close()
		self.db = canarydb.CanaryDB(self.tempdir + "canary.db")

		# Check for missing pongs
		missing = self.db.get_missing_pongs()

		self.assertEqual(1, len(missing))
		firstMissing = missing[0]
		self.assertEqual(3, len(firstMissing))
		self.assertEqual(uuid, firstMissing[0])
		self.assertEqual(address, firstMissing[1])
		delta = firstMissing[2].total_seconds() - expectedDelta.total_seconds()
		self.assertTrue(delta <= 10)