Beispiel #1
0
class ApiTestCase(AdminLoggedOutTestCase):
    """
    We take the more complicated way here, and implement the API tests
    with Python requests. This ensures that we are getting as close as possible
    to 'real' API clients, e.g. from JavaScript.

    CRSF token and JWT are transported as cookie by default, in Django.
    The HTTP verb methods allow to override this and add according headers
    with the information. This is intended to support testing JavaScript AJAX calls,
    with seem to have trouble accessing the cookies sometimes. Ask @Kat-Hi.
    """
    def setUp(self):
        super().setUp()
        self.client = RequestsClient()
        self.jwt = None
        self.csrf = None

    def get(self, relative_url, headers={}):
        if self.jwt:
            headers["Authorization"] = "Bearer " + self.jwt
        return self.client.get('http://testserver' + relative_url,
                               headers=headers)

    def patch(self, relative_url, data, headers={}):
        if self.jwt:
            headers["Authorization"] = "Bearer " + self.jwt
        if self.csrf:
            headers['X-CSRFToken'] = self.csrf
        return self.client.patch('http://testserver' + relative_url,
                                 json=data,
                                 headers=headers)

    def post(self, relative_url, data=None, headers={}):
        if self.jwt:
            headers["Authorization"] = "Bearer " + self.jwt
        if self.csrf:
            headers['X-CSRFToken'] = self.csrf
        return self.client.post('http://testserver' + relative_url,
                                json=data,
                                headers=headers)

    def options(self, relative_url, headers={}):
        return self.client.options('http://testserver' + relative_url)

    def api_login(self):
        response = self.post(f'/api/{API_VERSION}/login/', {
            'username': admin_data['username'],
            'password': admin_clear_password
        })
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('access_token', data)
        self.jwt = data['access_token']
Beispiel #2
0
 def options(self, relative_url, headers=None):
     client = RequestsClient()
     return client.options('http://testserver' + relative_url,
                           headers=self._all_headers(headers))
Beispiel #3
0
    def test_options_request_works_without_auth(self):
        client = RequestsClient()
        client.headers.update({'x-test': 'true'})

        response = client.options('http://testserver/documents/')
        assert response.status_code == 200