Example #1
0
    def get_prefs(self):
        """
        Load preferences

        Request data:
            user: username
            type: preference type

        This function loads preferences from the backend
        """
        logger.debug("get_prefs ...")

        # Fetch user's preferences from the backend ...
        try:
            resp = frontend.get_user_preferences(
                request.args.get('user', 'common'),
                self.object_type + '-' + request.args.get('type', 'common')
            )
            if resp and 'data' in resp:
                res = jsonify(**resp['data'])
                res.status_code = 200
                return res
            else:
                return ''
        except Exception as e:  # pragma: no cover - very difficult with unit testing ...
            logger.error("get_prefs, exception: %s", str(e))
            raise DatatableException(
                450,
                "get_prefs, exception: %s / %s" % (type(e), str(e))
            )

        return None
Example #2
0
    def test_7_user_preferences(self):
        print ""
        print "test user preferences"

        # Force authentication ...
        connection = frontend.login("admin", "admin", force=True)
        assert_true(frontend.authenticated)
        assert_true(frontend.token)

        connection = frontend.connect(username="******")
        assert_true(frontend.authenticated)
        assert_true(frontend.connected)

        parameters = {"a": 1, "b": "2"}
        response = frontend.set_user_preferences("admin", "test_prefs", parameters)
        # {u'_updated': u'Sun, 04 Oct 2015 08:14:41 GMT', u'_links': {u'self': {u'href': u'uipref/5610dff1f9e3854415d129be', u'title': u'Uipref'}}, u'_created': u'Sun, 04 Oct 2015 08:14:41 GMT', u'_status': u'OK', u'_id': u'5610dff1f9e3854415d129be', u'_etag': u'26569a70b70d9ea2f5df5983a685c41278db23cf'}
        print response
        assert_true("_updated" in response)

        parameters = frontend.get_user_preferences("admin", "test_prefs")
        # u'_updated': u'Sun, 04 Oct 2015 08:19:08 GMT', u'data': {u'a': 1, u'b': u'2'}, u'_links': {u'self': {u'href': u'uipref/5610dff1f9e3854415d129be', u'title': u'Uipref'}}, u'user': u'admin', u'_created': u'Sun, 04 Oct 2015 08:14:41 GMT', u'_id': u'5610dff1f9e3854415d129be', u'type': u'test_prefs', u'_etag': u'8c05b7d8ee64d18e10a98a7885569c48f4a68d13'}
        print parameters
        assert_true("data" in parameters)
        assert_true("a" in parameters["data"])
        assert_true(parameters["data"]["a"] == 1)
        assert_true("b" in parameters["data"])
        assert_true(parameters["data"]["b"] == "2")

        parameters = {"a": 2, "b": "3"}
        response = frontend.set_user_preferences("admin", "test_prefs", parameters)
        print response
        assert_true("_updated" in response)

        parameters = frontend.get_user_preferences("admin", "test_prefs")
        # u'_updated': u'Sun, 04 Oct 2015 08:19:08 GMT', u'data': {u'a': 1, u'b': u'2'}, u'_links': {u'self': {u'href': u'uipref/5610dff1f9e3854415d129be', u'title': u'Uipref'}}, u'user': u'admin', u'_created': u'Sun, 04 Oct 2015 08:14:41 GMT', u'_id': u'5610dff1f9e3854415d129be', u'type': u'test_prefs', u'_etag': u'8c05b7d8ee64d18e10a98a7885569c48f4a68d13'}
        print parameters
        assert_true("data" in parameters)
        assert_true("a" in parameters["data"])
        assert_true(parameters["data"]["a"] == 2)
        assert_true("b" in parameters["data"])
        assert_true(parameters["data"]["b"] == "3")

        connection = frontend.logout()
        assert_false(frontend.authenticated)
        assert_false(frontend.connected)
        assert_false(frontend.token)