Beispiel #1
0
    def test_get_user_canteens(self):
        """
        Users can have access to the full representation of their
        canteens (even if they are not published). This endpoint
        is paginated
        """
        user_canteens = [
            ManagerInvitationFactory.create().canteen,
            ManagerInvitationFactory.create().canteen,
        ]
        other_canteens = [
            CanteenFactory.create(),
            CanteenFactory.create(),
        ]
        user = authenticate.user
        for canteen in user_canteens:
            canteen.managers.add(user)

        response = self.client.get(reverse("user_canteens"))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        body = response.json().get("results")

        for user_canteen in user_canteens:
            self.assertTrue(any(x["id"] == user_canteen.id for x in body))

        for recieved_canteen in body:
            self.assertEqual(recieved_canteen["managers"][0]["email"],
                             user.email)
            self.assertTrue(
                "email" in recieved_canteen["managerInvitations"][0])

        for other_canteen in other_canteens:
            self.assertFalse(any(x["id"] == other_canteen.id for x in body))
Beispiel #2
0
    def test_plastic_badge_earned(self):
        """
        Test that the right canteens are identifies in plastic badge qs
        """
        # --- canteens which don't earn
        no_cooking = CanteenFactory.create()
        DiagnosticFactory.create(canteen=no_cooking,
                                 cooking_plastic_substituted=False)
        no_serving = CanteenFactory.create()
        DiagnosticFactory.create(canteen=no_serving,
                                 serving_plastic_substituted=False)
        no_bottles = CanteenFactory.create()
        DiagnosticFactory.create(canteen=no_bottles,
                                 plastic_bottles_substituted=False)
        no_tableware = CanteenFactory.create()
        DiagnosticFactory.create(canteen=no_tableware,
                                 plastic_tableware_substituted=False)
        # --- canteens which earn plastic badge:
        earned = CanteenFactory.create()
        DiagnosticFactory.create(
            canteen=earned,
            cooking_plastic_substituted=True,
            serving_plastic_substituted=True,
            plastic_bottles_substituted=True,
            plastic_tableware_substituted=True,
        )

        badges = badges_for_queryset(Diagnostic.objects.all())

        plastic_badge_qs = badges["plastic"]
        self.assertEqual(plastic_badge_qs.count(), 1)
        self.assertTrue(plastic_badge_qs.filter(canteen=earned).exists())
Beispiel #3
0
    def test_get_published_canteens(self):
        """
        Only published canteens with public data should be
        returned from this call
        """
        published_canteens = [
            CanteenFactory.create(publication_status=Canteen.PublicationStatus.PUBLISHED.value),
            CanteenFactory.create(publication_status=Canteen.PublicationStatus.PUBLISHED.value),
        ]
        private_canteens = [
            CanteenFactory.create(),
            CanteenFactory.create(publication_status=Canteen.PublicationStatus.PENDING.value),
        ]
        response = self.client.get(reverse("published_canteens"))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        body = response.json()
        self.assertEqual(body.get("count"), 2)

        results = body.get("results", [])

        for published_canteen in published_canteens:
            self.assertTrue(any(x["id"] == published_canteen.id for x in results))

        for private_canteen in private_canteens:
            self.assertFalse(any(x["id"] == private_canteen.id for x in results))

        for recieved_canteen in results:
            self.assertFalse("managers" in recieved_canteen)
            self.assertFalse("managerInvitations" in recieved_canteen)
Beispiel #4
0
    def test_available_filter_options(self):
        """
        Test that filter options with data are included in purchases list response
        """
        first_canteen = CanteenFactory.create()
        first_canteen.managers.add(authenticate.user)
        second_canteen = CanteenFactory.create()
        second_canteen.managers.add(authenticate.user)
        PurchaseFactory.create(
            description="avoine",
            canteen=first_canteen,
            category=Purchase.Category.PRODUITS_DE_LA_MER,
            characteristics=[Purchase.Characteristic.BIO],
        )
        PurchaseFactory.create(
            description="tomates",
            canteen=first_canteen,
            category=Purchase.Category.FRUITS_ET_LEGUMES,
            characteristics=[],
        )
        PurchaseFactory.create(
            description="pommes",
            canteen=second_canteen,
            category=Purchase.Category.PRODUITS_LAITIERS,
            characteristics=[Purchase.Characteristic.LABEL_ROUGE],
        )

        not_my_canteen = CanteenFactory.create()
        PurchaseFactory.create(
            description="secret",
            canteen=not_my_canteen,
            category=Purchase.Category.ALIMENTS_INFANTILES,
            characteristics=[Purchase.Characteristic.COMMERCE_EQUITABLE],
        )

        response = self.client.get(f"{reverse('purchase_list_create')}")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        body = response.json()
        categories = body.get("categories", [])
        self.assertEqual(len(categories), 3)
        self.assertIn(Purchase.Category.PRODUITS_DE_LA_MER, categories)
        self.assertNotIn(Purchase.Category.ALIMENTS_INFANTILES, categories)
        self.assertEqual(len(body.get("characteristics", [])), 2)
        canteens = body.get("canteens", [])
        self.assertEqual(len(canteens), 2)
        self.assertNotIn(not_my_canteen.id, canteens)

        response = self.client.get(
            f"{reverse('purchase_list_create')}?characteristics={Purchase.Characteristic.BIO}"
        )
        body = response.json()
        self.assertEqual(len(body["categories"]), 1)

        response = self.client.get(
            f"{reverse('purchase_list_create')}?category={Purchase.Category.PRODUITS_LAITIERS}"
        )
        body = response.json()
        self.assertEqual(len(body["characteristics"]), 1)
Beispiel #5
0
    def test_pagination_management_types(self):
        CanteenFactory.create(publication_status="published", management_type="conceded", name="Shiso")
        CanteenFactory.create(publication_status="published", management_type=None, name="Wasabi")

        url = f"{reverse('published_canteens')}"
        response = self.client.get(url)
        body = response.json()

        self.assertEqual(len(body.get("managementTypes")), 1)
        self.assertIn("conceded", body.get("managementTypes"))
Beispiel #6
0
    def test_search_accented_result(self):
        CanteenFactory.create(publication_status="published", name="Wakamé")
        CanteenFactory.create(publication_status="published", name="Shiitaké")

        search_term = "wakame"
        response = self.client.get(f"{reverse('published_canteens')}?search={search_term}")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])

        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].get("name"), "Wakamé")
 def test_cannot_update_canteen_for_reservation_expe(self):
     """
     Check that cannot update canteen on a reservation expe
     """
     canteen = CanteenFactory.create()
     canteen.managers.add(authenticate.user)
     reservation_expe = ReservationExpeFactory.create(canteen=canteen)
     payload = {"canteen": CanteenFactory.create().id}
     response = self.client.patch(reverse("canteen_reservation_expe", kwargs={"canteen_pk": canteen.id}), payload)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     reservation_expe.refresh_from_db()
     self.assertEqual(reservation_expe.canteen, canteen)
Beispiel #8
0
    def test_sectors_filter(self):
        school = SectorFactory.create(name="School")
        enterprise = SectorFactory.create(name="Enterprise")
        social = SectorFactory.create(name="Social")
        CanteenFactory.create(publication_status="published", sectors=[school], name="Shiso")
        CanteenFactory.create(publication_status="published", sectors=[enterprise], name="Wasabi")
        CanteenFactory.create(publication_status="published", sectors=[social], name="Mochi")
        CanteenFactory.create(publication_status="published", sectors=[school, social], name="Umami")

        url = f"{reverse('published_canteens')}?sectors={school.id}"
        response = self.client.get(url)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 2)
        result_names = list(map(lambda x: x.get("name"), results))
        self.assertIn("Shiso", result_names)
        self.assertIn("Umami", result_names)

        url = f"{reverse('published_canteens')}?sectors={enterprise.id}"
        response = self.client.get(url)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].get("name"), "Wasabi")

        url = f"{reverse('published_canteens')}?sectors={enterprise.id}&sectors={social.id}"
        response = self.client.get(url)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 3)
        result_names = list(map(lambda x: x.get("name"), results))
        self.assertIn("Wasabi", result_names)
        self.assertIn("Mochi", result_names)
        self.assertIn("Umami", result_names)
Beispiel #9
0
    def test_pagination_sectors(self):
        """
        The pagination endpoint should return all sectors that are used by canteens, even when the data is filtered by another sector
        """
        school = SectorFactory.create(name="School")
        enterprise = SectorFactory.create(name="Enterprise")
        administration = SectorFactory.create(name="Administration")
        # unused sectors shouldn't show up as an option
        SectorFactory.create(name="Unused")
        CanteenFactory.create(publication_status="published", sectors=[school, enterprise], name="Shiso")
        CanteenFactory.create(publication_status="published", sectors=[school], name="Wasabi")
        CanteenFactory.create(publication_status="published", sectors=[school], name="Mochi")
        CanteenFactory.create(publication_status="published", sectors=[administration], name="Umami")

        url = f"{reverse('published_canteens')}"
        response = self.client.get(url)
        body = response.json()

        self.assertEqual(len(body.get("sectors")), 3)
        self.assertIn(school.id, body.get("sectors"))
        self.assertIn(enterprise.id, body.get("sectors"))
        self.assertIn(administration.id, body.get("sectors"))

        url = f"{reverse('published_canteens')}?sectors={enterprise.id}"
        response = self.client.get(url)
        body = response.json()

        self.assertEqual(len(body.get("sectors")), 3)
        self.assertIn(school.id, body.get("sectors"))
        self.assertIn(enterprise.id, body.get("sectors"))
        self.assertIn(administration.id, body.get("sectors"))
Beispiel #10
0
    def test_canteen_stats_by_department(self):
        department = "01"
        year = 2020
        CanteenFactory.create(
            department=department,
            publication_status=Canteen.PublicationStatus.PUBLISHED.value)

        response = self.client.get(reverse("canteen_statistics"), {
            "department": department,
            "year": year
        })
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        body = response.json()
        self.assertEqual(body["canteenCount"], 1)
Beispiel #11
0
    def test_get_purchase_options(self):
        """
        A manager should be able to retrieve a list of products and providers that they've already entered on their own purchases
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        PurchaseFactory.create(description="avoine",
                               canteen=canteen,
                               provider="provider1")
        PurchaseFactory.create(description="pommes",
                               canteen=canteen,
                               provider="provider2")
        PurchaseFactory.create(description="pommes",
                               canteen=canteen,
                               provider="provider1")
        PurchaseFactory.create(description=None,
                               canteen=canteen,
                               provider=None)

        PurchaseFactory.create(description="secret product",
                               provider="secret provider")

        response = self.client.get(f"{reverse('purchase_options')}")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        body = response.json()
        self.assertEqual(len(body["products"]), 2)
        self.assertEqual(len(body["providers"]), 2)
        self.assertIn("avoine", body["products"])
        self.assertIn("provider2", body["providers"])
        self.assertNotIn("secret product", body["products"])
        self.assertNotIn("secret provider", body["providers"])
Beispiel #12
0
    def test_modify_canteen(self):
        """
        Users can modify the canteens they manage
        """
        canteen = CanteenFactory.create(city="Paris")
        canteen.managers.add(authenticate.user)
        payload = {
            "city": "Lyon",
            "siret": "21340172201787",
            "managementType": "direct",
            "reservationExpeParticipant": True,
            "satelliteCanteensCount": 130,
            "productionType": "central",
        }
        response = self.client.patch(
            reverse("single_canteen", kwargs={"pk": canteen.id}), payload)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        created_canteen = Canteen.objects.get(pk=canteen.id)
        self.assertEqual(created_canteen.city, "Lyon")
        self.assertEqual(created_canteen.siret, "21340172201787")
        self.assertEqual(created_canteen.management_type, "direct")
        self.assertEqual(created_canteen.satellite_canteens_count, 130)
        self.assertEqual(created_canteen.production_type, "central")
        self.assertEqual(created_canteen.reservation_expe_participant, True)
Beispiel #13
0
    def test_filter_by_date(self):
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        PurchaseFactory.create(description="avoine",
                               canteen=canteen,
                               date="2020-01-01")
        PurchaseFactory.create(description="tomates",
                               canteen=canteen,
                               date="2020-01-02")
        PurchaseFactory.create(description="pommes",
                               canteen=canteen,
                               date="2020-02-01")

        response = self.client.get(
            f"{reverse('purchase_list_create')}?date_after=2020-01-02")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 2)

        response = self.client.get(
            f"{reverse('purchase_list_create')}?date_before=2020-01-01")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 1)

        response = self.client.get(
            f"{reverse('purchase_list_create')}?date_after=2020-01-02&date_before=2020-02-01"
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 2)
Beispiel #14
0
    def test_updates_bad_data(self):
        """
        Check that updates with bad data are rejected
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        vegetarian_expe = VegetarianExpeFactory.create(
            canteen=canteen,
            satisfaction_guests_t0=3,
            waste_vegetarian_not_served_t0=70)

        response = self.client.patch(
            reverse("canteen_vegetarian_expe",
                    kwargs={"canteen_pk": canteen.id}),
            {"satisfactionGuestsT0": 6},
        )
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        vegetarian_expe.refresh_from_db()
        self.assertEqual(vegetarian_expe.satisfaction_guests_t0, 3)

        response = self.client.patch(
            reverse("canteen_vegetarian_expe",
                    kwargs={"canteen_pk": canteen.id}),
            {"wasteVegetarianNotServedT0": -90},
        )
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        vegetarian_expe.refresh_from_db()
        self.assertEqual(vegetarian_expe.waste_vegetarian_not_served_t0, 70)
Beispiel #15
0
    def test_authenticated_add_manager_existing_user(self):
        """
        If the email matches an existing user, add the user to the canteen managers
        without going through invitations table. No email sent for now
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        other_user = UserFactory.create(email="*****@*****.**")
        payload = {"canteenId": canteen.id, "email": other_user.email}

        response = self.client.post(reverse("add_manager"), payload)
        body = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(canteen.managers.all().get(id=other_user.id).id,
                         other_user.id)
        with self.assertRaises(ManagerInvitation.DoesNotExist):
            ManagerInvitation.objects.get(email=other_user.email)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to[0], "*****@*****.**")
        self.assertEqual(mail.outbox[0].from_email, "*****@*****.**")
        self.assertIn("Accèder à la cantine", mail.outbox[0].body)

        self.assertEqual(len(body["managers"]), canteen.managers.all().count())
        self.assertEqual(len(body["managerInvitations"]),
                         canteen.managerinvitation_set.all().count())
Beispiel #16
0
    def test_info_badge_earned(self):
        """
        Test that the right canteens are identified in info badge qs
        """
        no_communicated = CanteenFactory.create()
        DiagnosticFactory.create(canteen=no_communicated,
                                 communicates_on_food_quality=False)
        earned = CanteenFactory.create()
        DiagnosticFactory.create(canteen=earned,
                                 communicates_on_food_quality=True)

        badges = badges_for_queryset(Diagnostic.objects.all())

        info_badge_qs = badges["info"]
        self.assertEqual(info_badge_qs.count(), 1)
        self.assertTrue(info_badge_qs.filter(canteen=earned).exists())
Beispiel #17
0
    def test_create_vegetarian_expe(self):
        """
        Test that we can create a new vegetarian experiment for a canteen
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)

        payload = {
            "vegetarianMenuPercentageT0": 0.32,
            "satisfactionGuestsT0": 5,
        }

        response = self.client.post(
            reverse("canteen_vegetarian_expe",
                    kwargs={"canteen_pk": canteen.id}), payload)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        body = response.json()
        self.assertEqual(body["vegetarianMenuPercentageT0"], 0.32)
        self.assertEqual(body["satisfactionGuestsT0"], 5)

        self.assertEqual(
            float(
                VegetarianExpe.objects.get(
                    canteen=canteen).vegetarian_menu_percentage_t0), 0.32)
        self.assertEqual(
            VegetarianExpe.objects.get(canteen=canteen).satisfaction_guests_t0,
            5)
Beispiel #18
0
    def test_filter_by_characteristic(self):
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        PurchaseFactory.create(description="avoine",
                               canteen=canteen,
                               characteristics=[Purchase.Characteristic.BIO])
        PurchaseFactory.create(
            description="tomates",
            canteen=canteen,
            characteristics=[
                Purchase.Characteristic.BIO,
                Purchase.Characteristic.PECHE_DURABLE
            ],
        )
        PurchaseFactory.create(
            description="pommes",
            canteen=canteen,
            characteristics=[Purchase.Characteristic.PECHE_DURABLE])

        response = self.client.get(
            f"{reverse('purchase_list_create')}?characteristics={Purchase.Characteristic.BIO}"
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 2)

        response = self.client.get(
            f"{reverse('purchase_list_create')}?characteristics={Purchase.Characteristic.BIO}&characteristics={Purchase.Characteristic.PECHE_DURABLE}"
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])
        self.assertEqual(len(results), 3)
Beispiel #19
0
 def test_purchase_missing_year(self):
     canteen = CanteenFactory.create()
     canteen.managers.add(authenticate.user)
     response = self.client.get(
         reverse("canteen_purchases_summary",
                 kwargs={"canteen_pk": canteen.id}), )
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Beispiel #20
0
    def test_canteen_image_serialization(self):
        """
        A canteen with images should serialize those images
        """
        canteen = CanteenFactory.create(publication_status=Canteen.PublicationStatus.PUBLISHED.value)
        image_names = [
            "test-image-1.jpg",
            "test-image-2.jpg",
            "test-image-3.png",
        ]
        for image_name in image_names:
            path = os.path.join(CURRENT_DIR, f"files/{image_name}")
            with open(path, "rb") as image:
                file = File(image)
                file.name = image_name
                canteen_image = CanteenImage(image=file)
                canteen_image.canteen = canteen
                canteen_image.save()

        response = self.client.get(reverse("published_canteens"))
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        body = response.json()
        results = body.get("results", [])

        self.assertEqual(body.get("count"), 1)
        self.assertEqual(len(results[0].get("images")), 3)
Beispiel #21
0
    def test_create_duplicate_diagnostic(self):
        """
        Shouldn't be able to add a diagnostic with the same canteen and year
        as an existing diagnostic
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        self.client.post(
            reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}),
            {
                "year": 2020,
                "value_bio_ht": 10
            },
        )

        payload = {
            "year": 2020,
            "value_bio_ht": 1000,
        }
        try:
            with transaction.atomic():
                response = self.client.post(
                    reverse("diagnostic_creation",
                            kwargs={"canteen_pk": canteen.id}),
                    payload,
                )
        except BadRequest:
            pass
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        diagnostic = Diagnostic.objects.get(canteen__id=canteen.id)
        self.assertEqual(diagnostic.value_bio_ht, 10)
Beispiel #22
0
    def test_update_purchase(self):
        """
        A user can update the data from a purchase object
        """
        purchase = PurchaseFactory.create()
        purchase.canteen.managers.add(authenticate.user)
        new_canteen = CanteenFactory.create()
        new_canteen.managers.add(authenticate.user)

        payload = {
            "id": purchase.id,
            "canteen": new_canteen.id,
            "description": "Saumon",
            "provider": "Test provider",
            "price_ht": 15.23,
        }

        response = self.client.patch(reverse(
            "purchase_retrieve_update_destroy", kwargs={"pk": purchase.id}),
                                     payload,
                                     format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        purchase.refresh_from_db()
        self.assertEqual(purchase.canteen, new_canteen)
        self.assertEqual(purchase.description, "Saumon")
        self.assertEqual(purchase.provider, "Test provider")
        self.assertEqual(float(purchase.price_ht), 15.23)
Beispiel #23
0
    def test_location_overridden(self, mock):
        """
        If the canteen already has city/department data, update it on import
        to be consistent with handling of name, meal count, etc
        """
        canteen = CanteenFactory.create(
            siret="32441387130915",
            city_insee_code="55555",
            city="Ma ville",
            postal_code="66666",
            department=Department.ardeche,
        )
        canteen.managers.add(authenticate.user)

        address_api_text = "siret,citycode,postcode,result_citycode,result_postcode,result_city,result_context\n"
        address_api_text += '21340172201787,,11111,00000,11111,Ma ville,"01,Something,Other"\n'
        address_api_text += '73282932000074,00000,,00000,11111,Ma ville,"01,Something,Other"\n'
        address_api_text += '32441387130915,07293,11111,00000,22222,Saint-Romain-de-Lerps,"01,Something,Other"\n'

        mock.post(
            "https://api-adresse.data.gouv.fr/search/csv/",
            text=address_api_text,
        )
        with open("./api/tests/files/diagnostics_locations.csv") as diag_file:
            response = self.client.post(reverse("import_diagnostics"),
                                        {"file": diag_file})

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        canteen = Canteen.objects.get(siret="32441387130915")
        self.assertEqual(canteen.city_insee_code, "00000")
        self.assertEqual(canteen.postal_code, "22222")
        self.assertEqual(canteen.city, "Saint-Romain-de-Lerps")
        self.assertEqual(canteen.department, Department.ain)
Beispiel #24
0
    def test_canteen_image_modification(self):
        """
        The API should allow image addition and deletion for canteen managers
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        self.assertEqual(canteen.images.count(), 0)

        image_path = os.path.join(CURRENT_DIR, "files/test-image-1.jpg")
        image_base_64 = None
        with open(image_path, "rb") as image:
            image_base_64 = base64.b64encode(image.read()).decode("utf-8")

        # Create image
        payload = {
            "images": [{
                "image": "data:image/jpeg;base64," + image_base_64,
            }]
        }
        self.client.patch(reverse("single_canteen", kwargs={"pk": canteen.id}),
                          payload,
                          format="json")

        canteen.refresh_from_db()
        self.assertEqual(canteen.images.count(), 1)

        # Delete image
        payload = {"images": []}
        self.client.patch(reverse("single_canteen", kwargs={"pk": canteen.id}),
                          payload,
                          format="json")

        canteen.refresh_from_db()
        self.assertEqual(canteen.images.count(), 0)
Beispiel #25
0
 def test_get_single_unpublished_canteen(self):
     """
     A 404 is raised if we try to get a sinlge published canteen
     that has not been published by the manager.
     """
     private_canteen = CanteenFactory.create()
     response = self.client.get(reverse("single_published_canteen", kwargs={"pk": private_canteen.id}))
     self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Beispiel #26
0
    def test_diversification_badge_earned(self):
        """
        Test that the right canteens are identifies in diversification badge qs
        """
        primaire = SectorFactory(name="Scolaire primaire",
                                 category="education")
        secondaire = SectorFactory(name="Scolaire secondaire",
                                   category="education")

        # --- canteens which don't earn diversification badge:
        high_canteen = CanteenFactory.create()
        high_canteen.sectors.remove(primaire)
        high_canteen.sectors.remove(secondaire)
        DiagnosticFactory.create(
            canteen=high_canteen,
            vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.HIGH.value)

        low_canteen = CanteenFactory.create()
        low_canteen.sectors.add(primaire)
        DiagnosticFactory.create(
            canteen=low_canteen,
            vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.LOW.value)

        # --- canteens which earn diversification badge:
        daily_vege = CanteenFactory.create()
        daily_vege.sectors.remove(primaire)
        daily_vege.sectors.remove(secondaire)
        DiagnosticFactory.create(
            canteen=daily_vege,
            vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY.value)

        scolaire_mid_vege = CanteenFactory.create()
        scolaire_mid_vege.sectors.add(secondaire)
        DiagnosticFactory.create(
            canteen=scolaire_mid_vege,
            vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.MID.value)

        badges = badges_for_queryset(Diagnostic.objects.all())

        diversification_badge_qs = badges["diversification"]
        self.assertEqual(diversification_badge_qs.count(), 2)
        self.assertTrue(
            diversification_badge_qs.filter(canteen=daily_vege).exists())
        self.assertTrue(
            diversification_badge_qs.filter(
                canteen=scolaire_mid_vege).exists())
Beispiel #27
0
    def test_filter_by_canteen(self):
        canteen = CanteenFactory.create()
        other_canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)
        other_canteen.managers.add(authenticate.user)
        PurchaseFactory.create(description="avoine", canteen=canteen)
        PurchaseFactory.create(description="tomates", canteen=other_canteen)
        PurchaseFactory.create(description="pommes", canteen=canteen)

        canteen_id = canteen.id
        response = self.client.get(
            f"{reverse('purchase_list_create')}?canteen__id={canteen_id}")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = response.json().get("results", [])

        self.assertEqual(len(results), 2)
    def test_get_nonexistant_reservation_expe(self):
        """
        Test attempting to get a reservation expe that doesn't exist
        """
        canteen = CanteenFactory.create()
        canteen.managers.add(authenticate.user)

        response = self.client.get(reverse("canteen_reservation_expe", kwargs={"canteen_pk": canteen.id}))
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
Beispiel #29
0
 def test_unauthenticated_create_diagnostic_call(self):
     """
     When calling this API unathenticated we expect a 403
     """
     canteen = CanteenFactory.create()
     response = self.client.post(
         reverse("diagnostic_creation", kwargs={"canteen_pk": canteen.id}),
         {})
     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Beispiel #30
0
 def test_remove_manager_forbidden_canteen(self):
     """
     When calling this API on a canteen that the user doesn't manage,
     we expect a 404
     """
     canteen = CanteenFactory.create()
     payload = {"canteenId": canteen.id, "email": "*****@*****.**"}
     response = self.client.post(reverse("remove_manager"), payload)
     self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)