# Copyright (c) 2023, JINGROW
# See license.txt

import unittest
from unittest.mock import Mock, patch

import jingrow

from jcloud.jcloud.pagetype.agent_job.agent_job import AgentJob
from jcloud.jcloud.pagetype.site.test_site import create_test_bench
from jcloud.jcloud.pagetype.ssh_certificate.ssh_certificate import SSHCertificate
from jcloud.jcloud.pagetype.team.test_team import create_test_jcloud_admin_team
from jcloud.jcloud.pagetype.user_ssh_key.test_user_ssh_key import create_test_user_ssh_key


@patch.object(SSHCertificate, "validate_certificate_authority", new=Mock())
@patch.object(SSHCertificate, "generate_certificate", new=Mock())
@patch.object(SSHCertificate, "extract_certificate_details", new=Mock())
@patch.object(AgentJob, "enqueue_http_request", new=Mock())
class TestSSHCertificate(unittest.TestCase):
	def setUp(self):
		self.team = create_test_jcloud_admin_team()
		self.user = self.team.user

	def tearDown(self):
		jingrow.db.rollback()
		jingrow.set_user("Administrator")

	def test_jcloud_admin_user_can_create_ssh_certificate(self):
		bench = create_test_bench(self.user)
		group = bench.group
		jingrow.set_user(self.user)
		user_ssh_key = create_test_user_ssh_key(user=self.user)
		return jingrow.get_pg(
			{
				"pagetype": "SSH Certificate",
				"certificate_type": "User",
				"group": group,
				"user": jingrow.session.user,
				"user_ssh_key": user_ssh_key,
				"validity": "6h",
			}
		).insert()
