def test_create_token_for_user(self): """ Test that a token is created for the user """ payload = { 'email': '*****@*****.**', 'password': '******', } sample_user(**payload) res = self.client.post(TOKEN_URL, payload) self.assertIn('token', res.data) self.assertEqual(res.status_code, status.HTTP_200_OK)
def test_create_token_invalid_credentials(self): """ Test that token is not created if invalid credentials are given """ sample_user(email='*****@*****.**', password='******') payload = { 'email': '*****@*****.**', 'password': '******', } res = self.client.post(TOKEN_URL, payload) self.assertNotIn('token', res.data) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_existing_user(self): """ Test creating a duplicate fails """ payload = { 'email': '*****@*****.**', 'password': '******', 'name': 'yea boiiiii', } sample_user(**payload) res = self.client.post(CREATE_USER_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
def test_ingredient_str(self): """ Test the ingredient string representation """ ingredient = models.Ingredient.objects.create(user=sample_user(), name='Potato') self.assertEqual(str(ingredient), ingredient.name)
def test_recipe_str(self): """ Test the recipe string representation """ recipe = models.Recipe.objects.create( user=sample_user(), title='Steak and mushroom sauce', time_minutes=5, price=5.00, ) self.assertEqual(str(recipe), recipe.title)
def test_list_recipes_only_for_current_user(self): """ Recipes returned must be for the current user """ user2 = sample_user(email="*****@*****.**") sample_recipe(user=user2) recipe = sample_recipe(user=self.user) res = self.client.get(RECIPES_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]['title'], recipe.title)
def test_list_ingredients_only_for_current_user(self): """ Ingredients returned must be for the current user """ user2 = sample_user( email='*****@*****.**', password='******', name='aww yeaaaaa', ) Ingredient.objects.create(user=user2, name='Salt') ingredient = Ingredient.objects.create(user=self.user, name='Flour') res = self.client.get(INGREDIENTS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]['name'], ingredient.name)
def test_list_tags_for_only_current_user(self): """ Tags returned must be for the current user """ user2 = sample_user( email='*****@*****.**', password='******', name='aww yeaaaaa', ) Tag.objects.create(user=user2, name='Vegan') tag = Tag.objects.create(user=self.user, name='Dessert') res = self.client.get(TAGS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data[0]['name'], tag.name)
def setUp(self): self.user = sample_user() self.client = APIClient() self.client.force_authenticate(user=self.user)
def test_tag_str(self): """ Test the Tag string representation """ tag = models.Tag.objects.create(user=sample_user(), name='Vegan') self.assertEqual(str(tag), tag.name)
def setUp(self): self.client = Client() self.admin = sample_superuser() self.client.force_login(self.admin) self.user = sample_user()