# 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
    def setUp(self):
        self.clear_objects()

    def tearDown(self):
        self.clear_objects()

    def test_default_dataset_permissions_allow_reading(self):
        owner = User.objects.create(username='myowner')
        user = User.objects.create(username='myuser')
        dataset = DataSet.objects.create(slug='data', owner_id=owner.id)
        place = Place.objects.create(dataset_id=dataset.id, geometry='POINT(0 0)')

        # Make sure a permission objects were created
        self.assertEqual(dataset.permissions.count(), 1)

        # Make sure anonymous is allowed to read, not write.
        self.assertEqual(check_data_permission(None, None, 'retrieve', dataset, 'comments'), True)
        self.assertEqual(check_data_permission(None, None, 'update', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(None, None, 'create', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(None, None, 'destroy', dataset, 'comments'), False)

        # Make sure authenticated is allowed to read.
        self.assertEqual(check_data_permission(user, None, 'retrieve', dataset, 'comments'), True)
        self.assertEqual(check_data_permission(user, None, 'update', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(user, None, 'create', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(user, None, 'destroy', dataset, 'comments'), False)

        # Make sure owner is allowed to read.
        self.assertEqual(check_data_permission(owner, None, 'retrieve', dataset, 'comments'), True)
        self.assertEqual(check_data_permission(owner, None, 'update', dataset, 'comments'), True)

# 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

    def setUp(self):
        self.clear_objects()

    def tearDown(self):
        self.clear_objects()

    def test_default_dataset_permissions_allow_reading(self):
        owner = User.objects.create(username='myowner')
        user = User.objects.create(username='myuser')
        dataset = DataSet.objects.create(slug='data', owner_id=owner.id)
        place = Place.objects.create(dataset_id=dataset.id, geometry='POINT(0 0)')

        # Make sure a permission objects were created
        self.assertEqual(dataset.permissions.count(), 1)

        # Make sure anonymous is allowed to read, not write.
        self.assertEqual(check_data_permission(None, None, 'retrieve', dataset, 'comments'), True)
        self.assertEqual(check_data_permission(None, None, 'update', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(None, None, 'create', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(None, None, 'destroy', dataset, 'comments'), False)

        # Make sure authenticated is allowed to read.
        self.assertEqual(check_data_permission(user, None, 'retrieve', dataset, 'comments'), True)
        self.assertEqual(check_data_permission(user, None, 'update', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(user, None, 'create', dataset, 'comments'), False)
        self.assertEqual(check_data_permission(user, None, 'destroy', dataset, 'comments'), False)

        # Make sure owner is allowed to read.
        self.assertEqual(check_data_permission(owner, None, 'retrieve', dataset, 'comments'), True)
        self.assertEqual(check_data_permission(owner, None, 'update', dataset, 'comments'), True)

        self.assertEqual(check_data_permission(owner, None, 'create', dataset, 'comments'), True)