Commits

Chris Grubbs committed 94702e9

starting on edit form validation tests

  • Participants
  • Parent commits 5fdf32a

Comments (0)

Files changed (3)

File phlombay/recipes/tests.py

         self.client.login(username='user2', password='secret')
         response = self.client.get('/recipes/my-page/')
         self.assertTemplateUsed(response, 'recipes/my_page.html')
+        self.assertContains(response, 'Go Bananas')
 
     def test_my_page_not_logged_in(self):
         response = self.client.get('/recipes/my-page/')
         self.assertRedirects(response, '/recipes/login/?next=/recipes/my-page/')
+
+    def test_edit_recipe(self):
+        self.client.login(username='user2', password='secret')
+        response = self.client.get('/recipes/edit/3/')
+        self.assertContains(response, 'almond milk')
+        self.assertContains(response, 'Place all ingredients in a blender')
+        
+        # Submit blank form
+        response = self.client.post('/recipes/edit/3/', {})
+        self.assertFormError(response, 'recipe_form', 'name', u'This field is required.')
+        self.assertFormError(response, 'recipe_form', 'description', u'This field is required.')
+        self.assertFormError(response, 'recipe_form', 'time_required', u'This field is required.')
+        self.assertFormError(response, 'recipe_form', 'servings', u'This field is required.')
+        self.assertFormError(response, 'recipe_form', 'ingredients_json_blob', u'Recipes must have at least one ingredient.')
+        self.assertFormError(response, 'recipe_form', 'steps_json_blob', u'Recipes must have at least one step.')
+
+    def test_edit_recipe_user_does_not_own(self):
+        self.client.login(username='user2', password='secret')
+        response = self.client.get('/recipes/edit/1/')
+        self.assertRedirects(response, '/recipes/detail/1/')

File phlombay/recipes/views.py

                    'steps_json_blob': StepsWidget()}
 
     def clean_ingredients_json_blob(self):
-        ingredients_json = json.loads(self.cleaned_data['ingredients_json_blob'])
+        ingredients_json = json.loads(self.cleaned_data['ingredients_json_blob'])        
         cleaned_ingredients = []
+        
         for ingredient in ingredients_json:
             if ingredient['quantity'].strip() == '' and ingredient['unit'].strip() == '' and ingredient['ingredient'].strip() == '':
                 continue
             elif ingredient['quantity'].strip() == '' or ingredient['ingredient'].strip() == '':
-                raise forms.ValidationError('Ingredients must have a quantity and a name.')
+                raise forms.ValidationError(u'Ingredients must have a quantity and a name.')
             cleaned_ingredients.append(ingredient)
+
+        if not len(cleaned_ingredients):
+            raise forms.ValidationError(u'Recipes must have at least one ingredient.')
+
         return json.dumps(cleaned_ingredients)
 
     def clean_steps_json_blob(self):
         steps_json = json.loads(self.cleaned_data['steps_json_blob'])
         cleaned_steps = []
+
         for step in steps_json:
             if step['text'].strip() == '':
                 continue
             cleaned_steps.append(step)
+
+        if not len(cleaned_steps):
+            raise forms.ValidationError(u'Recipes must have at least one step.')
+
         return json.dumps(cleaned_steps)
 
 

File phlombay/templates/recipes/edit_recipe.html

 	<form method="post">
 		<div class="row">
 			<div class="span6">
-				<label>Recipe name</label>
-				{{ recipe_form.name }}
+				<div class="control-group{% if recipe_form.name.errors %} error{% endif %}">
+					<label>Recipe name</label>
+					<div class="controls">
+        				{{ recipe_form.name }}
+        				{% for error in recipe_form.name.errors %}
+        					<span class="help-inline">{{ error }}</span>
+        				{% endfor %}
+        			</div>
+				</div>
 				
-				<label>Description</label>
-				{{ recipe_form.description }}
+				<div class="control-group{% if recipe_form.description.errors %} error{% endif %}">
+					<label>Description</label>
+					<div class="controls">
+        				{{ recipe_form.description }}
+        				{% for error in recipe_form.description.errors %}
+        					<span class="help-inline">{{ error }}</span>
+        				{% endfor %}
+        			</div>
+				</div>
 			</div>
 			<div class="span6">
-				<label>Time required</label>
-				{{ recipe_form.time_required }}<span class="help-inline"><small>i.e. "15 minutes"</small></span>
+				<div class="control-group{% if recipe_form.time_required.errors %} error{% endif %}">
+					<label>Time required</label>
+					<div class="controls">
+        				{{ recipe_form.time_required }}
+        				{% if recipe_form.time_required.errors %}
+	        				{% for error in recipe_form.time_required.errors %}
+	        					<span class="help-inline">{{ error }}</span>
+	        				{% endfor %}
+	        			{% else %}
+	        				<span class="help-inline"><small>i.e. "15 minutes"</small></span>
+	        			{% endif %}
+        			</div>
+				</div>
 				
-				<label>Servings</label>
-				{{ recipe_form.servings }}<span class="help-inline"><small>How many servings this recipe yields</small></span>
+				<div class="control-group{% if recipe_form.servings.errors %} error{% endif %}">
+					<label>Servings</label>
+					<div class="controls">
+        				{{ recipe_form.servings }}
+        				{% if recipe_form.servings.errors %}
+	        				{% for error in recipe_form.servings.errors %}
+	        					<span class="help-inline">{{ error }}</span>
+	        				{% endfor %}
+        				{% else %}
+        					<span class="help-inline"><small>How many servings this recipe yields</small></span>
+        				{% endif %}
+        			</div>
+				</div>
 			</div>
 		</div>
 		
 			    		<a class="btn" href="javascript:;" id="addStep"><i class="icon-plus"></i> Add step</a>
 			    	</div>
 		    	</div>
-		    </div>
-			<ol id="stepsList">
-			{{ recipe_form.steps_json_blob }}
-			</ol>
+			    {% if recipe_form.steps_json_blob.errors %}
+					<div class="control-group error">
+					{% for error in recipe_form.steps_json_blob.errors %}
+						<span class="help-inline">{{ error }}</span>
+		        	{% endfor %}
+		    		</div>
+				{% endif %}
+				<ol id="stepsList">
+				{{ recipe_form.steps_json_blob }}
+				</ol>
+			</div>
 		</div>
 		
 		<div class="control-group">