# -*- coding: utf-8 -*-
"""Setup tests for this package."""
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from {{{package.dottedname}}}.testing import {{{package.uppercasename}}}_INTEGRATION_TESTING  # noqa: E501

import unittest


try:
    from Products.CMFPlone.utils import get_installer
except ImportError:
    get_installer = None


class TestSetup(unittest.TestCase):
    """Test that {{{ package.dottedname }}} is properly installed."""

    layer = {{{package.uppercasename}}}_INTEGRATION_TESTING

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        if get_installer:
            self.installer = get_installer(self.portal, self.layer['request'])
        else:
            self.installer = api.portal.get_tool('portal_quickinstaller')

    def test_product_installed(self):
        """Test if {{{ package.dottedname }}} is installed."""
        self.assertTrue(self.installer.is_product_installed(
            '{{{ package.dottedname }}}'))

    def test_browserlayer(self):
        """Test that I{{{ package.browserlayer }}} is registered."""
        from {{{ package.dottedname }}}.interfaces import (
            I{{{ package.browserlayer }}})
        from plone.browserlayer import utils
        self.assertIn(
            I{{{ package.browserlayer }}},
            utils.registered_layers())


class TestUninstall(unittest.TestCase):

    layer = {{{package.uppercasename}}}_INTEGRATION_TESTING

    def setUp(self):
        self.portal = self.layer['portal']
        if get_installer:
            self.installer = get_installer(self.portal, self.layer['request'])
        else:
            self.installer = api.portal.get_tool('portal_quickinstaller')
        roles_before = api.user.get_roles(TEST_USER_ID)
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        self.installer.uninstall_product('{{{ package.dottedname }}}')
        setRoles(self.portal, TEST_USER_ID, roles_before)

    def test_product_uninstalled(self):
        """Test if {{{ package.dottedname }}} is cleanly uninstalled."""
        self.assertFalse(self.installer.is_product_installed(
            '{{{ package.dottedname }}}'))

    def test_browserlayer_removed(self):
        """Test that I{{{ package.browserlayer }}} is removed."""
        from {{{ package.dottedname }}}.interfaces import \
            I{{{ package.browserlayer }}}
        from plone.browserlayer import utils
        self.assertNotIn(I{{{ package.browserlayer }}}, utils.registered_layers())
