55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import json
|
|
|
|
from django.test import TestCase, Client
|
|
from core.factories import UserFactory
|
|
|
|
|
|
class ApiAccessTestCase(TestCase):
|
|
|
|
def setUp(self):
|
|
self.query = json.dumps({
|
|
'operationName': 'ModulesQuery',
|
|
'query': 'query ModulesQuery { modules { edges { node { id }}}}',
|
|
'variables': None,
|
|
})
|
|
|
|
def test_graphqlEndpoint_shouldNotBeAccessibleWithoutLogin(self):
|
|
c = Client()
|
|
response = c.post('/api/graphql/', data=self.query, content_type='application/json')
|
|
self.assertEqual(response.status_code, 302)
|
|
self.assertEqual(response.url, '/login?next=/api/graphql/')
|
|
|
|
def test_graphqlEndpoint_shouldBeAccessibleWithLogin(self):
|
|
user = UserFactory(username='admin')
|
|
|
|
c = Client()
|
|
c.login(username='admin', password='test')
|
|
response = c.post('/api/graphql/', data=self.query, content_type='application/json')
|
|
|
|
self.assertEqual(200, response.status_code)
|
|
|
|
def test_publicGraphqlEndpoint_shouldBeAccessibleWithoutLogin(self):
|
|
|
|
query= json.dumps({
|
|
'operationName': 'Login',
|
|
'query': '''
|
|
mutation Login($input: LoginInput!){
|
|
login(input: $input) {
|
|
success
|
|
errors {
|
|
field
|
|
}
|
|
}
|
|
}
|
|
''',
|
|
'variables': {
|
|
'input': {
|
|
'usernameInput': 'test',
|
|
'passwordInput': 'test'
|
|
}
|
|
},
|
|
})
|
|
c = Client()
|
|
response = c.post('/api/graphql-public/', data=query, content_type='application/json')
|
|
self.assertEqual(response.status_code, 200)
|