Skip to content

Commit

Permalink
fix pr changes
Browse files Browse the repository at this point in the history
Add modified_at field in updated_fields
  • Loading branch information
susilnem authored and AdityaKhatri committed Aug 9, 2024
1 parent 4da80df commit d327d83
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
6 changes: 3 additions & 3 deletions apps/analysis_framework/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
AnalysisFrameworkMembership,
)
from .serializers import (
AnalysisFrameworkCloneGlSerializer,
AnalysisFrameworkCloneSerializer,
AnalysisFrameworkGqlSerializer as AnalysisFrameworkSerializer,
AnalysisFrameworkMembershipGqlSerializer as AnalysisFrameworkMembershipSerializer,
)
Expand All @@ -39,7 +39,7 @@

AnalysisFrameworkCloneInputType = generate_input_type_for_serializer(
'AnalysisFrameworkCloneInputType',
serializer_class=AnalysisFrameworkCloneGlSerializer,
serializer_class=AnalysisFrameworkCloneSerializer,
)


Expand Down Expand Up @@ -107,7 +107,7 @@ class Arguments:
data = AnalysisFrameworkCloneInputType(required=True)

result = graphene.Field(AnalysisFrameworkDetailType)
serializer_class = AnalysisFrameworkCloneGlSerializer
serializer_class = AnalysisFrameworkCloneSerializer
permissions = [AfP.Permission.CAN_CLONE_FRAMEWORK]


Expand Down
25 changes: 7 additions & 18 deletions apps/analysis_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,26 +711,15 @@ def create(self, validated_data):
return super().create(validated_data)


class AnalysisFrameworkCloneGlSerializer(serializers.Serializer):
class AnalysisFrameworkCloneSerializer(serializers.Serializer):
title = serializers.CharField(required=True)
description = serializers.CharField(required=False)
project = serializers.IntegerField(required=False)
project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all(), required=False)

class Meta:
model = AnalysisFramework
fields = ('title', 'description', 'project')

def validate(self, validated_data):
project_id = validated_data.get('project')
# Check if project exists and user has access to it
if project_id is not None:
project = Project.objects.filter(id=project_id).first()
if project is None:
raise serializers.ValidationError('Invalid project ID')
if not project.can_modify(self.context['request'].user):
raise serializers.ValidationError('User does not have permission to modify the project')
validated_data['project'] = project
return validated_data
def validate_project(self, project):
if not project.can_modify(self.context['request'].user):
raise serializers.ValidationError('User does not have permission to modify the project')
return project

def create(self, validated_data):
af = self.context['request'].active_af
Expand All @@ -740,5 +729,5 @@ def create(self, validated_data):
if project := validated_data.get('project'):
project.analysis_framework = new_af
project.modified_by = self.context['request'].user
project.save(update_fields=['analysis_framework', 'modified_by'])
project.save(update_fields=['analysis_framework', 'modified_by', 'modified_at'])
return new_af
18 changes: 0 additions & 18 deletions apps/analysis_framework/tests/snapshots/snap_test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@

snapshots = Snapshot()

snapshots['TestAnalysisFrameworkMutationSnapShotTestCase::test_analysis_framework_clone success'] = {
'data': {
'__typename': 'Mutation',
'analysisFramework': {
'analysisFrameworkClone': {
'errors': None,
'ok': True,
'result': {
'clonedFrom': '1',
'description': 'Af description',
'id': '2',
'title': 'AF (TEST)'
}
}
}
}
}

snapshots['TestAnalysisFrameworkMutationSnapShotTestCase::test_analysis_framework_create errors'] = {
'data': {
'__typename': 'Mutation',
Expand Down
44 changes: 35 additions & 9 deletions apps/analysis_framework/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,18 +805,22 @@ def test_analysis_framework_clone(self):

member_user = UserFactory.create()
non_member_user = UserFactory.create()
low_permission_user = UserFactory.create()

project = ProjectFactory.create()
project.add_member(member_user)
project.add_member(low_permission_user)
af = AnalysisFrameworkFactory.create(created_by=member_user, title='AF Orginal')
af.add_member(member_user)
af.add_member(member_user, role=self.af_owner)
private_af = AnalysisFrameworkFactory.create(created_by=member_user, title='AF Private Orginal', is_private=True)
private_af.add_member(low_permission_user)

minput = dict(
title='AF (TEST)',
description='Af description',
)

def _query_check(**kwargs):
def _public_af_query_check(**kwargs):
return self.query_check(
query,
minput=minput,
Expand All @@ -825,29 +829,51 @@ def _query_check(**kwargs):
**kwargs,
)

def _private_af_query_check(**kwargs):
return self.query_check(
query,
minput=minput,
mnested=['analysisFramework'],
variables={'id': private_af.id},
**kwargs,
)

# ---------- Without login
_query_check(assert_for_error=True)
_public_af_query_check(assert_for_error=True)
# ---------- With login
self.force_login(non_member_user)
# ---------- Let's Clone a new AF (Using create test data)
_query_check(assert_for_error=True)
_public_af_query_check(assert_for_error=True)

# ---------- With login (with access member)
self.force_login(member_user)
response = _query_check()
self.assertMatchSnapshot(response, 'success')
response = _public_af_query_check()
self.assertEqual(response['data']['analysisFramework']['analysisFrameworkClone']['result']['clonedFrom'], str(af.id))

# adding project to the input
minput['project'] = project.id

# with Login (non project member)
self.force_login(non_member_user)
_query_check(assert_for_error=True)
_public_af_query_check(assert_for_error=True)

# With Login (project member)
# with Login (project member with no permission on AF)
self.force_login(low_permission_user)
_public_af_query_check(assert_for_error=True)

# with Login (project member with no permission on Private AF)
self.force_login(member_user)
response = _query_check()['data']['analysisFramework']
_private_af_query_check(assert_for_error=True)

# With Login (project member with permission on Public AF)
self.force_login(member_user)
response = _public_af_query_check()['data']['analysisFramework']
project.refresh_from_db()
self.assertEqual(str(project.analysis_framework_id), response['analysisFrameworkClone']['result']['id'])

# with Login (project member with permission on Private AF)
private_af.add_member(member_user, role=self.af_owner)
response = _private_af_query_check()['data']['analysisFramework']
project.refresh_from_db()
self.assertEqual(str(project.analysis_framework_id), response['analysisFrameworkClone']['result']['id'])

Expand Down
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type AnalysisAutomaticSummaryType {
input AnalysisFrameworkCloneInputType {
title: String!
description: String
project: Int
project: ID
}

type AnalysisFrameworkDetailType {
Expand Down

0 comments on commit d327d83

Please sign in to comment.