Esempio n. 1
0
def test_login_with_params(fake_client):
    client.StoreClientCLI().login(
        ttl=20,
        acls=["package_access", "package_push"],
        packages=["fake-snap", "fake-other-snap"],
        channels=["stable/fake", "edge/fake"],
    )

    assert fake_client.login.mock_calls == [
        call(
            ttl=20,
            permissions=[
                "package_access",
                "package_push",
            ],
            channels=["stable/fake", "edge/fake"],
            packages=[
                endpoints.Package(package_name="fake-snap",
                                  package_type="snap"),
                endpoints.Package(package_name="fake-other-snap",
                                  package_type="snap"),
            ],
            description="snapcraft@fake-host",
            email="*****@*****.**",
            password="******",
        )
    ]
Esempio n. 2
0
def test_notify_upload_error(fake_client):
    fake_client.request.side_effect = [
        FakeResponse(status_code=200,
                     content=json.dumps(
                         {"status_details_url": "https://track"})),
        FakeResponse(
            status_code=200,
            content=json.dumps({
                "code": "processing",
                "processed": False
            }),
        ),
        FakeResponse(
            status_code=200,
            content=json.dumps({
                "code": "done",
                "processed": True,
                "errors": [{
                    "message": "bad-snap"
                }]
            }),
        ),
    ]

    with pytest.raises(errors.SnapcraftError) as raised:
        client.StoreClientCLI().notify_upload(
            snap_name="foo",
            upload_id="some-id",
            channels=["stable"],
            built_at=None,
            snap_file_size=999,
        )

    assert str(raised.value) == textwrap.dedent("""\
        Issues while processing snap:
        - bad-snap""")

    assert fake_client.request.mock_calls == [
        call(
            "POST",
            "https://dashboard.snapcraft.io/dev/api/snap-push/",
            json={
                "name": "foo",
                "series": "16",
                "updown_id": "some-id",
                "binary_filesize": 999,
                "channels": ["stable"],
                "source_uploaded": False,
            },
            headers={"Accept": "application/json"},
        ),
        call("GET", "https://track"),
        call("GET", "https://track"),
    ]
Esempio n. 3
0
def test_get_account_info(fake_client):
    client.StoreClientCLI().get_account_info()

    assert fake_client.request.mock_calls == [
        call(
            "GET",
            "https://dashboard.snapcraft.io/dev/api/account",
            headers={"Accept": "application/json"},
        ),
        call().json(),
    ]
Esempio n. 4
0
def test_login_otp(fake_client):
    fake_client.login.side_effect = [
        craft_store.errors.StoreServerError(
            FakeResponse(
                status_code=requests.codes.unauthorized,  # pylint: disable=no-member
                content=json.dumps({
                    "error_list": [{
                        "message": "2fa",
                        "code": "twofactor-required"
                    }]
                }),
            )),
        None,
    ]

    client.StoreClientCLI().login()

    assert fake_client.login.mock_calls == [
        call(
            ttl=31536000,
            permissions=[
                "package_access",
                "package_manage",
                "package_metrics",
                "package_push",
                "package_register",
                "package_release",
                "package_update",
            ],
            channels=None,
            packages=[],
            description="snapcraft@fake-host",
            email="*****@*****.**",
            password="******",
        ),
        call(
            ttl=31536000,
            permissions=[
                "package_access",
                "package_manage",
                "package_metrics",
                "package_push",
                "package_register",
                "package_release",
                "package_update",
            ],
            channels=None,
            packages=[],
            description="snapcraft@fake-host",
            email="*****@*****.**",
            password="******",
            otp="123456",
        ),
    ]
Esempio n. 5
0
def test_close(fake_client):
    client.StoreClientCLI().close(
        snap_id="12345",
        channel="edge",
    )

    assert fake_client.request.mock_calls == [
        call(
            "POST",
            "https://dashboard.snapcraft.io/dev/api/snaps/12345/close",
            json={"channels": ["edge"]},
        )
    ]
Esempio n. 6
0
def test_verify_upload(fake_client):
    client.StoreClientCLI().verify_upload(snap_name="foo")

    assert fake_client.request.mock_calls == [
        call(
            "POST",
            "https://dashboard.snapcraft.io/dev/api/snap-push/",
            json={
                "name": "foo",
                "dry_run": True
            },
            headers={"Accept": "application/json"},
        )
    ]
Esempio n. 7
0
def test_get_channel_map(fake_client, channel_map_payload):
    fake_client.request.return_value = FakeResponse(
        status_code=200, content=json.dumps(channel_map_payload))
    channel_map = client.StoreClientCLI().get_channel_map(
        snap_name="test-snap", )
    assert isinstance(channel_map, ChannelMap)

    assert fake_client.request.mock_calls == [
        call(
            "GET",
            "https://dashboard.snapcraft.io/api/v2/snaps/test-snap/channel-map",
            headers={"Accept": "application/json"},
        )
    ]
Esempio n. 8
0
def test_login_with_env(monkeypatch):
    monkeypatch.setenv("SNAPCRAFT_STORE_CREDENTIALS", "secret")

    with pytest.raises(errors.SnapcraftError) as raised:
        client.StoreClientCLI().login(
            ttl=20,
            acls=["package_access", "package_push"],
            packages=["fake-snap", "fake-other-snap"],
            channels=["stable/fake", "edge/fake"],
        )

    assert str(
        raised.value) == "Cannot login with 'SNAPCRAFT_STORE_CREDENTIALS' set."
    assert raised.value.resolution == (
        "Unset 'SNAPCRAFT_STORE_CREDENTIALS' and try again.")
Esempio n. 9
0
def test_notify_upload_channels(fake_client):
    fake_client.request.side_effect = [
        FakeResponse(status_code=200,
                     content=json.dumps(
                         {"status_details_url": "https://track"})),
        FakeResponse(
            status_code=200,
            content=json.dumps({
                "code": "processing",
                "processed": False
            }),
        ),
        FakeResponse(
            status_code=200,
            content=json.dumps({
                "code": "done",
                "processed": True,
                "revision": 42
            }),
        ),
    ]

    client.StoreClientCLI().notify_upload(
        snap_name="foo",
        upload_id="some-id",
        channels=["stable"],
        built_at=None,
        snap_file_size=999,
    )

    assert fake_client.request.mock_calls == [
        call(
            "POST",
            "https://dashboard.snapcraft.io/dev/api/snap-push/",
            json={
                "name": "foo",
                "series": "16",
                "updown_id": "some-id",
                "binary_filesize": 999,
                "channels": ["stable"],
                "source_uploaded": False,
            },
            headers={"Accept": "application/json"},
        ),
        call("GET", "https://track"),
        call("GET", "https://track"),
    ]
Esempio n. 10
0
def test_release(fake_client, progressive_percentage):
    client.StoreClientCLI().release(
        snap_name="snap",
        revision=10,
        channels=["beta", "edge"],
        progressive_percentage=progressive_percentage,
    )

    assert fake_client.request.mock_calls == [
        call(
            "POST",
            "https://dashboard.snapcraft.io/dev/api/snap-release/",
            json={
                "name": "snap",
                "revision": "10",
                "channels": ["beta", "edge"]
            },
        )
    ]
Esempio n. 11
0
def test_register(fake_client, private, store_id):
    client.StoreClientCLI().register("snap",
                                     is_private=private,
                                     store_id=store_id)

    expected_json = {
        "snap_name": "snap",
        "is_private": private,
        "series": "16",
    }
    if store_id:
        expected_json["store"] = store_id
    assert fake_client.request.mock_calls == [
        call(
            "POST",
            "https://dashboard.snapcraft.io/dev/api/register-name/",
            json=expected_json,
        )
    ]
Esempio n. 12
0
def test_login_from_401_request(fake_client):
    fake_client.request.side_effect = [
        craft_store.errors.StoreServerError(
            FakeResponse(
                status_code=401,
                content=json.dumps({
                    "error_list": [{
                        "code":
                        "macaroon-needs-refresh",
                        "message":
                        "Expired macaroon (age: 1234567 seconds)",
                    }]
                }),
            )),
        FakeResponse(status_code=200, content="text"),
    ]

    client.StoreClientCLI().request("GET", "http://url.com/path")

    assert fake_client.request.mock_calls == [
        call("GET", "http://url.com/path"),
        call("GET", "http://url.com/path"),
    ]
    assert fake_client.login.mock_calls == [
        call(
            ttl=31536000,
            permissions=[
                "package_access",
                "package_manage",
                "package_metrics",
                "package_push",
                "package_register",
                "package_release",
                "package_update",
            ],
            channels=None,
            packages=[],
            description="snapcraft@fake-host",
            email="*****@*****.**",
            password="******",
        )
    ]
Esempio n. 13
0
def test_login(fake_client):
    client.StoreClientCLI().login()

    assert fake_client.login.mock_calls == [
        call(
            ttl=31536000,
            permissions=[
                "package_access",
                "package_manage",
                "package_metrics",
                "package_push",
                "package_register",
                "package_release",
                "package_update",
            ],
            channels=None,
            packages=[],
            description="snapcraft@fake-host",
            email="*****@*****.**",
            password="******",
        )
    ]
Esempio n. 14
0
def test_login_from_401_request_with_env_credentials(monkeypatch, fake_client):
    monkeypatch.setenv(client.constants.ENVIRONMENT_STORE_CREDENTIALS, "foo")
    fake_client.request.side_effect = [
        craft_store.errors.StoreServerError(
            FakeResponse(
                status_code=401,
                content=json.dumps({
                    "error_list": [{
                        "code":
                        "macaroon-needs-refresh",
                        "message":
                        "Expired macaroon (age: 1234567 seconds)",
                    }]
                }),
            )),
    ]

    with pytest.raises(errors.SnapcraftError) as raised:
        client.StoreClientCLI().request("GET", "http://url.com/path")

    assert str(raised.value) == (
        "Provided credentials are no longer valid for the Snap Store.")
    assert raised.value.resolution == "Regenerate them and try again."