# Path: suma/api/resources.py
# class LinkResource(ResourceWrapper):
#     __name__ = 'LinkResource'
#     __parent__ = LinkFactory
# 
#     def __getitem__(self, key):
#         raise KeyError(key)
# 
# class LinkFactory(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def __getitem__(self, key):
#         svc = self.request.find_service(name='link')
#         link = svc.get_link_by_id_or_hashid(key)
#         if link:
#             return LinkResource(link)
#         raise KeyError(key)
# 
#     def unwrap(self):
#         return None
# 
# Path: suma/core/models/link.py
# class Link(Base, TimestampColumns):
#     hashid = Column(String, index=True, unique=True, nullable=False)
#     _url = Column('url', Unicode, index=True, nullable=False)
#     user_id = Column(BigInteger)
#     _hash = Column('hash', String, index=True, nullable=False)
#     _hash_netloc = Column('hash_netloc', String, nullable=False)
#     _hash_path = Column('hash_path', String, nullable=False)
#     meta = Column(MutableDict.as_mutable(JSONB), default={})
#     __table_args__ = (
#         Index('unique_url_and_user_id', _url, user_id, unique=True,
#               postgresql_where=(user_id != None)),
#         Index('unique_url_and_user_id_is_null', 'url', unique=True,
#               postgresql_where=(user_id == None))
#     )
# 
#     is_banned = column_property(
#         exists().where(
#             or_(
#                 BlockedLink.hash.in_([_hash, _hash_netloc, _hash_path])
#             )
#         )
#     )
# 
#     @property
#     def clicks(self):
#         if self._clicks:
#             return self._clicks.counter
#         return 0
#     
#     @hybrid_property
#     def url(self):
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._url = value
#         self._hash, self._hash_netloc, self._hash_path = create_url_hashes(value)
# 
# Path: suma/core/services/interfaces/link.py
# class ILinkService(Interface):
# 
#     def create_link(self, url, user_id=None):
#         pass
# 
#     def get_link_by_id_or_hashid(self, id_or_hashid):
#         pass
# 
#     def get_link_by_url(self, url, user_id=None):
#         pass
# 
#     def get_link_by_id(self, id):
#         pass
# 
#     def get_link_by_hashid(self, hashid):
#         pass
# 
#     def ban_url(self, url, mode='url'):
#         pass
# 
# Path: suma/core/services/interfaces/file.py
# class IFileService(Interface):
# 
#     def create(self, data, filename, folder):
#         pass
# 
#     def url(self, filename):
#         pass
# 
# Path: suma/core/services/interfaces/task.py
# class ITaskService(Interface):
#     def add_click(self, link_id, ip=None, referer=None):
#         pass
# 
#     def get_link_data(self, link_id):
#         pass


from suma.api.resources import LinkResource, LinkFactory
from suma.core.models import Link
from suma.core.services.interfaces import (
    ITaskService,
    ILinkService,
    IFileService
)
from zope.interface import implementer
    from suma.api.views import LinkView
    from suma.api.views import LinkView
    from suma.api.views import LinkView
import pytest
import os


@pytest.fixture
def fake_file_service(config):
    service = FakeFileService()
    config.register_service(service, name='file')
    return service

@pytest.fixture(autouse=True)
def routes(config):
    config.include('suma.api.routes')


def test_create_link_api_view(dummy_request, fake_link_service,
                              fake_task_service, fake_file_service):
    dummy_request.method = 'POST'
    dummy_request.post = '{"url": "http://google.com"}'
    dummy_request.json_body = {"url": "http://google.com"}
    view = LinkView(LinkFactory(dummy_request), dummy_request)
    response_obj = view.create_link()
    assert dummy_request.response.status_code == 201
    assert response_obj["data"]["id"] == 1
    assert response_obj["data"]["attributes"]["hashid"] == '1234'


def test_create_link_api_view_fail_validation(dummy_request, fake_link_service,
                                              fake_task_service, fake_file_service):
    dummy_request.method = 'POST'
    dummy_request.post = '{"unknown": "http://google.com"}'
    dummy_request.json_body = {"unknown": "http://google.com"}

# Path: suma/api/resources.py
# class LinkResource(ResourceWrapper):
#     __name__ = 'LinkResource'
#     __parent__ = LinkFactory
# 
#     def __getitem__(self, key):
#         raise KeyError(key)
# 
# class LinkFactory(object):
# 
#     def __init__(self, request):
#         self.request = request
# 
#     def __getitem__(self, key):
#         svc = self.request.find_service(name='link')
#         link = svc.get_link_by_id_or_hashid(key)
#         if link:
#             return LinkResource(link)
#         raise KeyError(key)
# 
#     def unwrap(self):
#         return None
# 
# Path: suma/core/models/link.py
# class Link(Base, TimestampColumns):
#     hashid = Column(String, index=True, unique=True, nullable=False)
#     _url = Column('url', Unicode, index=True, nullable=False)
#     user_id = Column(BigInteger)
#     _hash = Column('hash', String, index=True, nullable=False)
#     _hash_netloc = Column('hash_netloc', String, nullable=False)
#     _hash_path = Column('hash_path', String, nullable=False)
#     meta = Column(MutableDict.as_mutable(JSONB), default={})
#     __table_args__ = (
#         Index('unique_url_and_user_id', _url, user_id, unique=True,
#               postgresql_where=(user_id != None)),
#         Index('unique_url_and_user_id_is_null', 'url', unique=True,
#               postgresql_where=(user_id == None))
#     )
# 
#     is_banned = column_property(
#         exists().where(
#             or_(
#                 BlockedLink.hash.in_([_hash, _hash_netloc, _hash_path])
#             )
#         )
#     )
# 
#     @property
#     def clicks(self):
#         if self._clicks:
#             return self._clicks.counter
#         return 0
#     
#     @hybrid_property
#     def url(self):
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._url = value
#         self._hash, self._hash_netloc, self._hash_path = create_url_hashes(value)
# 
# Path: suma/core/services/interfaces/link.py
# class ILinkService(Interface):
# 
#     def create_link(self, url, user_id=None):
#         pass
# 
#     def get_link_by_id_or_hashid(self, id_or_hashid):
#         pass
# 
#     def get_link_by_url(self, url, user_id=None):
#         pass
# 
#     def get_link_by_id(self, id):
#         pass
# 
#     def get_link_by_hashid(self, hashid):
#         pass
# 
#     def ban_url(self, url, mode='url'):
#         pass
# 
# Path: suma/core/services/interfaces/file.py
# class IFileService(Interface):
# 
#     def create(self, data, filename, folder):
#         pass
# 
#     def url(self, filename):
#         pass
# 
# Path: suma/core/services/interfaces/task.py
# class ITaskService(Interface):
#     def add_click(self, link_id, ip=None, referer=None):
#         pass
# 
#     def get_link_data(self, link_id):
#         pass


# Path: tests/api/views/test_api_link_views.py
from suma.api.resources import LinkResource, LinkFactory
from suma.core.models import Link
from suma.core.services.interfaces import (
    ITaskService,
    ILinkService,
    IFileService
)
from zope.interface import implementer
    from suma.api.views import LinkView
    from suma.api.views import LinkView
    from suma.api.views import LinkView
import pytest
import os



@pytest.fixture
def fake_file_service(config):
    service = FakeFileService()
    config.register_service(service, name='file')
    return service

@pytest.fixture(autouse=True)
def routes(config):
    config.include('suma.api.routes')


def test_create_link_api_view(dummy_request, fake_link_service,
                              fake_task_service, fake_file_service):
    dummy_request.method = 'POST'
    dummy_request.post = '{"url": "http://google.com"}'
    dummy_request.json_body = {"url": "http://google.com"}
    view = LinkView(LinkFactory(dummy_request), dummy_request)
    response_obj = view.create_link()
    assert dummy_request.response.status_code == 201
    assert response_obj["data"]["id"] == 1
    assert response_obj["data"]["attributes"]["hashid"] == '1234'


def test_create_link_api_view_fail_validation(dummy_request, fake_link_service,
                                              fake_task_service, fake_file_service):
    dummy_request.method = 'POST'
    dummy_request.post = '{"unknown": "http://google.com"}'
    dummy_request.json_body = {"unknown": "http://google.com"}

    view = LinkView(LinkFactory(dummy_request), dummy_request)