def test_delete_api_key_not_allowed(client): user_offerer = UserOffererFactory() api_key = ApiKeyFactory() client.with_session_auth(user_offerer.user.email) response = client.delete(f"/offerers/api_keys/{api_key.prefix}") assert response.status_code == 403
def test_maybe_send_offerer_validation_email_does_not_send_email_if_all_validated(app): # Given user = users_factories.UserFactory() offerer = OffererFactory() user_offerer = UserOffererFactory(offerer=offerer, user=user) # When maybe_send_offerer_validation_email(offerer, user_offerer) # Then assert not mails_testing.outbox
def test_maximal_api_key_reached(client): user_offerer = UserOffererFactory() for i in range(5): ApiKeyFactory(prefix=f"prefix_{i}", offerer=user_offerer.offerer) client.with_session_auth(user_offerer.user.email) response = client.post(f"/offerers/{humanize(user_offerer.offerer.id)}/api_keys") assert response.status_code == 400 assert response.json["api_key_count_max"] == "Le nombre de clés maximal a été atteint" assert ApiKey.query.count() == 5
def when_feature_send_mail_to_users_is_enabled_sends_email_to_all_users_linked_to_offerer( self, mock_retrieve_data_for_new_offerer_validation_email): # Given offerer = UserOffererFactory().offerer # When send_validation_confirmation_email_to_pro(offerer) # Then mock_retrieve_data_for_new_offerer_validation_email.assert_called_once_with( offerer) assert mails_testing.outbox[0].sent_data["Mj-TemplateID"] == 778723
def test_suspend_pros_in_given_emails_providers_list(): # Given fraudulent_emails_providers = ["example.com"] admin_user = AdminFactory(email="*****@*****.**") fraudulent_user = ProFactory(email="*****@*****.**", ) offerer = OffererFactory() UserOffererFactory(user=fraudulent_user, offerer=offerer) # When suspend_fraudulent_pro_by_email_providers(fraudulent_emails_providers, admin_user, dry_run=False) # Then assert not fraudulent_user.isActive
def test_send_withdrawal_terms_to_newly_validated_offerer( self, mock_retrieve_data_for_new_offerer_validated_withdrawal_terms_email ): # Given offerer = UserOffererFactory().offerer # When send_withdrawal_terms_to_newly_validated_offerer(offerer) # Then mock_retrieve_data_for_new_offerer_validated_withdrawal_terms_email.assert_called_once( ) assert len(mails_testing.outbox) == 1 # test number of emails sent assert mails_testing.outbox[0].sent_data["Mj-TemplateID"] == 11330916
def test_send_attachment_validation_email_to_pro_offerer( self, mocked_retrieve_data_for_offerer_attachment_validation_email, app): # given user_offerer = UserOffererFactory() mocked_retrieve_data_for_offerer_attachment_validation_email.return_value = { "Html-part": "" } # when send_attachment_validation_email_to_pro_offerer(user_offerer) # then mocked_retrieve_data_for_offerer_attachment_validation_email.assert_called_once_with( user_offerer.offerer) assert mails_testing.outbox[0].sent_data["Html-part"] == ""
def test_delete_offerer_and_venue(): # Given fraudulent_emails_providers = ["example.com"] admin_user = AdminFactory(email="*****@*****.**") fraudulent_user = ProFactory(email="*****@*****.**", ) offerer = OffererFactory() UserOffererFactory(user=fraudulent_user, offerer=offerer) VenueFactory(managingOfferer=offerer) # When suspend_fraudulent_pro_by_email_providers(fraudulent_emails_providers, admin_user, dry_run=False) # Then assert Offerer.query.count() == 0 assert Venue.query.count() == 0
def test_maybe_send_offerer_validation_email_sends_email_to_pass_culture_when_objects_to_validate( mock_api_entreprise, app ): # Given response_return_value = MagicMock(status_code=200, text="") response_return_value.json = MagicMock( return_value={ "unite_legale": {"etablissement_siege": {"siret": ""}, "etablissements": [], "activite_principale": ""} } ) mock_api_entreprise.return_value = response_return_value user = users_factories.UserFactory() offerer = OffererFactory(validationToken="12356") user_offerer = UserOffererFactory(offerer=offerer, user=user) # When maybe_send_offerer_validation_email(offerer, user_offerer) # Then assert len(mails_testing.outbox) == 1 assert mails_testing.outbox[0].sent_data["To"] == "*****@*****.**"
def test_api_key_journey(client): booking = booking_factories.IndividualBookingFactory() user_offerer = UserOffererFactory(offerer=booking.offerer) client.with_session_auth(user_offerer.user.email) response = client.post(f"/offerers/{humanize(user_offerer.offerer.id)}/api_keys") assert response.status_code == 200 saved_key = find_api_key(response.json["apiKey"]) assert saved_key.offererId == user_offerer.offerer.id # test generated api key grants authentication on bookings API response = client.get( f"/v2/bookings/token/{booking.token.lower()}", headers={"Authorization": f"""Bearer {response.json["apiKey"]}"""}, ) assert response.status_code == 200 # test user can delete the generated api key response = client.delete(f"/offerers/api_keys/{saved_key.prefix}") assert response.status_code == 204 assert ApiKey.query.count() == 0
def save_bookings_recap_sandbox(): yesterday = datetime.utcnow() - timedelta(days=1) today = datetime.utcnow() beneficiary1 = BeneficiaryGrant18Factory(publicName="Riri Duck", firstName="Riri", lastName="Duck", email="*****@*****.**") beneficiary2 = BeneficiaryGrant18Factory( publicName="Fifi Brindacier", firstName="Fifi", lastName="Brindacier", email="*****@*****.**", ) beneficiary3 = BeneficiaryGrant18Factory( publicName="LouLou Duck", firstName="Loulou", lastName="Duck", email="*****@*****.**", ) pro = ProFactory( publicName="Balthazar Picsou", firstName="Balthazar", lastName="Picsou", email="*****@*****.**", ) offerer = OffererFactory(siren="645389012") UserOffererFactory(user=pro, offerer=offerer) venue1 = VenueFactory(managingOfferer=offerer, name="Cinéma Le Monde Perdu", siret="64538901265877") venue2 = VenueFactory(managingOfferer=offerer, name="Librairie Atlantis", siret="64538901201379") venue3 = VenueFactory(managingOfferer=offerer, name="Théatre Mordor", siret="64538954601379") venue4_virtual = VenueFactory(managingOfferer=offerer, name="Un lieu virtuel", siret=None, isVirtual=True) product1_venue1 = EventProductFactory( name="Jurassic Park", subcategoryId=subcategories.SEANCE_CINE.id) offer1_venue1 = EventOfferFactory(product=product1_venue1, venue=venue1, isDuo=True) stock_1_offer1_venue1 = EventStockFactory(offer=offer1_venue1, beginningDatetime=yesterday, quantity=None, price=12.99) product2_venue1 = EventProductFactory( name="Matrix", subcategoryId=subcategories.SEANCE_CINE.id) offer2_venue1 = EventOfferFactory(product=product2_venue1, venue=venue1, isDuo=False) stock_2_offer2_venue1 = EventStockFactory(offer=offer2_venue1, beginningDatetime=today, quantity=None, price=0) product1_venue2 = ThingProductFactory( name="Fondation", subcategoryId=subcategories.LIVRE_PAPIER.id, extraData={"isbn": "9788804119135"}) offer1_venue2 = ThingOfferFactory(product=product1_venue2, venue=venue2) stock_1_offer1_venue2 = ThingStockFactory(offer=offer1_venue2, quantity=42, price=9.99) product2_venue2 = ThingProductFactory( name="Martine à la playa", subcategoryId=subcategories.LIVRE_PAPIER.id, extraData={"isbn": "9787605639121"}, ) offer2_venue2 = ThingOfferFactory(product=product2_venue2, venue=venue2) stock_1_offer2_venue2 = ThingStockFactory(offer=offer2_venue2, quantity=12, price=49.99) product1_venue3 = EventProductFactory( name="Danse des haricots", subcategoryId=subcategories.SPECTACLE_REPRESENTATION.id) offer1_venue3 = EventOfferFactory(product=product1_venue3, venue=venue3) stock_1_offer1_venue3 = EventStockFactory(offer=offer1_venue3, quantity=44, price=18.50) product1_venue4 = ThingProductFactory( name="Le livre des haricots", subcategoryId=subcategories.LIVRE_PAPIER.id) offer1_venue4 = ThingOfferFactory(product=product1_venue4, venue=venue4_virtual) stock_1_offer1_venue4 = ThingStockFactory(offer=offer1_venue4, quantity=70, price=10.99) IndividualBookingFactory( individualBooking__user=beneficiary1, stock=stock_1_offer1_venue1, dateCreated=datetime(2020, 3, 18, 14, 56, 12, 0), isUsed=True, dateUsed=datetime(2020, 3, 22, 17, 00, 10, 0), quantity=2, ) IndividualBookingFactory( individualBooking__user=beneficiary1, stock=stock_2_offer2_venue1, dateCreated=datetime(2020, 4, 22, 9, 17, 12, 0), ) IndividualBookingFactory( individualBooking__user=beneficiary2, stock=stock_1_offer1_venue1, dateCreated=datetime(2020, 3, 18, 12, 18, 12, 0), isUsed=True, dateUsed=datetime(2020, 5, 2), quantity=2, ) booking2_beneficiary2 = IndividualBookingFactory( individualBooking__user=beneficiary2, stock=stock_1_offer1_venue2, dateCreated=datetime(2020, 4, 12, 14, 31, 12, 0), isCancelled=False, ) booking1_beneficiary3 = IndividualBookingFactory( individualBooking__user=beneficiary3, stock=stock_2_offer2_venue1, dateCreated=datetime(2020, 1, 4, 19, 31, 12, 0), isCancelled=False, isUsed=True, dateUsed=datetime(2020, 1, 4, 23, 00, 10, 0), quantity=2, ) booking2_beneficiary3 = IndividualBookingFactory( individualBooking__user=beneficiary3, stock=stock_1_offer1_venue2, dateCreated=datetime(2020, 3, 21, 22, 9, 12, 0), isCancelled=False, ) booking3_beneficiary1 = UsedIndividualBookingFactory( individualBooking__user=beneficiary1, stock=stock_1_offer1_venue3, dateCreated=datetime(2020, 4, 12, 14, 31, 12, 0), ) payment_booking3_beneficiary1 = PaymentFactory( booking=booking3_beneficiary1) PaymentStatusFactory(payment=payment_booking3_beneficiary1, status=TransactionStatus.PENDING) booking3_beneficiary2 = UsedIndividualBookingFactory( individualBooking__user=beneficiary2, stock=stock_1_offer1_venue3, dateCreated=datetime(2020, 4, 12, 19, 31, 12, 0), dateUsed=datetime(2020, 4, 22, 17, 00, 10, 0), ) PaymentFactory(booking=booking3_beneficiary2) PaymentStatusFactory(payment=payment_booking3_beneficiary1, status=TransactionStatus.SENT) booking3_beneficiary3 = UsedIndividualBookingFactory( individualBooking__user=beneficiary3, stock=stock_1_offer1_venue3, dateCreated=datetime(2020, 4, 12, 22, 9, 12, 0), ) payment_booking3_beneficiary3 = PaymentFactory( booking=booking3_beneficiary3) PaymentStatusFactory(payment=payment_booking3_beneficiary3, status=TransactionStatus.ERROR) UsedIndividualBookingFactory( individualBooking__user=beneficiary3, stock=stock_1_offer1_venue2, dateCreated=datetime(2020, 3, 21, 22, 9, 12, 0), ) booking5_beneficiary3 = IndividualBookingFactory( individualBooking__user=beneficiary3, stock=stock_1_offer1_venue4, dateCreated=datetime(2020, 3, 21, 22, 9, 12, 0), isCancelled=False, ) booking6_beneficiary3 = UsedIndividualBookingFactory( individualBooking__user=beneficiary3, stock=stock_1_offer2_venue2, dateCreated=datetime(2020, 3, 21, 22, 9, 12, 0), dateUsed=datetime(2020, 4, 22, 21, 9, 12, 0), ) payment_booking6_beneficiary3 = PaymentFactory( booking=booking6_beneficiary3) PaymentStatusFactory(payment=payment_booking6_beneficiary3, status=TransactionStatus.SENT) booking7_beneficiary2 = UsedIndividualBookingFactory( individualBooking__user=beneficiary2, stock=stock_1_offer2_venue2, dateCreated=datetime(2020, 4, 21, 22, 6, 12, 0), dateUsed=datetime(2020, 4, 22, 22, 9, 12, 0), ) payment_booking7_beneficiary2 = PaymentFactory( booking=booking7_beneficiary2) PaymentStatusFactory(payment=payment_booking7_beneficiary2, status=TransactionStatus.RETRY) UsedIndividualBookingFactory( individualBooking__user=beneficiary1, stock=stock_1_offer2_venue2, dateCreated=datetime(2020, 2, 21, 22, 6, 12, 0), dateUsed=datetime(2020, 4, 22, 23, 9, 12, 0), ) payment_booking8_beneficiary1 = PaymentFactory( booking=booking7_beneficiary2) PaymentStatusFactory(payment=payment_booking8_beneficiary1, status=TransactionStatus.PENDING) bookings_to_cancel = [ booking2_beneficiary2, booking1_beneficiary3, booking2_beneficiary3, booking3_beneficiary2, booking5_beneficiary3, ] for booking in bookings_to_cancel: try: booking.cancel_booking() except (BookingIsAlreadyUsed, BookingIsAlreadyCancelled) as e: logger.info(str(e), extra={"booking": booking.id}) repository.save(*bookings_to_cancel)
def create_industrial_educational_bookings() -> None: educational_current_year = educational_factories.EducationalYearFactory() educational_next_year = educational_factories.EducationalYearFactory() educational_institutions = [ educational_factories.EducationalInstitutionFactory( institutionId="0780032L"), educational_factories.EducationalInstitutionFactory( institutionId="0781839A"), educational_factories.EducationalInstitutionFactory( institutionId="0290047U"), educational_factories.EducationalInstitutionFactory( institutionId="0290198H"), educational_factories.EducationalInstitutionFactory( institutionId="0910620E"), educational_factories.EducationalInstitutionFactory( institutionId="0560071Y"), ] now = datetime.datetime.now(datetime.timezone.utc) stocks = [] venue = VenueFactory(name="Opéra Royal de Versailles", isPermanent=True) UserOffererFactory(validationToken=None, offerer=venue.managingOfferer) educational_redactor = educational_factories.EducationalRedactorFactory( email="*****@*****.**") user_offerer_reimbursements = UserOffererFactory( validationToken=None, user__email="*****@*****.**") venue_reimbursements = VenueFactory( name="Théâtre des potirons", isPermanent=True, managingOfferer=user_offerer_reimbursements.offerer) for stock_data in FAKE_STOCK_DATA: stocks.append( EducationalEventStockFactory( quantity=100, price=stock_data["price"], beginningDatetime=now + datetime.timedelta(days=stock_data["timedelta"]), offer__durationMinutes=60, offer__withdrawalDetails= "Récupération du ticket à l'adresse du lieu", offer__description= "Une description multi-lignes.\nOù il est notamment question du nombre d'élèves.\nNbr d'élèves max: 50", offer__name=stock_data["name"], offer__venue=venue, )) for stock in stocks: mediation = MediationFactory(offer=stock.offer, credit="Crédit photo") store_public_object_from_sandbox_assets("thumbs", mediation, mediation.offer.subcategoryId) next_year_stocks = [ EducationalEventStockFactory( quantity=100, price=1200, beginningDatetime=educational_next_year.beginningDate + datetime.timedelta(days=10), offer__durationMinutes=60, offer__withdrawalDetails= "Récupération du ticket à l'adresse du lieu", offer__description= "Une description multi-lignes.\nOù il est notamment question du nombre d'élèves.\nNbr d'élèves max: 50", offer__name= "Stage d'initiation à la photographie : prise en main de l'appareil-photo", offer__venue=venue, ), EducationalEventStockFactory( quantity=60, price=1400, beginningDatetime=educational_next_year.beginningDate + datetime.timedelta(days=15), offer__durationMinutes=60, offer__withdrawalDetails= "Récupération du ticket à l'adresse du lieu", offer__description= "Une description multi-lignes.\nOù il est notamment question du nombre d'élèves.\nNbr d'élèves max: 50", offer__name= "Explorer la nature au Parc Zoologique et Botanique de Mulhouse", offer__venue=venue, ), ] deposits = [] for educational_institution in educational_institutions: deposits.append( educational_factories.EducationalDepositFactory( educationalInstitution=educational_institution, educationalYear=educational_current_year, amount=20000, )) deposits.append( educational_factories.EducationalDepositFactory( educationalInstitution=educational_institution, educationalYear=educational_next_year, amount=25000, isFinal=False, )) for stock in stocks: for educational_institution in educational_institutions: EducationalBookingFactory( educationalBooking__educationalRedactor=educational_redactor, educationalBooking__educationalInstitution= educational_institution, educationalBooking__educationalYear=educational_current_year, educationalBooking__confirmationLimitDate=now + datetime.timedelta(days=10), cancellation_limit_date=now + datetime.timedelta(days=4), status=BookingStatus.PENDING, stock=stock, ) UsedEducationalBookingFactory( educationalBooking__educationalRedactor=educational_redactor, educationalBooking__educationalInstitution= educational_institution, educationalBooking__educationalYear=educational_current_year, educationalBooking__confirmationLimitDate=now - datetime.timedelta(days=20), cancellation_limit_date=now - datetime.timedelta(days=15), dateUsed=now - datetime.timedelta(8), status=BookingStatus.USED, stock=EducationalEventStockFactory( quantity=100, price=1200, beginningDatetime=now - datetime.timedelta(days=10), bookingLimitDatetime=now - datetime.timedelta(days=10), offer__venue=venue_reimbursements, ), ) for next_year_stock in next_year_stocks: for educational_institution in educational_institutions: EducationalBookingFactory( educationalBooking__educationalRedactor=educational_redactor, educationalBooking__educationalInstitution= educational_institution, educationalBooking__educationalYear=educational_next_year, educationalBooking__confirmationLimitDate=now + datetime.timedelta(days=30), status=BookingStatus.PENDING, stock=next_year_stock, )