def test_pull_request(self): """POST /payload (pull_request) performs the checks""" queue = self.MyQueue() # Replace the default Redis queue app.config["queue"] = queue pull_request_event = { "action": "opened", "number": 1, "pull_request": { "title": "Lorem ipsum", "url": "https://github.com/pulls/1", "commits_url": "https://github.com/pulls/1/commits", "statuses_url": "https://github.com/pulls/1/statuses", "head": { "sha": "1" } } } tester = app.test_client(self) response = tester.post("/payload", content_type="application/json", headers=(("X-GitHub-Event", "pull_request"), ("X-GitHub-Delivery", "1")), data=json.dumps(pull_request_event)) assert_that(response.status_code, equal_to(200)) body = json.loads(response.data) assert_that(body["payload"]["state"], equal_to("pending")) (fn, pull_request_url, status_url, config) = queue.dequeue() self.assertEquals(pull_request, fn) assert_that(fn, equal_to(pull_request)) assert_that(pull_request_url, equal_to("https://github.com/pulls/1"))
def test_ping_fail(self): """Ping is expected to be JSON encoded.""" tester = app.test_client(self) response = tester.post( "/payload", headers=(("X-GitHub-Event", "ping"), ("X-GitHub-Delivery", "1")), data="not JSON" ) body = json.loads(response.data) self.assertEqual(500, response.status_code) self.assertEqual(u"failure", body["status"])
def test_ping(self): """POST /payload (ping) is ignored by kwalitee""" tester = app.test_client(self) response = tester.post("/payload", content_type="application/json", headers=(("X-GitHub-Event", "ping"), ("X-GitHub-Delivery", "1")), data=json.dumps({"hook_id": 1, "zen": "Responsive is better " "than fast."})) assert_that(response.status_code, equal_to(200))
def test_ping(self): """Ping should be silently ignored by kwalitee.""" tester = app.test_client(self) response = tester.post( "/payload", content_type="application/json", headers=(("X-GitHub-Event", "ping"), ("X-GitHub-Delivery", "1")), data=json.dumps({"hook_id": 1, "zen": "Responsive is better " "than fast."}), ) self.assertEqual(200, response.status_code)
def test_ping_no_headers(self): """POST /payload (ping) expects a X-GitHub-Event header""" tester = app.test_client(self) response = tester.post("/payload", data=json.dumps({"hook_id": 1, "zen": "Responsive is better " "than fast."})) body = json.loads(response.data) assert_that(response.status_code, equal_to(500)) assert_that(body["exception"], equal_to("No X-GitHub-Event HTTP header found")) assert_that(body["status"], equal_to(u"failure"))
def test_not_a_ping(self): """POST /payload (pong) rejects an unknown event""" tester = app.test_client(self) response = tester.post("/payload", headers=(("X-GitHub-Event", "pong"), ("X-GitHub-Delivery", "1")), data=json.dumps({"hook_id": 1, "zen": "Responsive is better " "than fast."})) body = json.loads(response.data) assert_that(response.status_code, equal_to(500)) assert_that(body["exception"], equal_to("Event pong is not supported")) assert_that(body["status"], equal_to(u"failure"))
def test_pull_request(self): """Pull request should do the checks...""" commits = [{ "url": "https://github.com/pulls/1/commits", "sha": 1, "comments_url": "https://github.com/commits/1/comments", "commit": { "message": "fix all the bugs!" } }] httpretty.register_uri(httpretty.GET, "https://github.com/pulls/1/commits", body=json.dumps(commits), content_type="application/json") comment = {"id": 1} httpretty.register_uri(httpretty.POST, "https://github.com/commits/1/comments", status=201, body=json.dumps(comment), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://github.com/pulls/1/statuses", status=201, body=json.dumps(status), content_type="application/json") tester = app.test_client(self) pull_request = { "action": "opened", "number": 1, "pull_request": { "url": "https://github.com/pulls/1", "commits_url": "https://github.com/pulls/1/commits", "statuses_url": "https://github.com/pulls/1/statuses", "head": { "sha": "1" } } } response = tester.post("/payload", content_type="application/json", headers=(("X-GitHub-Event", "pull_request"), ("X-GitHub-Delivery", "1")), data=json.dumps(pull_request)) body = json.loads(httpretty.last_request().body) self.assertEqual(200, response.status_code) self.assertEqual(u"error", body["state"])
def test_simple_status(self): """GET /status/sha1 displays the associated text file""" sha = "deadbeef" instance_path = tempfile.mkdtemp() app.instance_path = instance_path filename = os.path.join(app.instance_path, "status_{0}.txt".format(sha)) with open(filename, "w+") as f: f.write("\n".join(["{0}: Signature missing", "{0}: Needs more reviewers"]).format(sha)) tester = app.test_client(self) response = tester.get("status/{0}".format(sha)) assert_that(response.status_code, equal_to(200)) assert_that(str(response.data), contains_string("Signature missing")) assert_that(str(response.data), contains_string("Needs more reviewers")) shutil.rmtree(instance_path)
def test_simple_status(self): """GET / displays some recent statuses""" instance_path = tempfile.mkdtemp() app.instance_path = instance_path filenames = [] for sha in range(10): filename = os.path.join(app.instance_path, "status_{0}.txt".format(sha)) with open(filename, "w+") as f: f.write("\n".join(["{0}: Signature missing", "{0}: Needs more reviewers"]).format(sha)) filenames.append(filename) tester = app.test_client(self) response = tester.get("/") assert_that(response.status_code, equal_to(200)) for sha in range(10): assert_that("/status/{0}".format(sha), is_in(str(response.data))) shutil.rmtree(instance_path)