Exemplo n.º 1
0
    def test_download_response(self):
        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        data = client.action(schema, ['response', 'download'])
        assert data.basename == 'download.png'
        assert data.read() == b'some file content'
    def test_multipart_encoding_in_body(self):
        from coreapi.utils import File

        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        example = {
            'foo': File(name='example.txt', content='123'),
            'bar': 'abc'
        }
        data = client.action(schema, ['encoding', 'multipart-body'],
                             params={'example': example})

        expected = {
            'method': 'POST',
            'content_type': 'multipart/form-data',
            'query_params': {},
            'data': {
                'bar': 'abc'
            },
            'files': {
                'foo': {
                    'name': 'example.txt',
                    'content': '123'
                }
            }
        }
        assert data == expected
    def test_multipart_encoding(self):
        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        with tempfile.NamedTemporaryFile() as temp:
            temp.write(b'example file content')
            temp.flush()
            temp.seek(0)

            name = os.path.basename(temp.name)
            data = client.action(schema, ['encoding', 'multipart'],
                                 params={'example': temp})

        expected = {
            'method': 'POST',
            'content_type': 'multipart/form-data',
            'query_params': {},
            'data': {},
            'files': {
                'example': {
                    'name': name,
                    'content': 'example file content'
                }
            }
        }
        assert data == expected
 def test_query_params(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['location', 'query'],
                          params={'example': 123})
     expected = {'method': 'GET', 'query_params': {'example': '123'}}
     assert data == expected
Exemplo n.º 5
0
    def test_text_response(self):
        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        data = client.action(schema, ['response', 'text'])

        expected = '123'
        assert data == expected
Exemplo n.º 6
0
 def test_query_params_with_multiple_values(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['location', 'query'], params={'example': [1, 2, 3]})
     expected = {
         'method': 'GET',
         'query_params': {'example': ['1', '2', '3']}
     }
     assert data == expected
 def test_query_params(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['location', 'query'], params={'example': 123})
     expected = {
         'method': 'GET',
         'query_params': {'example': '123'}
     }
     assert data == expected
Exemplo n.º 8
0
class CourseTests(APITestCase):
    def setUp(self):
        # create one dummy teacher
        self.teacher = Teacher.objects.create(
            user = User.objects.create_user(
                username = "******",
                email = "*****@*****.**"
            )
        )
        
        # create one dummy student
        self.student = Student.objects.create(
            user = User.objects.create_user(
                username = "******",
                email = "*****@*****.**"
            )
        )

        # create courses
        self.courses = [
            Course.objects.create(
                name = "Croozers Logic Course",
                teacher_id = self.teacher.user.id,
                description = "Keep Kroozin!",
                deadline = datetime.datetime.now(),
                student_count = 5,
                price = 0
            ),
            Course.objects.create(
                name = "Mathematik",
                teacher_id = self.teacher.user.id,
                description = "Mein Mathe",
                deadline = datetime.datetime.now(),
                student_count = 5,
                price = 20
            )
        ]

        # assign student to course
        self.student.courses.add(self.courses[0])
        
        # create tokens for each user
        self.student_token = createToken(self.student.user)
        self.teacher_token = createToken(self.teacher.user)

        self.student_client = CoreAPIClient()
        self.student_client.session.headers.update({"Authentication": "JWT " + self.student_token})

    def test_get_student_courses(self):
        url = reverse("course-list")
        response = self.student_client.get(url)
        
        self.assertEqual(response.status_code, status.HTTP_200_OK, url)
        print(response.data)
        self.assertEqual(response.data, {"id": 5})
        
 def test_api_client(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     assert schema.title == 'Example API'
     assert schema.url == 'https://api.example.com/'
     assert schema['simple_link'].description == 'example link'
     assert schema['location']['query'].fields[
         0].schema.description == 'example field'
     data = client.action(schema, ['simple_link'])
     expected = {'method': 'GET', 'query_params': {}}
     assert data == expected
Exemplo n.º 10
0
 def test_urlencoded_encoding_in_body(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['encoding', 'urlencoded-body'], params={'example': {'foo': 123, 'bar': True}})
     expected = {
         'method': 'POST',
         'content_type': 'application/x-www-form-urlencoded',
         'query_params': {},
         'data': {'foo': '123', 'bar': 'true'},
         'files': {}
     }
     assert data == expected
Exemplo n.º 11
0
 def test_body_params(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['location', 'body'], params={'example': 123})
     expected = {
         'method': 'POST',
         'content_type': 'application/json',
         'query_params': {},
         'data': 123,
         'files': {}
     }
     assert data == expected
Exemplo n.º 12
0
 def test_urlencoded_encoding_multiple_values(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['encoding', 'urlencoded'], params={'example': [1, 2, 3]})
     expected = {
         'method': 'POST',
         'content_type': 'application/x-www-form-urlencoded',
         'query_params': {},
         'data': {'example': ['1', '2', '3']},
         'files': {}
     }
     assert data == expected
 def test_api_client(self):
     client = CoreAPIClient()
     schema = client.get('http://api.example.com/')
     assert schema.title == 'Example API'
     assert schema.url == 'https://api.example.com/'
     assert schema['simple_link'].description == 'example link'
     assert schema['location']['query'].fields[0].schema.description == 'example field'
     data = client.action(schema, ['simple_link'])
     expected = {
         'method': 'GET',
         'query_params': {}
     }
     assert data == expected
Exemplo n.º 14
0
 def test_coreapi(self):
     client = CoreAPIClient()
     client.session.auth = HTTPBasicAuth(self.user.data['username'],
                                         self.user.data['password'])
     client.session.headers.update({'x-test': 'true'})
     schema = client.get('http://testserver/api/v1/schema/')
     result = client.action(schema, ['user', 'list'])
     self.assertEqual(result['count'], 1)
     create_data = dict(username='******', password='******', password2='123')
     result = client.action(schema, ['user', 'add'], create_data)
     self.assertEqual(result['username'], create_data['username'])
     self.assertFalse(result['is_staff'])
     self.assertTrue(result['is_active'])
Exemplo n.º 15
0
    def test_multipart_encoding_no_file(self):
        # When no file is included, multipart encoding should still be used.
        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        data = client.action(schema, ['encoding', 'multipart'], params={'example': 123})

        expected = {
            'method': 'POST',
            'content_type': 'multipart/form-data',
            'query_params': {},
            'data': {'example': '123'},
            'files': {}
        }
        assert data == expected
Exemplo n.º 16
0
    def test_raw_upload_explicit_content_type(self):
        from coreapi.utils import File

        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        example = File('example.txt', '123', 'text/html')
        data = client.action(schema, ['encoding', 'raw_upload'], params={'example': example})

        expected = {
            'method': 'POST',
            'files': {'file': {'name': 'example.txt', 'content': '123'}},
            'content_type': 'text/html'
        }
        assert data == expected
    def test_multipart_encoding_in_body(self):
        from coreapi.utils import File

        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        example = {'foo': File(name='example.txt', content='123'), 'bar': 'abc'}
        data = client.action(schema, ['encoding', 'multipart-body'], params={'example': example})

        expected = {
            'method': 'POST',
            'content_type': 'multipart/form-data',
            'query_params': {},
            'data': {'bar': 'abc'},
            'files': {'foo': {'name': 'example.txt', 'content': '123'}}
        }
        assert data == expected
Exemplo n.º 18
0
    def test_raw_upload(self):
        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        with tempfile.NamedTemporaryFile(delete=False) as temp:
            temp.write(b'example file content')
            temp.flush()
            temp.seek(0)

            name = os.path.basename(temp.name)
            data = client.action(schema, ['encoding', 'raw_upload'], params={'example': temp})

        expected = {
            'method': 'POST',
            'files': {'file': {'name': name, 'content': 'example file content'}},
            'content_type': 'application/octet-stream'
        }
        assert data == expected
Exemplo n.º 19
0
    def test_multipart_encoding_string_file_content(self):
        # Test for `coreapi.utils.File` support.
        from coreapi.utils import File

        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        example = File(name='example.txt', content='123')
        data = client.action(schema, ['encoding', 'multipart'], params={'example': example})

        expected = {
            'method': 'POST',
            'content_type': 'multipart/form-data',
            'query_params': {},
            'data': {},
            'files': {'example': {'name': 'example.txt', 'content': '123'}}
        }
        assert data == expected
    def test_multipart_encoding(self):
        client = CoreAPIClient()
        schema = client.get('http://api.example.com/')

        with tempfile.NamedTemporaryFile() as temp:
            temp.write(b'example file content')
            temp.flush()
            temp.seek(0)

            name = os.path.basename(temp.name)
            data = client.action(schema, ['encoding', 'multipart'], params={'example': temp})

        expected = {
            'method': 'POST',
            'content_type': 'multipart/form-data',
            'query_params': {},
            'data': {},
            'files': {'example': {'name': name, 'content': 'example file content'}}
        }
        assert data == expected
Exemplo n.º 21
0
 def test_session_headers(self):
     client = CoreAPIClient()
     client.session.headers.update({'X-Custom-Header': 'foo'})
     schema = client.get('http://api.example.com/')
     data = client.action(schema, ['headers'])
     assert data['headers']['X-CUSTOM-HEADER'] == 'foo'