import pytest

from dashboard.widgets import widgets


@pytest.mark.django_db
class TestWidgetsPermissions(object):

    @pytest.mark.unit
    def test_default_permissions(self, add_user_with_permissions):
        """ Check that any user can see default widget """
        user = add_user_with_permissions(username='user', password='ssssddddfff')
        widget = widgets.DashboardWidget()

        assert widget.user_can_see_widget(user)

    @pytest.mark.unit
    @pytest.mark.parametrize("widget", widgets.all_subclasses(widgets.DashboardWidget))
    def test_user_can_see_widget(self, widget, add_user_with_permissions):
        """ Check that user with correct permissions can see widget """
        cur = widget()
        user = add_user_with_permissions(username='user', password='ssssddddfff', permissions=cur.perms)

        assert cur.user_can_see_widget(user)

    @pytest.mark.unit
    @pytest.mark.parametrize("widget", widgets.all_subclasses(widgets.DashboardWidget))
    def test_user_cant_see_widget(self, widget, add_user_with_permissions):
        """ Check that user without correct permissions can't see widget

        Test will skip widgets with no permissions, because every user can see such widgets
        """
        cur = widget()

        if len(cur.perms) == 0:
            pytest.skip(f"Can't run test for {widget}, because every user can see it")

        user = add_user_with_permissions(username='user', password='ssssddddfff')

        assert not cur.user_can_see_widget(user)
