def test__update_an_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # dummy inputs TEXT = 'text updated' URL = "http://localhost" # set up updates data2 = response.data for key in ('name', ): for lang in ('fi', 'en', 'sv'): if lang in data2[key]: data2[key][lang] = '%s updated' % data2[key][lang] data2['offers'] = [ { "is_free": False, "price": {"en": TEXT, "sv": TEXT, "fi": TEXT}, "description": {"en": TEXT, "fi": TEXT}, "info_url": {"en": URL, "sv": URL, "fi": URL} } ] data2['keywords'] = data2['keywords'][:1] data2['in_language'] = data2['in_language'][:2] # store updates event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert assert_event_data_is_equal(data2, response2.data)
def test__cancel_a_postponed_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # remove the start_time data2 = response.data data2['start_time'] = None # update the event event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert backend postponed the event data2['event_status'] = 'EventPostponed' assert_event_data_is_equal(data2, response2.data) # mark the event cancelled data2 = response.data data2['event_status'] = 'EventCancelled' # update the event response2 = update_with_put(api_client, event_id, data2) # assert backend cancelled the event data2['event_status'] = 'EventCancelled' assert_event_data_is_equal(data2, response2.data)
def test__reschedule_a_cancelled_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # mark the event cancelled data2 = response.data data2['event_status'] = 'EventCancelled' # update the event event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert backend cancelled the event data2['event_status'] = 'EventCancelled' assert_event_data_is_equal(data2, response2.data) # create a new datetime and remove the cancelled status new_datetime = (timezone.now() + timedelta(days=3)).isoformat().replace( '+00:00', 'Z') data3 = response2.data data3['start_time'] = new_datetime data3['end_time'] = new_datetime data3.pop('event_status') # update the event event_id = data3.pop('@id') response3 = update_with_put(api_client, event_id, data3) # assert backend rescheduled the event data3['event_status'] = 'EventRescheduled' assert_event_data_is_equal(data3, response3.data)
def test__update_minimal_event_with_autopopulated_fields_with_put( api_client, minimal_event_dict, user, organization): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) data = response.data assert_event_data_is_equal(minimal_event_dict, data) event_id = data['@id'] response2 = update_with_put(api_client, event_id, minimal_event_dict) assert_event_data_is_equal(data, response2.data) event = Event.objects.get(id=data['id']) assert event.created_by == user assert event.last_modified_by == user assert event.created_time is not None assert event.last_modified_time is not None assert event.data_source.id == settings.SYSTEM_DATA_SOURCE_ID assert event.publisher == organization # events are automatically marked as ending at midnight, local time expected_end_time = (timezone.localtime(event.start_time).replace( hour=0, minute=0, second=0, microsecond=0).astimezone(pytz.utc) + timedelta(days=1)) assert event.end_time == expected_end_time assert event.has_end_time is False
def test__reschedule_a_cancelled_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # mark the event cancelled data2 = response.data data2['event_status'] = 'EventCancelled' # update the event event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert backend cancelled the event data2['event_status'] = 'EventCancelled' assert_event_data_is_equal(data2, response2.data) # create a new datetime and remove the cancelled status new_datetime = (timezone.now() + timedelta(days=3)).isoformat() data3 = response2.data data3['start_time'] = new_datetime data3['end_time'] = new_datetime data3.pop('event_status') # update the event event_id = data3.pop('@id') response3 = update_with_put(api_client, event_id, data3) # assert backend rescheduled the event data3['event_status'] = 'EventRescheduled' assert_event_data_is_equal(data3, response3.data)
def test__location_n_events_updated(api_client, minimal_event_dict, user, data_source, other_data_source, place2): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) call_command('update_n_events') assert Place.objects.get(id=data_source.id + ':test_location').n_events == 1 data2 = response.data print('got the post response') print(data2) # change the location event_id = data2.pop('@id') data2['location'] = {'@id': location_id(place2)} response2 = update_with_put(api_client, event_id, data2) print('got the put response') print(response2.data) call_command('update_n_events') assert Place.objects.get(id=data_source.id + ':test_location').n_events == 0 assert Place.objects.get(id=other_data_source.id + ':test_location_2').n_events == 1
def test__keyword_n_events_updated(api_client, minimal_event_dict, user, data_source, organization, make_keyword_id): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) call_command('update_n_events') assert Keyword.objects.get(id=data_source.id + ':test').n_events == 1 data2 = response.data print('got the post response') print(data2) # change the keyword and add an audience event_id = data2.pop('@id') data2['keywords'] = [{ '@id': make_keyword_id(data_source, organization, 'test2') }] data2['audience'] = [{ '@id': make_keyword_id(data_source, organization, 'test3') }] response2 = update_with_put(api_client, event_id, data2) print('got the put response') print(response2.data) call_command('update_n_events') assert Keyword.objects.get(id=data_source.id + ':test').n_events == 0 assert Keyword.objects.get(id=data_source.id + ':test2').n_events == 1 assert Keyword.objects.get(id=data_source.id + ':test3').n_events == 1
def test__api_key_with_organization_can_create_an_event(api_client, minimal_event_dict, data_source, organization): data_source.owner = organization data_source.save() response = create_with_post(api_client, minimal_event_dict, data_source) assert_event_data_is_equal(minimal_event_dict, response.data)
def test__create_a_minimal_event_with_naive_datetime(api_client, minimal_event_dict, user): api_client.force_authenticate(user=user) minimal_event_dict['start_time'] = (datetime.now() + timedelta(days=1)).isoformat() response = create_with_post(api_client, minimal_event_dict) # API should have assumed UTC datetime minimal_event_dict['start_time'] = pytz.utc.localize(dateutil_parse(minimal_event_dict['start_time'])).\ isoformat().replace('+00:00', 'Z') assert_event_data_is_equal(minimal_event_dict, response.data)
def test__create_a_draft_event_without_location_and_keyword(list_url, api_client, minimal_event_dict, user): api_client.force_authenticate(user=user) minimal_event_dict.pop("location") minimal_event_dict.pop("keywords") minimal_event_dict["publication_status"] = "draft" response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) # the drafts should not be visible to unauthorized users api_client.logout() resp2 = api_client.get(response.data["@id"]) assert "@id" not in resp2.data
def test__postpone_an_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # remove the start_time data2 = response.data data2['start_time'] = None # update the event event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert backend postponed the event data2['event_status'] = 'EventPostponed' assert_event_data_is_equal(data2, response2.data) # try to cancel marking as postponed data2 = response2.data data2['event_status'] = 'EventScheduled' response3 = api_client.put(event_id, data2, format='json') # assert the event does not revert back to scheduled assert response3.status_code == 400 assert 'event_status' in response2.data # reschedule and try to cancel marking new_datetime = (timezone.now() + timedelta(days=3)).isoformat().replace( '+00:00', 'Z') data2['start_time'] = new_datetime data2['end_time'] = new_datetime data2['event_status'] = 'EventScheduled' response3 = api_client.put(event_id, data2, format='json') # assert the event does not revert back to scheduled assert response3.status_code == 400 assert 'event_status' in response3.data # reschedule, but do not try to cancel marking data2 = response2.data new_datetime = (timezone.now() + timedelta(days=3)).isoformat().replace( '+00:00', 'Z') data2['start_time'] = new_datetime data2['end_time'] = new_datetime data2.pop('event_status') event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert the event is marked rescheduled data2['event_status'] = 'EventRescheduled' assert_event_data_is_equal(data2, response2.data)
def test__api_key_with_organization_can_create_a_suborganization_event(api_client, minimal_event_dict, data_source, organization, organization2): data_source.owner = organization data_source.save() organization2.parent = organization organization2.save() minimal_event_dict['publisher'] = organization2.id response = create_with_post(api_client, minimal_event_dict, data_source) assert_event_data_is_equal(minimal_event_dict, response.data) assert ApiKeyUser.objects.all().count() == 1
def test__create_a_draft_event_without_location_and_keyword( list_url, api_client, minimal_event_dict, user): api_client.force_authenticate(user=user) minimal_event_dict.pop('location') minimal_event_dict.pop('keywords') minimal_event_dict['publication_status'] = 'draft' response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) # the drafts should not be visible to unauthorized users api_client.logout() resp2 = api_client.get(response.data['@id']) assert '@id' not in resp2.data
def test__postpone_an_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # remove the start_time data2 = response.data data2['start_time'] = None # update the event event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert backend postponed the event data2['event_status'] = 'EventPostponed' assert_event_data_is_equal(data2, response2.data) # try to cancel marking as postponed data2 = response2.data data2['event_status'] = 'EventScheduled' response3 = api_client.put(event_id, data2, format='json') # assert the event does not revert back to scheduled assert response3.status_code == 400 assert 'event_status' in response2.data # reschedule and try to cancel marking new_datetime = (timezone.now() + timedelta(days=3)).isoformat() data2['start_time'] = new_datetime data2['end_time'] = new_datetime data2['event_status'] = 'EventScheduled' response3 = api_client.put(event_id, data2, format='json') # assert the event does not revert back to scheduled assert response3.status_code == 400 assert 'event_status' in response3.data # reschedule, but do not try to cancel marking data2 = response2.data new_datetime = (timezone.now() + timedelta(days=3)).isoformat() data2['start_time'] = new_datetime data2['end_time'] = new_datetime data2.pop('event_status') event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert the event is marked rescheduled data2['event_status'] = 'EventRescheduled' assert_event_data_is_equal(data2, response2.data)
def test__reschedule_an_event_with_put(api_client, complex_event_dict, user): # create an event #api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # create a new datetime new_datetime = (timezone.now() + timedelta(days=3)).isoformat().replace( '+00:00', 'Z') data2 = response.data data2['start_time'] = new_datetime data2['end_time'] = new_datetime # update the event event_id = data2.pop('@id') data2['pin'] = '1234a' data2.pop('publisher') data2.pop('data_source') response2 = update_with_put(api_client, event_id, data2) # assert backend rescheduled the event data2['event_status'] = 'EventRescheduled' response2.data.pop('publisher') response2.data.pop('data_source') assert_event_data_is_equal(data2, response2.data) # try to cancel marking as rescheduled data2['event_status'] = 'EventScheduled' data2['pin'] = '1234a' response3 = api_client.put(event_id, data2, format='json') # assert the event does not revert back to scheduled assert response3.status_code == 400 assert 'event_status' in response3.data # create a new datetime again new_datetime = (timezone.now() + timedelta(days=3)).isoformat().replace( '+00:00', 'Z') data2 = response2.data data2['start_time'] = new_datetime data2['end_time'] = new_datetime # update the event again data2['pin'] = '1234a' response2 = update_with_put(api_client, event_id, data2) # assert the event remains rescheduled data2['event_status'] = 'EventRescheduled' assert_event_data_is_equal(data2, response2.data)
def test__api_key_with_another_organization_can_create_an_event(api_client, minimal_event_dict, data_source, organization, other_data_source, organization2): data_source.owner = organization data_source.save() other_data_source.owner = organization2 other_data_source.save() response = create_with_post(api_client, minimal_event_dict, data_source) assert_event_data_is_equal(minimal_event_dict, response.data) assert ApiKeyUser.objects.all().count() == 1 response = create_with_post(api_client, minimal_event_dict, other_data_source) assert_event_data_is_equal(minimal_event_dict, response.data) assert ApiKeyUser.objects.all().count() == 2
def test_start_time_and_end_time_validation_with_past_events_allowed( api_client, minimal_event_dict, user, data_source): api_client.force_authenticate(user) data_source.create_past_events = True data_source.save() minimal_event_dict['start_time'] = (timezone.now() - timedelta(days=2)).isoformat().replace('+00:00', 'Z') minimal_event_dict['end_time'] = (timezone.now() - timedelta(days=1)).isoformat().replace('+00:00', 'Z') response = create_with_post(api_client, minimal_event_dict, data_source) assert_event_data_is_equal(minimal_event_dict, response.data)
def test__create_a_draft_event_without_location_and_keyword(list_url, api_client, minimal_event_dict, user): api_client.force_authenticate(user=user) minimal_event_dict.pop('location') minimal_event_dict.pop('keywords') minimal_event_dict['publication_status'] = 'draft' response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) # the drafts should not be visible to unauthorized users api_client.logout() resp2 = api_client.get(response.data['@id']) assert '@id' not in resp2.data
def test_can_edit_events_in_the_past_with_past_events_allowed( api_client, event, minimal_event_dict, user, data_source): api_client.force_authenticate(user) data_source.edit_past_events = True data_source.save() event.start_time = timezone.now() - timedelta(days=2) event.end_time = timezone.now() - timedelta(days=1) event.save(update_fields=('start_time', 'end_time')) response = api_client.put(reverse('event-detail', kwargs={'pk': event.id}), minimal_event_dict, format='json') assert response.status_code == 200 assert_event_data_is_equal(minimal_event_dict, response.data)
def test__cannot_update_an_event_without_a_short_description(api_client, minimal_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) data2 = response.data # delete name data2['short_description'] = {'fi': None} event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) assert response2.status_code == 400 assert 'short_description' in response2.data data2.pop('short_description') response2 = update_with_put(api_client, event_id, data2) assert response2.status_code == 400 assert 'short_description' in response2.data
def test__update_an_event_with_naive_datetime(api_client, minimal_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) data2 = response.data # store updates event_id = data2.pop('@id') data2['start_time'] = (datetime.now() + timedelta(days=1)).isoformat() response2 = update_with_put(api_client, event_id, data2) # API should have assumed UTC datetime data2['start_time'] = pytz.utc.localize(dateutil_parse(data2['start_time'])).isoformat().replace('+00:00', 'Z') data2['event_status'] = 'EventRescheduled' # assert assert_event_data_is_equal(data2, response2.data)
def test__cancel_an_event_with_put(api_client, complex_event_dict, user): # create an event api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) # mark the event cancelled data2 = response.data data2['event_status'] = 'EventCancelled' # cancelling (like deleting) should be allowed even if event is incomplete (external events etc.) data2['keywords'] = [] data2['location'] = None # update the event event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) # assert backend cancelled the event data2['event_status'] = 'EventCancelled' assert_event_data_is_equal(data2, response2.data)
def test__update_a_draft_with_put(api_client, minimal_event_dict, user): # create an event api_client.force_authenticate(user=user) minimal_event_dict.pop('location') minimal_event_dict.pop('keywords') minimal_event_dict['publication_status'] = 'draft' response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) data2 = response.data print('got the post response') print(data2) # store updates event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) print('got the put response') print(response2.data) # assert assert_event_data_is_equal(data2, response2.data)
def test__cannot_update_a_draft_without_a_name(api_client, minimal_event_dict, user): # create an event api_client.force_authenticate(user=user) minimal_event_dict.pop('location') minimal_event_dict.pop('keywords') minimal_event_dict['publication_status'] = 'draft' response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data) data2 = response.data # delete name data2['name'] = {'fi': None} event_id = data2.pop('@id') response2 = update_with_put(api_client, event_id, data2) assert response2.status_code == 400 assert 'name' in response2.data data2.pop('name') response2 = update_with_put(api_client, event_id, data2) assert response2.status_code == 400 assert 'name' in response2.data
def test__create_a_minimal_event_with_post(api_client, minimal_event_dict, user): api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data)
def test__create_a_minimal_event_with_post(api_client, minimal_event_dict, user): #api_client.force_authenticate(user=user) response = create_with_post(api_client, minimal_event_dict) assert_event_data_is_equal(minimal_event_dict, response.data)
def test__create_a_complex_event_with_post(api_client, complex_event_dict, user): api_client.force_authenticate(user=user) response = create_with_post(api_client, complex_event_dict) assert_event_data_is_equal(complex_event_dict, response.data)