# Path: server/models.py
# class Computer(models.Model):
#     # recovery_key = models.CharField(max_length=200, verbose_name="Recovery Key")
#     serial = models.CharField(max_length=200, verbose_name="Serial Number", unique=True)
#     username = models.CharField(max_length=200, verbose_name="User Name")
#     computername = models.CharField(max_length=200, verbose_name="Computer Name")
#     last_checkin = models.DateTimeField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.computername
# 
#     class Meta:
#         ordering = ["serial"]
#         permissions = (
#             ("can_approve", (u"Can approve requests to see encryption keys")),
#         )
# 
# class Secret(models.Model):
#     computer = models.ForeignKey(Computer, on_delete=models.CASCADE)
#     secret = EncryptedCharField(max_length=256)
#     secret_type = models.CharField(
#         max_length=256, choices=SECRET_TYPES, default="recovery_key"
#     )
#     date_escrowed = models.DateTimeField(auto_now_add=True)
#     rotation_required = models.BooleanField(default=False)
# 
#     def validate_unique(self, *args, **kwargs):
#         if (
#             self.secret
#             in [
#                 str(s)
#                 for s in self.__class__.objects.filter(
#                     secret_type=self.secret_type, computer=self.computer
#                 )
#             ]
#             and not self.rotation_required
#         ):
#             raise ValidationError("already used")
#         super(Secret, self).validate_unique(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
#         self.validate_unique()
#         super(Secret, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.secret
# 
#     class Meta:
#         ordering = ["-date_escrowed"]
# 
# class Request(models.Model):
#     secret = models.ForeignKey(Secret, on_delete=models.PROTECT)
#     # computer = models.ForeignKey(Computer, null=True, related_name='computers')
#     requesting_user = models.ForeignKey(
#         User, related_name="requesting_user", on_delete=models.CASCADE
#     )
#     approved = models.NullBooleanField(verbose_name="Approved?")
#     auth_user = models.ForeignKey(
#         User, null=True, related_name="auth_user", on_delete=models.PROTECT
#     )
#     reason_for_request = models.TextField()
#     reason_for_approval = models.TextField(
#         blank=True, null=True, verbose_name="Approval Notes"
#     )
#     date_requested = models.DateTimeField(auto_now_add=True)
#     date_approved = models.DateTimeField(blank=True, null=True)
#     current = models.BooleanField(default=True)
# 
#     def __str__(self):
#         return "%s - %s" % (self.secret, self.requesting_user)


from django.test import TestCase, Client
from django.contrib.auth.models import User
from datetime import datetime
from server.models import Computer, Secret, Request


class RequestProcess(TestCase):
    def test_request_passes_correct_data_to_template(self):
        admin = User.objects.create_superuser("admin", "a@a.com", "sekrit")
        tech = User.objects.create_user("tech", "a@a.com", "password")
        tech.save()
        tech_test_computer = Computer(
            serial="TECHSERIAL", username="Daft Tech", computername="compy587"
        )
        tech_test_computer.save()
        test_secret = Secret(
            computer=tech_test_computer,
            secret="SHHH-DONT-TELL",
            date_escrowed=datetime.now(),
        )
        test_secret.save()
        secret_request = Request(secret=test_secret, requesting_user=tech)
        secret_request.save()
        client = Client()
        login_response = self.client.post(

# Path: server/models.py
# class Computer(models.Model):
#     # recovery_key = models.CharField(max_length=200, verbose_name="Recovery Key")
#     serial = models.CharField(max_length=200, verbose_name="Serial Number", unique=True)
#     username = models.CharField(max_length=200, verbose_name="User Name")
#     computername = models.CharField(max_length=200, verbose_name="Computer Name")
#     last_checkin = models.DateTimeField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.computername
# 
#     class Meta:
#         ordering = ["serial"]
#         permissions = (
#             ("can_approve", (u"Can approve requests to see encryption keys")),
#         )
# 
# class Secret(models.Model):
#     computer = models.ForeignKey(Computer, on_delete=models.CASCADE)
#     secret = EncryptedCharField(max_length=256)
#     secret_type = models.CharField(
#         max_length=256, choices=SECRET_TYPES, default="recovery_key"
#     )
#     date_escrowed = models.DateTimeField(auto_now_add=True)
#     rotation_required = models.BooleanField(default=False)
# 
#     def validate_unique(self, *args, **kwargs):
#         if (
#             self.secret
#             in [
#                 str(s)
#                 for s in self.__class__.objects.filter(
#                     secret_type=self.secret_type, computer=self.computer
#                 )
#             ]
#             and not self.rotation_required
#         ):
#             raise ValidationError("already used")
#         super(Secret, self).validate_unique(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
#         self.validate_unique()
#         super(Secret, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.secret
# 
#     class Meta:
#         ordering = ["-date_escrowed"]
# 
# class Request(models.Model):
#     secret = models.ForeignKey(Secret, on_delete=models.PROTECT)
#     # computer = models.ForeignKey(Computer, null=True, related_name='computers')
#     requesting_user = models.ForeignKey(
#         User, related_name="requesting_user", on_delete=models.CASCADE
#     )
#     approved = models.NullBooleanField(verbose_name="Approved?")
#     auth_user = models.ForeignKey(
#         User, null=True, related_name="auth_user", on_delete=models.PROTECT
#     )
#     reason_for_request = models.TextField()
#     reason_for_approval = models.TextField(
#         blank=True, null=True, verbose_name="Approval Notes"
#     )
#     date_requested = models.DateTimeField(auto_now_add=True)
#     date_approved = models.DateTimeField(blank=True, null=True)
#     current = models.BooleanField(default=True)
# 
#     def __str__(self):
#         return "%s - %s" % (self.secret, self.requesting_user)


# Path: server/tests.py
from django.test import TestCase, Client
from django.contrib.auth.models import User
from datetime import datetime
from server.models import Computer, Secret, Request



class RequestProcess(TestCase):
    def test_request_passes_correct_data_to_template(self):
        admin = User.objects.create_superuser("admin", "a@a.com", "sekrit")
        tech = User.objects.create_user("tech", "a@a.com", "password")
        tech.save()
        tech_test_computer = Computer(
            serial="TECHSERIAL", username="Daft Tech", computername="compy587"
        )
        tech_test_computer.save()
        test_secret = Secret(
            computer=tech_test_computer,
            secret="SHHH-DONT-TELL",
            date_escrowed=datetime.now(),
        )
        test_secret.save()
        secret_request = Request(secret=test_secret, requesting_user=tech)
        secret_request.save()
        client = Client()
        login_response = self.client.post(

            "/login/", {"username": "admin", "password": "sekrit"}, follow=True