Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Feb 7, 2024
1 parent fc2a943 commit ea018db
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 40 deletions.
10 changes: 8 additions & 2 deletions addon_service/authorized_storage_account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ def __init__(self, *args, **kwargs):

included_serializers = {
"account_owner": "addon_service.serializers.InternalUserSerializer",
"external_storage_service": "addon_service.serializers.ExternalStorageServiceSerializer",
"configured_storage_addons": "addon_service.serializers.ConfiguredStorageAddonSerializer",
"external_storage_service": (
"addon_service.serializers.ExternalStorageServiceSerializer"
),
"configured_storage_addons": (
"addon_service.serializers.ConfiguredStorageAddonSerializer"
),
}

def create(self, validate_data):
account_owner = validate_data["account_owner"]
authorized_capabilities = validate_data["authorized_capabilities"]
external_storage_service = validate_data["external_storage_service"]
# TODO(ENG-5189): Update this once credentials format is finalized
credentials, created = ExternalCredentials.objects.get_or_create(
Expand All @@ -92,6 +97,7 @@ def create(self, validate_data):
return AuthorizedStorageAccount.objects.create(
external_storage_service=external_storage_service,
external_account=external_account,
authorized_capabilities=authorized_capabilities,
)

class Meta:
Expand Down
4 changes: 1 addition & 3 deletions addon_service/capability/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ def to_base_enum(self) -> enum.Enum:

class IntStorageCapability(_IntEnumForEnum, base_enum=StorageCapability):
ACCESS = 1
BROWSE = 2
UPDATE = 3
COMMIT = 4
UPDATE = 2
2 changes: 1 addition & 1 deletion addon_service/capability/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, /, internal_enum, external_enum, **kwargs):

def to_internal_value(self, data):
_names = super().to_internal_value(data)
return {self._to_internal_enum_member(_name) for _name in _names}
return [self._to_internal_enum_member(_name) for _name in _names]

def to_representation(self, value):
_member_list = super().to_representation(value)
Expand Down
1 change: 1 addition & 0 deletions addon_service/configured_storage_addon/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
InternalResource,
)


RESOURCE_NAME = get_resource_type_from_model(ConfiguredStorageAddon)


Expand Down
16 changes: 3 additions & 13 deletions addon_service/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2024-02-07 20:42
# Generated by Django 4.2.7 on 2024-02-07 22:04

import django.contrib.postgres.fields
import django.db.models.deletion
Expand Down Expand Up @@ -32,12 +32,7 @@ class Migration(migrations.Migration):
"authorized_capabilities",
django.contrib.postgres.fields.ArrayField(
base_field=models.IntegerField(
choices=[
(1, "ACCESS"),
(2, "BROWSE"),
(3, "UPDATE"),
(4, "COMMIT"),
]
choices=[(1, "ACCESS"), (2, "UPDATE")]
),
size=None,
),
Expand Down Expand Up @@ -233,12 +228,7 @@ class Migration(migrations.Migration):
"connected_capabilities",
django.contrib.postgres.fields.ArrayField(
base_field=models.IntegerField(
choices=[
(1, "ACCESS"),
(2, "BROWSE"),
(3, "UPDATE"),
(4, "COMMIT"),
]
choices=[(1, "ACCESS"), (2, "UPDATE")]
),
size=None,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def test_post(self):
"attributes": {
"username": "<placeholder-username>",
"password": "<placeholder-password>",
"authorized_capabilities": ["access", "update"],
},
"relationships": {
"external_storage_service": {
Expand All @@ -203,7 +204,9 @@ def test_post(self):
"account_owner": {
"data": {
"type": "internal-users",
"id": self._csa.base_account.external_account.owner.user_uri,
"id": (
self._csa.base_account.external_account.owner.user_uri
),
}
},
},
Expand Down
31 changes: 21 additions & 10 deletions addon_service/tests/test_by_type/test_configured_storage_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

from addon_service import models as db
from addon_service.configured_storage_addon.views import ConfiguredStorageAddonViewSet
from addon_service.internal_resource.models import InternalResource
from addon_service.tests import _factories
from addon_service.tests._helpers import get_test_request
from addon_service.internal_resource.models import InternalResource


class TestConfiguredStorageAddonAPI(APITestCase):
Expand Down Expand Up @@ -160,6 +160,9 @@ def setUpTestData(cls):
cls.default_payload = {
"data": {
"type": "configured-storage-addons",
"attributes": {
"connected_capabilities": ["access"],
},
"relationships": {
"base_account": {
"data": {
Expand All @@ -177,17 +180,17 @@ def setUpTestData(cls):
}
}



def test_post_without_resource(self):
"""
Test for request made without an InternalResource in the system, so one must be created
Test for request made without an InternalResource in the system, so one must be
created
"""
assert not self._asa.configured_storage_addons.exists() # sanity/factory check


response = self.client.post(
reverse("configured-storage-addons-list"), self.default_payload, format="vnd.api+json"
reverse("configured-storage-addons-list"),
self.default_payload,
format="vnd.api+json",
)
self.assertEqual(response.status_code, 201)
configured_storage_addon = self._asa.configured_storage_addons.first()
Expand All @@ -196,17 +199,25 @@ def test_post_without_resource(self):

def test_post_with_resource(self):
"""
Test for request made with a pre-existing InternalResource in the system, don't create one.
Test for request made with a pre-existing InternalResource in the system, don't
create one.
"""
assert not self._asa.configured_storage_addons.exists() # sanity/factory check
resource = _factories.InternalResourceFactory()
self.default_payload['data']['relationships']['authorized_resource']['data']['id'] = resource.resource_uri
self.default_payload["data"]["relationships"]["authorized_resource"]["data"][
"id"
] = resource.resource_uri

response = self.client.post(
reverse("configured-storage-addons-list"), self.default_payload, format="vnd.api+json"
reverse("configured-storage-addons-list"),
self.default_payload,
format="vnd.api+json",
)
self.assertEqual(response.status_code, 201)
configured_storage_addon = self._asa.configured_storage_addons.first()
assert configured_storage_addon
assert configured_storage_addon.authorized_resource.resource_uri == resource.resource_uri
assert (
configured_storage_addon.authorized_resource.resource_uri
== resource.resource_uri
)
assert InternalResource.objects.all().count() == 1
10 changes: 4 additions & 6 deletions addon_toolkit/categories/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

class StorageCapability(enum.StrEnum):
ACCESS = "access"
BROWSE = "browse"
UPDATE = "update"
COMMIT = "commit"


# what a base StorageInterface could be like (incomplete)
Expand Down Expand Up @@ -46,19 +44,19 @@ async def pls_delete_item(self, item_id: str):
##
# "tree-read" operations:

@proxy_operation(capability=StorageCapability.BROWSE)
@proxy_operation(capability=StorageCapability.ACCESS)
async def get_root_item_ids(self) -> PagedResult:
raise NotImplementedError

@proxy_operation(capability=StorageCapability.BROWSE)
@proxy_operation(capability=StorageCapability.ACCESS)
async def get_parent_item_id(self, item_id: str) -> str | None:
raise NotImplementedError

@proxy_operation(capability=StorageCapability.BROWSE)
@proxy_operation(capability=StorageCapability.ACCESS)
async def get_item_path(self, item_id: str) -> str:
raise NotImplementedError

@proxy_operation(capability=StorageCapability.BROWSE)
@proxy_operation(capability=StorageCapability.ACCESS)
async def get_child_item_ids(self, item_id: str) -> PagedResult:
raise NotImplementedError

Expand Down
39 changes: 35 additions & 4 deletions addon_toolkit/tests/test_addon_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ class _MyChecksumArchiveImplementation(_MyChecksumArchiveInterface):
def url_for_get(self, checksum_iri) -> str:
return f"https://myarchive.example///{checksum_iri}"

async def query_relations(self, checksum_iri, query=None):
# maybe yield rdf triples (or twoples with implicit subject)
yield ("http://purl.org/dc/terms/references", "checksum:foo:bar")

def url_for_put(self, checksum_iri):
# TODO: how to represent "send a PUT request here"?
# return RedirectLadle(
Expand Down Expand Up @@ -96,3 +92,38 @@ def test_operation_list(self):
set(self.my_addon_category.operations_declared(capability_id="nothing")),
set(),
)
self.assertEqual(
set(
self.my_addon_category.operations_implemented(
self._MyChecksumArchiveImplementation,
)
),
{_get_operation, _put_operation},
)
self.assertEqual(
set(
self.my_addon_category.operations_implemented(
self._MyChecksumArchiveImplementation,
capability_id="get-it",
)
),
{_get_operation},
)
self.assertEqual(
set(
self.my_addon_category.operations_implemented(
self._MyChecksumArchiveImplementation,
capability_id="put-it",
)
),
{_put_operation},
)
self.assertEqual(
set(
self.my_addon_category.operations_implemented(
self._MyChecksumArchiveImplementation,
capability_id="nothing",
)
),
set(),
)

0 comments on commit ea018db

Please sign in to comment.