<commit_before># Copyright 2021 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

""" Copyright 2012, 2013 UW Information Technology, University of Washington
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

from django.test import TestCase
from django.conf import settings
from django.test.client import Client, encode_multipart
from django.core.files.uploadedfile import SimpleUploadedFile
from spotseeker_server.models import Item, ItemImage, Spot
from os.path import abspath, dirname
import os
import random
import tempfile
import shutil
from django.test.utils import override_settings
from mock import patch
from spotseeker_server import models

TEST_ROOT = abspath(dirname(__file__))


@override_settings(SPOTSEEKER_AUTH_MODULE='spotseeker_server.auth.all_ok')
@override_settings(SPOTSEEKER_AUTH_ADMINS=('demo_user',))
class ItemImagePUTTest(TestCase):
    """ Tests updating a ItemImage by PUTting to
        /api/v1/Item/<Item id>/image/<image_id>.
    """

    def setUp(self):
        self.TEMP_DIR = tempfile.mkdtemp()
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            spot = Spot.objects.create(
                name="This is a spot fot testing PUTting images")
            spot.save()
            item = Item.objects.create(
                name="This is to test PUTtingimages",
                spot=spot
            )
            item.save()
            self.item = item

            self.url = '/api/v1/item/{0}'.format(self.item.pk)
            self.url = self.url

            # GIF
            gif = self.item.itemimage_set.create(
                description="This is the GIF test",
                image=SimpleUploadedFile(
                    "test_gif.gif",
                    open(
                        "%s/../resources/test_gif.gif" % TEST_ROOT, 'rb'
                    ).read(),
                    'image/gif'
                )
            )

            self.gif = gif
            self.gif_url = "%s/image/%s" % (self.url, self.gif.pk)

            # JPEG
            jpeg = self.item.itemimage_set.create(
                description="This is the JPEG test",
                image=SimpleUploadedFile(
                    "test_jpeg.jpg",
                    open(
                        "%s/../resources/test_jpeg.jpg" % TEST_ROOT, 'rb'
                    ).read(),
                    'image/jpeg'
                )
            )

            self.jpeg = jpeg
            self.jpeg_url = "%s/image/%s" % (self.url, self.jpeg.pk)

            # PNG
            png = self.item.itemimage_set.create(
                description="This is the PNG test",
                image=SimpleUploadedFile(
                    "test_png.png",
                    open(
                        "%s/../resources/test_png.png" % TEST_ROOT, 'rb'
                    ).read(),
                    'image/png'
                )
            )

            self.png = png
            self.png_url = "%s/image/%s" % (self.url, self.png.pk)

    def test_bad_url(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            item = Item.objects.create(name="This is the wrong Item")

            url = "/api/v1/item/{0}/image/{1}".format(
                item.pk,
                self.jpeg.pk)
            response = c.put(url, '{}', content_type="application/json")
            self.assertEquals(response.status_code, 404)

    def test_invalid_url(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            bad_url = "%s/image/aa" % self.url
            response = c.put(bad_url,
                             '{}',
                             content_type="application/json")
            self.assertEquals(response.status_code, 404)

    def test_invalid_id_too_high(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            test_id = self.gif.pk + 10000
            test_url = "%s/image/%s" % (self.url, test_id)
            response = c.put(test_url,
                             '{}',
                             content_type="application/json")
            self.assertEquals(response.status_code, 404)

    def test_valid_same_type_with_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            response = c.get(self.jpeg_url)
            try:
                etag = unicode(response['etag'])
            except NameError:
                etag = response['etag']
            new_jpeg_name = "testing PUT name: {0}".format(random.random())

            response = c.put(
                self.jpeg_url,
                files={
                    "description": new_jpeg_name,
                    "image": SimpleUploadedFile(
                        new_jpeg_name,
                        open(
                            "%s/../resources/test_jpeg2.jpg" % TEST_ROOT, 'rb'
                        ).read(),
                        'image/jpeg'
                    )
                },
                If_Match=etag
            )
            f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT, 'rb')
            f2 = open("%s/../resources/test_jpeg.jpg" % TEST_ROOT, 'rb')
            self.assertEquals(response.status_code, 200)
            self.assertEquals(int(response["content-length"]),
                              len(f.read()))
            self.assertNotEqual(int(response["content-length"]),
                                len(f2.read()))
            self.assertEquals(response["content-type"], "image/jpeg")

    def test_valid_different_image_type_valid_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            response = c.get(self.gif_url)
            try:
                etag = unicode(response['etag'])
            except NameError:
                etag = response['etag']
            new_name = "testing PUT name: {0}".format(random.random())

            response = c.put(
                self.gif_url,
                files={
                    "description": new_name,
                    "image": SimpleUploadedFile(
                        new_name,
                        open(
                            "%s/../resources/test_png.png" % TEST_ROOT, 'rb'
                        ).read(),
                        'image/png'
                    )
                },
                content_type="multipart/form-data; boundary=--aklsjf--",
                If_Match=etag
            )
            self.assertEquals(response.status_code, 200)
            f = open("%s/../resources/test_png.png" % TEST_ROOT, 'rb')
            f2 = open("%s/../resources/test_gif.gif" % TEST_ROOT, 'rb')

            # Just to be sure
            response = c.get(self.gif_url)
            self.assertEquals(response["content-type"], "image/png")
            self.assertEquals(int(response["content-length"]),
                              len(f.read()))
            self.assertNotEqual(int(response["content-length"]),
                                len(f2.read()))

    def test_invalid_image_type_valid_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            response = c.get(self.gif_url)
            etag = response["ETag"]

            f = open("%s/../resources/test_png.png" % TEST_ROOT, 'rb')
            f2 = open("%s/../resources/test_gif.gif" % TEST_ROOT, 'rb')

            new_name = "testing PUT name: {0}".format(random.random())

            c = Client()
            f = open("%s/../resources/fake_jpeg.jpg" % TEST_ROOT, 'rb')
            response = c.put(
                self.gif_url,
                files={"description": "This is a text file", "image": f},
                If_Match=etag
            )
            f.close()
            self.assertEquals(response.status_code, 400)

    # Want this to be one of the first tests to run
    def test_a_valid_image_no_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            # GIF
            f = open("%s/../resources/test_gif2.gif" % TEST_ROOT, 'rb')
            new_gif_name = "testing PUT name: {0}".format(random.random())
            response = c.put(self.gif_url,
                             files={"description": new_gif_name,
                                    "image": f},
                             content_type="image/gif")
            self.assertEquals(response.status_code, 400)

            updated_img = ItemImage.objects.get(pk=self.gif.pk)
            self.assertEquals(updated_img.image, self.gif.image)

            # JPEG
            f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT, 'rb')
            new_jpeg_name = "testing PUT name: {0}".format(random.random())
            response = c.put(self.gif_url,
                             files={"description": new_jpeg_name,
                                    "image": f},
                             content_type="image/jpeg")
            self.assertEquals(response.status_code, 400)

            updated_img = ItemImage.objects.get(pk=self.jpeg.pk)
            self.assertEquals(updated_img.description,
                              self.jpeg.description)

            # PNG
            f = open("%s/../resources/test_png2.png" % TEST_ROOT, 'rb')
            new_png_name = "testing PUT name: {0}".format(random.random())
            response = c.put(self.gif_url,
                             files={"description": new_png_name,
                                    "image": f},
                             content_type="image/png")
            self.assertEquals(response.status_code, 400)

            updated_img = ItemImage.objects.get(pk=self.png.pk)
            self.assertEquals(updated_img.description,
                              self.png.description)

            response = c.get(self.gif_url)
            content_length = response["content-length"]
            self.assertNotEqual(os.fstat(f.fileno()).st_size,
                                int(content_length))

            f = open("%s/../resources/test_gif.gif" % TEST_ROOT, 'rb')
            self.assertEquals(os.fstat(f.fileno()).st_size,
                              int(content_length))

    def tearDown(self):
        shutil.rmtree(self.TEMP_DIR)<commit_msg>Reformat and update copyright.<commit_after># Copyright 2021 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

from django.test import TestCase
from django.conf import settings
from django.test.client import Client, encode_multipart
from django.core.files.uploadedfile import SimpleUploadedFile
from spotseeker_server.models import Item, ItemImage, Spot
from os.path import abspath, dirname
import os
import random
import tempfile
import shutil
from django.test.utils import override_settings
from mock import patch
from spotseeker_server import models

TEST_ROOT = abspath(dirname(__file__))


@override_settings(SPOTSEEKER_AUTH_MODULE="spotseeker_server.auth.all_ok")
@override_settings(SPOTSEEKER_AUTH_ADMINS=("demo_user",))
class ItemImagePUTTest(TestCase):
    """Tests updating a ItemImage by PUTting to
    /api/v1/Item/<Item id>/image/<image_id>.
    """

    def setUp(self):
        self.TEMP_DIR = tempfile.mkdtemp()
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            spot = Spot.objects.create(
                name="This is a spot fot testing PUTting images"
            )
            spot.save()
            item = Item.objects.create(
                name="This is to test PUTtingimages", spot=spot
            )
            item.save()
            self.item = item

            self.url = "/api/v1/item/{0}".format(self.item.pk)
            self.url = self.url

            # GIF
            gif = self.item.itemimage_set.create(
                description="This is the GIF test",
                image=SimpleUploadedFile(
                    "test_gif.gif",
                    open(
                        "%s/../resources/test_gif.gif" % TEST_ROOT, "rb"
                    ).read(),
                    "image/gif",
                ),
            )

            self.gif = gif
            self.gif_url = "%s/image/%s" % (self.url, self.gif.pk)

            # JPEG
            jpeg = self.item.itemimage_set.create(
                description="This is the JPEG test",
                image=SimpleUploadedFile(
                    "test_jpeg.jpg",
                    open(
                        "%s/../resources/test_jpeg.jpg" % TEST_ROOT, "rb"
                    ).read(),
                    "image/jpeg",
                ),
            )

            self.jpeg = jpeg
            self.jpeg_url = "%s/image/%s" % (self.url, self.jpeg.pk)

            # PNG
            png = self.item.itemimage_set.create(
                description="This is the PNG test",
                image=SimpleUploadedFile(
                    "test_png.png",
                    open(
                        "%s/../resources/test_png.png" % TEST_ROOT, "rb"
                    ).read(),
                    "image/png",
                ),
            )

            self.png = png
            self.png_url = "%s/image/%s" % (self.url, self.png.pk)

    def test_bad_url(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            item = Item.objects.create(name="This is the wrong Item")

            url = "/api/v1/item/{0}/image/{1}".format(item.pk, self.jpeg.pk)
            response = c.put(url, "{}", content_type="application/json")
            self.assertEquals(response.status_code, 404)

    def test_invalid_url(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            bad_url = "%s/image/aa" % self.url
            response = c.put(bad_url, "{}", content_type="application/json")
            self.assertEquals(response.status_code, 404)

    def test_invalid_id_too_high(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            test_id = self.gif.pk + 10000
            test_url = "%s/image/%s" % (self.url, test_id)
            response = c.put(test_url, "{}", content_type="application/json")
            self.assertEquals(response.status_code, 404)

    def test_valid_same_type_with_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            response = c.get(self.jpeg_url)
            try:
                etag = unicode(response["etag"])
            except NameError:
                etag = response["etag"]
            new_jpeg_name = "testing PUT name: {0}".format(random.random())

            response = c.put(
                self.jpeg_url,
                files={
                    "description": new_jpeg_name,
                    "image": SimpleUploadedFile(
                        new_jpeg_name,
                        open(
                            "%s/../resources/test_jpeg2.jpg" % TEST_ROOT, "rb"
                        ).read(),
                        "image/jpeg",
                    ),
                },
                If_Match=etag,
            )
            f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT, "rb")
            f2 = open("%s/../resources/test_jpeg.jpg" % TEST_ROOT, "rb")
            self.assertEquals(response.status_code, 200)
            self.assertEquals(int(response["content-length"]), len(f.read()))
            self.assertNotEqual(
                int(response["content-length"]), len(f2.read())
            )
            self.assertEquals(response["content-type"], "image/jpeg")

    def test_valid_different_image_type_valid_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            response = c.get(self.gif_url)
            try:
                etag = unicode(response["etag"])
            except NameError:
                etag = response["etag"]
            new_name = "testing PUT name: {0}".format(random.random())

            response = c.put(
                self.gif_url,
                files={
                    "description": new_name,
                    "image": SimpleUploadedFile(
                        new_name,
                        open(
                            "%s/../resources/test_png.png" % TEST_ROOT, "rb"
                        ).read(),
                        "image/png",
                    ),
                },
                content_type="multipart/form-data; boundary=--aklsjf--",
                If_Match=etag,
            )
            self.assertEquals(response.status_code, 200)
            f = open("%s/../resources/test_png.png" % TEST_ROOT, "rb")
            f2 = open("%s/../resources/test_gif.gif" % TEST_ROOT, "rb")

            # Just to be sure
            response = c.get(self.gif_url)
            self.assertEquals(response["content-type"], "image/png")
            self.assertEquals(int(response["content-length"]), len(f.read()))
            self.assertNotEqual(
                int(response["content-length"]), len(f2.read())
            )

    def test_invalid_image_type_valid_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            response = c.get(self.gif_url)
            etag = response["ETag"]

            f = open("%s/../resources/test_png.png" % TEST_ROOT, "rb")
            f2 = open("%s/../resources/test_gif.gif" % TEST_ROOT, "rb")

            new_name = "testing PUT name: {0}".format(random.random())

            c = Client()
            f = open("%s/../resources/fake_jpeg.jpg" % TEST_ROOT, "rb")
            response = c.put(
                self.gif_url,
                files={"description": "This is a text file", "image": f},
                If_Match=etag,
            )
            f.close()
            self.assertEquals(response.status_code, 400)

    # Want this to be one of the first tests to run
    def test_a_valid_image_no_etag(self):
        with self.settings(MEDIA_ROOT=self.TEMP_DIR):
            c = Client()
            # GIF
            f = open("%s/../resources/test_gif2.gif" % TEST_ROOT, "rb")
            new_gif_name = "testing PUT name: {0}".format(random.random())
            response = c.put(
                self.gif_url,
                files={"description": new_gif_name, "image": f},
                content_type="image/gif",
            )
            self.assertEquals(response.status_code, 400)

            updated_img = ItemImage.objects.get(pk=self.gif.pk)
            self.assertEquals(updated_img.image, self.gif.image)

            # JPEG
            f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT, "rb")
            new_jpeg_name = "testing PUT name: {0}".format(random.random())
            response = c.put(
                self.gif_url,
                files={"description": new_jpeg_name, "image": f},
                content_type="image/jpeg",
            )
            self.assertEquals(response.status_code, 400)

            updated_img = ItemImage.objects.get(pk=self.jpeg.pk)
            self.assertEquals(updated_img.description, self.jpeg.description)

            # PNG
            f = open("%s/../resources/test_png2.png" % TEST_ROOT, "rb")
            new_png_name = "testing PUT name: {0}".format(random.random())
            response = c.put(
                self.gif_url,
                files={"description": new_png_name, "image": f},
                content_type="image/png",
            )
            self.assertEquals(response.status_code, 400)

            updated_img = ItemImage.objects.get(pk=self.png.pk)
            self.assertEquals(updated_img.description, self.png.description)

            response = c.get(self.gif_url)
            content_length = response["content-length"]
            self.assertNotEqual(
                os.fstat(f.fileno()).st_size, int(content_length)
            )

            f = open("%s/../resources/test_gif.gif" % TEST_ROOT, "rb")
            self.assertEquals(
                os.fstat(f.fileno()).st_size, int(content_length)
            )

    def tearDown(self):
        shutil.rmtree(self.TEMP_DIR)
{'fraction_non_alphanumeric': 0.11838901334372261, 'fraction_numerical': 0.00642836271549625, 'mean_word_length': 1.5402028698664028, 'pattern_counts': {'":': 24, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2636829', 'n_tokens_mistral': 6001, 'n_tokens_neox': 5689, 'n_words': 1055}