def test_push_wip_commit(app, repository): """Worker push /commits/1 has wip as a component and is ignored""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "wip: herp derp\n\nBy: John Doe <*****@*****.**>", }, "files": [{ "filename": "spam/__init__.py", "status": "added", "raw_url": "https://github.com/raw/1/spam/__init__.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"]) assert_that(cs.is_pending()) httpretty.enable() push( cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", { "COMPONENTS": ["comp"], "SIGNATURES": ["By"], "TRUSTED_DEVELOPERS": ["*****@*****.**"], "CHECK_LICENSE": False, "repository": repository.id }) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(2), "1x GET, 1x POST") expected_requests = ["", "success"] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected)) cs = CommitStatus.query.filter_by(repository_id=repository.id, sha=commit["sha"]).first() assert_that(cs) assert_that(cs.state, equal_to("success")) assert_that(cs.errors, equal_to(0))
def test_push_broken_commit_message(app, repository): """Worker push /commits/1 is invalid (message)""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "Fix all the bugs!" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") eggs_py = '"""Eggs are boiled."""\n' httpretty.register_uri(httpretty.GET, "https://github.com/raw/1/spam/eggs.py", body=eggs_py, content_type="text/plain") httpretty.register_uri(httpretty.POST, "https://api.github.com/commits/1/comments", status=201, body=json.dumps({"id": 1}), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"]) assert_that(cs.is_pending()) httpretty.enable() push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", { "CHECK_LICENSE": False, "repository": repository.id }) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST") expected_requests = ["", "needs more reviewers", "", "error"] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected))
def test_push_wip_commit(app, repository): """Worker push /commits/1 has wip as a component and is ignored""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "wip: herp derp\n\nBy: John Doe <*****@*****.**>", }, "files": [{ "filename": "spam/__init__.py", "status": "added", "raw_url": "https://github.com/raw/1/spam/__init__.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"]) assert_that(cs.is_pending()) httpretty.enable() push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"COMPONENTS": ["comp"], "SIGNATURES": ["By"], "TRUSTED_DEVELOPERS": ["*****@*****.**"], "CHECK_LICENSE": False, "repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(2), "1x GET, 1x POST") expected_requests = [ "", "success" ] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected)) cs = CommitStatus.query.filter_by(repository_id=repository.id, sha=commit["sha"]).first() assert_that(cs) assert_that(cs.state, equal_to("success")) assert_that(cs.errors, equal_to(0))
def test_push_known_commit(repository, session): """Worker push /commits/1 is not rechecked if known""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "Fix all the bugs!" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") cs = CommitStatus(repository, "1", "https://github.com/commits/1", { "message": ["error 1", "error 2"], "files": {} }) session.add(cs) session.commit() assert_that(cs.is_pending(), equal_to(False)) httpretty.enable() body = push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(1), "1x GET") assert_that(body["description"], contains_string("[error] 2 errors"))
def test_push_known_commit(repository, session): """Worker push /commits/1 is not rechecked if known""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "Fix all the bugs!" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") cs = CommitStatus(repository, "1", "https://github.com/commits/1", {"message": ["error 1", "error 2"], "files": {}}) session.add(cs) session.commit() assert_that(cs.is_pending(), equal_to(False)) httpretty.enable() body = push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(1), "1x GET") assert_that(body["description"], contains_string("[error] 2 errors"))
def test_push_half_known_commit(repository, session): """Worker push /commits/1 checks the files if none""" httpretty.reset() commit = { "sha": "1", "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "Fix all the bugs!" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") eggs_py = "if foo == bar:\n print('derp')\n" httpretty.register_uri(httpretty.GET, "https://github.com/raw/1/spam/eggs.py", body=eggs_py, content_type="text/plain") httpretty.register_uri(httpretty.POST, "https://api.github.com/commits/1/comments", status=201, body=json.dumps({"id": 1}), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus(repository, "1", "https://github.com/commits/1", { "message": [], "files": None }) session.add(cs) session.commit() assert_that(cs.is_pending(), equal_to(False)) httpretty.enable() push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST") expected_requests = ["", "", "F821 undefined name 'foo'", "error"] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected)) cs = CommitStatus.query.filter_by(id=cs.id).first() assert_that(cs) assert_that(cs.is_pending(), equal_to(False)) assert_that(cs.content["files"]["spam/eggs.py"]["errors"], has_item("1: D100 Missing docstring in public module"))
def test_push_broken_files(repository): """Worker push /commits/1 is invalid (files)""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "comp: bob\n\nBy: John <*****@*****.**>" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") eggs_py = "if foo == bar:\n print('derp')\n" httpretty.register_uri(httpretty.GET, "https://github.com/raw/1/spam/eggs.py", body=eggs_py, content_type="text/plain") httpretty.register_uri(httpretty.POST, "https://api.github.com/commits/1/comments", status=201, body=json.dumps({"id": 1}), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"]) assert_that(cs.is_pending()) httpretty.enable() push( cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", { "COMPONENTS": ["comp"], "SIGNATURES": ["By"], "TRUSTED_DEVELOPERS": ["*****@*****.**"], "repository": repository.id }) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST") expected_requests = ["", "", "F821 undefined name 'foo'", "error"] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected))
def test_push_half_known_commit(repository, session): """Worker push /commits/1 checks the files if none""" httpretty.reset() commit = { "sha": "1", "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "Fix all the bugs!" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") eggs_py = "if foo == bar:\n print('derp')\n" httpretty.register_uri(httpretty.GET, "https://github.com/raw/1/spam/eggs.py", body=eggs_py, content_type="text/plain") httpretty.register_uri(httpretty.POST, "https://api.github.com/commits/1/comments", status=201, body=json.dumps({"id": 1}), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus(repository, "1", "https://github.com/commits/1", {"message": [], "files": None}) session.add(cs) session.commit() assert_that(cs.is_pending(), equal_to(False)) httpretty.enable() push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST") expected_requests = [ "", "", "F821 undefined name 'foo'", "error" ] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected)) cs = CommitStatus.query.filter_by(id=cs.id).first() assert_that(cs) assert_that(cs.is_pending(), equal_to(False)) assert_that(cs.content["files"]["spam/eggs.py"]["errors"], has_item("1: D100 Missing docstring in public module"))
def test_push_broken_files(repository): """Worker push /commits/1 is invalid (files)""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "comp: bob\n\nBy: John <*****@*****.**>" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") eggs_py = "if foo == bar:\n print('derp')\n" httpretty.register_uri(httpretty.GET, "https://github.com/raw/1/spam/eggs.py", body=eggs_py, content_type="text/plain") httpretty.register_uri(httpretty.POST, "https://api.github.com/commits/1/comments", status=201, body=json.dumps({"id": 1}), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"]) assert_that(cs.is_pending()) httpretty.enable() push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"COMPONENTS": ["comp"], "SIGNATURES": ["By"], "TRUSTED_DEVELOPERS": ["*****@*****.**"], "repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST") expected_requests = [ "", "", "F821 undefined name 'foo'", "error" ] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected))
def test_push_broken_commit_message(app, repository): """Worker push /commits/1 is invalid (message)""" httpretty.reset() commit = { "sha": 1, "url": "https://api.github.com/commits/1", "html_url": "https://github.com/commits/1", "comments_url": "https://api.github.com/commits/1/comments", "commit": { "message": "Fix all the bugs!" }, "files": [{ "filename": "spam/eggs.py", "status": "modified", "raw_url": "https://github.com/raw/1/spam/eggs.py" }] } httpretty.register_uri(httpretty.GET, "https://api.github.com/commits/1", body=json.dumps(commit), content_type="application/json") eggs_py = '"""Eggs are boiled."""\n' httpretty.register_uri(httpretty.GET, "https://github.com/raw/1/spam/eggs.py", body=eggs_py, content_type="text/plain") httpretty.register_uri(httpretty.POST, "https://api.github.com/commits/1/comments", status=201, body=json.dumps({"id": 1}), content_type="application/json") status = {"id": 1, "state": "success"} httpretty.register_uri(httpretty.POST, "https://api.github.com/statuses/1", status=201, body=json.dumps(status), content_type="application/json") cs = CommitStatus.find_or_create(repository, commit["sha"], commit["url"]) assert_that(cs.is_pending()) httpretty.enable() push(cs.id, "https://api.github.com/commits/1", "https://api.github.com/statuses/1", {"CHECK_LICENSE": False, "repository": repository.id}) httpretty.disable() latest_requests = httpretty.HTTPretty.latest_requests assert_that(len(latest_requests), equal_to(4), "2x GET, 2x POST") expected_requests = [ "", "needs more reviewers", "", "error" ] for expected, request in zip(expected_requests, latest_requests): assert_that(str(request.parsed_body), contains_string(expected))