Skip to content

Commit

Permalink
client: add unregister_name method (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Mar 31, 2023
1 parent 527ef9b commit d643e70
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
12 changes: 12 additions & 0 deletions craft_store/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,15 @@ def register_name(

response = self.request("POST", self._base_url + endpoint, json=request_json)
return response.json()["id"]

def unregister_name(self, name: str) -> str:
"""Unregister a name with no published packages.
:param name: The name to unregister.
:returns: the ID of the deleted name.
"""
endpoint = f"/v1/{self._endpoints.namespace}/{name}"
response = self.request("DELETE", self._base_url + endpoint)

return response.json()["package-id"]
45 changes: 45 additions & 0 deletions tests/integration/test_register_unregister.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2023 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import datetime
import os

import pytest


@pytest.mark.skipif(
not os.getenv("CRAFT_STORE_CHARMCRAFT_CREDENTIALS"),
reason="CRAFT_STORE_CHARMCRAFT_CREDENTIALS are not set",
)
def test_register_unregister_cycle(charm_client):
whoami = charm_client.whoami()
account_id = whoami.get("account", {}).get("id")
timestamp_us = int(datetime.datetime.utcnow().timestamp() * 1_000_000)
charm_name = f"test-charm-{account_id}-{timestamp_us}"

names = [result.name for result in charm_client.list_registered_names()]
assert charm_name not in names, "Charm name already registered, test setup failed."

charm_client.register_name(charm_name, entity_type="charm")

names = [result.name for result in charm_client.list_registered_names()]
assert charm_name not in names, "Charm was not successfully registered."

charm_client.unregister_package(charm_name)

names = [result.name for result in charm_client.list_registered_names()]
assert charm_name not in names, "Charm was not successfully unregistered."
13 changes: 12 additions & 1 deletion tests/unit/test_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,16 @@ def test_register_name(charm_client, name, entity_type, private, team, expected_
)

charm_client.request.assert_called_once_with(
"POST", charm_client._base_url + "/v1/charm", json=expected_json
"POST", "https://staging.example.com/v1/charm", json=expected_json
)


def test_unregister_name(charm_client):
charm_client.request = Mock()
charm_client.request.return_value.json.return_value = {"package-id": "abc"}

charm_client.unregister_name("test-charm-abcxyz")

charm_client.request.assert_called_once_with(
"DELETE", "https://staging.example.com/v1/charm/test-charm-abcxyz"
)

0 comments on commit d643e70

Please sign in to comment.