Esempio n. 1
0
    def test_last_modified_as_cache_key(self, timestamp1, timestamp2,
                                        cache_stale_expected):
        client = Client()

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp1,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertTrue(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp1,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertFalse(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp2,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertEqual(cache_stale_expected, mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp2,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertFalse(mocked_get.called)
Esempio n. 2
0
    def test_last_modified_as_cache_key(self, timestamp1, timestamp2, cache_stale_expected):
        client = Client()

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp1, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertTrue(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp1, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertFalse(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp2, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertEqual(cache_stale_expected, mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp2, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertFalse(mocked_get.called)
Esempio n. 3
0
    def test_validate_request(self):
        client = Client()
        path = reverse('validate')

        # GET validation
        response = client.get(path,
                              data=dict(
                                  get_required=123,
                                  get_not_required=123,
                                  unknown=123,
                              ))
        self.assertEqual(
            200,
            response.status_code,
        )

        # POST validation
        response = client.post(
            path,
            data=json.dumps(
                dict(
                    post_required=123,
                    post_not_required=123,
                    unknown=123,
                )),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )

        # GET and POST validation
        response = client.patch(
            '{path}?{query}'.format(
                path=path,
                query=urllib.parse.urlencode(dict(get_required=123)),
            ),
            data=json.dumps(dict(post_required=123)),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )
Esempio n. 4
0
    def test_file_validate_request(self):
        excepted_json = {"data": "document.txt"}

        client = Client()
        path = reverse('validate_file')

        response = client.post(path,
                               data={
                                   "file":
                                   SimpleUploadedFile(
                                       "document.txt",
                                       b"file_content",
                                       content_type="text/plain")
                               })

        self.assertEqual(200, response.status_code)
        self.assertDictEqual(excepted_json,
                             json.loads(response.content.decode()))
Esempio n. 5
0
 def test_validate_request_errors(self, method, query, data, invalid_params):
     client = Client()
     path = reverse('validate')
     response = client.generic(
         method=method,
         path='{path}?{query}'.format(
             path=path,
             query=urllib.parse.urlencode(query),
         ),
         data=json.dumps(data),
         content_type='application/json',
     )
     self.assertEqual(
         400,
         response.status_code,
     )
     data = json.loads(response.content.decode())
     actual_invalid_params = data.get('error', {}).get('description', {}).keys()
     self.assertSetEqual(invalid_params, set(actual_invalid_params))
Esempio n. 6
0
    def test_validate_request(self):
        client = Client()
        path = reverse('validate')

        # GET validation
        response = client.get(path, data=dict(
            get_required=123,
            get_not_required=123,
            unknown=123,
        ))
        self.assertEqual(
            200,
            response.status_code,
        )

        # POST validation
        response = client.post(
            path,
            data=json.dumps(dict(
                post_required=123,
                post_not_required=123,
                unknown=123,
            )),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )

        # GET and POST validation
        response = client.patch(
            '{path}?{query}'.format(
                path=path,
                query=urllib.parse.urlencode(dict(get_required=123)),
            ),
            data=json.dumps(dict(post_required=123)),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )
Esempio n. 7
0
 def test_validate_request_errors(self, method, query, data,
                                  invalid_params):
     client = Client()
     path = reverse('validate')
     response = client.generic(
         method=method,
         path='{path}?{query}'.format(
             path=path,
             query=urllib.parse.urlencode(query),
         ),
         data=json.dumps(data),
         content_type='application/json',
     )
     self.assertEqual(
         400,
         response.status_code,
     )
     data = json.loads(response.content.decode())
     actual_invalid_params = data.get('error', {}).get('description',
                                                       {}).keys()
     self.assertSetEqual(invalid_params, set(actual_invalid_params))
Esempio n. 8
0
    def test_file_validate_request(self):
        excepted_json = {
            "data": "document.txt"
        }

        client = Client()
        path = reverse('validate_file')

        response = client.post(
            path,
            data={
                "file": SimpleUploadedFile(
                    "document.txt",
                    b"file_content",
                    content_type="text/plain"
                )
            }
        )

        self.assertEqual(200, response.status_code)
        self.assertDictEqual(
            excepted_json,
            json.loads(response.content.decode())
        )
Esempio n. 9
0
 def test_file_validate_request_errors(self):
     client = Client()
     path = reverse('validate_file')
     response = client.post(path)
     self.assertEqual(400, response.status_code)
Esempio n. 10
0
    def test_response_headers(self):
        client = Client()

        # GET request of not cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cachable content
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content
        response = client.post(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cached content without If-Modified-Since provided
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content with If-Modified-Since provided
        response = client.post(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of non-cachable content
        response = client.get(reverse('ok'))
        self.assertIn('Cache-Control', response)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('max-age=0', response['Cache-Control'])
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])
Esempio n. 11
0
 def test_status_code(self, url_name, expected_status_code):
     client = Client()
     response = client.get(reverse(url_name))
     self.assertEqual(expected_status_code, response.status_code)
Esempio n. 12
0
 def test_file_validate_request_errors(self):
     client = Client()
     path = reverse('validate_file')
     response = client.post(path)
     self.assertEqual(400, response.status_code)
Esempio n. 13
0
    def test_response_headers(self):
        client = Client()

        # GET request of not cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cachable content
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content
        response = client.post(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cached content without If-Modified-Since provided
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content with If-Modified-Since provided
        response = client.post(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of non-cachable content
        response = client.get(reverse('ok'))
        self.assertIn('Cache-Control', response)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('max-age=0', response['Cache-Control'])
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])
Esempio n. 14
0
 def test_status_code(self, url_name, expected_status_code):
     client = Client()
     response = client.get(reverse(url_name))
     self.assertEqual(expected_status_code, response.status_code)