-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed7fabd
commit 07f071b
Showing
8 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import graphene | ||
|
||
from utils.graphene.mutation import ( | ||
generate_input_type_for_serializer, | ||
PsGrapheneMutation, | ||
) | ||
|
||
from .models import File | ||
from .schema import GalleryFileType | ||
from .serializers import ( | ||
FileSerializer | ||
) | ||
|
||
FileUploadInputType = generate_input_type_for_serializer( | ||
'FileUploadInputType', | ||
serializer_class=FileSerializer | ||
) | ||
|
||
|
||
class UploadFile(PsGrapheneMutation): | ||
class Arguments: | ||
data = FileUploadInputType(required=True) | ||
|
||
result = graphene.Field(GalleryFileType) | ||
serializer_class = FileSerializer | ||
model = File | ||
permissions = [] | ||
|
||
|
||
class Mutation(): | ||
file_upload = UploadFile.Field() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from utils.graphene.tests import GraphQLTestCase | ||
import json | ||
|
||
from graphene_file_upload.django.testing import GraphQLFileUploadTestCase | ||
from django.core.files.temp import NamedTemporaryFile | ||
|
||
from user.factories import UserFactory | ||
|
||
|
||
class TestUploadFileMutation(GraphQLFileUploadTestCase, GraphQLTestCase): | ||
UPLOAD_FILE = ''' | ||
mutation MyMutation ($data: FileUploadInputType!) { | ||
fileUpload(data: $data) { | ||
ok | ||
errors | ||
result { | ||
id | ||
file { | ||
name | ||
url | ||
} | ||
} | ||
} | ||
} | ||
''' | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.variables = { | ||
"data": {"title": 'test', "file": None} | ||
} | ||
self.user = UserFactory.create() | ||
self.force_login(self.user) | ||
|
||
def test_upload_preview_image(self): | ||
file_text = b'preview image text' | ||
with NamedTemporaryFile(suffix='.png') as t_file: | ||
t_file.write(file_text) | ||
t_file.seek(0) | ||
response = self._client.post( | ||
'/graphql', | ||
data={ | ||
'operations': json.dumps({ | ||
'query': self.UPLOAD_FILE, | ||
'variables': self.variables | ||
}), | ||
't_file': t_file, | ||
'map': json.dumps({ | ||
't_file': ['variables.data.file'] | ||
}) | ||
} | ||
) | ||
content = response.json() | ||
self.assertResponseNoErrors(response) | ||
|
||
self.assertTrue(content['data']['fileUpload']['ok'], content) | ||
self.assertTrue(content['data']['fileUpload']['result']['file']["name"]) | ||
file_name = content['data']['fileUpload']['result']['file']["name"] | ||
file_url = content['data']['fileUpload']['result']['file']["url"] | ||
self.assertTrue(file_name.endswith('.png')) | ||
self.assertTrue(file_url.endswith(file_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters