Skip to content

Commit

Permalink
fix: Added story map title context to permissions error
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Oct 5, 2023
1 parent 64911e3 commit 5086ea2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
13 changes: 12 additions & 1 deletion terraso_backend/apps/graphql/schema/story_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.

from datetime import datetime

import django_filters
import graphene
import rules
Expand Down Expand Up @@ -345,11 +347,20 @@ def mutate_and_get_payload(cls, root, info, **kwargs):
"Attempt to approve a Membership, but user has no permission",
extra=kwargs,
)
raise GraphQLNotAllowedException(
error = GraphQLNotAllowedException(
model_name=Membership.__name__,
operation=MutationTypes.UPDATE,
message="permissions_validation",
)
return cls(
errors=[{"message": str(error)}],
story_map=StoryMap(
id="",
title=story_map.title,
created_at=datetime.now(),
updated_at=datetime.now(),
),
)

try:
membership.membership_list.approve_membership(
Expand Down
5 changes: 3 additions & 2 deletions terraso_backend/apps/story_map/permission_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ def allowed_to_approve_story_map_membership(user, obj):
@rules.predicate
def allowed_to_approve_story_map_membership_with_token(user, obj):
membership = obj.get("membership")
request_user = user
return request_user.id == membership.user.id
if not membership.user:
return False
return user.id == membership.user.id


@rules.predicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def test_story_map_approve_membership_with_token_for_unregistered_user(


def test_story_map_approve_membership_with_token_for_registered_user_fails_due_user_mismatch(
client_query, story_map_user_memberships_approve_tokens
client_query, story_map_user_memberships_approve_tokens, story_maps
):
token = story_map_user_memberships_approve_tokens[1]

Expand All @@ -469,6 +469,12 @@ def test_story_map_approve_membership_with_token_for_registered_user_fails_due_u
$input: StoryMapMembershipApproveTokenMutationInput!
){
approveStoryMapMembershipToken(input: $input) {
storyMap {
title
id
createdAt
updatedAt
}
membership {
id
membershipStatus
Expand All @@ -485,7 +491,14 @@ def test_story_map_approve_membership_with_token_for_registered_user_fails_due_u
)
json_response = response.json()

print(json_response)

assert "errors" in json_response["data"]["approveStoryMapMembershipToken"]
error_result = json_response["data"]["approveStoryMapMembershipToken"]["errors"][0]["message"]
json_error = json.loads(error_result)
assert json_error[0]["code"] == "update_not_allowed"
assert (
json_response["data"]["approveStoryMapMembershipToken"]["storyMap"]["title"]
== story_maps[0].title
)
assert json_response["data"]["approveStoryMapMembershipToken"]["storyMap"]["id"] == ""

0 comments on commit 5086ea2

Please sign in to comment.