def tests_404(self): product = factories.ProductFactory() access_token = token.return_token() head = {"Bearer": access_token} url = f"https://applifting-python-excercise-ms.herokuapp.com/api/v1/products/{product.id}/offers" response = requests.get(url, headers=head) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def tests_no_auth(self): product = factories.ProductFactory() data = serializers.ProductSerializer(product).data url = "https://applifting-python-excercise-ms.herokuapp.com/api/v1/products/register/" response = requests.post(url, data=data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) head = {"Bearer": "abc"} response = requests.post(url, data=data, headers=head) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def tests_update(self): product = factories.ProductFactory() data = { "name": "Harry Potter and the Methods of Rationality", "description": "A book about a wizard" } self.client.credentials(HTTP_AUTHORIZATION=self.access_token) response = self.client.put(f"/product/{product.id}/", data=data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json()["name"], "Harry Potter and the Methods of Rationality")
def tests_create(self): product = factories.ProductFactory() offer = models.Offer.objects.create(price=159, items_in_stock=12, product=product) factories.OfferFactory() self.assertEqual(models.Offer.objects.all().count(), 2) self.assertEqual( models.Offer.objects.filter(product=product).count(), 1) self.assertEqual( models.Offer.objects.get(product=product).price, offer.price) self.assertEqual( models.Offer.objects.get(product=product).items_in_stock, offer.items_in_stock)
def tests_delete(self): product = factories.ProductFactory() self.client.credentials(HTTP_AUTHORIZATION=self.access_token) response = self.client.delete(f"/product/{product.id}/") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def tests_get(self): product = factories.ProductFactory() for _ in range(5): factories.OfferFactory(product=product) self.assertEqual( models.Offer.objects.filter(product=product).count(), 5)
def tests_delete(self): product = factories.ProductFactory() self.assertEqual(models.Product.objects.all().count(), 1) product.delete() self.assertEqual(models.Product.objects.all().count(), 0)
def tests_get(self): for _ in range(5): factories.ProductFactory() products = models.Product.objects.all() self.assertEqual(products.count(), 5)