def test(self): criteria = 'abc' request = toolbox.add_request(criteria) self.assertEqual(criteria, request.criteria) self.assertTrue(request.requested_at) self.assertFalse(request.file_type) self.assertFalse(request.requester) self.assertFalse(request.content_type) self.assertTrue(request.id) content_type = 'text/csv' request = toolbox.add_request(criteria, content_type=content_type) self.assertEqual(criteria, request.criteria) self.assertEqual(content_type, request.content_type) self.assertTrue(request.requested_at) self.assertFalse(request.file_type) self.assertFalse(request.requester) file_type = 'some-report' request = toolbox.add_request(criteria, content_type=content_type, file_type=file_type) self.assertEqual(criteria, request.criteria) self.assertEqual(content_type, request.content_type) self.assertEqual(file_type, request.file_type) self.assertTrue(request.requested_at) self.assertFalse(request.requester) requester = User.objects.create_user('something', '*****@*****.**', 'abc') request = toolbox.add_request(criteria, content_type=content_type, file_type=file_type, requester=requester) self.assertEqual(criteria, request.criteria) self.assertEqual(content_type, request.content_type) self.assertEqual(file_type, request.file_type) self.assertEqual(requester, request.requester) self.assertTrue(request.requested_at)
def test_already_finished(self): registry.register(SimpleHandler, ['something']) request = toolbox.add_request('abc', file_type='something') request.finished_at = timezone.now() request.save() self.assertFalse(registry.dispatch(request.id)) self.assertEqual(0, len(SimpleHandler.handled))
def test_request_does_not_exist(self): request = toolbox.add_request('abc', requester=self.user) token = toolbox.create_token(request) request.delete() http_request = self.factory.get('/something') http_request.user = self.user self.assertRaises(Http404, serve_file, http_request, token)
def test_other_user_with_general_token(self): other_user = User.objects.create_user('oho', '*****@*****.**', 'nono') with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL='/'): request = toolbox.add_request('abc', requester=self.user) toolbox.attach_file(request, ContentFile(b'abc')) token = toolbox.create_general_token(request) http_request = self.factory.get('/something') http_request.user = other_user # With no verification and no requester required response = serve_file(http_request, token, require_requester=False, verify_requester=False) self.assertEqual(response.status_code, 200) # Now try with simply not requiring the requester http_request.user = other_user self.assertRaises(UserIsNotRequester, serve_file, http_request, token, require_requester=False) # Now try with requiring the requester but not verifying http_request.user = other_user self.assertRaises(SuspiciousToken, serve_file, http_request, token, verify_requester=False)
def test_requester_required_but_not_present(self): request = toolbox.add_request('abc') token = toolbox.create_general_token(request) request.delete() self.assertRaises(BadSignature, toolbox.decode, token, require_requester=True)
def test_no_file_yet(self): request = toolbox.add_request('abc') self.assertRaises(Http404, toolbox.serve, request) to_raise = ValueError self.assertRaises(to_raise, toolbox.serve, request, raise_on_missing=to_raise)
def test_general(self): request = toolbox.add_request('abc') token = toolbox.create_general_token(request) request.delete() self.assertRaises(FileRequest.DoesNotExist, toolbox.decode, token, require_requester=False)
def test_invalid_token(self): with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL='/'): request = toolbox.add_request('abc', requester=self.user) toolbox.attach_file(request, ContentFile(b'abc')) token = toolbox.create_token(request) http_request = self.factory.get('/something') http_request.user = self.user self.assertRaises(SuspiciousToken, serve_file, http_request, token + 'a')
def test_other_user(self): other_user = User.objects.create_user('oho', '*****@*****.**', 'nono') with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL='/'): request = toolbox.add_request('abc', requester=self.user) toolbox.attach_file(request, ContentFile(b'abc')) token = toolbox.create_token(request) http_request = self.factory.get('/something') http_request.user = other_user self.assertRaises(UserIsNotRequester, serve_file, http_request, token)
def test_token_expired(self, decode_mock): decode_mock.side_effect = SignatureExpired with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL='/'): request = toolbox.add_request('abc', requester=self.user) toolbox.attach_file(request, ContentFile(b'abc')) token = toolbox.create_token(request) http_request = self.factory.get('/something') http_request.user = self.user self.assertRaises(SignatureHasExpired, serve_file, http_request, token)
def test(self): request = toolbox.add_request('abc') with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL=''): contents = ContentFile('something special') toolbox.attach_file(request, contents, 'something.txt', 'text/plain') path = request.filehandle.path self.assertTrue(os.path.isfile(path)) request.delete() self.assertFalse(os.path.isfile(path))
def test_other_user_with_no_verification(self): other_user = User.objects.create_user('oho', '*****@*****.**', 'nono') with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL='/'): request = toolbox.add_request('abc', requester=self.user) toolbox.attach_file(request, ContentFile(b'abc')) token = toolbox.create_token(request) http_request = self.factory.get('/something') http_request.user = other_user response = serve_file(http_request, token, verify_requester=False) self.assertEqual(response.status_code, 200)
def test(self): requester = User.objects.create_user('ownow', '*****@*****.**', 'mypassword') request = toolbox.add_request('abc', requester=requester) token = toolbox.create_token(request) # Clear the requester request.requester = None request.save() self.assertRaises(FileRequest.DoesNotExist, toolbox.decode, token, require_requester=True) request.requester = requester request.save() found, data = toolbox.decode(token, require_requester=True) self.assertEqual(found, request)
def test_attach_local_file(self): fh, temppath = tempfile.mkstemp('test') os.close(fh) with open(temppath, 'w') as fh: fh.write(six.text_type('something special')) request = toolbox.add_request('abc') with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL=''): toolbox.attach_local_file(request, temppath, 'something.txt', 'text/plain') path = request.filehandle.path self.assertTrue(os.path.isfile(path)) request.delete() self.assertFalse(os.path.isfile(path))
def test_general(self): requester = User.objects.create_user('ownow', '*****@*****.**', 'mypassword') request = toolbox.add_request('abc', requester=requester) token = toolbox.create_general_token(request) data = toolbox.get_default_signer().load(token) self.assertEquals(data.get('id'), '%s' % request.id) self.assertFalse('requester' in data) # Now with additional data additional_data = {'abc': 123, 'z': ['y', 'z', 1]} token = toolbox.create_general_token(request, data=additional_data) data = toolbox.get_default_signer().load(token) additional_data['id'] = '%s' % request.id self.assertDictEqual(data, additional_data)
def test(self): request = toolbox.add_request('abc') with override_settings(MEDIA_ROOT=self.tempdir, MEDIA_URL=''): contents = ContentFile('something special') toolbox.attach_file(request, contents, 'something.txt') response = toolbox.serve(request) self.assertEqual(response['Content-Type'], 'application/octet-stream') self.assertEqual(response['Content-Disposition'], 'attachment; filename="something.txt"') self.assertEqual(response.content, b'something special') response = toolbox.serve(request, default_content_type='text/plain') self.assertEqual(response['Content-Type'], 'text/plain') self.assertEqual(response.content, b'something special') self.assertEqual(response['Content-Disposition'], 'attachment; filename="something.txt"') request.filename = '' request.save() response = toolbox.serve(request, default_content_type='text/plain') self.assertEqual(response['Content-Type'], 'text/plain') self.assertEqual(response.content, b'something special') self.assertFalse(response.get('Content-Disposition'))
def test(self): registry.register(SimpleHandler, ['something']) request = toolbox.add_request('abc', file_type='something') self.assertTrue(registry.dispatch(request.id)) self.assertEqual([request], SimpleHandler.handled)
def test_no_handler(self): request = toolbox.add_request('abc') self.assertRaises(KeyError, registry.dispatch, request.id) registry.register(SimpleHandler, ['something']) self.assertRaises(KeyError, registry.dispatch, request.id)
def test_missing_requester(self): request = toolbox.add_request('abc') self.assertRaises(RuntimeError, toolbox.create_token, request)
def test_general_does_not_require_requester(self): request = toolbox.add_request('abc') toolbox.create_general_token(request)