# Path: src/sa_api_v2/apikey/models.py
# class ApiKey(CloneableModelMixin, models.Model):
#     key = models.CharField(max_length=KEY_SIZE, unique=True, default=generate_unique_api_key)
#     logged_ip = models.GenericIPAddressField(blank=True, null=True)
#     last_used = models.DateTimeField(blank=True, default=now)
#     dataset = models.ForeignKey(DataSet, on_delete=models.CASCADE, blank=True, related_name='keys')
# 
#     class Meta:
#         app_label = 'sa_api_v2'
#         db_table = 'apikey_apikey'
# 
#     def login(self, ip_address):
#         self.logged_ip = ip_address
#         self.save()
# 
#     def logout(self):
#         # YAGNI?
#         self.logged_ip = None
#         self.save()
# 
#     @property
#     def owner(self):
#         try:
#             return self.dataset.owner
#         except AttributeError:
#             return None
# 
#     def __str__(self):
#         return self.__unicode__()
# 
#     def __unicode__(self):
#         return self.key
# 
#     def clone_related(self, onto):
#         for permission in self.permissions.all():
#             permission.clone(overrides={'key': onto})
# 
#     def get_ignore_fields(self, ModelClass):
#         fields = super(ApiKey, self).get_ignore_fields(ModelClass)
#         # Do not copy over the actual key value
#         if ModelClass == ApiKey:
#             fields.add('key')
#         return fields
# 
#     def save(self, *args, **kwargs):
#         if self.logged_ip == '':
#             self.logged_ip = None
#         return super(ApiKey, self).save(*args, **kwargs)


import json
import mock
import os.path
from django.test import TestCase
from django.test.client import RequestFactory
from django.core.cache import cache
from django.core.urlresolvers import reverse
from ..models import (Attachment, DataSet, User, Group, SubmittedThing, Action, Place, Submission,
    DataSetPermission, check_data_permission, DataIndex, IndexedValue)
from ..apikey.models import ApiKey
from mock import patch
        from django.core.files.images import ImageFile
        from django.core.files.base import ContentFile
        self.assertNotEqual(clone.id, group.id)

        clone_submitters = clone.submitters.all()
        group_submitters = group.submitters.all()

        clone_submitter_ids = set([s.id for s in clone_submitters])
        group_submitter_ids = set([s.id for s in group_submitters])
        self.assertEqual(clone_submitter_ids, group_submitter_ids)

    def test_apikey_can_be_cloned(self):
        dataset = DataSet.objects.create(owner=self.owner, slug='dataset')
        key = ApiKey.objects.create(dataset=dataset)

        # Clone the object and make sure the clone's values are initialized
        # correctly.
        clone = key.clone()
        self.assertEqual(clone.dataset, key.dataset)
        self.assertNotEqual(clone.id, key.id)

        # Keys must not repeat across system
        self.assertNotEqual(clone.key, key.key)


class DataPermissionTests (TestCase):
    def clear_objects(self):
        User.objects.all().delete()
        DataSet.objects.all().delete()
        Place.objects.all().delete()
        Submission.objects.all().delete()
        Action.objects.all().delete()

# Path: src/sa_api_v2/apikey/models.py
# class ApiKey(CloneableModelMixin, models.Model):
#     key = models.CharField(max_length=KEY_SIZE, unique=True, default=generate_unique_api_key)
#     logged_ip = models.GenericIPAddressField(blank=True, null=True)
#     last_used = models.DateTimeField(blank=True, default=now)
#     dataset = models.ForeignKey(DataSet, on_delete=models.CASCADE, blank=True, related_name='keys')
# 
#     class Meta:
#         app_label = 'sa_api_v2'
#         db_table = 'apikey_apikey'
# 
#     def login(self, ip_address):
#         self.logged_ip = ip_address
#         self.save()
# 
#     def logout(self):
#         # YAGNI?
#         self.logged_ip = None
#         self.save()
# 
#     @property
#     def owner(self):
#         try:
#             return self.dataset.owner
#         except AttributeError:
#             return None
# 
#     def __str__(self):
#         return self.__unicode__()
# 
#     def __unicode__(self):
#         return self.key
# 
#     def clone_related(self, onto):
#         for permission in self.permissions.all():
#             permission.clone(overrides={'key': onto})
# 
#     def get_ignore_fields(self, ModelClass):
#         fields = super(ApiKey, self).get_ignore_fields(ModelClass)
#         # Do not copy over the actual key value
#         if ModelClass == ApiKey:
#             fields.add('key')
#         return fields
# 
#     def save(self, *args, **kwargs):
#         if self.logged_ip == '':
#             self.logged_ip = None
#         return super(ApiKey, self).save(*args, **kwargs)


# Path: src/sa_api_v2/tests/test_models.py
import json
import mock
import os.path
from django.test import TestCase
from django.test.client import RequestFactory
from django.core.cache import cache
from django.core.urlresolvers import reverse
from ..models import (Attachment, DataSet, User, Group, SubmittedThing, Action, Place, Submission,
    DataSetPermission, check_data_permission, DataIndex, IndexedValue)
from ..apikey.models import ApiKey
from mock import patch
        from django.core.files.images import ImageFile
        from django.core.files.base import ContentFile

        self.assertNotEqual(clone.id, group.id)

        clone_submitters = clone.submitters.all()
        group_submitters = group.submitters.all()

        clone_submitter_ids = set([s.id for s in clone_submitters])
        group_submitter_ids = set([s.id for s in group_submitters])
        self.assertEqual(clone_submitter_ids, group_submitter_ids)

    def test_apikey_can_be_cloned(self):
        dataset = DataSet.objects.create(owner=self.owner, slug='dataset')
        key = ApiKey.objects.create(dataset=dataset)

        # Clone the object and make sure the clone's values are initialized
        # correctly.
        clone = key.clone()
        self.assertEqual(clone.dataset, key.dataset)
        self.assertNotEqual(clone.id, key.id)

        # Keys must not repeat across system
        self.assertNotEqual(clone.key, key.key)


class DataPermissionTests (TestCase):
    def clear_objects(self):
        User.objects.all().delete()
        DataSet.objects.all().delete()
        Place.objects.all().delete()
        Submission.objects.all().delete()
        Action.objects.all().delete()

        ApiKey.objects.all().delete()