# Path: sshkm/models.py
# class Host(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     superuser = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     status = models.CharField(max_length=10, null=True, blank=True)
#     last_status = models.DateTimeField(null=True, blank=True)
# 
# class Group(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     members = models.ManyToManyField('Key', through='KeyGroup', blank=True)
# 
# class Key(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     firstname = models.CharField(max_length=191, null=True, blank=True)
#     lastname = models.CharField(max_length=191, null=True, blank=True)
#     email = models.EmailField(max_length=191, null=True, blank=True)
#     publickey = models.TextField(null=True, blank=True)
#     member_of = models.ManyToManyField('Group', through='KeyGroup', blank=True)
# 
# class Osuser(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     home = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
# 
# class Permission(models.Model):
#     group = models.ForeignKey(Group, on_delete=models.CASCADE)
#     host = models.ForeignKey(Host, on_delete=models.CASCADE)
#     osuser = models.ForeignKey(Osuser, on_delete=models.CASCADE)
# 
#     class Meta:
#         unique_together = ("group", "host", "osuser")


import time
from django.test import TestCase, Client, override_settings
from sshkm.models import Host, Group, Key, Osuser, Permission

class Test01(TestCase):
    fixtures = ['sshkm/tests/fixtures/testdata.json']

    def setUp(self):
        self.username = "admin"
        self.password = "admin"

        self.client.post('/login/', {'username': self.username, 'password': self.password})

    def test_group_new(self):
        response = self.client.post('/group/save/', {'name': 'testgroup1', 'description': 'testgroup1 description'})
        self.assertEqual(Group.objects.get(name='testgroup1').description, "testgroup1 description")

    def test_key(self):
        response = self.client.post('/key/save/', {'name': 'testkey2', 'description': 'testkey2 description'})
        self.assertEqual(Key.objects.get(name='testkey2').description, "testkey2 description")
        response = self.client.get('/key/delete/', {'id': Key.objects.get(name='testkey2').id}, follow=True)

# Path: sshkm/models.py
# class Host(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     superuser = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     status = models.CharField(max_length=10, null=True, blank=True)
#     last_status = models.DateTimeField(null=True, blank=True)
# 
# class Group(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     members = models.ManyToManyField('Key', through='KeyGroup', blank=True)
# 
# class Key(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     firstname = models.CharField(max_length=191, null=True, blank=True)
#     lastname = models.CharField(max_length=191, null=True, blank=True)
#     email = models.EmailField(max_length=191, null=True, blank=True)
#     publickey = models.TextField(null=True, blank=True)
#     member_of = models.ManyToManyField('Group', through='KeyGroup', blank=True)
# 
# class Osuser(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     home = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
# 
# class Permission(models.Model):
#     group = models.ForeignKey(Group, on_delete=models.CASCADE)
#     host = models.ForeignKey(Host, on_delete=models.CASCADE)
#     osuser = models.ForeignKey(Osuser, on_delete=models.CASCADE)
# 
#     class Meta:
#         unique_together = ("group", "host", "osuser")


# Path: sshkm/tests/test_01.py
import time
from django.test import TestCase, Client, override_settings
from sshkm.models import Host, Group, Key, Osuser, Permission


class Test01(TestCase):
    fixtures = ['sshkm/tests/fixtures/testdata.json']

    def setUp(self):
        self.username = "admin"
        self.password = "admin"

        self.client.post('/login/', {'username': self.username, 'password': self.password})

    def test_group_new(self):
        response = self.client.post('/group/save/', {'name': 'testgroup1', 'description': 'testgroup1 description'})
        self.assertEqual(Group.objects.get(name='testgroup1').description, "testgroup1 description")

    def test_key(self):
        response = self.client.post('/key/save/', {'name': 'testkey2', 'description': 'testkey2 description'})
        self.assertEqual(Key.objects.get(name='testkey2').description, "testkey2 description")
        response = self.client.get('/key/delete/', {'id': Key.objects.get(name='testkey2').id}, follow=True)

        self.assertEqual(Key.objects.filter(name='testkey2').count(), 0)