def test_file_url(): urlpath = "/rp/jobs/files/" domain = "http://domain.tld" hexes = ["".join([choice(hexdigits) for i in range(32)]) for j in range(6)] with patch.object(settings, "CANONICAL_PORT", "2323"): for hx in hexes: assert file_url(hx) == "%s:2323%s%s/" % (domain, urlpath, hx) for port in ("80", "443", ""): with patch.object(settings, "CANONICAL_PORT", port): for hx in hexes: assert file_url(hx) == "%s%s%s/" % (domain, urlpath, hx)
def test_file_url(): urlpath = "/rp/jobs/files/" domain = "http://domain.tld" hexes = ["".join([choice(hexdigits) for i in range(32)]) for j in range(6)] with patch.object(settings, "CANONICAL_PORT", "2323"): for hx in hexes: assert file_url(hx) == "{0}:2323{1}{2}/".format( domain, urlpath, hx) for port in ("80", "443", ""): with patch.object(settings, "CANONICAL_PORT", port): for hx in hexes: assert file_url(hx) == "{0}{1}{2}/".format(domain, urlpath, hx)
def test_create_file(self): with NamedTemporaryFile(suffix=".xml", delete=True) as tmp: tmp.write(self.file_contents.encode("utf-8")) tmp_name = ospath.split(tmp.name)[-1] tmp.seek(0) response = self.client.post("/rp/jobs/files/", {"file": tmp}) self.assertEquals(201, response.status_code) data = response.data self.assertEquals(self.get_hashed_contents(), data["hexhash"]) self.assertEquals(tmp_name, data["filename"]) self.assertEquals("File contents not shown.", data["contents"]) self.assertEquals(file_url(self.get_hashed_contents()), data["url"]) return response
def test_create_file(self): with NamedTemporaryFile(suffix=".xml", delete=True) as tmp: tmp.write(self.file_contents.encode("utf-8")) tmp_name = ospath.split(tmp.name)[-1] tmp.seek(0) response = self.client.post( "/rp/jobs/files/", {"file": tmp}) self.assertEquals(201, response.status_code) data = response.data self.assertEquals(self.get_hashed_contents(), data["hexhash"]) self.assertEquals(tmp_name, data["filename"]) self.assertEquals("File contents not shown.", data["contents"]) self.assertEquals(file_url(self.get_hashed_contents()), data["url"]) return response
def create(self, request, *args, **kwargs): """ Overrides the ``create`` method of ``mixins.CreateModelMixin`` in order to add the file contents to the database. Side effects Alters the DB. :arg HttpRequest request: the incoming request. :rtype: Response :returns: JSON or HTML of the information about the file (status 201), or about why the file couldn't be added (status 400). """ serialized = self.get_serializer(data=request.data) serialized.is_valid(raise_exception=True) uploaded_file = request.data["file"] if uploaded_file.size > self.size_limit: return Response( dict(error="File too large ({0}-byte limit).".format( self.size_limit)), status=status.HTTP_400_BAD_REQUEST ) if uploaded_file.multiple_chunks(): contents = b"".join(chunk for chunk in uploaded_file.chunks()) else: contents = uploaded_file.read() sha = hashlib.sha256(contents) hexhash = sha.hexdigest() filename = uploaded_file.name url = file_url(hexhash) if not RegulationFile.objects.filter(hexhash=hexhash).exists(): serialized.save(contents=contents, file=uploaded_file, filename=filename, hexhash=hexhash, url=url) headers = self.get_success_headers(serialized.data) return Response(serialized.data, status=status.HTTP_201_CREATED, headers=headers) else: return Response(dict(error="File already present."), status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): """ Overrides the ``create`` method of ``mixins.CreateModelMixin`` in order to add the file contents to the database. Side effects Alters the DB. :arg HttpRequest request: the incoming request. :rtype: Response :returns: JSON or HTML of the information about the file (status 201), or about why the file couldn't be added (status 400). """ serialized = self.get_serializer(data=request.data) serialized.is_valid(raise_exception=True) uploaded_file = request.data["file"] if uploaded_file.size > self.size_limit: return Response(dict(error="File too large (%s-byte limit)." % self.size_limit), status=status.HTTP_400_BAD_REQUEST) if uploaded_file.multiple_chunks(): contents = b"".join(chunk for chunk in uploaded_file.chunks()) else: contents = uploaded_file.read() sha = hashlib.sha256(contents) hexhash = sha.hexdigest() filename = uploaded_file.name url = file_url(hexhash) if not RegulationFile.objects.filter(hexhash=hexhash).exists(): serialized.save(contents=contents, file=uploaded_file, filename=filename, hexhash=hexhash, url=url) headers = self.get_success_headers(serialized.data) return Response(serialized.data, status=status.HTTP_201_CREATED, headers=headers) else: return Response(dict(error="File already present."), status=status.HTTP_400_BAD_REQUEST)