def test_register_feedstock_token_notoken(gh_mock, git_mock, tmp_mock,
                                          osuran_mock, secrets_mock, tmpdir,
                                          repo):
    gh_mock.return_value = "abc123"
    tmp_mock.TemporaryDirectory.return_value.__enter__.return_value = str(
        tmpdir)
    secrets_mock.token_hex.return_value = "fgh"
    osuran_mock.return_value = b"\x80SA"

    user = "******"
    project = "bar"
    os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
    pth = os.path.expanduser("~/.conda-smithy/foo_bar.token")
    token_json_pth = os.path.join(tmpdir, "tokens", "bar.json")

    try:
        with pytest.raises(RuntimeError) as e:
            register_feedstock_token(user, project, repo)
    finally:
        if os.path.exists(pth):
            os.remove(pth)

    git_mock.Repo.clone_from.assert_not_called()

    repo = git_mock.Repo.clone_from.return_value
    repo.index.add.assert_not_called()
    repo.index.commit.assert_not_called()
    repo.remote.return_value.pull.assert_not_called()
    repo.remote.return_value.push.assert_not_called()

    assert not os.path.exists(token_json_pth)

    assert "No token found in" in str(e.value)
def test_feedstock_tokens_roundtrip(gh_mock, git_mock, tmp_mock, tmpdir, repo,
                                    project):
    gh_mock.return_value = "abc123"
    tmp_mock.TemporaryDirectory.return_value.__enter__.return_value = str(
        tmpdir)

    user = "******"
    pth = os.path.expanduser("~/.conda-smithy/foo_%s.token" % project)
    token_json_pth = os.path.join(tmpdir, "tokens", "%s.json" % project)
    os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)

    try:
        generate_and_write_feedstock_token(user, project)
        assert os.path.exists(pth)

        register_feedstock_token(user, project, repo)
        assert os.path.exists(token_json_pth)

        with open(pth, "r") as fp:
            feedstock_token = fp.read().strip()

        retval = is_valid_feedstock_token(user, project, feedstock_token, repo)
    finally:
        if os.path.exists(pth):
            os.remove(pth)

    assert retval
def test_register_feedstock_token_works(gh_mock, git_mock, tmp_mock,
                                        osuran_mock, secrets_mock, tmpdir,
                                        repo):
    gh_mock.return_value = "abc123"
    tmp_mock.TemporaryDirectory.return_value.__enter__.return_value = str(
        tmpdir)
    secrets_mock.token_hex.return_value = "fgh"
    osuran_mock.return_value = b"\x80SA"

    user = "******"
    project = "bar"
    os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
    pth = os.path.expanduser("~/.conda-smithy/foo_%s.token" % project)
    token_json_pth = os.path.join(tmpdir, "tokens", "%s.json" % project)

    try:
        generate_and_write_feedstock_token(user, project)

        register_feedstock_token(user, project, repo)

    finally:
        if os.path.exists(pth):
            os.remove(pth)

    git_mock.Repo.clone_from.assert_called_once_with(
        "abc123",
        str(tmpdir),
        depth=1,
    )

    repo = git_mock.Repo.clone_from.return_value
    repo.index.add.assert_called_once_with(token_json_pth)
    repo.index.commit.assert_called_once_with(
        "[ci skip] [skip ci] [cf admin skip] ***NO_CI*** added token for %s/%s"
        % (user, project))
    repo.remote.return_value.pull.assert_called_once_with(rebase=True)
    repo.remote.return_value.push.assert_called_once_with()

    salted_token = scrypt.hash("fgh", b"\x80SA", buflen=256)
    data = {
        "salt": b"\x80SA".hex(),
        "hashed_token": salted_token.hex(),
    }

    with open(token_json_pth, "r") as fp:
        assert json.load(fp) == data
示例#4
0
    def __call__(self, args):
        from conda_smithy.feedstock_tokens import (
            register_feedstock_token_with_proviers,
            register_feedstock_token,
            feedstock_token_exists,
        )
        from conda_smithy.ci_register import drone_default_endpoint

        drone_endpoints = args.drone_endpoints
        if drone_endpoints is None:
            drone_endpoints = [drone_default_endpoint]

        owner = args.user or args.organization
        repo = os.path.basename(os.path.abspath(args.feedstock_directory))

        if args.token_repo is None:
            token_repo = (
                "https://${GITHUB_TOKEN}@github.com/%s/feedstock-tokens" %
                owner)
        else:
            token_repo = args.token_repo

        if feedstock_token_exists(owner, repo, token_repo):
            raise RuntimeError("Token for repo %s/%s already exists!" %
                               (owner, repo))

        print("Registering the feedstock tokens. Can take up to ~30 seconds.")

        # do all providers first
        register_feedstock_token_with_proviers(
            owner,
            repo,
            drone=args.drone,
            circle=args.circle,
            travis=args.travis,
            azure=args.azure,
            github_actions=args.github_actions,
            drone_endpoints=drone_endpoints,
        )

        # then if that works do the github repo
        register_feedstock_token(owner, repo, token_repo)

        print("Successfully registered the feedstock token!")
def test_register_feedstock_token_exists_already(gh_mock, git_mock, tmp_mock,
                                                 osuran_mock, secrets_mock,
                                                 tmpdir, repo):
    gh_mock.return_value = "abc123"
    tmp_mock.TemporaryDirectory.return_value.__enter__.return_value = str(
        tmpdir)
    secrets_mock.token_hex.return_value = "fgh"
    osuran_mock.return_value = b"\x80SA"

    user = "******"
    project = "bar"
    os.makedirs(os.path.join(tmpdir, "tokens"), exist_ok=True)
    pth = os.path.expanduser("~/.conda-smithy/foo_bar.token")
    token_json_pth = os.path.join(tmpdir, "tokens", "bar.json")
    with open(token_json_pth, "w") as fp:
        fp.write("blarg")

    try:
        generate_and_write_feedstock_token(user, project)

        with pytest.raises(RuntimeError) as e:
            register_feedstock_token(user, project, repo)

    finally:
        if os.path.exists(pth):
            os.remove(pth)

    git_mock.Repo.clone_from.assert_called_once_with(
        "abc123",
        str(tmpdir),
        depth=1,
    )

    repo = git_mock.Repo.clone_from.return_value
    repo.index.add.assert_not_called()
    repo.index.commit.assert_not_called()
    repo.remote.return_value.pull.assert_not_called()
    repo.remote.return_value.push.assert_not_called()

    assert "Token for repo foo/bar already exists!" in str(e.value)