Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix login crash #1673

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion charmcraft/application/commands/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def run(self, parsed_args):
if namespace_value is not None:
kwargs[arg_name] = namespace_value

packages = utils.get_packages(charms=parsed_args.charm, bundles=parsed_args.bundle) or None
packages = (
utils.get_packages(charms=parsed_args.charm or [], bundles=parsed_args.bundle or [])
or None
)

if parsed_args.export:
credentials = self._services.store.get_credentials(packages=packages, **kwargs)
Expand Down
15 changes: 13 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ def simple_charm():
)


@pytest.fixture()
def mock_store_client():
client = mock.Mock(spec_set=store.Client)

client.whoami.return_value = {
"account": {"username": "test-user"},
}

return client


@pytest.fixture()
def service_factory(
fs, fake_project_dir, fake_prime_dir, simple_charm
fs, fake_project_dir, fake_prime_dir, simple_charm, mock_store_client
) -> services.CharmcraftServiceFactory:
factory = services.CharmcraftServiceFactory(app=APP_METADATA)

Expand All @@ -66,7 +77,7 @@ def service_factory(

factory.project = simple_charm

factory.store.client = mock.Mock(spec_set=store.Client)
factory.store.client = mock_store_client

return factory

Expand Down
48 changes: 47 additions & 1 deletion tests/unit/commands/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
"""Unit tests for store commands."""
import argparse
import datetime
import pathlib
import textwrap
from unittest import mock

import craft_cli.pytest_plugin
import craft_store
import pytest
from craft_store import models

from charmcraft import errors, store
from charmcraft.application.commands import SetResourceArchitecturesCommand
from charmcraft.application.commands.store import FetchLibs
from charmcraft.application.commands.store import FetchLibs, LoginCommand
from charmcraft.application.main import APP_METADATA
from charmcraft.models.project import CharmLib
from charmcraft.utils import cli
Expand All @@ -35,6 +38,49 @@
"""


def test_login_basic_no_export(service_factory, mock_store_client):
cmd = LoginCommand({"app": APP_METADATA, "services": service_factory})

cmd.run(
argparse.Namespace(
charm=None,
bundle=None,
channel=None,
permission=None,
ttl=None,
export=None,
)
)


@pytest.mark.parametrize("charm", [None, ["my-charm"]])
@pytest.mark.parametrize("bundle", [None, ["my-bundle"]])
@pytest.mark.parametrize("channel", [None, ["edge", "latest/stable"]])
@pytest.mark.parametrize("permission", [None, [], ["package-manage"]])
@pytest.mark.parametrize("ttl", [None, 0, 2**65])
def test_login_export(
monkeypatch, service_factory, mock_store_client, charm, bundle, channel, permission, ttl
):
mock_client_cls = mock.Mock(return_value=mock_store_client)
monkeypatch.setattr(craft_store, "StoreClient", mock_client_cls)
mock_store_client.login.return_value = "Some store credentials"
cmd = LoginCommand({"app": APP_METADATA, "services": service_factory})

cmd.run(
argparse.Namespace(
charm=charm,
bundle=bundle,
channel=channel,
permission=permission,
ttl=ttl,
export=pathlib.Path("charmhub.login"),
)
)

assert pathlib.Path("charmhub.login").read_text() == "Some store credentials"
mock_store_client.login.assert_called_once()


@pytest.mark.parametrize(
("updates", "expected"),
[
Expand Down
Loading