def test_reservation_comment_create( user_api_client, user, general_admin, reservation, new_reservation_comment_data): COMMENT_CREATED_BODY = """Target type: {{ target_type }} Created by: {{ created_by.display_name }} Created at: {{ created_at|format_datetime }} Resource: {{ reservation.resource }} Reservation: {{ reservation|reservation_time }} {{ text }} """ NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.RESERVATION_COMMENT_CREATED, short_message="Reservation comment added for {{ reservation.resource }}", subject="Reservation comment added for {{ reservation.resource }}", body=COMMENT_CREATED_BODY ) user.preferred_language = DEFAULT_LANG user.save() unit = reservation.resource.unit unit.manager_email = '*****@*****.**' unit.save() response = user_api_client.post(LIST_URL, data=new_reservation_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 1 new_comment = Comment.objects.latest('id') assert new_comment.created_at assert new_comment.created_by == user assert new_comment.content_type == ContentType.objects.get_for_model(Reservation) assert new_comment.object_id == reservation.id assert new_comment.text == new_reservation_comment_data['text'] created_at = format_datetime_tz(new_comment.created_at, reservation.resource.unit.get_tz()) strings = [ "Created by: %s" % user.get_display_name(), "Created at: %s" % created_at, "Resource: %s" % reservation.resource.name, "Reservation: to 4.4.2115 klo 9.00–10.00", new_reservation_comment_data['text'], ] check_received_mail_exists("Reservation comment added for %s" % reservation.resource.name, unit.manager_email, strings) # Next make sure that a comment by another user reaches the reserver user_api_client.force_authenticate(user=general_admin) response = user_api_client.post(LIST_URL, data=new_reservation_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 2 assert len(mail.outbox) == 2 check_received_mail_exists("Reservation comment added for %s" % reservation.resource.name, user.email, [])
def test_draft_posted(draft_posted_notification_template, user, draft_event): strings = [ "draft posted body, event name: %s!" % draft_event.name, ] html_body = "draft posted <b>HTML</b> body, event name: %s!" % draft_event.name check_received_mail_exists("draft posted subject, event name: %s!" % draft_event.name, user.email, strings, html_body=html_body)
def test_catering_order_comment_create2( user_api_client, user, general_admin, catering_order, new_catering_order_comment_data): COMMENT_CREATED_BODY = """Target type: {{ target_type }} Created by: {{ created_by.display_name }} Created at: {{ created_at|format_datetime }} Resource: {{ catering_order.reservation.resource.name }} Reservation: {{ catering_order.reservation|reservation_time }} Serving time: {{ catering_order.serving_time }} {{ text }} """ NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_COMMENT_CREATED, short_message="Catering comment added for {{ catering_order.reservation.resource.name }}", subject="Catering comment added for {{ catering_order.reservation.resource.name }}", body=COMMENT_CREATED_BODY ) user.preferred_language = DEFAULT_LANG user.save() provider = catering_order.get_provider() provider.notification_email = '*****@*****.**' provider.save() # First make sure that the notification from a user's comment reaches catering. response = user_api_client.post(LIST_URL, data=new_catering_order_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 1 comment = Comment.objects.first() reservation = catering_order.reservation created_at = format_datetime_tz(comment.created_at, reservation.resource.unit.get_tz()) strings = [ "Created by: %s" % user.get_display_name(), "Created at: %s" % created_at, "Resource: %s" % reservation.resource.name, "Reservation: to 4.4.2115 klo 9.00–10.00", "Serving time: 12.00", new_catering_order_comment_data['text'], ] check_received_mail_exists("Catering comment added for %s" % reservation.resource.name, provider.notification_email, strings) # Next make sure that a comment by another user reaches the reserver user_api_client.force_authenticate(user=general_admin) response = user_api_client.post(LIST_URL, data=new_catering_order_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 2 assert len(mail.outbox) == 2 check_received_mail_exists("Catering comment added for %s" % reservation.resource.name, user.email, [])
def test_reservation_created_notification(order_with_products): user = order_with_products.reservation.user user.preferred_language = 'fi' user.save() order_with_products.set_state(Order.CONFIRMED) assert len(mail.outbox) == 1 check_received_mail_exists( 'Reservation created subject.', order_with_products.reservation.billing_email_address, get_expected_strings(order_with_products), )
def test_user_created(user_created_notification_template, super_user): user = get_user_model().objects.create(username='******', first_name='New', last_name='Creature', email='*****@*****.**') strings = [ "new user created - user email: %s" % user.email, ] html_body = "<b>new user created</b> - user email: %s!" % user.email check_received_mail_exists("user created", super_user.email, strings, html_body=html_body)
def test_event_published(event_published_notification_template, user, draft_event): draft_event.created_by = user draft_event.publication_status = PublicationStatus.PUBLIC draft_event.save() strings = [ "event published body, event name: %s!" % draft_event.name, ] html_body = "event published <b>HTML</b> body, event name: %s!" % draft_event.name check_received_mail_exists("event published subject, event name: %s!" % draft_event.name, user.email, strings, html_body=html_body)
def test_unpublished_event_deleted(event_deleted_notification_template, user, event): event.created_by = user event.save() event.soft_delete() strings = [ "event deleted body, event name: %s!" % event.name, ] html_body = "event deleted <b>HTML</b> body, event name: %s!" % event.name check_received_mail_exists("event deleted subject, event name: %s!" % event.name, user.email, strings, html_body=html_body)
def test_draft_event_deleted(event_deleted_notification_template, user, event): event.created_by = user event.publication_status = PublicationStatus.DRAFT event.save() event.soft_delete() strings = [ "event deleted body, event name: %s!" % event.name, ] html_body = "event deleted <b>HTML</b> body, event name: %s!" % event.name assert len(mail.outbox) == 1 check_received_mail_exists("event deleted subject, event name: %s!" % event.name, user.email, strings, html_body=html_body)
def test_reservation_cancelled_notification(order_with_products, order_state, notification_expected): user = order_with_products.reservation.user user.preferred_language = 'fi' user.save() if order_state == Order.CANCELLED: Reservation.objects.filter(id=order_with_products.reservation.id).update(state=Reservation.CONFIRMED) Order.objects.filter(id=order_with_products.id).update(state=Order.CONFIRMED) order_with_products.refresh_from_db() order_with_products.set_state(order_state) if notification_expected: assert len(mail.outbox) == 1 check_received_mail_exists( 'Reservation cancelled subject.', order_with_products.reservation.user.email, get_expected_strings(order_with_products), ) else: assert len(mail.outbox) == 0
def test_catering_notifications(user, user_api_client, catering_product, reservation, new_order_data): provider = catering_product.category.provider provider.notification_email = '*****@*****.**' provider.save() reservation.number_of_participants = 42 reservation.save() # # Create # CREATED_BODY = """Resource: {{ resource }} Reservation: {{ reservation|reservation_time }} Participants: {{ reservation.number_of_participants }} Serving time: {{ serving_time }} Invoicing data: {{ invoicing_data }} Message: {{ message }} Order lines: {% for line in order_lines %} {{ line.quantity }}x {{ line.product }} {% endfor %} """ strings = [ "Resource: %s" % reservation.resource.name, "Participants: %d" % reservation.number_of_participants, "Reservation: to 4.4.2115 klo 9.00–10.00", "Serving time: 13.00", "Invoicing data: %s" % new_order_data['invoicing_data'], "Message: %s" % new_order_data['message'], " 2x Kahvi\n" ] NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_CREATED, short_message="Catering order for {{ resource }} created", subject="Catering order for {{ resource }} created", body=CREATED_BODY) response = user_api_client.post(LIST_URL, data=new_order_data, format='json') assert response.status_code == 201 user.preferred_language = DEFAULT_LANG user.save() check_received_mail_exists( "Catering order for %s created" % reservation.resource.name, provider.notification_email, strings) # Test that serving time is set to the reservation begin time # if no specific serving time is given. order = CateringOrder.objects.first() order.serving_time = None order.save() context = order.get_notification_context(DEFAULT_LANG) assert context['serving_time'] == '9.00' # # Modify # product2 = CateringProduct.objects.create( name_fi='Aatsipoppa', category=catering_product.category, ) NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_MODIFIED, short_message="Catering order for {{ resource }} modified", subject="Catering order for {{ resource }} modified", body=CREATED_BODY) strings = [" 3x Kahvi\n" " 4x Aatsipoppa\n"] new_order_data['order_lines'][0]['quantity'] = 3 new_order_data['order_lines'].append(dict(product=product2.id, quantity=4)) new_order_data['serving_time'] = None detail_url = get_detail_url(order) response = user_api_client.put(detail_url, data=new_order_data, format='json') assert response.status_code == 200 check_received_mail_exists( "Catering order for %s modified" % reservation.resource.name, provider.notification_email, strings) # Make sure we send a notification also if the underlying reservation changes. reservation = CateringOrder.objects.first().reservation reservation.begin += datetime.timedelta(hours=1) reservation.end += datetime.timedelta(hours=1) reservation.save() reservation.set_state(Reservation.CONFIRMED, reservation.user) check_received_mail_exists( "Catering order for %s modified" % reservation.resource.name, provider.notification_email, ["Serving time: 10.00"]) # # Delete # NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_DELETED, short_message="Catering order for {{ resource }} deleted", subject="Catering order for {{ resource }} deleted", body="") response = user_api_client.delete(detail_url, data=new_order_data, format='json') assert response.status_code == 204 check_received_mail_exists( "Catering order for %s deleted" % reservation.resource.name, provider.notification_email, []) response = user_api_client.post(LIST_URL, data=new_order_data, format='json') assert response.status_code == 201 reservation.set_state(Reservation.CANCELLED, reservation.user) check_received_mail_exists( "Catering order for %s deleted" % reservation.resource.name, provider.notification_email, [])
def test_catering_order_comment_create2(user_api_client, user, staff_user, catering_order, new_catering_order_comment_data): COMMENT_CREATED_BODY = """Target type: {{ target_type }} Created by: {{ created_by.display_name }} Created at: {{ created_at|format_datetime }} Resource: {{ catering_order.reservation.resource.name }} Reservation: {{ catering_order.reservation|reservation_time }} Serving time: {{ catering_order.serving_time }} {{ text }} """ NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_COMMENT_CREATED, short_message= "Catering comment added for {{ catering_order.reservation.resource.name }}", subject= "Catering comment added for {{ catering_order.reservation.resource.name }}", body=COMMENT_CREATED_BODY) user.preferred_language = DEFAULT_LANG user.save() provider = catering_order.get_provider() provider.notification_email = '*****@*****.**' provider.save() # First make sure that the notification from a user's comment reaches catering. response = user_api_client.post(LIST_URL, data=new_catering_order_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 1 comment = Comment.objects.first() reservation = catering_order.reservation created_at = format_datetime_tz(comment.created_at, reservation.resource.unit.get_tz()) strings = [ "Created by: %s" % user.get_display_name(), "Created at: %s" % created_at, "Resource: %s" % reservation.resource.name, "Reservation: to 4.4.2115 klo 9.00–10.00", "Serving time: 12.00", new_catering_order_comment_data['text'], ] check_received_mail_exists( "Catering comment added for %s" % reservation.resource.name, provider.notification_email, strings) # Next make sure that a comment by another user reaches the reserver user_api_client.force_authenticate(user=staff_user) response = user_api_client.post(LIST_URL, data=new_catering_order_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 2 assert len(mail.outbox) == 2 check_received_mail_exists( "Catering comment added for %s" % reservation.resource.name, user.email, [])
def test_reservation_comment_create(user_api_client, user, staff_user, reservation, new_reservation_comment_data): COMMENT_CREATED_BODY = """Target type: {{ target_type }} Created by: {{ created_by.display_name }} Created at: {{ created_at|format_datetime }} Resource: {{ reservation.resource }} Reservation: {{ reservation|reservation_time }} {{ text }} """ NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.RESERVATION_COMMENT_CREATED, short_message= "Reservation comment added for {{ reservation.resource }}", subject="Reservation comment added for {{ reservation.resource }}", body=COMMENT_CREATED_BODY) user.preferred_language = DEFAULT_LANG user.save() unit = reservation.resource.unit unit.manager_email = '*****@*****.**' unit.save() response = user_api_client.post(LIST_URL, data=new_reservation_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 1 new_comment = Comment.objects.latest('id') assert new_comment.created_at assert new_comment.created_by == user assert new_comment.content_type == ContentType.objects.get_for_model( Reservation) assert new_comment.object_id == reservation.id assert new_comment.text == new_reservation_comment_data['text'] created_at = format_datetime_tz(new_comment.created_at, reservation.resource.unit.get_tz()) strings = [ "Created by: %s" % user.get_display_name(), "Created at: %s" % created_at, "Resource: %s" % reservation.resource.name, "Reservation: to 4.4.2115 klo 9.00–10.00", new_reservation_comment_data['text'], ] check_received_mail_exists( "Reservation comment added for %s" % reservation.resource.name, unit.manager_email, strings) # Next make sure that a comment by another user reaches the reserver user_api_client.force_authenticate(user=staff_user) response = user_api_client.post(LIST_URL, data=new_reservation_comment_data) assert response.status_code == 201 assert Comment.objects.count() == 2 assert len(mail.outbox) == 2 check_received_mail_exists( "Reservation comment added for %s" % reservation.resource.name, user.email, [])
def test_catering_notifications(user, user_api_client, catering_product, reservation, new_order_data): provider = catering_product.category.provider provider.notification_email = '*****@*****.**' provider.save() reservation.number_of_participants = 42 reservation.save() # # Create # CREATED_BODY = """Resource: {{ resource }} Reservation: {{ reservation|reservation_time }} Participants: {{ reservation.number_of_participants }} Serving time: {{ serving_time }} Invoicing data: {{ invoicing_data }} Message: {{ message }} Order lines: {% for line in order_lines %} {{ line.quantity }}x {{ line.product }} {% endfor %} """ strings = [ "Resource: %s" % reservation.resource.name, "Participants: %d" % reservation.number_of_participants, "Reservation: to 4.4.2115 klo 9.00–10.00", "Serving time: 13.00", "Invoicing data: %s" % new_order_data['invoicing_data'], "Message: %s" % new_order_data['message'], " 2x Kahvi\n" ] NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_CREATED, short_message="Catering order for {{ resource }} created", subject="Catering order for {{ resource }} created", body=CREATED_BODY ) response = user_api_client.post(LIST_URL, data=new_order_data, format='json') assert response.status_code == 201 user.preferred_language = DEFAULT_LANG user.save() check_received_mail_exists("Catering order for %s created" % reservation.resource.name, provider.notification_email, strings) # Test that serving time is set to the reservation begin time # if no specific serving time is given. order = CateringOrder.objects.first() order.serving_time = None order.save() context = order.get_notification_context(DEFAULT_LANG) assert context['serving_time'] == '9.00' # # Modify # product2 = CateringProduct.objects.create( name_fi='Aatsipoppa', category=catering_product.category, ) NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_MODIFIED, short_message="Catering order for {{ resource }} modified", subject="Catering order for {{ resource }} modified", body=CREATED_BODY ) strings = [ " 3x Kahvi\n" " 4x Aatsipoppa\n" ] new_order_data['order_lines'][0]['quantity'] = 3 new_order_data['order_lines'].append(dict(product=product2.id, quantity=4)) new_order_data['serving_time'] = None detail_url = get_detail_url(order) response = user_api_client.put(detail_url, data=new_order_data, format='json') assert response.status_code == 200 check_received_mail_exists("Catering order for %s modified" % reservation.resource.name, provider.notification_email, strings) # Make sure we send a notification also if the underlying reservation changes. reservation = CateringOrder.objects.first().reservation reservation.begin += datetime.timedelta(hours=1) reservation.end += datetime.timedelta(hours=1) reservation.save() reservation.set_state(Reservation.CONFIRMED, reservation.user) check_received_mail_exists("Catering order for %s modified" % reservation.resource.name, provider.notification_email, ["Serving time: 10.00"]) # # Delete # NotificationTemplate.objects.language(DEFAULT_LANG).create( type=NotificationType.CATERING_ORDER_DELETED, short_message="Catering order for {{ resource }} deleted", subject="Catering order for {{ resource }} deleted", body="" ) response = user_api_client.delete(detail_url, data=new_order_data, format='json') assert response.status_code == 204 check_received_mail_exists("Catering order for %s deleted" % reservation.resource.name, provider.notification_email, []) response = user_api_client.post(LIST_URL, data=new_order_data, format='json') assert response.status_code == 201 reservation.set_state(Reservation.CANCELLED, reservation.user) check_received_mail_exists("Catering order for %s deleted" % reservation.resource.name, provider.notification_email, [])