Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
squad/api/rest.py: Add URLs to access attachments
Browse files Browse the repository at this point in the history
Attachments can be accessed via testruns, by adding
"attachments?filename=<filename>" to the end of the testrun URL.
However, this is quite unintuitive and prone to user error.

Add a "download_url" field to attachments, which creates a download URL
in each file's entry in the attachments list.

Signed-off-by: Katie Worton <[email protected]>
  • Loading branch information
katieworton committed Nov 6, 2023
1 parent 1900edf commit e6af066
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion squad/api/rest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import urllib.parse
import yaml

from django.db.models import Q, F, Value as V, CharField, Prefetch
Expand Down Expand Up @@ -1310,9 +1311,19 @@ class StatusViewSet(NestedViewSetMixin, ModelViewSet):

class AttachmentSerializer(serializers.ModelSerializer):

download_url = serializers.SerializerMethodField()

class Meta:
model = Attachment
fields = ('filename', 'mimetype', 'length')
fields = ('download_url', 'filename', 'mimetype', 'length')

def get_download_url(self, attachment):
request = self.context.get('request')
if request is None:
return None
base_url = rest_reverse('testrun-detail', args=[attachment.test_run.pk], request=request)
filename_url_encoded = urllib.parse.quote(attachment.filename, safe='')
return f'{base_url}attachments/?filename={filename_url_encoded}'


class TestRunSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
Expand Down
1 change: 1 addition & 0 deletions test/api/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ def test_testruns_attachments(self):

data = self.hit('/api/testruns/%d/' % self.testrun.id)
expected = {
'download_url': 'http://testserver/api/testruns/%d/attachments/?filename=%s' % (self.testrun.id, filename),
'filename': filename,
'length': 147,
'mimetype': 'application/octet-stream'
Expand Down
10 changes: 10 additions & 0 deletions test/frontend/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ def test_attachment(self):
self.assertEqual('text/plain', response['Content-Type'])
self.assertEqual(b'text file', response.content)

def test_attachment_download_url(self):
data = bytes('text file', 'utf-8')
filename = 'foo.txt'
attachment = self.test_run.attachments.create(filename=filename, length=len(data), mimetype="text/plain")
attachment.save_file(filename, data)
# NOTE: /api/testruns/%s/attachments?filename=foo.txt redirects to /api/testruns/%s/attachments/?filename=foo.txt
response = self.hit('/api/testruns/%s/attachments/?filename=foo.txt' % (self.test_run.id))
self.assertEqual('text/plain', response['Content-Type'])
self.assertEqual(b'text file', response.content)

def test_log(self):
response = self.hit('/mygroup/myproject/build/1.0/testrun/%s/suite/%s/test/%s/log' % (self.test_run.id, self.suite.slug, self.test.name))
self.assertEqual('text/plain', response['Content-Type'])
Expand Down

0 comments on commit e6af066

Please sign in to comment.