Skip to content
Snippets Groups Projects
Commit 89d15870 authored by Andy Summers's avatar Andy Summers
Browse files

Fix more image tests

parent 184fc5ee
No related branches found
No related tags found
1 merge request!4Fix more image tests
...@@ -24,7 +24,7 @@ from django.test.utils import override_settings ...@@ -24,7 +24,7 @@ from django.test.utils import override_settings
from mock import patch from mock import patch
from spotseeker_server import models from spotseeker_server import models
TEST_ROOT = abspath(dirname(__file__)) IMAGE_ROOT = abspath(os.path.join(settings.BASE_DIR, "media"))
@override_settings(SPOTSEEKER_AUTH_MODULE='spotseeker_server.auth.all_ok') @override_settings(SPOTSEEKER_AUTH_MODULE='spotseeker_server.auth.all_ok')
...@@ -35,8 +35,7 @@ class ItemImagePUTTest(TestCase): ...@@ -35,8 +35,7 @@ class ItemImagePUTTest(TestCase):
""" """
def setUp(self): def setUp(self):
self.TEMP_DIR = tempfile.mkdtemp() with self.settings(MEDIA_ROOT=IMAGE_ROOT):
with self.settings(MEDIA_ROOT=self.TEMP_DIR):
spot = Spot.objects.create( spot = Spot.objects.create(
name="This is a spot fot testing PUTting images") name="This is a spot fot testing PUTting images")
spot.save() spot.save()
...@@ -51,7 +50,7 @@ class ItemImagePUTTest(TestCase): ...@@ -51,7 +50,7 @@ class ItemImagePUTTest(TestCase):
self.url = self.url self.url = self.url
# GIF # GIF
f = open("%s/../resources/test_gif.gif" % TEST_ROOT) f = open("%s/test_gif.gif" % IMAGE_ROOT, 'rb')
gif = self.item.itemimage_set.create( gif = self.item.itemimage_set.create(
description="This is the GIF test", description="This is the GIF test",
image=File(f)) image=File(f))
...@@ -61,7 +60,7 @@ class ItemImagePUTTest(TestCase): ...@@ -61,7 +60,7 @@ class ItemImagePUTTest(TestCase):
self.gif_url = "%s/image/%s" % (self.url, self.gif.pk) self.gif_url = "%s/image/%s" % (self.url, self.gif.pk)
# JPEG # JPEG
f = open("%s/../resources/test_jpeg.jpg" % TEST_ROOT) f = open("%s/test_jpeg.jpg" % IMAGE_ROOT, 'rb')
jpeg = self.item.itemimage_set.create( jpeg = self.item.itemimage_set.create(
description="This is the JPEG test", description="This is the JPEG test",
image=File(f)) image=File(f))
...@@ -71,7 +70,7 @@ class ItemImagePUTTest(TestCase): ...@@ -71,7 +70,7 @@ class ItemImagePUTTest(TestCase):
self.jpeg_url = "%s/image/%s" % (self.url, self.jpeg.pk) self.jpeg_url = "%s/image/%s" % (self.url, self.jpeg.pk)
# PNG # PNG
f = open("%s/../resources/test_png.png" % TEST_ROOT) f = open("%s/test_png.png" % IMAGE_ROOT, 'rb')
png = self.item.itemimage_set.create( png = self.item.itemimage_set.create(
description="This is the PNG test", description="This is the PNG test",
image=File(f)) image=File(f))
...@@ -81,7 +80,7 @@ class ItemImagePUTTest(TestCase): ...@@ -81,7 +80,7 @@ class ItemImagePUTTest(TestCase):
self.png_url = "%s/image/%s" % (self.url, self.png.pk) self.png_url = "%s/image/%s" % (self.url, self.png.pk)
def test_bad_url(self): def test_bad_url(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
item = Item.objects.create(name="This is the wrong Item") item = Item.objects.create(name="This is the wrong Item")
...@@ -92,7 +91,7 @@ class ItemImagePUTTest(TestCase): ...@@ -92,7 +91,7 @@ class ItemImagePUTTest(TestCase):
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
def test_invalid_url(self): def test_invalid_url(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
bad_url = "%s/image/aa" % self.url bad_url = "%s/image/aa" % self.url
response = c.put(bad_url, response = c.put(bad_url,
...@@ -101,7 +100,7 @@ class ItemImagePUTTest(TestCase): ...@@ -101,7 +100,7 @@ class ItemImagePUTTest(TestCase):
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
def test_invalid_id_too_high(self): def test_invalid_id_too_high(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
test_id = self.gif.pk + 10000 test_id = self.gif.pk + 10000
test_url = "%s/image/%s" % (self.url, test_id) test_url = "%s/image/%s" % (self.url, test_id)
...@@ -111,13 +110,13 @@ class ItemImagePUTTest(TestCase): ...@@ -111,13 +110,13 @@ class ItemImagePUTTest(TestCase):
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
def test_valid_same_type_with_etag(self): def test_valid_same_type_with_etag(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
response = c.get(self.jpeg_url) response = c.get(self.jpeg_url)
etag = response['etag'] etag = response['etag']
f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT) f = open("%s/test_jpeg2.jpg" % IMAGE_ROOT, 'rb')
f2 = open("%s/../resources/test_jpeg.jpg" % TEST_ROOT) f2 = open("%s/test_jpeg.jpg" % IMAGE_ROOT, 'rb')
new_jpeg_name = "testing PUT name: {0}".format(random.random()) new_jpeg_name = "testing PUT name: {0}".format(random.random())
...@@ -125,7 +124,7 @@ class ItemImagePUTTest(TestCase): ...@@ -125,7 +124,7 @@ class ItemImagePUTTest(TestCase):
files={"description": new_jpeg_name, files={"description": new_jpeg_name,
"image": f}, "image": f},
If_Match=etag) If_Match=etag)
f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT) f = open("%s/test_jpeg2.jpg" % IMAGE_ROOT, 'rb')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(int(response["content-length"]), self.assertEqual(int(response["content-length"]),
len(f.read())) len(f.read()))
...@@ -134,14 +133,14 @@ class ItemImagePUTTest(TestCase): ...@@ -134,14 +133,14 @@ class ItemImagePUTTest(TestCase):
self.assertEqual(response["content-type"], "image/jpeg") self.assertEqual(response["content-type"], "image/jpeg")
def test_valid_different_image_type_valid_etag(self): def test_valid_different_image_type_valid_etag(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
response = c.get(self.gif_url) response = c.get(self.gif_url)
etag = response["etag"] etag = response["etag"]
f = open("%s/../resources/test_png.png" % TEST_ROOT) f = open("%s/test_png.png" % IMAGE_ROOT, 'rb')
f2 = open("%s/../resources/test_gif.gif" % TEST_ROOT) f2 = open("%s/test_gif.gif" % IMAGE_ROOT, 'rb')
new_name = "testing PUT name: {0}".format(random.random()) new_name = "testing PUT name: {0}".format(random.random())
...@@ -152,7 +151,7 @@ class ItemImagePUTTest(TestCase): ...@@ -152,7 +151,7 @@ class ItemImagePUTTest(TestCase):
"boundary=--aklsjf--", "boundary=--aklsjf--",
If_Match=etag) If_Match=etag)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
f = open("%s/../resources/test_png.png" % TEST_ROOT) f = open("%s/test_png.png" % IMAGE_ROOT, 'rb')
# Just to be sure # Just to be sure
response = c.get(self.gif_url) response = c.get(self.gif_url)
...@@ -163,18 +162,18 @@ class ItemImagePUTTest(TestCase): ...@@ -163,18 +162,18 @@ class ItemImagePUTTest(TestCase):
len(f2.read())) len(f2.read()))
def test_invalid_image_type_valid_etag(self): def test_invalid_image_type_valid_etag(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
response = c.get(self.gif_url) response = c.get(self.gif_url)
etag = response["ETag"] etag = response["ETag"]
f = open("%s/../resources/test_png.png" % TEST_ROOT) f = open("%s/test_png.png" % IMAGE_ROOT, 'rb')
f2 = open("%s/../resources/test_gif.gif" % TEST_ROOT) f2 = open("%s/test_gif.gif" % IMAGE_ROOT, 'rb')
new_name = "testing PUT name: {0}".format(random.random()) new_name = "testing PUT name: {0}".format(random.random())
c = Client() c = Client()
f = open("%s/../resources/fake_jpeg.jpg" % TEST_ROOT) f = open("%s/fake_jpeg.jpg" % IMAGE_ROOT, 'rb')
response = c.put(self.gif_url, response = c.put(self.gif_url,
files={"description": "This is a text file", files={"description": "This is a text file",
"image": f}, "image": f},
...@@ -184,10 +183,10 @@ class ItemImagePUTTest(TestCase): ...@@ -184,10 +183,10 @@ class ItemImagePUTTest(TestCase):
# Want this to be one of the first tests to run # Want this to be one of the first tests to run
def test_a_valid_image_no_etag(self): def test_a_valid_image_no_etag(self):
with self.settings(MEDIA_ROOT=self.TEMP_DIR): with self.settings(MEDIA_ROOT=IMAGE_ROOT):
c = Client() c = Client()
# GIF # GIF
f = open("%s/../resources/test_gif2.gif" % TEST_ROOT) f = open("%s/test_gif2.gif" % IMAGE_ROOT, 'rb')
new_gif_name = "testing PUT name: {0}".format(random.random()) new_gif_name = "testing PUT name: {0}".format(random.random())
response = c.put(self.gif_url, response = c.put(self.gif_url,
files={"description": new_gif_name, files={"description": new_gif_name,
...@@ -199,7 +198,7 @@ class ItemImagePUTTest(TestCase): ...@@ -199,7 +198,7 @@ class ItemImagePUTTest(TestCase):
self.assertEqual(updated_img.image, self.gif.image) self.assertEqual(updated_img.image, self.gif.image)
# JPEG # JPEG
f = open("%s/../resources/test_jpeg2.jpg" % TEST_ROOT) f = open("%s/test_jpeg2.jpg" % IMAGE_ROOT, 'rb')
new_jpeg_name = "testing PUT name: {0}".format(random.random()) new_jpeg_name = "testing PUT name: {0}".format(random.random())
response = c.put(self.gif_url, response = c.put(self.gif_url,
files={"description": new_jpeg_name, files={"description": new_jpeg_name,
...@@ -212,7 +211,7 @@ class ItemImagePUTTest(TestCase): ...@@ -212,7 +211,7 @@ class ItemImagePUTTest(TestCase):
self.jpeg.description) self.jpeg.description)
# PNG # PNG
f = open("%s/../resources/test_png2.png" % TEST_ROOT) f = open("%s/test_png2.png" % IMAGE_ROOT, 'rb')
new_png_name = "testing PUT name: {0}".format(random.random()) new_png_name = "testing PUT name: {0}".format(random.random())
response = c.put(self.gif_url, response = c.put(self.gif_url,
files={"description": new_png_name, files={"description": new_png_name,
...@@ -229,9 +228,6 @@ class ItemImagePUTTest(TestCase): ...@@ -229,9 +228,6 @@ class ItemImagePUTTest(TestCase):
self.assertNotEqual(os.fstat(f.fileno()).st_size, self.assertNotEqual(os.fstat(f.fileno()).st_size,
int(content_length)) int(content_length))
f = open("%s/../resources/test_gif.gif" % TEST_ROOT) f = open("%s/test_gif.gif" % IMAGE_ROOT, 'rb')
self.assertEqual(os.fstat(f.fileno()).st_size, self.assertEqual(os.fstat(f.fileno()).st_size,
int(content_length)) int(content_length))
def tearDown(self):
shutil.rmtree(self.TEMP_DIR)
...@@ -65,6 +65,9 @@ urlpatterns = [ ...@@ -65,6 +65,9 @@ urlpatterns = [
path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/constrain/<str:thumb_dimensions>', path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/constrain/<str:thumb_dimensions>',
csrf_exempt(ThumbnailView().run), csrf_exempt(ThumbnailView().run),
{'constrain': True}), {'constrain': True}),
path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/',
csrf_exempt(ThumbnailView().run),
name='spot-image-thumb'),
path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/<str:thumb_dimensions>', path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/<str:thumb_dimensions>',
csrf_exempt(ThumbnailView().run), csrf_exempt(ThumbnailView().run),
name='spot-image-thumb'), name='spot-image-thumb'),
......
...@@ -98,7 +98,7 @@ class ShareSpaceView(RESTDispatch): ...@@ -98,7 +98,7 @@ class ShareSpaceView(RESTDispatch):
path = getattr(settings, path = getattr(settings,
'SS_APP_SPACE_PATH', 'SS_APP_SPACE_PATH',
'/space/{{ spot_id }}/{{ spot_name }}') '/space/{{ spot_id }}/{{ spot_name }}')
path = re.sub(r'{{\s*spot_id\s*}}', spot_id, path) path = re.sub(r'{{\s*spot_id\s*}}', str(spot_id), path)
path = \ path = \
re.sub(r'{{\s*spot_name\s*}}', urlquote(spot.name), path) re.sub(r'{{\s*spot_name\s*}}', urlquote(spot.name), path)
hash_val = \ hash_val = \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment