some basic testing for external recipe edits

This commit is contained in:
vabene1111 2020-03-17 17:48:23 +01:00
parent 7518d8c6b1
commit 6fcbc9f0cd
2 changed files with 32 additions and 5 deletions

View File

@ -1,7 +1,7 @@
from django.contrib import auth from django.contrib import auth
from django.urls import reverse from django.urls import reverse
from cookbook.models import Recipe, RecipeIngredient, Ingredient, Unit from cookbook.models import Recipe, RecipeIngredient, Ingredient, Unit, Storage
from cookbook.tests.views.test_views import TestViews from cookbook.tests.views.test_views import TestViews
@ -97,3 +97,32 @@ class TestEditsRecipe(TestViews):
with open('cookbook/tests/resources/image.png', 'rb') as file: with open('cookbook/tests/resources/image.png', 'rb') as file:
r = self.client.post(url, {'name': "Changed", 'working_time': 15, 'waiting_time': 15, 'image': file}) r = self.client.post(url, {'name': "Changed", 'working_time': 15, 'waiting_time': 15, 'image': file})
self.assertEqual(r.status_code, 200) self.assertEqual(r.status_code, 200)
def test_external_recipe_update(self):
storage = Storage.objects.create(
name='TestStorage',
method=Storage.DROPBOX,
created_by=auth.get_user(self.client),
token='test',
username='test',
password='test',
)
recipe = Recipe.objects.create(
name='Test',
created_by=auth.get_user(self.client),
storage=storage,
)
url = reverse('edit_external_recipe', args=[recipe.pk])
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
r = self.anonymous_client.get(url)
self.assertEqual(r.status_code, 302)
r = self.client.post(url, {'name': 'Test', 'working_time': 15, 'waiting_time': 15, })
recipe.refresh_from_db()
self.assertEqual(recipe.working_time, 15)
self.assertEqual(recipe.waiting_time, 15)

View File

@ -277,13 +277,11 @@ class ExternalRecipeUpdate(LoginRequiredMixin, UpdateView):
old_recipe = Recipe.objects.get(pk=self.object.pk) old_recipe = Recipe.objects.get(pk=self.object.pk)
if not old_recipe.name == self.object.name: if not old_recipe.name == self.object.name:
if self.object.storage.method == Storage.DROPBOX: if self.object.storage.method == Storage.DROPBOX:
Dropbox.rename_file(old_recipe, Dropbox.rename_file(old_recipe, self.object.name) # TODO central location to handle storage type switches
self.object.name) # TODO central location to handle storage type switches
if self.object.storage.method == Storage.NEXTCLOUD: if self.object.storage.method == Storage.NEXTCLOUD:
Nextcloud.rename_file(old_recipe, self.object.name) Nextcloud.rename_file(old_recipe, self.object.name)
self.object.file_path = os.path.dirname(self.object.file_path) + '/' + self.object.name + \ self.object.file_path = os.path.dirname(self.object.file_path) + '/' + self.object.name + os.path.splitext(self.object.file_path)[1]
os.path.splitext(self.object.file_path)[1]
messages.add_message(self.request, messages.SUCCESS, _('Changes saved!')) messages.add_message(self.request, messages.SUCCESS, _('Changes saved!'))
return super(ExternalRecipeUpdate, self).form_valid(form) return super(ExternalRecipeUpdate, self).form_valid(form)