def test_import_purge_true(self):
        """Test purge=true."""
        from pas.plugins.headers.exportimport import import_properties

        self._configurePlugin()
        settings = {"purge": True}
        import_properties(self._makeContext(json.dumps(settings)))
        self.assert_plugin_has_default_settings()
    def test_import_non_dictionary(self):
        """Test how import handles a file without a dictionary."""
        from pas.plugins.headers.exportimport import import_properties

        self._configurePlugin()
        settings = [{"userid_header": "header_user"}]
        with self.assertRaises(ValueError):
            import_properties(self._makeContext(json.dumps(settings)))
        self.assertEqual(self.plugin.userid_header, "foo")
    def test_import_bad_json(self):
        """Test how import handles a bad json."""
        from pas.plugins.headers.exportimport import import_properties

        self._configurePlugin()
        with self.assertRaises(ValueError):
            import_properties(
                self._makeContext('{"userid_header": "missing end quote}')
            )
    def test_import_unknown_property(self):
        """Test that import does not fail for an unknown property."""
        from pas.plugins.headers.exportimport import import_properties

        settings = {
            "unknown": "hello",
            "userid_header": "my_uid",
        }
        import_properties(self._makeContext(json.dumps(settings)))
        self.assertEqual(self.plugin.userid_header, "my_uid")
        marker = object()
        self.assertIs(getattr(self.plugin, "unknown", marker), marker)
    def test_import_full(self):
        """Test a full import."""
        from pas.plugins.headers.exportimport import import_properties

        settings = {
            "allowed_roles": ["missile", "target"],
            "deny_unauthorized": True,
            "memberdata_to_header": [
                "uid|PROFILE_uid",
                "fullname|PROFILE_firstname PROFILE_lastname",
                "role|PROFILE_role|lower",
            ],
            "redirect_url": "https://maurits.vanrees.org",
            "required_headers": ["uid", "role"],
            "roles_header": "portal_roles",
            "userid_header": "uid",
        }
        import_properties(self._makeContext(json.dumps(settings)))
        self.assertTupleEqual(
            self.plugin.allowed_roles,
            ("missile", "target"),
        )
        self.assertTrue(self.plugin.deny_unauthorized)
        self.assertTupleEqual(
            self.plugin.memberdata_to_header,
            (
                "uid|PROFILE_uid",
                "fullname|PROFILE_firstname PROFILE_lastname",
                "role|PROFILE_role|lower",
            ),
        )
        self.assertEqual(self.plugin.redirect_url, "https://maurits.vanrees.org")
        self.assertTupleEqual(
            self.plugin.required_headers,
            ("uid", "role"),
        )
        self.assertEqual(self.plugin.roles_header, "portal_roles")
        self.assertEqual(self.plugin.userid_header, "uid")

        # Explicitly test the types.
        # We want string: when you save the properties form
        # in the ZMI, you always get a string, in both Py 2 and 3
        # And we want tuples, not lists.
        self.assertIsInstance(self.plugin.allowed_roles, tuple)
        self.assertIsInstance(self.plugin.allowed_roles[0], str)
        self.assertIsInstance(self.plugin.deny_unauthorized, bool)
        self.assertIsInstance(self.plugin.memberdata_to_header, tuple)
        self.assertIsInstance(self.plugin.memberdata_to_header[0], str)
        self.assertIsInstance(self.plugin.redirect_url, str)
        self.assertIsInstance(self.plugin.required_headers, tuple)
        self.assertIsInstance(self.plugin.required_headers[0], str)
        self.assertIsInstance(self.plugin.roles_header, str)
        self.assertIsInstance(self.plugin.userid_header, str)
    def test_import_no_plugin(self):
        """Test that import does not fail when plugin is not there."""
        from pas.plugins.headers.exportimport import import_properties
        from pas.plugins.headers.utils import get_plugin

        self._removePlugin()
        settings = {
            "purge": True,
            "deny_unauthorized": True,
            "userid_header": "my_uid",
        }
        import_properties(self._makeContext(json.dumps(settings)))
        self.assertIsNone(get_plugin(self.portal))
    def test_import_purge_and_set(self):
        """Test purge=true and setting some values."""
        from pas.plugins.headers.exportimport import import_properties

        self._configurePlugin()
        settings = {
            "purge": True,
            "deny_unauthorized": True,
            "userid_header": "my_uid",
        }
        import_properties(self._makeContext(json.dumps(settings)))
        self.assertTrue(self.plugin.deny_unauthorized)
        self.assertTupleEqual(self.plugin.memberdata_to_header, ())
        self.assertEqual(self.plugin.redirect_url, "")
        self.assertTupleEqual(self.plugin.required_headers, ())
        self.assertEqual(self.plugin.userid_header, "my_uid")
    def test_import_empty(self):
        """When the import file is empty, nothing should go wrong."""
        from pas.plugins.headers.exportimport import import_properties

        import_properties(self._makeContext(""))
    def test_import_no_file(self):
        """When the import file is not there, nothing should go wrong."""
        from pas.plugins.headers.exportimport import import_properties

        import_properties(self._makeContext())