Skip to content

Commit

Permalink
tests: Add full Bugzilla attach API coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Cole Robinson <crobinso@redhat.com>
  • Loading branch information
crobinso committed Jan 10, 2020
1 parent 48797ca commit f6b6725
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bugzilla/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ def attachfile(self, idlist, attachfile, description, **kwargs):
kwargs['summary'] = description

data = f.read()
if not isinstance(data, bytes):
if not isinstance(data, bytes): # pragma: no cover
data = data.encode(locale.getpreferredencoding())

kwargs['ids'] = listify(idlist)
Expand Down Expand Up @@ -1586,7 +1586,7 @@ def openattachment_data(self, attachment_dict):

if hasattr(data, "data"):
# This is for xmlrpc Binary
content = data.data
content = data.data # pragma: no cover
else:
import base64
content = base64.b64decode(data)
Expand Down
5 changes: 5 additions & 0 deletions tests/data/mockargs/test_api_attachments_create1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{'content_type': 'text/plain',
'file_name': 'bz-attach-get1.txt',
'ids': [123456],
'is_private': True,
'summary': 'some desc'}
1 change: 1 addition & 0 deletions tests/data/mockargs/test_attachments_get1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions tests/data/mockargs/test_attachments_getall1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'exclude_fields': ['bar'], 'include_fields': ['foo']}
2 changes: 2 additions & 0 deletions tests/data/mockargs/test_attachments_update1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{'flags': [{'is_patch': True, 'name': 'needinfo', 'value': 'foobar'}],
'ids': [112233]}
2 changes: 2 additions & 0 deletions tests/mockbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def bug_attachment_get(self, *args):
return self.__helper(args)
def bug_attachment_get_all(self, *args):
return self.__helper(args)
def bug_attachment_update(self, *args):
return self.__helper(args)

def bug_create(self, *args):
return self.__helper(args)
Expand Down
52 changes: 52 additions & 0 deletions tests/test_api_attachments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#

import os

import pytest

import tests
import tests.mockbackend


def test_api_attachments():
# misc coverage testing for Bugzilla attachment APIs
fakebz = tests.mockbackend.make_bz(
bug_attachment_get_all_args=(
"data/mockargs/test_attachments_getall1.txt"),
bug_attachment_get_all_return={},
bug_attachment_update_args=(
"data/mockargs/test_attachments_update1.txt"),
bug_attachment_update_return={},
bug_attachment_get_args=(
"data/mockargs/test_attachments_get1.txt"),
bug_attachment_get_return=(
"data/mockreturn/test_attach_get1.txt"),
bug_attachment_create_args=(
"data/mockargs/test_api_attachments_create1.txt"),
bug_attachment_create_return={
"attachments": {"123456": {}, "456789": []}},
)

# coverage for include/exclude handling
fakebz.get_attachments([123456], None,
include_fields=["foo"], exclude_fields="bar")

# coverage for updateattachment
fakebz.updateattachmentflags(None, "112233", "needinfo",
value="foobar", is_patch=True)

# coverage for openattachment
fobj = fakebz.openattachment(502352)
assert "Hooray" in str(fobj.read())

# Error on bad input type
with pytest.raises(TypeError):
fakebz.attachfile([123456], None, "some desc")

# Misc attachfile() pieces
attachfile = os.path.dirname(__file__) + "/data/bz-attach-get1.txt"
ret = fakebz.attachfile([123456], attachfile, "some desc",
isprivate=True)
assert ret == [123456, 456789]

0 comments on commit f6b6725

Please sign in to comment.