예제 #1
0
def test_push(app, repository):
    """POST /payload (push) performs the checks"""
    queue = MyQueue()
    # Replace the default Redis queue
    app.config["queue"] = queue

    push_event = {
        "commits": [{
            "id": "1",
            "url": "https://github.com/commits/1"
        }, {
            "id": "2",
            "url": "https://github.com/commits/2"
        }],
        "repository": {
            "name": "test",
            "owner": {
                "name": "invenio"
            }
        }
    }

    tester = app.test_client()
    response = tester.post("/payload",
                           content_type="application/json",
                           headers=(("X-GitHub-Event", "push"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(push_event))

    assert_that(response.status_code, equal_to(200))
    body = json.loads(response.data)
    assert_that(body["payload"]["state"], equal_to("pending"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="1").first()

    (fn, commit_id, commit_url, status_url, config) = queue.dequeue()
    assert_that(fn, equal_to(push))
    assert_that(commit_id, equal_to(cs.id))
    assert_that(
        commit_url,
        equal_to("https://api.github.com"
                 "/repos/invenio/test/commits/1"))
    assert_that(status_url, contains_string("/invenio/test/commits/1"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="2").first()

    (fn, commit_id, commit_url, status_url, config) = queue.dequeue()
    assert_that(fn, equal_to(push))
    assert_that(commit_id, equal_to(cs.id))
    assert_that(
        commit_url,
        equal_to("https://api.github.com"
                 "/repos/invenio/test/commits/2"))
    assert_that(status_url, contains_string("/invenio/test/commits/2"))
예제 #2
0
def test_push(app, repository):
    """POST /payload (push) performs the checks"""
    queue = MyQueue()
    # Replace the default Redis queue
    app.config["queue"] = queue

    push_event = {
        "commits": [{
            "id": "1",
            "url": "https://github.com/commits/1"
        }, {
            "id": "2",
            "url": "https://github.com/commits/2"
        }],
        "repository": {
            "name": "test",
            "owner": {
                "name": "invenio"
            }
        }
    }

    tester = app.test_client()
    response = tester.post("/payload", content_type="application/json",
                           headers=(("X-GitHub-Event", "push"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(push_event))

    assert_that(response.status_code, equal_to(200))
    body = json.loads(response.data)
    assert_that(body["payload"]["state"], equal_to("pending"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="1").first()

    (fn, commit_id, commit_url, status_url, config) = queue.dequeue()
    assert_that(fn, equal_to(push))
    assert_that(commit_id, equal_to(cs.id))
    assert_that(commit_url, equal_to("https://api.github.com"
                                     "/repos/invenio/test/commits/1"))
    assert_that(status_url, contains_string("/invenio/test/commits/1"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="2").first()

    (fn, commit_id, commit_url, status_url, config) = queue.dequeue()
    assert_that(fn, equal_to(push))
    assert_that(commit_id, equal_to(cs.id))
    assert_that(commit_url, equal_to("https://api.github.com"
                                     "/repos/invenio/test/commits/2"))
    assert_that(status_url, contains_string("/invenio/test/commits/2"))
예제 #3
0
def test_push_to_unknown_repository(app):
    """POST /payload (push) with unknown repository should fails."""
    queue = MyQueue()
    config = dict(app.config)
    app.config["queue"] = queue

    push_event = {
        "commits": [{
            "id": "1",
            "url": "https://github.com/commits/1"
        }],
        "repository": {
            "name": "eggs",
            "owner": {
                "name": "spam"
            }
        }
    }

    tester = app.test_client()
    response = tester.post("/payload",
                           content_type="application/json",
                           headers=(("X-GitHub-Event", "push"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(push_event))

    assert_that(response.status_code, equal_to(200))
    body = json.loads(response.data)
    assert_that(body["payload"]["state"], equal_to("error"))
    assert_that(body["payload"]["description"], contains_string("spam/eggs"))
    assert_that(body["payload"]["context"],
                equal_to(app.config.get("CONTEXT")))

    app.config = config
예제 #4
0
def test_invalid_pull_request_action(app, owner, repository):
    """POST /payload (pull_request) performs the action checks"""
    httpretty.reset()
    queue = MyQueue()
    # Replace the default Redis queue
    app.config["queue"] = queue

    pull_request_event = {
        "action": "labeled",
        "number": 1,
        "pull_request": {
            "title": "Lorem ipsum",
            "url": "https://api.github.com/pulls/1",
            "html_url": "https://github.com/pulls/1",
            "commits_url": "https://api.github.com/pulls/1/commits",
            "statuses_url": "https://api.github.com/pulls/1/statuses",
            "head": {
                "sha": "2",
                "label": "spam:wip/my-branch",
                "ref": "wip/my-branch"
            }
        },
        "repository": {
            "name": "test",
            "owner": {
                "login": "******"
            }
        }
    }
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps([]),
                           content_type="application/json")

    tester = app.test_client()
    httpretty.enable()

    response = tester.post("/payload",
                           content_type="application/json",
                           headers=(("X-GitHub-Event", "pull_request"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(pull_request_event))
    httpretty.disable()

    assert_that(response.status_code, equal_to(500))
예제 #5
0
def test_push_with_auto_create(app, repository):
    """POST /payload (push) performs the checks"""
    queue = MyQueue()
    # Replace the default Redis queue
    config = dict(app.config)
    app.config["queue"] = queue
    app.config["AUTO_CREATE"] = True

    push_event = {
        "commits": [{
            "id": "1",
            "url": "https://github.com/commits/1"
        }],
        "repository": {
            "name": "doe",
            "owner": {
                "name": "john"
            }
        }
    }

    tester = app.test_client()
    response = tester.post("/payload",
                           content_type="application/json",
                           headers=(("X-GitHub-Event", "push"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(push_event))

    assert_that(response.status_code, equal_to(200))
    body = json.loads(response.data)
    assert_that(body["payload"]["state"], equal_to("pending"))

    repo = Repository.query.filter_by(name="doe").first()
    assert_that(repo)
    assert_that(repo.owner.name, equal_to("john"))
    app.config = config
예제 #6
0
def test_pull_request(app, owner, repository):
    """POST /payload (pull_request) performs the checks"""
    httpretty.reset()
    queue = MyQueue()
    # Replace the default Redis queue
    app.config["queue"] = queue

    pull_request_event = {
        "action": "opened",
        "number": 1,
        "pull_request": {
            "title": "Lorem ipsum",
            "url": "https://api.github.com/pulls/1",
            "html_url": "https://github.com/pulls/1",
            "commits_url": "https://api.github.com/pulls/1/commits",
            "statuses_url": "https://api.github.com/pulls/1/statuses",
            "head": {
                "sha": "2",
                "label": "spam:wip/my-branch",
                "ref": "wip/my-branch"
            }
        },
        "repository": {
            "name": "test",
            "owner": {
                "login": "******"
            }
        }
    }

    commits = [
        {
            "url": "https://api.github.com/commits/1",
            "sha": "1",
            "html_url": "https://github.com/commits/1",
            "comments_url": "https://api.github.com/commits/1/comments",
            "commit": {
                "message": "herp derp"
            }
        }, {
            "url": "https://api.github.com/commits/2",
            "sha": "2",
            "html_url": "https://github.com/commits/2",
            "comments_url": "https://api.github.com/commits/2/comments",
            "commit": {
                "message": "fix all the bugs!"
            }
        }
    ]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")

    tester = app.test_client()
    httpretty.enable()
    response = tester.post("/payload", content_type="application/json",
                           headers=(("X-GitHub-Event", "pull_request"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(pull_request_event))
    httpretty.disable()

    assert_that(response.status_code, equal_to(200))
    body = json.loads(response.data)
    assert_that(body["payload"]["state"], equal_to("pending"))

    (fn, bs_id, pull_request_url, status_url, config) = queue.dequeue()
    assert_that(fn, equal_to(pull_request))
    assert_that(bs_id, greater_than(0))
    assert_that(pull_request_url,
                equal_to("https://api.github.com/pulls/1"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="1").first()
    assert_that(cs)
    assert_that(cs.state, equal_to("pending"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="2").first()
    assert_that(cs)
    assert_that(cs.state, equal_to("pending"))

    bs = BranchStatus.query.filter_by(commit_id=cs.id,
                                      name="spam:wip/my-branch").first()
    assert_that(bs)
    assert_that(bs.is_pending())
    assert_that(bs.state, equal_to("pending"))
예제 #7
0
def test_pull_request(app, owner, repository):
    """POST /payload (pull_request) performs the checks"""
    httpretty.reset()
    queue = MyQueue()
    # Replace the default Redis queue
    app.config["queue"] = queue

    pull_request_event = {
        "action": "opened",
        "number": 1,
        "pull_request": {
            "title": "Lorem ipsum",
            "url": "https://api.github.com/pulls/1",
            "html_url": "https://github.com/pulls/1",
            "commits_url": "https://api.github.com/pulls/1/commits",
            "statuses_url": "https://api.github.com/pulls/1/statuses",
            "head": {
                "sha": "2",
                "label": "spam:wip/my-branch",
                "ref": "wip/my-branch"
            }
        },
        "repository": {
            "name": "test",
            "owner": {
                "login": "******"
            }
        }
    }

    commits = [{
        "url": "https://api.github.com/commits/1",
        "sha": "1",
        "html_url": "https://github.com/commits/1",
        "comments_url": "https://api.github.com/commits/1/comments",
        "commit": {
            "message": "herp derp"
        }
    }, {
        "url": "https://api.github.com/commits/2",
        "sha": "2",
        "html_url": "https://github.com/commits/2",
        "comments_url": "https://api.github.com/commits/2/comments",
        "commit": {
            "message": "fix all the bugs!"
        }
    }]
    httpretty.register_uri(httpretty.GET,
                           "https://api.github.com/pulls/1/commits",
                           body=json.dumps(commits),
                           content_type="application/json")

    tester = app.test_client()
    httpretty.enable()
    response = tester.post("/payload",
                           content_type="application/json",
                           headers=(("X-GitHub-Event", "pull_request"),
                                    ("X-GitHub-Delivery", "1")),
                           data=json.dumps(pull_request_event))
    httpretty.disable()

    assert_that(response.status_code, equal_to(200))
    body = json.loads(response.data)
    assert_that(body["payload"]["state"], equal_to("pending"))

    (fn, bs_id, pull_request_url, status_url, config) = queue.dequeue()
    assert_that(fn, equal_to(pull_request))
    assert_that(bs_id, greater_than(0))
    assert_that(pull_request_url, equal_to("https://api.github.com/pulls/1"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="1").first()
    assert_that(cs)
    assert_that(cs.state, equal_to("pending"))

    cs = CommitStatus.query.filter_by(repository_id=repository.id,
                                      sha="2").first()
    assert_that(cs)
    assert_that(cs.state, equal_to("pending"))

    bs = BranchStatus.query.filter_by(commit_id=cs.id,
                                      name="spam:wip/my-branch").first()
    assert_that(bs)
    assert_that(bs.is_pending())
    assert_that(bs.state, equal_to("pending"))