-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
test_attachment_url.py
54 lines (45 loc) · 2.04 KB
/
test_attachment_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# coding: utf-8
import requests
from django.urls import reverse
from django_digest.test import DigestAuth
from django_digest.test import Client as DigestClient
from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.logger.models import Attachment
from onadata.apps.viewer.views import attachment_url
from onadata.libs.utils.storage import delete_user_storage
class TestAttachmentUrl(TestBase):
def setUp(self):
self.attachment_count = 0
TestBase.setUp(self)
self._create_user_and_login()
self._publish_transportation_form()
self._submit_transport_instance_w_attachment()
self.url = reverse(
'attachment_url', kwargs={'size': 'original'})
def test_attachment_url(self):
self.assertEqual(
Attachment.objects.count(), self.attachment_count + 1)
response = self.client.get(
self.url, {"media_file": self.attachment_media_file})
self.assertEqual(response.status_code, 200) # nginx is used as proxy
def test_attachment_url_with_digest_auth(self):
self.client.logout()
response = self.client.get(
self.url, {'media_file': self.attachment_media_file}
)
self.assertEqual(response.status_code, 401) # nginx is used as proxy
self.assertTrue('WWW-Authenticate' in response)
digest_client = DigestClient()
digest_client.set_authorization(self.login_username, self.login_password)
response = digest_client.get(self.url, {'media_file': self.attachment_media_file})
self.assertEqual(response.status_code, 200)
def test_attachment_not_found(self):
response = self.client.get(
self.url, {"media_file": "non_existent_attachment.jpg"})
self.assertEqual(response.status_code, 404)
def test_attachment_has_mimetype(self):
attachment = Attachment.objects.all().reverse()[0]
self.assertEqual(attachment.mimetype, 'image/jpeg')
def tearDown(self):
if self.user:
delete_user_storage(self.user.username)