|
| 1 | +import io |
| 2 | + |
1 | 3 | from bx_py_utils.test_utils.html_assertion import HtmlAssertionMixin
|
2 | 4 | from django.contrib.auth.models import User
|
3 | 5 | from django.test import TestCase
|
|
6 | 8 |
|
7 | 9 | from inventory import __version__
|
8 | 10 | from inventory.models import ItemImageModel, ItemModel
|
| 11 | +from inventory.models.item import ItemFileModel |
9 | 12 | from inventory.permissions import get_or_create_normal_user_group
|
10 | 13 |
|
11 | 14 |
|
@@ -136,3 +139,59 @@ def test_new_item_with_image(self):
|
136 | 139 | assert image.name == 'test.png'
|
137 | 140 | assert image.item == item
|
138 | 141 | assert image.user_id == self.normaluser.pk
|
| 142 | + |
| 143 | + def test_new_item_with_file(self): |
| 144 | + self.client.force_login(self.normaluser) |
| 145 | + |
| 146 | + in_memory_file = io.BytesIO(b'FooBar') |
| 147 | + in_memory_file.seek(0) |
| 148 | + in_memory_file.name = 'a-file-upload.dat' |
| 149 | + |
| 150 | + response = self.client.post( |
| 151 | + path='/admin/inventory/itemmodel/add/', |
| 152 | + data={ |
| 153 | + 'kind': 'with-file', |
| 154 | + 'name': 'With File!', |
| 155 | + |
| 156 | + 'itemimagemodel_set-TOTAL_FORMS': '0', |
| 157 | + 'itemimagemodel_set-INITIAL_FORMS': '0', |
| 158 | + 'itemimagemodel_set-MIN_NUM_FORMS': '0', |
| 159 | + 'itemimagemodel_set-MAX_NUM_FORMS': '1000', |
| 160 | + 'itemimagemodel_set-__prefix__-position': '0', |
| 161 | + |
| 162 | + 'itemfilemodel_set-TOTAL_FORMS': '1', |
| 163 | + 'itemfilemodel_set-INITIAL_FORMS': '0', |
| 164 | + 'itemfilemodel_set-MIN_NUM_FORMS': '0', |
| 165 | + 'itemfilemodel_set-MAX_NUM_FORMS': '1000', |
| 166 | + 'itemfilemodel_set-0-position': '0', |
| 167 | + 'itemfilemodel_set-0-file': in_memory_file, |
| 168 | + 'itemfilemodel_set-__prefix__-position': '0', |
| 169 | + |
| 170 | + 'itemlinkmodel_set-TOTAL_FORMS': '0', |
| 171 | + 'itemlinkmodel_set-INITIAL_FORMS': '0', |
| 172 | + 'itemlinkmodel_set-MIN_NUM_FORMS': '0', |
| 173 | + 'itemlinkmodel_set-MAX_NUM_FORMS': '1000', |
| 174 | + 'itemlinkmodel_set-__prefix__-position': '0', |
| 175 | + |
| 176 | + '_save': 'Save', |
| 177 | + }, |
| 178 | + ) |
| 179 | + self.assertRedirects(response, expected_url='/admin/inventory/itemmodel/') |
| 180 | + |
| 181 | + data = list(ItemModel.objects.values_list('kind__name', 'name')) |
| 182 | + assert data == [('with-file', 'With File!')] |
| 183 | + |
| 184 | + item = ItemModel.objects.first() |
| 185 | + |
| 186 | + self.assert_messages(response, expected_messages=[ |
| 187 | + f'The Item "<a href="/admin/inventory/itemmodel/{item.pk}/change/"> - With File!</a>"' |
| 188 | + f' was added successfully.' |
| 189 | + ]) |
| 190 | + |
| 191 | + assert item.user_id == self.normaluser.pk |
| 192 | + |
| 193 | + assert ItemFileModel.objects.count() == 1 |
| 194 | + instance = ItemFileModel.objects.first() |
| 195 | + assert instance.name == 'test.png' |
| 196 | + assert instance.item == item |
| 197 | + assert instance.user_id == self.normaluser.pk |
0 commit comments