def setUp(self):
     """Create the database objects required for the tests."""
     self.area = WaterbalanceArea()
     self.area.active = True
     self.area.name = "WaterbalanceArea for PermissionTests"
     self.area.save()
     self.scenario = WaterbalanceScenario()
     self.scenario.active = True
     self.scenario.name = "WaterbalanceScenario for PermissionTests"
     self.scenario.public = True
     self.scenario.save()
     self.configuration = WaterbalanceConf()
     self.configuration.waterbalance_area = self.area
     self.configuration.waterbalance_scenario = self.scenario
     self.configuration.calculation_start_date = datetime(2011, 7, 15)
     self.configuration.calculation_end_date = datetime(2011, 7, 16)
     self.configuration.save()
     self.user = User.objects.create_user('john', '*****@*****.**', 'johnpassword')
     self.user.save()
class PermissionTests(TestCase):
    """Implements the tests for function retrieve_viewable_configurations."""

    def setUp(self):
        """Create the database objects required for the tests."""
        self.area = WaterbalanceArea()
        self.area.active = True
        self.area.name = "WaterbalanceArea for PermissionTests"
        self.area.save()
        self.scenario = WaterbalanceScenario()
        self.scenario.active = True
        self.scenario.name = "WaterbalanceScenario for PermissionTests"
        self.scenario.public = True
        self.scenario.save()
        self.configuration = WaterbalanceConf()
        self.configuration.waterbalance_area = self.area
        self.configuration.waterbalance_scenario = self.scenario
        self.configuration.calculation_start_date = datetime(2011, 7, 15)
        self.configuration.calculation_end_date = datetime(2011, 7, 16)
        self.configuration.save()
        self.user = User.objects.create_user('john', '*****@*****.**', 'johnpassword')
        self.user.save()

    def tearDown(self):
        """Delete the database objects we created for the tests."""
        self.user.delete()
        self.scenario.delete()
        self.area.delete()

    def test_a(self):
        """A user is allowed to view a public scenario.

        The user is not logged in.

        """
        configurations = retrieve_viewable_configurations(AnonymousUser())
        self.assertEqual([self.configuration], list(configurations))

    def test_b(self):
        """A user is not allowed to view a non-public scenario.

        The user is not logged in.

        """
        self.scenario.public = False
        self.scenario.save()

        configurations = retrieve_viewable_configurations(AnonymousUser())
        self.assertEqual([], list(configurations))

    def test_c(self):
        """A user is allowed to view a public scenario.

        The user is logged in.

        """
        user = authenticate(username="******", password="******")
        assert user is not None

        configurations = retrieve_viewable_configurations(user)
        self.assertEqual([self.configuration], list(configurations))

    def test_d(self):
        """A user is not-allowed to view a non-public scenario.

        The user is logged in.

        """
        self.scenario.public = False
        self.scenario.save()
        user = authenticate(username="******", password="******")
        assert user is not None

        configurations = retrieve_viewable_configurations(user)
        self.assertEqual([], list(configurations))

    def test_e(self):
        """A user is allowed to view a non-public scenario.

        The user is logged in and has the right to view non-public scenarios.

        """
        self.scenario.public = False
        self.scenario.save()
        permission = Permission.objects.get(codename="see_not_public_scenarios")
        assert permission is not None
        self.user.user_permissions.add(permission)
        self.user.save()
        user = authenticate(username="******", password="******")
        assert user is not None

        configurations = retrieve_viewable_configurations(user)
        self.assertEqual([self.configuration], list(configurations))

    def test_f(self):
        """A user is not-allowed to view a non-public scenario.

        The user is logged in, has the right to view non-public scenarios but
        is inactive.

        """
        self.scenario.public = False
        self.scenario.save()
        permission = Permission.objects.get(codename="see_not_public_scenarios")
        assert permission is not None
        self.user.user_permissions.add(permission)
        self.user.is_active = False
        self.user.save()
        user = authenticate(username="******", password="******")
        assert user is not None
        assert not user.is_active

        configurations = retrieve_viewable_configurations(user)
        self.assertEqual([], list(configurations))