def test_send_monthly_invoice(self): """Method to test monthly invoices""" with self.app.test_request_context(): TicketFeesFactory(service_fee=10.23, maximum_fee=11) test_event = EventFactoryBasic(state='published') test_user = UserFactory() test_order = OrderFactory(status='completed') test_order.completed_at = datetime.datetime.now( ) - datetime.timedelta(days=30) test_order.amount = 100 test_order.event = test_event test_ticket_holder = AttendeeFactory() test_ticket_holder.event = test_event test_ticket_holder.order = test_order test_event.owner = test_user db.session.commit() send_monthly_event_invoice() event_invoice = EventInvoice.query.get(1) self.assertEqual(event_invoice.amount, 10.23)
def setUp(self): super().setUp() with self.app.test_request_context(): user = UserFactory() save_to_db(user) event = EventFactoryBasic() event.user_id = user.id save_to_db(event) # Authenticate User self.auth = { 'Authorization': "JWT " + create_access_token(user.id, fresh=True) }
def test_edit_stream_rooms_different_event_organizer_error( db, client, user, jwt): stream = get_stream(db, user=user, with_event=True) event = EventFactoryBasic() db.session.commit() data = json.dumps({ 'data': { 'id': str(stream.id), 'type': 'video-stream', "relationships": { "event": { "data": { "id": str(event.id), "type": "event" } } }, } }) response = client.patch( f'/v1/video-streams/{stream.id}', content_type='application/vnd.api+json', headers=jwt, data=data, ) assert response.status_code == 403 assert (json.loads(response.data)['errors'][0]['detail'] == "You don't have access to the provided event")
def test_throw_free_tickets(client, db, jwt): tickets = _create_tickets([0, 0], event=EventFactoryBasic(), type='free') db.session.commit() response = client.post( '/v1/orders/create-order', content_type='application/json', headers=jwt, data=json.dumps({'tickets': _create_ticket_dict(tickets, [1, 2])}), ) assert response.status_code == 403 assert json.loads(response.data) == { "errors": [{ "status": 403, "source": { "pointer": "/data/relationships/user", "code": "unverified-user", }, "title": "Access Forbidden", "detail": "Unverified user cannot place free orders", }], "jsonapi": { "version": "1.0" }, }
def test_group_patch_access_deny(db, client, user, jwt): event = EventFactoryBasic() role, _ = get_or_create(Role, name='moderator', title_name='Moderator') UsersEventsRoles(user=user, event=event, role=role) db.session.commit() group = GroupFactory(user_id=user.id) db.session.commit() data = json.dumps({ "data": { "type": "group", "id": str(group.id), "relationships": { "events": { "data": [{ "id": str(event.id), "type": "event" }] }, }, "attributes": { "name": "eventgp1" }, } }) response = client.patch( f'/v1/groups/{group.id}', content_type='application/vnd.api+json', headers=jwt, data=data, ) assert (json.loads( response.data)['errors'][0]['detail'] == 'Event owner access required')
def test_export_basic(db): test_event = EventFactoryBasic(identifier='asdfgh', name='Hoopa Loopa', location_name='Narnia', timezone='Asia/Kolkata') test_microlocation = MicrolocationSubFactory( name='online', event=test_event, ) test_session = SessionFactory( title='Gooseberry Muffin', event=test_event, microlocation=test_microlocation, ) db.session.commit() test_cal_str = to_ical(test_session.event, include_sessions=True) test_cal = icalendar.Calendar.from_ical(test_cal_str) event = test_cal.subcomponents[0] assert event['summary'] == 'Hoopa Loopa' assert event['url'] == 'http://eventyay.com/e/asdfgh' assert event['location'] == 'Narnia' timezone = test_cal.subcomponents[1] assert timezone['TZID'] == 'Asia/Kolkata' session = test_cal.subcomponents[2] assert session['summary'] == 'Gooseberry Muffin' assert session[ 'url'] == f'http://eventyay.com/e/asdfgh/session/{test_session.id}' assert (session['location'] == f'online') assert ICalExporter.export(test_session.event_id) == test_cal_str
def test_group_post_access_allow(db, client, user, jwt): user.is_verified = True event = EventFactoryBasic() role, _ = get_or_create(Role, name='owner', title_name='Owner') UsersEventsRoles(user=user, event=event, role=role) db.session.commit() data = json.dumps({ "data": { "type": "group", "relationships": { "events": { "data": [{ "id": str(event.id), "type": "event" }] }, }, "attributes": { "name": "eventgp2" }, } }) response = client.post( f'/v1/groups', content_type='application/vnd.api+json', headers=jwt, data=data, ) assert json.loads( response.data)['data']['attributes']['name'] == "eventgp2"
def test_ignore_on_order_event_update(client, db, user, jwt): order_id = create_order(db, user) order = Order.query.get(order_id) order_event = order.event event = EventFactoryBasic() db.session.commit() data = json.dumps({ 'data': { 'type': 'order', 'id': order_id, "relationships": { "event": { "data": { "id": str(event.id), "type": "event" } } }, } }) response = client.patch( f'/v1/orders/{order_id}', content_type='application/vnd.api+json', headers=jwt, data=data, ) db.session.refresh(order) assert response.status_code == 200 assert order.event == order_event
def test_should_create_attendee_forms(self): """Method to test custom forms for attendees of an event.""" with self.app.test_request_context(): event = EventFactoryBasic() save_to_db(event) create_custom_forms_for_attendees(event) self.assertEqual(get_count(CustomForms.query), 3)
def test_event_no_stream(db, client): event = EventFactoryBasic(state='published') db.session.commit() response = client.get(f'/v1/events/{event.id}/has-streams') assert json.loads(response.data) == {"can_access": False, "exists": False}
def test_save_to_db(db): """Method to test the function save_to_db""" obj = EventFactoryBasic() save_to_db(obj) event = Event.query.filter_by(id=obj.id).first() assert event.id == obj.id assert event == obj
def get_event(db, user=None): event = EventFactoryBasic(state='draft') if user: role, _ = get_or_create(Role, name='owner', title_name='Owner') UsersEventsRoles(user=user, event=event, role=role) db.session.commit() return event
def test_safe_query(db): """Method to test the function safe_query""" event = EventFactoryBasic() save_to_db(event) obj = safe_query(Event, 'id', event.id, 'event_id') assert event.id == obj.id assert event == obj
def test_allow_deleted_event_for_admin(db, app): obj = EventFactoryBasic(deleted_at=datetime.now()) db.session.commit() with app.test_request_context('?get_trashed=true'): event = safe_query(Event, 'id', obj.id, 'id') assert event.id == obj.id assert event == obj
def test_reject_deleted_tickets(db): event = EventFactoryBasic() tickets = TicketSubFactory.create_batch(3, event=event) tickets.append(TicketSubFactory(deleted_at=datetime.now(), event=event)) db.session.commit() with pytest.raises(ObjectNotFound, match=r'Tickets not found for IDs: .*'): validate_tickets([ticket.id for ticket in tickets])
def test_event_stream_rooms_no_access(db, client, jwt): event = EventFactoryBasic(state='published') MicrolocationSubVideoStreamFactory(event=event) db.session.commit() response = client.get(f'/v1/events/{event.id}/has-streams', headers=jwt) assert json.loads(response.data) == {"can_access": False, "exists": True}
def _create_discount_code(db, **kwargs): tickets = TicketSubFactory.create_batch(3, event=EventFactoryBasic(), **kwargs) discount = DiscountCodeTicketSubFactory(tickets_number=5, tickets=tickets) db.session.commit() return discount, [{'id': ticket.id} for ticket in tickets]
def test_event_stream_identifier(db, client): event = EventFactoryBasic(state='published') VideoStreamFactoryBase(event=event) db.session.commit() response = client.get(f'/v1/events/{event.identifier}/has-streams') assert json.loads(response.data) == {"can_access": False, "exists": True}
def _create_donation_tickets(db): event = EventFactoryBasic() tickets = _create_tickets([10, 20], event=event) tickets.append( TicketSubFactory(type='donation', max_price=20, min_price=10, event=event) ) db.session.commit() return _create_ticket_dict(tickets[::-1], [2, 1, 3])
def test_get_identity(self): """Method to test identity of authenticated user""" with self.app.test_request_context(): user = UserFactory() save_to_db(user) event = EventFactoryBasic() event.user_id = user.id save_to_db(event) # Authenticate User self.auth = { 'Authorization': "JWT " + create_access_token(user.id, fresh=True) } with self.app.test_request_context(headers=self.auth): self.assertEquals(get_identity().id, user.id)
def test_should_not_expire_valid_orders(self): """Method to test to not mark valid orders as expired""" with self.app.test_request_context(): obj = OrderFactory() event = EventFactoryBasic() obj.event = event db.session.commit() set_expiry_for_order(obj) self.assertEqual(obj.status, 'initializing')
def test_event_stream_rooms(db, client, user, jwt): event = EventFactoryBasic(state='published') MicrolocationSubVideoStreamFactory(event=event) AttendeeOrderSubFactory(event=event, email=user.email, order__status='completed') db.session.commit() response = client.get(f'/v1/events/{event.id}/has-streams', headers=jwt) assert json.loads(response.data) == {"can_access": True, "exists": True}
def test_group_role_access(app, db, user, jwt): event = EventFactoryBasic() with app.test_request_context(headers=jwt): assert has_access('is_coorganizer', event_id=event.id) == False group = GroupSubFactory(user=user) role, _ = get_or_create(Role, name='organizer', title_name='Organizer') ugr = UsersGroupsRoles( email=user.email, user=user, group=group, role=role, accepted=True, role_id=role.id, ) event.group_id = group.id db.session.commit() with app.test_request_context(headers=jwt): assert has_access('is_coorganizer', event_id=event.id) == True
def test_export(self): """Test to check event contents in xCal format""" with self.app.test_request_context(): test_event = EventFactoryBasic() save_to_db(test_event) xcal = XCalExporter() xcal_string = xcal.export(test_event.id) xcal_original = fromstring(xcal_string) assert fromstring(tostring(xcal_original))[0][3].text == "example" assert fromstring(tostring(xcal_original))[0][2].text == \ "Schedule for sessions at example"
def test_export(self): """Test to check event contents in pentabarfxml format""" with self.app.test_request_context(): test_event = EventFactoryBasic() save_to_db(test_event) pentabarf_export = PentabarfExporter() pentabarf_string = pentabarf_export.export(test_event.id) pentabarf_original = fromstring(pentabarf_string) self.assertEqual(pentabarf_original.find('conference/title').text, "example") self.assertEqual( pentabarf_original.find('conference/start').text, "2099-12-13" )
def test_should_expire_outdated_order(self): """Method to test expiration of outdated orders""" with self.app.test_request_context(): obj = OrderFactory() order_expiry_time = get_settings()['order_expiry_time'] event = EventFactoryBasic() obj.event = event obj.created_at = datetime.now( timezone.utc) - timedelta(minutes=order_expiry_time) set_expiry_for_order(obj) self.assertEqual(obj.status, 'expired')
def test_assigning_old_form_name(db): event = EventFactoryBasic() speaker_custom_form = CustomForms(event=event, field_identifier='speakingExperience', form='speaker', type='text') attendee_custom_form = CustomForms(event=event, field_identifier='taxBusinessInfo', form='attendee', type='text') session_custom_form = CustomForms(event=event, field_identifier='longAbstract', form='session', type='text') none_custom_form = CustomForms( event=event, field_identifier='showNumber', name='Show Number', form='attendee', type='text', ) db.session.commit() assert speaker_custom_form.name == 'Speaking Experience' assert none_custom_form.name == 'Show Number' db.session.execute("update custom_forms set name = 'None'") db.session.commit() db.session.refresh(speaker_custom_form) db.session.refresh(attendee_custom_form) db.session.refresh(session_custom_form) db.session.refresh(none_custom_form) assert speaker_custom_form.name == 'None' assert attendee_custom_form.name == 'None' assert session_custom_form.name == 'None' assert none_custom_form.name == 'None' assign_field_names(db.session) db.session.refresh(speaker_custom_form) db.session.refresh(attendee_custom_form) db.session.refresh(session_custom_form) db.session.refresh(none_custom_form) assert speaker_custom_form.name == 'Speaking Experience' assert attendee_custom_form.name == 'Tax Business Info' assert session_custom_form.name == 'Long Abstract' # cannot restore custom form field name assert none_custom_form.name == 'None'
def get_stream(db, user=None, with_event=False, **kwargs): stream = VideoStreamFactoryBase(**kwargs) if with_event: event = EventFactoryBasic() stream.event = event else: room = MicrolocationSubVideoStreamFactory(video_stream=stream) event = room.event if user: role, _ = get_or_create(Role, name='owner', title_name='Owner') UsersEventsRoles(user=user, event=event, role=role) db.session.commit() return stream
def test_validate_discount_code_require_same_event_id(db): discount, tickets = _create_discount_code(db) discount.event = EventFactoryBasic() db.session.commit() with pytest.raises(UnprocessableEntityError, match='Invalid Discount Code'): discount.validate(event_id='40', tickets=tickets) with pytest.raises(UnprocessableEntityError, match='Invalid Discount Code'): discount.validate(event_id=100, tickets=tickets) assert discount.validate(event_id=discount.event_id, tickets=tickets) == discount
def test_get_or_create(db): """Method to test the function get_or_create""" event = EventFactoryBasic() save_to_db(event) obj, is_created = get_or_create(Event, name=event.name) assert event.id == obj.id assert is_created is False obj, is_created = get_or_create(Event, name="new event", starts_at=event.starts_at, ends_at=event.ends_at) assert event.id != obj.id assert is_created is True