# Path: securedpi_locks/models.py
# class Lock(models.Model):
#     """Define class for user profile."""
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='locks',
#         on_delete=models.CASCADE
#     )
#     name = models.CharField(max_length=20)
#     location = models.CharField(max_length=50)
#     description = models.CharField(max_length=50, blank=True)
#     date_created = models.DateField(auto_now_add=True)
#     date_modified = models.DateField(auto_now=True)
#     serial = models.CharField(max_length=50, unique=True)
#     status = models.CharField(max_length=20, default='unlocked')
#     is_active = models.BooleanField(default=False)
#     facial_recognition = models.BooleanField(default=False)
#     RFID = models.CharField(max_length=100, blank=True)
# 
#     def __str__(self):
#         return 'Lock for {}'.format(self.user)
# 
# Path: securedpi_events/models.py
# class Event(models.Model):
#     """Define class for access events."""
# 
#     lock_id = models.CharField(max_length=20, blank=True)
#     action = models.CharField(max_length=30, default='unlock')
#     RFID = models.CharField(max_length=100, blank=True)
#     photo = models.ImageField(
#         upload_to='lock_photos',
#         blank=True,
#         null=True)
#     date_created = models.DateTimeField(auto_now_add=True)
#     serial = models.CharField(max_length=20)
#     status = models.CharField(max_length=20, default='failed')
#     mtype = models.CharField(max_length=20)
# 
#     def __str__(self):
#         """Show event."""
#         return 'Event for {}'.format(self.lock_id)


from django.test import TestCase
from django.contrib.auth.models import User
from securedpi_locks.models import Lock
from securedpi_events.models import Event
from rest_framework import status
from rest_framework.test import APITestCase
            'serial': '123'
        }
        # number of locks before posting to the 'locks' api endpoint
        self.assertEqual(self.user.locks.count(), 1)
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # number of locks after
        self.assertEqual(self.user.locks.count(), 2)
        self.assertEqual(self.user.locks.last().name, 'lock2')
        self.assertEqual(self.user.locks.last().location, 'home')
        self.assertEqual(self.user.locks.last().serial, '123')

    def test_post_to_events(self):
        """
        Prove that an Event instance is created upon post request
        to the 'events' api endpoint.
        """
        url = '/api/events/'
        data = {
            'lock_id': self.lock.pk,
            'serial': self.lock.serial,
            'mtype': 'manual',
            'action': 'unlock'
        }
        # number of events before posting to the 'events' api endpoint
        self.assertEqual(Event.objects.all().count(), 1)
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # number of locks after
        self.assertEqual(Event.objects.all().count(), 2)

# Path: securedpi_locks/models.py
# class Lock(models.Model):
#     """Define class for user profile."""
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='locks',
#         on_delete=models.CASCADE
#     )
#     name = models.CharField(max_length=20)
#     location = models.CharField(max_length=50)
#     description = models.CharField(max_length=50, blank=True)
#     date_created = models.DateField(auto_now_add=True)
#     date_modified = models.DateField(auto_now=True)
#     serial = models.CharField(max_length=50, unique=True)
#     status = models.CharField(max_length=20, default='unlocked')
#     is_active = models.BooleanField(default=False)
#     facial_recognition = models.BooleanField(default=False)
#     RFID = models.CharField(max_length=100, blank=True)
# 
#     def __str__(self):
#         return 'Lock for {}'.format(self.user)
# 
# Path: securedpi_events/models.py
# class Event(models.Model):
#     """Define class for access events."""
# 
#     lock_id = models.CharField(max_length=20, blank=True)
#     action = models.CharField(max_length=30, default='unlock')
#     RFID = models.CharField(max_length=100, blank=True)
#     photo = models.ImageField(
#         upload_to='lock_photos',
#         blank=True,
#         null=True)
#     date_created = models.DateTimeField(auto_now_add=True)
#     serial = models.CharField(max_length=20)
#     status = models.CharField(max_length=20, default='failed')
#     mtype = models.CharField(max_length=20)
# 
#     def __str__(self):
#         """Show event."""
#         return 'Event for {}'.format(self.lock_id)


# Path: securedpi_api/tests.py
from django.test import TestCase
from django.contrib.auth.models import User
from securedpi_locks.models import Lock
from securedpi_events.models import Event
from rest_framework import status
from rest_framework.test import APITestCase

            'serial': '123'
        }
        # number of locks before posting to the 'locks' api endpoint
        self.assertEqual(self.user.locks.count(), 1)
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # number of locks after
        self.assertEqual(self.user.locks.count(), 2)
        self.assertEqual(self.user.locks.last().name, 'lock2')
        self.assertEqual(self.user.locks.last().location, 'home')
        self.assertEqual(self.user.locks.last().serial, '123')

    def test_post_to_events(self):
        """
        Prove that an Event instance is created upon post request
        to the 'events' api endpoint.
        """
        url = '/api/events/'
        data = {
            'lock_id': self.lock.pk,
            'serial': self.lock.serial,
            'mtype': 'manual',
            'action': 'unlock'
        }
        # number of events before posting to the 'events' api endpoint
        self.assertEqual(Event.objects.all().count(), 1)
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        # number of locks after
        self.assertEqual(Event.objects.all().count(), 2)

        self.assertEqual(Event.objects.last().lock_id, str(self.lock.pk))