-
Notifications
You must be signed in to change notification settings - Fork 8
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
POST logic/Permissions/Test fixes #20
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wandered thru with some thoughts, looks good!
resource_uri = None | ||
try: | ||
resource_uri = ResourceReference.objects.get( | ||
id=request.data.get("authoirized_resource", {}).get("id") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id=request.data.get("authoirized_resource", {}).get("id") | |
id=request.data.get("authorized_resource", {}).get("id") |
(nit: could also skip the query when there's no id
)
|
||
|
||
class CanCreateCSA(permissions.BasePermission): | ||
class SessionUserIsReferencedResourceAdmin(permissions.BasePermission): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this permission class is still specific to ConfiguredStorageAddon (assumes the referenced resource is at authorized_resource
) -- not necessarily a problem, but the rename made me think it would be more generic... could take a field_name
param
"Some form of auth is necessary or POSTS are ignored." | ||
] | ||
self._mock_osf = MockOSF() | ||
self.addCleanup(self._mock_osf.stop) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(lil nit/suggestion) if we're on python 3.11+ and MockOSF were usable as context manager (maybe start the patchers in __enter__
and stop them in __exit__
), could do
self._mock_osf = MockOSF()
self.enterContext(self._mock_osf)
...which isn't much different, but for some reason i'm still glad enterContext
was added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know about enter_context
! The only examples I'm seeing are in the context of an ExitStack, so the code might look like
with ExitStack as stack:
self._mock_osf = MockOSF().enter_context()
Maybe worth a quick experiment to see if it works, since that would also make MockOSF easier to use in individual test functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh hey that's confusing -- there's contextlib.ExitStack.enter_context and also unittest.TestCase.enterContext (where TestCase kinda has its own exit stack, but in camelCase) -- it's the TestCase one i think would help, and yeah agree using MockOSF as a context manager makes it nicely reusable
@@ -15,7 +15,7 @@ | |||
class AuthorizedResourceField(ResourceRelatedField): | |||
def to_internal_value(self, data): | |||
resource_reference, _ = ResourceReference.objects.get_or_create( | |||
resource_uri=data["id"] | |||
resource_uri=data["resource_uri"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
am i reading correctly that the relationship in json is expected to be
"authorized_resource": {
"data": {
"resource_uri": "http://...",
"type": "resource-references"
}
}
?
i get the desire to use the resource iri instead of the resource-reference id, but i wonder if there's a more jsonapi-friendly way to do it (rather than a resource identifier object without id
/lid
) -- maybe a authorized_resource_uri
on-create-only attribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with re-using the "write-only" field approach we agreed on for credentials here instead of the relationship (though, yeah, need to not let it be PATCH-able). That would clean up the Permissions logic above, too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://openscience.atlassian.net/browse/ENG-5418
This should satisfy the comments on the Permissions class, too
reverse("authorized-storage-accounts-list"), payload, format="vnd.api+json" | ||
) | ||
self.assertEqual(_resp.status_code, 201) | ||
created_account_id = int(_resp.data['url'].rstrip('/').split('/')[-1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be half-dozen-of-another, but could avoid parsing a url by rendering the response to json
_created_account_id = _resp.json()['data']['id']
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call, this was just copy-pasted into this class instead of living in its own
app/authentication.py
Outdated
@@ -33,6 +33,7 @@ class SkipAuthMethod(exceptions.APIException): | |||
|
|||
|
|||
def authenticate_resource(request, uri, required_permission): | |||
print('WHAT?!!!!\n\n\n') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥪
WHAT:
session_user_uri
provided by OSF instead of accepting a relationshipexternal_storage_service
resource_reference
relationship to POST theresource_uri
, not the IDresource_uri
field in theauthoirzed_resource
relationship to check OSF permissions@with_mocked_httpx_get
decorator with a configurable mock for the OSF