def test_create_token_for_user(self): """Test that a token is created for user""" payload = {"email": "*****@*****.**", "password": "******"} create_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_valid_credentials(self): """Test that token is not created if invalid credentials are given""" create_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_user_exist(self): """Test creating user that already exist""" payload = { "email": "*****@*****.**", "password": "******", "name": "some name" } create_user(**payload) res = self.client.post(CREATE_USER_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_same_tags_for_diff_users(self): """Test that users can have the same tag""" other_user = create_user(email="*****@*****.**", password="******") self.client.post(TAGS_URL, {"name": "vege"}) self.client.force_authenticate(user=other_user) res = self.client.post(TAGS_URL, {"name": "vege"}) self.assertEqual(res.status_code, status.HTTP_201_CREATED)
def test_recipes_limited_to_user(self): """Test that only recipes for authenticated user are returned""" user2 = create_user(email="*****@*****.**") sample_recipe(user2) recipe = sample_recipe(self.user, title="Other Title") res = self.client.get(RECIPE_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_tags_belong_to_proper_user(self): """Test that tag creation happens only for the owner of the tag""" # create other user other_user = create_user(email="*****@*****.**", password="******") # create tag for user1 and for user2 test_tag = Tag.objects.create(name="Meat", user=self.user) Tag.objects.create(name="Vegetables", user=other_user) Tag.objects.create(name="Others", user=self.user) res = self.client.get(TAGS_URL) # test response code self.assertEqual(res.status_code, status.HTTP_200_OK) # test for number of tags for a single user self.assertEqual(len(res.data), 2) # test if response data is the same as data from the database call self.assertEqual(res.data[0]["name"], test_tag.name)
def setUp(self): self.user = create_user(email="*****@*****.**", password="******") self.client = APIClient() self.client.force_authenticate(user=self.user)
def setUp(self): self.client = APIClient() self.user = create_user() self.client.force_authenticate(user=self.user)
def setUp(self): self.client = APIClient() self.user = create_user() self.client.force_authenticate(self.user) self.recipe = sample_recipe(user=self.user)