# Path: models/subscription.py
# class Subscription(ndb.Model):
#     """Models a webhook subscription."""
#     request_method = ndb.StringProperty(required=True, default='post')
#     request_format = ndb.StringProperty(required=True, default=CONTENT_TYPE_JSON)
#     request_url = ndb.StringProperty(required=True)
#     active = ndb.BooleanProperty(required=True, default=False)
#     event = ndb.StringProperty(required=True)
#     secret = ndb.StringProperty(required=True)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
#     owner_key = ndb.KeyProperty(kind=Employee)
# 
#     @classmethod
#     def create_from_dict(cls, d, persist=True):
#         new_subscription = cls()
#         new_subscription.owner_key = Employee.get_current_employee().key
#         new_subscription.request_url = d['request_url']
#         new_subscription.active = d['active']
#         new_subscription.event = d['event']
#         new_subscription.secret = d['secret']
# 
#         if persist is True:
#             new_subscription.put()
# 
#         return new_subscription
# 
#     @classmethod
#     def all_active_for_event(cls, event):
#         return cls.query(
#             cls.active == True,  # noqa
#             cls.event == event,
#         )
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })
# 
# Path: testing/factories/subscription.py
# def create_subscription(
#     request_url='johndoe',
#     active=False,
#     event='lovesent',
#     secret='secret',
# ):
#     return Subscription.create_from_dict({
#         'request_url': request_url,
#         'active': active,
#         'event': event,
#         'secret': secret,
#     })


import mock
import unittest
import logic.subscription
from models import Subscription
from testing.factories import create_employee
from testing.factories import create_subscription
# -*- coding: utf-8 -*-



class SubscriptionTest(unittest.TestCase):
    nosegae_datastore_v3 = True

    @mock.patch('models.subscription.Employee', autospec=True)
    def test_delete_subscription(self, mock_model_employee):
        mock_model_employee.get_current_employee.return_value = create_employee()

        subscription = create_subscription()
        self.assertIsNotNone(Subscription.get_by_id(subscription.key.id()))

        logic.subscription.delete_subscription(subscription.key.id())


# Path: models/subscription.py
# class Subscription(ndb.Model):
#     """Models a webhook subscription."""
#     request_method = ndb.StringProperty(required=True, default='post')
#     request_format = ndb.StringProperty(required=True, default=CONTENT_TYPE_JSON)
#     request_url = ndb.StringProperty(required=True)
#     active = ndb.BooleanProperty(required=True, default=False)
#     event = ndb.StringProperty(required=True)
#     secret = ndb.StringProperty(required=True)
#     timestamp = ndb.DateTimeProperty(auto_now_add=True)
#     owner_key = ndb.KeyProperty(kind=Employee)
# 
#     @classmethod
#     def create_from_dict(cls, d, persist=True):
#         new_subscription = cls()
#         new_subscription.owner_key = Employee.get_current_employee().key
#         new_subscription.request_url = d['request_url']
#         new_subscription.active = d['active']
#         new_subscription.event = d['event']
#         new_subscription.secret = d['secret']
# 
#         if persist is True:
#             new_subscription.put()
# 
#         return new_subscription
# 
#     @classmethod
#     def all_active_for_event(cls, event):
#         return cls.query(
#             cls.active == True,  # noqa
#             cls.event == event,
#         )
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })
# 
# Path: testing/factories/subscription.py
# def create_subscription(
#     request_url='johndoe',
#     active=False,
#     event='lovesent',
#     secret='secret',
# ):
#     return Subscription.create_from_dict({
#         'request_url': request_url,
#         'active': active,
#         'event': event,
#         'secret': secret,
#     })


# Path: tests/logic/subscription_test.py
import mock
import unittest
import logic.subscription
from models import Subscription
from testing.factories import create_employee
from testing.factories import create_subscription

# -*- coding: utf-8 -*-



class SubscriptionTest(unittest.TestCase):
    nosegae_datastore_v3 = True

    @mock.patch('models.subscription.Employee', autospec=True)
    def test_delete_subscription(self, mock_model_employee):
        mock_model_employee.get_current_employee.return_value = create_employee()

        subscription = create_subscription()
        self.assertIsNotNone(Subscription.get_by_id(subscription.key.id()))

        logic.subscription.delete_subscription(subscription.key.id())


        self.assertIsNone(Subscription.get_by_id(subscription.key.id()))