118 lines
3.8 KiB
Python
118 lines
3.8 KiB
Python
from django.test import TestCase, RequestFactory
|
|
from graphene.test import Client
|
|
from graphql_relay import to_global_id
|
|
|
|
from api.schema import schema
|
|
from api.utils import get_object
|
|
from books.factories import ContentBlockFactory, ModuleFactory
|
|
from books.models import ContentBlock
|
|
from core.factories import UserFactory
|
|
|
|
|
|
class NewContentBlockMutationTest(TestCase):
|
|
def setUp(self):
|
|
module = ModuleFactory()
|
|
user = UserFactory(username='aschi')
|
|
content_block = ContentBlockFactory(module=module)
|
|
|
|
request = RequestFactory().get('/')
|
|
request.user = user
|
|
self.client = Client(schema=schema, context_value=request)
|
|
|
|
self.sibling_id = to_global_id('ContentBlock', content_block.pk)
|
|
|
|
def test_add_new_content_block(self):
|
|
self.assertEqual(ContentBlock.objects.count(), 1)
|
|
|
|
mutation = """
|
|
mutation AddContentBlock($input: AddContentBlockInput!) {
|
|
addContentBlock(input: $input) {
|
|
newContentBlock {
|
|
id
|
|
title
|
|
slug
|
|
contents
|
|
type
|
|
}
|
|
errors
|
|
clientMutationId
|
|
}
|
|
}
|
|
"""
|
|
|
|
title = "Hello World"
|
|
|
|
result = self.client.execute(mutation, variables={
|
|
'input': {
|
|
"contentBlock": {
|
|
"title": title,
|
|
"contents": [
|
|
{
|
|
"type": "text_block",
|
|
"value": {
|
|
"text": "Hello there"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
"after": self.sibling_id
|
|
}
|
|
})
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(ContentBlock.objects.count(), 2)
|
|
|
|
new_content_block = result.get('data').get('addContentBlock').get('newContentBlock')
|
|
self.assertEqual(new_content_block.get('title'), title)
|
|
self.assertEqual(len(new_content_block['contents']), 1)
|
|
|
|
id = new_content_block.get('id')
|
|
content_block_page = get_object(ContentBlock, id)
|
|
|
|
self.assertEqual(content_block_page.title, title)
|
|
|
|
def test_addNewContentBlock_withImageUrlBlock(self):
|
|
self.assertEqual(ContentBlock.objects.count(), 1)
|
|
|
|
mutation = """
|
|
mutation AddContentBlock($input: AddContentBlockInput!) {
|
|
addContentBlock(input: $input) {
|
|
newContentBlock {
|
|
id
|
|
title
|
|
slug
|
|
contents
|
|
type
|
|
}
|
|
errors
|
|
clientMutationId
|
|
}
|
|
}
|
|
"""
|
|
|
|
title = "Hello World"
|
|
|
|
result = self.client.execute(mutation, variables={
|
|
'input': {
|
|
"contentBlock": {
|
|
"title": title,
|
|
"type": "NORMAL",
|
|
"contents": [
|
|
{
|
|
"type": "image_url_block",
|
|
'value': {
|
|
"url": "/test.png"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
"after": self.sibling_id
|
|
}
|
|
})
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(ContentBlock.objects.count(), 2)
|
|
|
|
new_content_block = result.get('data').get('addContentBlock').get('newContentBlock')
|
|
self.assertEqual(new_content_block.get('title'), title)
|
|
self.assertEqual(len(new_content_block['contents']), 1)
|
|
self.assertEqual(new_content_block['contents'], [{'type': 'image_url_block', 'value': {'url': '/test.png'}}])
|