Exemple #1
0
    def testURLCombinations(self):

        (a, p, c, r) = (self.a, self.p, self.c, self.r)

        # Test request with component + component ID
        self.assertEqual(r.url(method="", id=5),
                         "/%s/pr/person/5/contact.xml?test=test" % a)
        self.assertEqual(r.url(method="", vars=None),
                         "/%s/pr/person/%s/contact.xml?test=test" % (a, p))
        self.assertEqual(
            r.url(id="[id]", method="review"),
            "/%s/pr/person/%%5Bid%%5D/contact/review.xml?test=test" % a)
        self.assertEqual(r.url(method="deduplicate", target=0, vars={}),
                         "/%s/pr/person/%s/contact/deduplicate.xml" % (a, p))

        # Test request with component (without component ID)
        r = S3Request(prefix="pr",
                      name="person",
                      c="pr",
                      f="person",
                      args=[self.p, "contact", "method"],
                      vars=Storage(format="xml", test="test"))

        self.assertEqual(r.url(method="", id=5),
                         "/%s/pr/person/5/contact.xml?test=test" % a)
        self.assertEqual(r.url(method="", vars=None),
                         "/%s/pr/person/%s/contact.xml?test=test" % (a, p))
        self.assertEqual(
            r.url(id="[id]", method="review"),
            "/%s/pr/person/%%5Bid%%5D/contact/review.xml?test=test" % a)
        self.assertEqual(r.url(method="deduplicate", target=0, vars={}),
                         "/%s/pr/person/%s/contact/deduplicate.xml" % (a, p))

        # Test request without component
        r = S3Request(prefix="pr",
                      name="person",
                      c="pr",
                      f="person",
                      args=[self.p, "method"],
                      vars=Storage(format="xml", test="test"))

        self.assertEqual(r.url(method="", id=5),
                         "/%s/pr/person/5.xml?test=test" % a)
        self.assertEqual(r.url(method="", vars=None),
                         "/%s/pr/person.xml?test=test" % a)
        self.assertEqual(r.url(id="[id]", method="review"),
                         "/%s/pr/person/%%5Bid%%5D/review.xml?test=test" % a)
        self.assertEqual(r.url(method="deduplicate", target=0, vars={}),
                         "/%s/pr/person/deduplicate.xml" % a)
Exemple #2
0
def test_pdf2():
    """
        Generate a Test PDF that will demonstrate S3RL_PDF functionality
    """
    r = S3Request(prefix="gis", name="hierarchy")
    from s3.s3export import S3Exporter
    exporter = S3Exporter().pdf
    return exporter(r.resource,
                    request=r,
                    method="list",
                    pdf_title=T("PDF Test Card II"),
                    pdf_table_autogrow="B",
                    pdf_header=header,
                    pdf_footer=footer)
Exemple #3
0
def test_pdf1():
    """
        Generate a Test PDF that will demonstrate S3RL_PDF functionality
    """
    r = S3Request(prefix="pr", name="person")
    T = current.T
    from s3.s3export import S3Exporter
    exporter = S3Exporter().pdf
    return exporter(r.resource,
                    request=r,
                    method="list",
                    pdf_title=T("PDF Test Card I"),
                    pdf_header=header,
                    pdf_callback=body_1,
                    pdf_footer=footer)
Exemple #4
0
    def setUp(self):

        current.auth.override = True

        s3db = current.s3db
        ptable = s3db.pr_person
        ctable = s3db.pr_contact
        if not hasattr(self, "r"):
            record = current.db(ptable.pe_id == ctable.pe_id).select(
                ctable.id, ptable.id, limitby=(0, 1)).first()
            self.assertNotEqual(record, None)
            self.a = current.request.application
            self.p = str(record[ptable.id])
            self.c = str(record[ctable.id])
            self.r = S3Request(prefix="pr",
                               name="person",
                               c="pr",
                               f="person",
                               args=[self.p, "contact", self.c, "method"],
                               vars=Storage(format="xml", test="test"))
Exemple #5
0
    def testURLTargetOverrideMaster(self):

        (a, p, c, r) = (self.a, self.p, self.c, self.r)
        r = S3Request(prefix="pr",
                      name="person",
                      c="pr",
                      f="person",
                      args=[self.p, "method"],
                      vars=Storage(format="xml", test="test"))

        # No change
        self.assertEqual(r.url(target=None),
                         "/%s/pr/person/%s/method.xml?test=test" % (a, p))
        self.assertEqual(r.url(target=p),
                         "/%s/pr/person/%s/method.xml?test=test" % (a, p))

        # Set to None (resets method)
        self.assertEqual(r.url(target=0), "/%s/pr/person.xml?test=test" % a)

        # Change target ID (retains method)
        self.assertEqual(r.url(target=5),
                         "/%s/pr/person/5/method.xml?test=test" % a)
Exemple #6
0
    def testURLMethodOverride(self):

        (a, p, c, r) = (self.a, self.p, self.c, self.r)

        # No change
        self.assertEqual(
            r.url(method=None),
            "/%s/pr/person/%s/contact/%s/method.xml?test=test" % (a, p, c))

        # Set to None (resets target record ID)
        self.assertEqual(r.url(method=""),
                         "/%s/pr/person/%s/contact.xml?test=test" % (a, p))

        # Change method (retains target record ID)
        self.assertEqual(
            r.url(method="read"),
            "/%s/pr/person/%s/contact/%s/read.xml?test=test" % (a, p, c))

        # Test without component
        r = S3Request(prefix="pr",
                      name="person",
                      c="pr",
                      f="person",
                      args=[self.p, "method"],
                      vars=Storage(format="xml", test="test"))

        # No change
        self.assertEqual(r.url(method=None),
                         "/%s/pr/person/%s/method.xml?test=test" % (a, p))

        # Set to None (resets target record ID and method)
        self.assertEqual(r.url(method=""), "/%s/pr/person.xml?test=test" % a)

        # Change method (retains target record ID)
        self.assertEqual(r.url(method="read"),
                         "/%s/pr/person/%s/read.xml?test=test" % (a, p))
Exemple #7
0
    def testPOSTFilter(self):
        """ Test POST filter interpretation with multipart request body """

        assertEqual = self.assertEqual
        assertNotIn = self.assertNotIn
        assertIn = self.assertIn

        request = current.request
        request.env.content_type = "multipart/form-data"

        # Test with valid filter expression JSON
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "form",
                "test": "retained"
            },
            post_vars={
                "~.name|~.comments__like": '''["first","second"]''',
                "~.other_field__lt": '''"1"''',
                "multi.nonstr__belongs": '''[1,2,3]''',
                "service_organisation.service_id__belongs": "1",
                "other": "testing",
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars
        post_vars = r.post_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Filter queries from POST vars JSON-decoded and added to GET vars:
        assertEqual(get_vars.get("~.name|~.comments__like"),
                    ["first", "second"])
        assertEqual(get_vars.get("~.other_field__lt"), "1")

        # Edge-cases (non-str values) properly converted:
        assertEqual(get_vars.get("multi.nonstr__belongs"), ["1", "2", "3"])
        assertEqual(get_vars.get("service_organisation.service_id__belongs"),
                    "1")

        # Filter queries removed from POST vars:
        assertNotIn("~.name|~.comments__like", post_vars)
        assertNotIn("~.other_field__lt", post_vars)
        assertNotIn("service_organisation.service_id__belongs", post_vars)

        # Non-queries retained in POST vars:
        assertIn("other", post_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test without $search
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={"test": "retained"},
            post_vars={
                "service_organisation.service_id__belongs": "1",
                "other": "testing",
            },
        )

        # Method should still be POST:
        assertEqual(r.http, "POST")
        get_vars = r.get_vars
        post_vars = r.post_vars

        # $search never was in GET vars - confirm this to exclude test regression
        assertNotIn("$search", get_vars)

        # Filter queries from POST vars not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Filter queries still in POST vars:
        assertIn("service_organisation.service_id__belongs", post_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")
Exemple #8
0
    def testPOSTFilterAjax(self):
        """ Test POST filter interpretation with JSON request body """

        assertEqual = self.assertEqual
        assertNotIn = self.assertNotIn

        request = current.request

        # Test with valid filter expression JSON
        jsonstr = '''{"service_organisation.service_id__belongs":"1","~.example__lt":1,"~.other__like":[1,2]}'''
        request._body = StringIO(jsonstr)
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body added to GET vars (always str, or list of str):
        assertEqual(get_vars.get("service_organisation.service_id__belongs"),
                    "1")
        assertEqual(get_vars.get("~.example__lt"), "1")
        assertEqual(get_vars.get("~.other__like"), ["1", "2"])

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test without $search
        request._body = StringIO(
            '{"service_organisation.service_id__belongs":"1"}')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={"test": "retained"},
        )

        # Method should still be POST:
        assertEqual(r.http, "POST")
        get_vars = r.get_vars

        # $search never was in GET vars - confirm this to exclude test regression
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test with valid JSON but invalid filter expression
        request._body = StringIO('[1,2,3]')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test with empty body
        request._body = StringIO('')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")
Exemple #9
0
    def testPOSTFilterAjax(self):
        """ Test POST filter interpretation with JSON request body """

        assertEqual = self.assertEqual
        assertNotIn = self.assertNotIn

        request = current.request

        # Test with valid filter expression JSON
        jsonstr = '''{"service_organisation.service_id__belongs":"1","~.example__lt":1,"~.other__like":[1,2],"~.name__like":"*Liquiçá*"}'''
        request._body = StringIO(jsonstr)
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Verify that parsed $filter vars can safely be re-encoded as GET URL
        try:
            r.url()
        except (UnicodeDecodeError, UnicodeEncodeError):
            self.fail(
                "r.url raises Unicode exception with non-ASCII characters in $filter"
            )

        # Filter queries from JSON body added to GET vars (always str, or list of str):
        assertEqual(get_vars.get("service_organisation.service_id__belongs"),
                    "1")
        assertEqual(get_vars.get("~.example__lt"), "1")
        assertEqual(get_vars.get("~.other__like"), ["1", "2"])
        assertEqual(get_vars.get("~.name__like"), "*Liquiçá*")

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test without $search
        request._body = StringIO(
            '{"service_organisation.service_id__belongs":"1"}')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={"test": "retained"},
        )

        # Method should still be POST:
        assertEqual(r.http, "POST")
        get_vars = r.get_vars

        # $search never was in GET vars - confirm this to exclude test regression
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test with valid JSON but invalid filter expression
        request._body = StringIO('[1,2,3]')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test with empty body
        request._body = StringIO('')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
        )

        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars

        # $search removed from GET vars:
        assertNotIn("$search", get_vars)

        # Filter queries from JSON body not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)

        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")
Exemple #10
0
    def testPOSTFilter(self):
        """ Test POST filter interpretation with multipart request body """

        assertEqual = self.assertEqual
        assertNotIn = self.assertNotIn
        assertIn = self.assertIn

        request = current.request
        request.env.content_type = "multipart/form-data"

        # Test with valid filter expression JSON
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "form",
                "test": "retained"
            },
            post_vars={
                "service_organisation.service_id__belongs": "1",
                "other": "testing",
            },
        )
        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars
        post_vars = r.post_vars
        # $search removed from GET vars:
        assertNotIn("$search", get_vars)
        # Filter queries from POST vars added to GET vars:
        assertEqual(get_vars.get("service_organisation.service_id__belongs"),
                    "1")
        # Filter queries removed from POST vars:
        assertNotIn("service_organisation.service_id__belongs", post_vars)
        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test without $search
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={"test": "retained"},
            post_vars={
                "service_organisation.service_id__belongs": "1",
                "other": "testing",
            },
        )
        # Method should still be POST:
        assertEqual(r.http, "POST")
        get_vars = r.get_vars
        post_vars = r.post_vars
        # $search never was in GET vars - confirm this to exclude test regression
        assertNotIn("$search", get_vars)
        # Filter queries from POST vars not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)
        # Filter queries still in POST vars:
        assertIn("service_organisation.service_id__belongs", post_vars)
        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")

        # Test with empty post vars
        request._body = StringIO('')
        r = S3Request(
            prefix="org",
            name="organisation",
            http="POST",
            get_vars={
                "$search": "ajax",
                "test": "retained"
            },
            post_vars={
                "service_organisation.service_id__belongs": "1",
                "other": "testing",
            },
        )
        # Method changed to GET:
        assertEqual(r.http, "GET")
        get_vars = r.get_vars
        post_vars = r.post_vars
        # $search removed from GET vars:
        assertNotIn("$search", get_vars)
        # Filter queries from POST vars not added to GET vars:
        assertNotIn("service_organisation.service_id__belongs", get_vars)
        # Filter queries still in POST vars:
        assertIn("service_organisation.service_id__belongs", post_vars)
        # Must retain other GET vars:
        assertEqual(get_vars.get("test"), "retained")