# Copyright (c) 2018, JINGROW and Contributors
# License: MIT. See LICENSE
import hashlib

import jingrow
from jingrow.tests.utils import JingrowTestCase

test_records = []


class TestTransactionLog(JingrowTestCase):
	def test_validate_chaining(self):
		jingrow.get_pg(
			{
				"pagetype": "Transaction Log",
				"reference_pagetype": "Test Pagetype",
				"document_name": "Test Page 1",
				"data": "first_data",
			}
		).insert(ignore_permissions=True)

		second_log = jingrow.get_pg(
			{
				"pagetype": "Transaction Log",
				"reference_pagetype": "Test Pagetype",
				"document_name": "Test Page 2",
				"data": "second_data",
			}
		).insert(ignore_permissions=True)

		third_log = jingrow.get_pg(
			{
				"pagetype": "Transaction Log",
				"reference_pagetype": "Test Pagetype",
				"document_name": "Test Page 3",
				"data": "third_data",
			}
		).insert(ignore_permissions=True)

		sha = hashlib.sha256()
		sha.update(
			jingrow.safe_encode(str(third_log.transaction_hash))
			+ jingrow.safe_encode(str(second_log.chaining_hash))
		)

		self.assertEqual(sha.hexdigest(), third_log.chaining_hash)
