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())
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())
def test_edit_diagnostic_bad_total(self): """ Do not save edits to a diagnostic which make the sum of the values > total """ diagnostic = DiagnosticFactory.create(year=2019, value_total_ht=10, value_bio_ht=5, value_sustainable_ht=2) diagnostic.canteen.managers.add(authenticate.user) payload = {"value_sustainable_ht": 999} response = self.client.patch( reverse( "diagnostic_edition", kwargs={ "canteen_pk": diagnostic.canteen.id, "pk": diagnostic.id }, ), payload, ) diagnostic.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(diagnostic.value_sustainable_ht, 2)
def test_edit_cancelled_diagnostic(self): """ A diagnostic can be edited if a cancelled teledeclaration object linked to it exists """ diagnostic = DiagnosticFactory.create(year=2019) diagnostic.canteen.managers.add(authenticate.user) Teledeclaration.createFromDiagnostic( diagnostic, authenticate.user, status=Teledeclaration.TeledeclarationStatus.CANCELLED, ) payload = {"year": 2020} response = self.client.patch( reverse( "diagnostic_edition", kwargs={ "canteen_pk": diagnostic.canteen.id, "pk": diagnostic.id }, ), payload, ) diagnostic.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(diagnostic.year, 2020)
def test_generate_pdf(self): """ The user can get a justificatif in PDF for a teledeclaration """ canteen = CanteenFactory.create() canteen.managers.add(authenticate.user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) teledeclaration = Teledeclaration.createFromDiagnostic(diagnostic, authenticate.user) response = self.client.get(reverse("teledeclaration_pdf", kwargs={"pk": teledeclaration.id})) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_generate_pdf_unauthorized(self): """ Only managers of the canteen can get PDF documents """ manager = UserFactory.create() canteen = CanteenFactory.create() canteen.managers.add(manager) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) teledeclaration = Teledeclaration.createFromDiagnostic(diagnostic, manager) response = self.client.get(reverse("teledeclaration_pdf", kwargs={"pk": teledeclaration.id})) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_create_unauthorized(self): """ Only managers of the canteen can create teledeclarations """ manager = UserFactory.create() canteen = CanteenFactory.create() canteen.managers.add(manager) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) payload = {"diagnosticId": diagnostic.id} response = self.client.post(reverse("teledeclaration_create"), payload) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_no_siret(self): """ A few canteens don't have SIRETs - make sure teledeclarations for different canteens with no SIRET aren't flagged as duplicates """ user = authenticate.user canteen = CanteenFactory.create(siret="") canteen.managers.add(user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) Teledeclaration.createFromDiagnostic(diagnostic, user) payload = {"diagnosticId": diagnostic.id} response = self.client.post(reverse("teledeclaration_create"), payload) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) canteen2 = CanteenFactory.create(siret="") canteen2.managers.add(user) diagnostic2 = DiagnosticFactory.create(canteen=canteen2, year=2020) payload = {"diagnosticId": diagnostic2.id} response = self.client.post(reverse("teledeclaration_create"), payload) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_duplicate(self): """ We can only have one submitted teledeclaration per canteen/year """ user = authenticate.user canteen = CanteenFactory.create(siret="12345678912345") canteen.managers.add(user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) Teledeclaration.createFromDiagnostic(diagnostic, user) payload = {"diagnosticId": diagnostic.id} response = self.client.post(reverse("teledeclaration_create"), payload) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_incomplete_diagnostic(self): """ A diagnostic missing approvisionnement information cannot be used to create a teledeclaration """ user = authenticate.user canteen = CanteenFactory.create() canteen.managers.add(user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020, value_bio_ht=None) payload = {"diagnosticId": diagnostic.id} response = self.client.post(reverse("teledeclaration_create"), payload) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_waste_badge_earned(self): """ Test that the right canteens are identifies in waste badge qs """ # --- Canteens which don't earn waste badge: waste_actions_only = CanteenFactory.create(daily_meal_count=2999) DiagnosticFactory.create(canteen=waste_actions_only, has_waste_diagnostic=False, waste_actions=["action1"]) waste_diagnostic_only = CanteenFactory.create(daily_meal_count=2999) DiagnosticFactory.create(canteen=waste_diagnostic_only, has_waste_diagnostic=True, waste_actions=[]) large_canteen_no_badge = CanteenFactory.create(daily_meal_count=3000) DiagnosticFactory.create( canteen=large_canteen_no_badge, has_waste_diagnostic=True, waste_actions=["action1"], has_donation_agreement=False, ) # --- Canteens which earn waste badge: small_canteen = CanteenFactory.create(daily_meal_count=2999) DiagnosticFactory.create(canteen=small_canteen, has_waste_diagnostic=True, waste_actions=["action1"]) large_canteen = CanteenFactory.create(daily_meal_count=3000) DiagnosticFactory.create(canteen=large_canteen, has_waste_diagnostic=True, waste_actions=["action1"], has_donation_agreement=True) badges = badges_for_queryset(Diagnostic.objects.all()) waste_badge_qs = badges["waste"] self.assertEqual(waste_badge_qs.count(), 2) self.assertTrue(waste_badge_qs.filter(canteen=small_canteen).exists()) self.assertTrue(waste_badge_qs.filter(canteen=large_canteen).exists())
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())
def test_cancel(self): """ A submitted teledeclaration can be cancelled """ user = authenticate.user canteen = CanteenFactory.create() canteen.managers.add(user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) teledeclaration = Teledeclaration.createFromDiagnostic(diagnostic, user) payload = {"teledeclarationId": teledeclaration.id} response = self.client.post(reverse("teledeclaration_cancel"), payload) self.assertEqual(response.status_code, status.HTTP_200_OK) db_teledeclaration = Teledeclaration.objects.get(pk=teledeclaration.id) self.assertEqual(db_teledeclaration.status, Teledeclaration.TeledeclarationStatus.CANCELLED) body = response.json() self.assertEqual(body["teledeclaration"]["status"], "CANCELLED")
def test_edit_diagnostic_unauthorized(self): """ The user can only edit diagnostics of canteens they manage """ diagnostic = DiagnosticFactory.create() payload = {"year": 2020} response = self.client.patch( reverse( "diagnostic_edition", kwargs={ "canteen_pk": diagnostic.canteen.id, "pk": diagnostic.id }, ), payload, ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_edit_diagnostic(self): """ The user can edit a diagnostic of a canteen they manage """ diagnostic = DiagnosticFactory.create(year=2019) diagnostic.canteen.managers.add(authenticate.user) payload = {"year": 2020} response = self.client.patch( reverse( "diagnostic_edition", kwargs={ "canteen_pk": diagnostic.canteen.id, "pk": diagnostic.id }, ), payload, ) diagnostic.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(diagnostic.year, 2020)
def test_user_canteen_teledeclaration(self): """ The teledeclaration information should only be visible to managers of the canteen """ user = authenticate.user canteen = CanteenFactory.create() canteen.managers.add(user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) Teledeclaration.createFromDiagnostic( diagnostic, user, Teledeclaration.TeledeclarationStatus.CANCELLED) new_teledeclaration = Teledeclaration.createFromDiagnostic( diagnostic, user) response = self.client.get(reverse("user_canteens")) body = response.json().get("results") json_canteen = next(filter(lambda x: x["id"] == canteen.id, body)) json_diagnostic = next( filter(lambda x: x["id"] == diagnostic.id, json_canteen["diagnostics"])) self.assertEqual(json_diagnostic["teledeclaration"]["id"], new_teledeclaration.id)
def test_filter_appro_values(self): """ Should be able to filter by bio %, sustainable %, combined % based on last year's diagnostic """ good_canteen = CanteenFactory.create(publication_status="published", name="Shiso") medium_canteen = CanteenFactory.create(publication_status="published", name="Wasabi") sustainable_canteen = CanteenFactory.create(publication_status="published", name="Umami") bad_canteen = CanteenFactory.create(publication_status="published", name="Mochi") secretly_good_canteen = CanteenFactory.create(publication_status="draft", name="Secret") publication_year = date.today().year - 1 DiagnosticFactory.create( canteen=good_canteen, year=publication_year, value_total_ht=100, value_bio_ht=30, value_sustainable_ht=30, ) DiagnosticFactory.create( canteen=secretly_good_canteen, year=publication_year, value_total_ht=100, value_bio_ht=30, value_sustainable_ht=30, ) DiagnosticFactory.create( canteen=medium_canteen, year=publication_year, value_total_ht=1000, value_bio_ht=150, value_sustainable_ht=350, ) DiagnosticFactory.create( canteen=sustainable_canteen, year=publication_year, value_total_ht=100, value_bio_ht=0, value_sustainable_ht=60, ) DiagnosticFactory.create( canteen=bad_canteen, year=2019, value_total_ht=100, value_bio_ht=30, value_sustainable_ht=30, ) DiagnosticFactory.create( canteen=bad_canteen, year=publication_year, value_total_ht=10, value_bio_ht=0, value_sustainable_ht=0, ) url = f"{reverse('published_canteens')}?min_portion_bio={0.2}" response = self.client.get(url) results = response.json().get("results", []) self.assertEqual(len(results), 1) result_names = list(map(lambda x: x.get("name"), results)) self.assertIn("Shiso", result_names) url = f"{reverse('published_canteens')}?min_portion_combined={0.5}" 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("Shiso", result_names) self.assertIn("Wasabi", result_names) self.assertIn("Umami", result_names) url = f"{reverse('published_canteens')}?min_portion_bio={0.1}&min_portion_combined={0.5}" 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("Wasabi", result_names)
def test_canteen_statistics(self): """ This public endpoint returns some summary statistics for a region and a location """ # TODO: more nuance when choosing canteens to get stats for? # How do we know that the canteen diagnostic is done? # Could check for total value ht # create 5 canteens (3 in region of interest), 1 unpublished region = "01" year = 2020 school = SectorFactory.create(name="School") enterprise = SectorFactory.create(name="Enterprise") social = SectorFactory.create(name="Social") published = CanteenFactory.create( region=region, publication_status=Canteen.PublicationStatus.PUBLISHED.value, sectors=[school, enterprise], daily_meal_count=50, ) unpublished = CanteenFactory.create( region=region, publication_status=Canteen.PublicationStatus.DRAFT.value, sectors=[school], daily_meal_count=50, ) other_region = CanteenFactory.create(region="03", sectors=[social], daily_meal_count=50) # relevant diagnostics DiagnosticFactory.create( canteen=published, year=year, value_total_ht=100, value_bio_ht=20, value_sustainable_ht=30, has_waste_diagnostic=False, waste_actions=[], vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY, plastic_tableware_substituted=False, communicates_on_food_quality=False, ) DiagnosticFactory.create( canteen=unpublished, year=year, value_total_ht=1000, value_bio_ht=400, value_sustainable_ht=500, has_waste_diagnostic=True, waste_actions=["action1", "action2"], vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.LOW, cooking_plastic_substituted=True, serving_plastic_substituted=True, plastic_bottles_substituted=True, plastic_tableware_substituted=True, communicates_on_food_quality=True, ) # irrelevant diagnostics DiagnosticFactory.create( canteen=published, year=2019, value_total_ht=100, value_bio_ht=100, value_sustainable_ht=0, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY, ) DiagnosticFactory.create( canteen=other_region, year=year, value_total_ht=100, value_bio_ht=100, value_sustainable_ht=0, cooking_plastic_substituted=True, serving_plastic_substituted=True, plastic_bottles_substituted=True, plastic_tableware_substituted=True, communicates_on_food_quality=True, ) response = self.client.get(reverse("canteen_statistics"), { "region": region, "year": year }) self.assertEqual(response.status_code, status.HTTP_200_OK) body = response.json() self.assertEqual(body["canteenCount"], 2) self.assertEqual(body["publishedCanteenCount"], 1) self.assertEqual(body["bioPercent"], 30) self.assertEqual(body["sustainablePercent"], 40) self.assertEqual(body["approPercent"], 100) self.assertEqual(body["wastePercent"], 50) self.assertEqual(body["diversificationPercent"], 50) self.assertEqual(body["plasticPercent"], 50) self.assertEqual(body["infoPercent"], 50) expected_sectors = {} expected_sectors[str(school.id)] = 2 expected_sectors[str(enterprise.id)] = 1 expected_sectors[str(social.id)] = 0 self.assertEqual(body["sectors"], expected_sectors) # can also call without location info response = self.client.get(reverse("canteen_statistics"), {"year": 2020}) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_appro_badge_earned(self): """ Test that the right canteens are identified in appro badge queryset """ # --- Canteens which don't earn appro badge: zero_total = CanteenFactory.create() DiagnosticFactory.create(canteen=zero_total, value_total_ht=0) null_total = CanteenFactory.create() DiagnosticFactory.create(canteen=null_total, value_total_ht=None) bio_lacking = CanteenFactory.create() DiagnosticFactory.create(canteen=bio_lacking, value_total_ht=100, value_bio_ht=19, value_sustainable_ht=31) # not convinced the following shouldn't get a badge but not sure how to make the Sum function work null_sustainable = CanteenFactory.create( region=Region.ile_de_france.value) DiagnosticFactory.create(canteen=null_sustainable, value_total_ht=100, value_bio_ht=50, value_sustainable_ht=None) # --- Canteens which earn appro badge: earned = CanteenFactory.create() DiagnosticFactory.create(canteen=earned, value_total_ht=100, value_bio_ht=20, value_sustainable_ht=30) # rules per outre mer territories guadeloupe = CanteenFactory.create(region=Region.guadeloupe.value) DiagnosticFactory.create(canteen=guadeloupe, value_total_ht=100, value_bio_ht=5, value_sustainable_ht=15) mayotte = CanteenFactory.create(region=Region.mayotte.value) DiagnosticFactory.create(canteen=mayotte, value_total_ht=100, value_bio_ht=2, value_sustainable_ht=3) # TODO: rules per outre mer territories: Saint-Pierre-et-Miquelon # st_pierre_et_miquelon = CanteenFactory.create(region=Region.st_pierre_et_miquelon.value) # DiagnosticFactory.create(canteen=st_pierre_et_miquelon, value_total_ht=100, value_bio_ht=10, value_sustainable_ht=20) badges = badges_for_queryset(Diagnostic.objects.all()) appro_badge_qs = badges["appro"] self.assertTrue(appro_badge_qs.filter(canteen=earned).exists()) self.assertTrue(appro_badge_qs.filter(canteen=guadeloupe).exists()) self.assertTrue(appro_badge_qs.filter(canteen=mayotte).exists()) self.assertEqual(appro_badge_qs.count(), 3)
def test_create(self): """ A teledeclaration can be created from a valid Diagnostic """ user = authenticate.user canteen = CanteenFactory.create() canteen.managers.add(user) diagnostic = DiagnosticFactory.create(canteen=canteen, year=2020) payload = {"diagnosticId": diagnostic.id} response = self.client.post(reverse("teledeclaration_create"), payload) self.assertEqual(response.status_code, status.HTTP_201_CREATED) body = response.json() teledeclaration = Teledeclaration.objects.first() self.assertEqual(body["teledeclaration"]["id"], teledeclaration.id) self.assertEqual(body["teledeclaration"]["status"], "SUBMITTED") self.assertEqual(teledeclaration.diagnostic, diagnostic) self.assertEqual(teledeclaration.canteen, canteen) self.assertEqual(teledeclaration.year, 2020) self.assertEqual(teledeclaration.applicant, user) self.assertEqual(teledeclaration.canteen_siret, canteen.siret) self.assertEqual(teledeclaration.status, Teledeclaration.TeledeclarationStatus.SUBMITTED) declared_data = teledeclaration.declared_data self.assertEqual(declared_data["year"], 2020) json_canteen = declared_data["canteen"] self.assertEqual(json_canteen["name"], canteen.name) self.assertEqual(json_canteen["siret"], canteen.siret) self.assertEqual(json_canteen["city_insee_code"], canteen.city_insee_code) json_teledeclaration = declared_data["teledeclaration"] self.assertEqual(json_teledeclaration["value_bio_ht"], diagnostic.value_bio_ht) self.assertEqual( json_teledeclaration["value_sustainable_ht"], diagnostic.value_sustainable_ht, ) self.assertEqual(json_teledeclaration["value_total_ht"], diagnostic.value_total_ht) self.assertEqual( json_teledeclaration["has_waste_diagnostic"], diagnostic.has_waste_diagnostic, ) self.assertEqual(json_teledeclaration["has_waste_plan"], diagnostic.has_waste_plan) self.assertEqual( json_teledeclaration["has_donation_agreement"], diagnostic.has_donation_agreement, ) self.assertEqual(json_teledeclaration["has_waste_measures"], diagnostic.has_waste_measures) self.assertEqual( json_teledeclaration["has_diversification_plan"], diagnostic.has_diversification_plan, ) self.assertEqual( json_teledeclaration["cooking_plastic_substituted"], diagnostic.cooking_plastic_substituted, ) self.assertEqual( json_teledeclaration["serving_plastic_substituted"], diagnostic.serving_plastic_substituted, ) self.assertEqual( json_teledeclaration["plastic_bottles_substituted"], diagnostic.plastic_bottles_substituted, ) self.assertEqual( json_teledeclaration["plastic_tableware_substituted"], diagnostic.plastic_tableware_substituted, ) self.assertEqual( json_teledeclaration["communicates_on_food_plan"], diagnostic.communicates_on_food_plan, ) self.assertEqual( json_teledeclaration["communicates_on_food_quality"], diagnostic.communicates_on_food_quality, )