Example #1
0
 def get_file(filepath):
     fileobj = Mock()
     if filepath.endswith("node"):
         fileobj.contents = node.as_json()
     elif filepath.endswith("definition"):
         fileobj.contents = definitions_file.as_yaml()
     else:
         type(fileobj).contents = mock.PropertyMock(side_effect=ztpserver.repository.FileObjectNotFound)
     return fileobj
Example #2
0
 def get_file(filepath):
     fileobj = Mock()
     if filepath.endswith("node"):
         fileobj.contents = node.as_json()
     elif filepath.endswith("definition"):
         fileobj.contents = definitions_file.as_yaml()
     elif filepath.endswith("attributes"):
         fileobj.contents = attributes_file.as_yaml()
     return fileobj
Example #3
0
    def test_upload_an_image(self, ImageTruck, delay):
        truck = Mock()
        ImageTruck.new_from_stream.return_value = truck
        truck.filename = "CA7"
        truck.url.return_value = "ess three"
        truck.contents = b""
        truck.content_type = "image/jpeg"

        session = Client().session()
        album = Album(name="11:11 Eleven Eleven")
        session.add(album)
        session.flush()

        response = self.app.post(
            "/add", data={"url": "", "album_id": album.album_id, "file": (StringIO(str("booya")), "img.jpg")}
        )

        image = session.query(Image).one()

        eq_(image.filename, "CA7")
        eq_(image.source_url, "")
        eq_(image.album_id, album.album_id)

        eq_(response.status_code, 302, response.data)
        eq_(response.headers["Location"], "http://localhost/image/{0}".format(image.image_id))

        contents = session.query(ImageContents).one()
        eq_(image.image_id, contents.image_id)

        delay.assert_called_with([], process_image, contents.image_contents_id)
    def test_tag_no_arguments(self):
        parser = Mock()
        token = Mock()
        token.contents = Mock(methods=["split"])
        token.contents.split.return_value = ["tagged_in_object"]

        self.assertRaises(template.TemplateSyntaxError, TaggedInObjects.tag, parser, token)
Example #5
0
 def mock_truck(self):
     truck = Mock()
     with open(SOME_GIF, "r") as fh:
         truck.contents = fh.read()
     truck.url.return_value = "https://catsnap.cdn/ca7face"
     truck.filename = "ca7face"
     truck.content_type = "image/gif"
     return truck
Example #6
0
    def test_get_members_tag_missing_arg(self):
        parser = Parser(None)
        token = Mock(spec=["split_contents"], name="Taxonomy Mock")
        token.contents = "get_members %s" % self.taxonomy_group.name
        token.split_contents.return_value = ("get_members", self.taxonomy_group.name, self.taxonomy_item.name)
        token.split_contents.side_effect = ValueError

        from templatetags.taxonomy_tags import get_taxonomy_members

        self.assertRaises(TemplateSyntaxError, get_taxonomy_members, *[parser, token])
Example #7
0
    def test_reorients_images(self, ReorientImage, ImageTruck):
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck
        ReorientImage.reorient_image.return_value = image_data

        process_image(transaction_id, contents.image_contents_id)

        ReorientImage.reorient_image.assert_called_with(image_data)
Example #8
0
    def test_uploads_to_s3(self, ImageTruck):
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        process_image(transaction_id, contents.image_contents_id)

        ImageTruck.assert_called_with(image_data, "image/png", None)
        truck.upload.assert_called_with()
Example #9
0
    def test_makes_resizes(self, ResizeImage, ImageTruck):
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        process_image(transaction_id, contents.image_contents_id)

        call_args = ResizeImage.make_resizes.call_args
        eq_(call_args[0][0], image)
        eq_(call_args[0][1], truck)
        # the 3rd arg is the after_upload callback, which we can't assert well
        eq_(len(call_args[0]), 3)
Example #10
0
    def test_deletes_processed_contents(self, ImageTruck):
        session = Client().session()
        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        process_image(transaction_id, contents.image_contents_id)

        session = Client().session()
        contents = session.query(ImageContents).all()

        eq_(contents, [])
    def test_tag(self, Variable, search):
        parser = Mock()
        token = Mock()
        token.contents = Mock(methods=["split"])
        token.contents.split.return_value = ["tagged_in_object", "tagged_in as tags"]
        m = Mock()
        m.groups.return_value = ("tagged_in", "tags")
        search.return_value = m

        node = BaseTaggedInObjects.tag(parser, token)

        self.assertTrue(token.contents.split.called)
        search.assert_called_with(r"(.*?) as (\w+)", "tagged_in as tags")
        self.assertTrue(m.groups.called)
        self.assertEqual(node.tagged_in, Variable.return_value)
        self.assertEqual(node.tags, Variable.return_value)
Example #12
0
    def test_upload_an_image_twice(self, ImageTruck, delay):
        truck = Mock()
        ImageTruck.new_from_stream.return_value = truck
        truck.filename = "CA7"
        truck.url.return_value = "ess three"
        truck.contents = b""
        truck.content_type = "image/jpeg"

        response = self.app.post("/add", data={"url": "", "file": (StringIO(str("booya")), "img.jpg")})
        eq_(response.status_code, 302)
        response = self.app.post("/add", data={"url": "", "file": (StringIO(str("booya")), "img.jpg")})
        eq_(response.status_code, 302)

        session = Client().session()
        image = session.query(Image).one()
        contentses = session.query(ImageContents).all()
        for contents in contentses:
            eq_(contents.image_id, image.image_id)
        contents_calls = [call([], process_image, x.image_contents_id) for x in contentses]
        delay.assert_has_calls(contents_calls)
Example #13
0
 def test_readMyFuse(self, mock_fuse):
     mock_fuse.return_value = None
     conn = MagicMock()
     buf = ctypes.create_string_buffer(4)
     fuse = MyFuse(
         VOFS("vos:/anode", "/tmp/vos_", None, conn=conn, cache_limit=100, cache_max_flush_threads=3),
         "/tmp/vospace",
         fsname="vos:",
         nothreads=5,
         foreground=False,
     )
     fuse.raw_fi = True
     fuse.encoding = "ascii"
     fuse.operations = Mock()
     fuse.operations.return_value = 3
     fip = Mock()
     fip.contents = "somevalue"
     retsize = fuse.read("/some/path", buf, 10, 1, fip)
     fuse.operations.assert_called_once_with("read", "/some/path", 10, 1, "somevalue", buf)
     self.assertEqual(3, retsize, "Wrong buffer size")
Example #14
0
    def test_reprocess_image(self, ImageTruck, delay):
        truck = Mock()
        truck.contents = b"a party in my mouth and everyone is invited"
        truck.content_type = "image/jpeg"
        ImageTruck.new_from_image.return_value = truck

        session = Client().session()
        image = Image(filename="facecafe")
        session.add(image)
        session.flush()

        response = self.app.post("/image/reprocess/{0}.json".format(image.image_id))
        eq_(response.status_code, 200)
        body = json.loads(response.data)
        eq_(body, {"status": "ok"})

        contents = session.query(ImageContents).one()
        eq_(image.image_id, contents.image_id)
        eq_(contents.contents, "a party in my mouth and everyone is invited")
        eq_(contents.content_type, "image/jpeg")

        delay.assert_called_with([], process_image, contents.image_contents_id)
Example #15
0
    def test_upload_an_image_with_json_format(self, ImageTruck, delay):
        truck = Mock()
        ImageTruck.new_from_url.return_value = truck
        truck.filename = "CA741C"
        truck.url.return_value = "cloudfrunt.nut/CA741C"
        truck.contents = b""
        truck.content_type = "image/gif"

        task_id = str(uuid.uuid4())
        delay.return_value = task_id

        response = self.app.post("/add.json", data={"album": "", "url": "imgur.com/cool_cat.gif"})
        eq_(response.status_code, 200, response.data)

        session = Client().session()
        image = session.query(Image).one()
        body = json.loads(response.data)

        eq_(body, [{"url": "cloudfrunt.nut/CA741C", "image_id": image.image_id, "task_id": task_id}])
        contents = session.query(ImageContents).one()
        eq_(contents.image_id, image.image_id)
        delay.assert_called_with([], process_image, contents.image_contents_id)
Example #16
0
    def test_resize_happens_before_main_upload(self, ResizeImage, ImageTruck):
        class StopDoingThingsNow(StandardError):
            pass

        transaction_id = TaskTransaction.new_id()
        image_data = self.image_data()
        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        truck.upload.side_effect = StopDoingThingsNow
        ImageTruck.return_value = truck

        try:
            process_image(transaction_id, contents.image_contents_id)
        except StopDoingThingsNow:
            pass

        call_args = ResizeImage.make_resizes.call_args
        eq_(call_args[0][0], image)
        eq_(call_args[0][1], truck)
        # the 3rd arg is the after_upload callback, which we can't assert well
        eq_(len(call_args[0]), 3)
Example #17
0
    def test_calculates_metadata(self, ImageTruck, ReorientImage, ResizeImage):
        session = Client().session()
        with open(EXIF_JPG, "r") as fh:
            image_data = fh.read()

        (image, contents) = self.setup_contents(image_data)
        truck = Mock()
        truck.contents = image_data
        ImageTruck.return_value = truck

        transaction_id = TaskTransaction.new_id()
        session.flush()
        process_image(transaction_id, contents.image_contents_id)

        image = session.query(Image).filter(Image.image_id == image.image_id).one()

        eq_(image.camera, "SAMSUNG NX210")
        eq_(image.photographed_at, "2013-05-03 09:17:02")
        eq_(image.aperture, "1/4.5")
        eq_(image.shutter_speed, "1/800")
        eq_(image.focal_length, 30.0)
        eq_(image.iso, 200)
def node_factory(node, invocation):
    mt = Mock()
    mt.contents = invocation
    return node(Mock(), mt)
 def test_too_many_args(self):
     mt = Mock()
     mt.contents = "tag url resize '48x48' as as_var extra"
     self.assertRaises(TemplateSyntaxError, LazythumbNode, Mock(), mt)
 def test_too_few_args(self):
     mt = Mock()
     mt.contents = "tag url resize"
     self.assertRaises(TemplateSyntaxError, LazythumbNode, Mock(), mt)
 def test_invalid_action(self):
     mt = Mock()
     mt.contents = "tag url boom '48' as as_var"
     self.assertRaises(TemplateSyntaxError, LazythumbNode, Mock(), mt)