Пример #1
0
    def test_update_many(self):
        w = self.app
        env = dict(REMOTE_ADDR="192.168.0.1")   # has access without key.

        changes = [
            Change("r1001", "root", "fancy comments",
                   ["/", "/goanna", "/goanna/webapi"], "goanna", "master"),
            Change("r2001", "lsc", "fly fishing is awesome", "/", "pyqa"),
        ]

        # serialise transaction data
        payload = dict(data=Goanna.pack_list_of_changes(changes))

        # post bulk update
        w.post("/update", payload, extra_environ=env, status=ACCESS_OK)

        # query results
        r = w.get("/query/goanna/master", status=ACCESS_OK)
        data = unpack(r.body)
        self.assertTrue(data.get("revision", None) == "r1001")

        r = w.get("/query/pyqa", status=ACCESS_OK)
        data = unpack(r.body)
        self.assertEqual(data.get("revision", None), "r2001")

        w.get("/query/pyqa/master", status=NOT_FOUND)

        # invalid data
        bad_payload = dict(data=Goanna.pack_list_of_changes(changes)[3:])
        w.post("/update", bad_payload, extra_environ=env, status=BAD_REQUEST)
Пример #2
0
    def test_basic_query(self):
        g = self.goanna

        # query unknown data
        self.assertEqual(g.query_range("p0"), [])
        self.assertEqual(g.query_range("p0", "b0"), [])
        self.assertEqual(g.query_latest("p0"), None)
        self.assertEqual(g.query_latest("p0", "b0"), None)

        # add change
        c = Change("r123", "lsc", "This is a fake commit", [
            "/hello/world/.",
            "/hello/world/test.txt",
            "/hello/kitty/is/too_pink.com",
            "/donny/darko/.now",
            "/he/.",
        ], "project")
        g.post_changes(c)

        # this should not affect unknown projects
        self.assertEqual(g.query_latest("p0"), None)

        # query for latest change
        self.assertEqual(g.query_latest("project")[0], "r123")
        self.assertEqual(g.query_latest("project", None)[0], "r123")
        self.assertEqual(g.query_latest("project", "nobranch"), None)

        self.assertTrue(utils.unpack(g.query_latest("project", details=True)),
                        c.get_data())
        ts0 = g.query_latest("project")[1]

        sleep(0.05)  # sleep a while so we get a different timestamp
        # add multiple changes
        changes = [
            Change("897", "lsc", "commit", ["/", "/a"], "p0", "b0"),
            Change("s91", "djw", "comment", "/", "project"),
            Change("deadbeef", "root", "none", "/world", "p2", "b2"),
        ]
        g.post_changes(changes)
        self.assertEqual(g.query_latest("project", None)[0], "s91")

        ts1 = g.query_latest("project")[1]
        self.assertNotEqual(ts0, ts1)

        self.assertEqual(g.query_range("project", ts_upper=ts0), [])
        self.assertEqual(g.query_range("project", ts_lower=ts1), [])
        self.assertEqual(g.query_range("project", ts_lower=ts0), ["s91"])
        self.assertEqual(g.query_range("project", ts_upper=ts1), ["r123"])
        self.assertEqual(g.query_range("project"), ["r123", "s91"])

        r_list = g.query_range("project", details=True)
        self.assertTrue(utils.unpack(r_list[0]), c.get_data())
        self.assertTrue(utils.unpack(r_list[0]), changes[1].get_data())

        # test packing of list of Change instance
        packed_data = Goanna.pack_list_of_changes(changes)
        changes_2 = Goanna.unpack_list_of_changes(packed_data)
        self.assertEqual(changes, changes_2)
Пример #3
0
    def test_standard_update_and_query(self):
        w = self.app
        env = dict(REMOTE_ADDR="192.168.0.1")   # has access without key.

        # query unknown content
        w.get("/query/p0/b0", status=NOT_FOUND)
        w.get("/query/p0/b0?from=123;to=5325", status=NOT_FOUND)

        # post some content
        c = Change("r123", "lsc", "no comments", ["/"], "p1")
        payload = dict(data=Goanna.pack_list_of_changes([c]))
        w.post("/update", payload, extra_environ=env, status=ACCESS_OK)

        # other branch should not exist
        w.get("/query/p1/b0", status=NOT_FOUND)

        # validate content
        r = w.get("/query/p1?brief", status=ACCESS_OK)  # brief version
        R = unpack(r.body)
        self.assertTrue(R["revision"] == c.revision)
        self.assertTrue("files" not in R)

        s = w.get("/query/p1", status=ACCESS_OK)
        S = unpack(s.body)
        self.assertTrue(S["revision"] == c.revision)
        self.assertTrue(S["project"] == c.project)
        self.assertTrue(S["branch"] == c.branch)
        self.assertTrue(S["files"] == c.data["files"])
        self.assertTrue(S["comments"] == c.data["comments"])
Пример #4
0
 def _generate_payload(self):
     changes = self.pending_changes
     self.pending_changes = []
     payload = dict(data=Goanna.pack_list_of_changes(changes))
     if self.key:
         payload["key"] = self.key
     return urlencode(payload)
Пример #5
0
    def test_access_control(self):
        w = self.app
        changes = [Change("r123", "lsc", "no comments", ["/"], "p0")]
        payload = dict(data=Goanna.pack_list_of_changes(changes))

        env = dict(REMOTE_ADDR="8.8.8.8")   # should have no access
        w.post("/update", payload, extra_environ=env, status=FORBIDDEN)

        env = dict(REMOTE_ADDR="192.168.0.1")   # Can access without key
        w.post("/update", payload, extra_environ=env, status=ACCESS_OK)

        env = dict(REMOTE_ADDR="192.168.0.55")  # no access without key
        w.post("/update", payload, extra_environ=env, status=UNAUTHORISED)

        payload["key"] = "wrong_key"    # wrong key. still no access
        w.post("/update", payload, extra_environ=env, status=UNAUTHORISED)

        payload["key"] = "rahsia"   # key OK
        w.post("/update", payload, extra_environ=env, status=ACCESS_OK)