def test_user_entered_events( self, client, user_token, event, user, group_with_user ): """ Given event and user, When fetch user events, Then response contains user events. """ response = client.get('/api/events/user_entered_events', **user_token) assert len(response.data) == 1 assert response.data[0] == { 'id': 1, 'title': event.title, 'place': event.place, 'description': event.description, 'created_by': user.username, 'participation_groups': [group_with_user.pk], 'start_date': datetime_to_drf(event.start_date), 'end_date': datetime_to_drf(event.end_date) }
def test_group_events(self, client, user_token, event, group_with_user): """ Given group, And event with group, When list group events, Then response contains given event. """ response = client.get(f'/api/groups/{group_with_user.pk}/events', **user_token) assert response.data == [{ 'id': 1, 'title': event.title, 'place': event.place, 'description': event.description, 'created_by': event.created_by.username, 'participation_groups': [group_with_user.pk], 'start_date': datetime_to_drf(event.start_date), 'end_date': datetime_to_drf(event.end_date) }]
def test_user_created_events( self, client, user_token, event, user ): """ Given user and event, When fetch events created by user, Then response contains created by user events. """ response = client.get('/api/events/user_created_events', **user_token) assert len(response.data) assert response.data[0] == { 'id': 1, 'title': event.title, 'place': event.place, 'description': event.description, 'created_by': user.username, 'participation_groups': list(event.participation_groups.values_list('pk', flat=True)), 'start_date': datetime_to_drf(event.start_date), 'end_date': datetime_to_drf(event.end_date) }
def test_event_comments_list( self, client, user_token, event, event_comments, user ): """ Given event, And event comments, When list event comments, Then response contains event comments. """ response = client.get( '/api/event_comments', {'event': event.id}, **user_token ) assert response.data == [ { 'id': 1, 'text': 'op', 'created_by': user.username, 'created': datetime_to_drf(event_comments[0].created) }, { 'id': 2, 'text': 'oppa', 'created_by': user.username, 'created': datetime_to_drf(event_comments[1].created) }, { 'id': 3, 'text': 'op oppa', 'created_by': user.username, 'created': datetime_to_drf(event_comments[2].created) }, ]
def test_event_list( self, client, user_token, event, user, group_with_user ): """ Given event, When list events, Then response contains list of all events. """ response = client.get( '/api/events', **user_token ) assert response.data == [ { 'id': 1, 'title': event.title, 'place': event.place, 'description': event.description, 'created_by': user.username, 'participation_groups': [group_with_user.pk], 'start_date': datetime_to_drf(event.start_date), 'end_date': None } ]
def test_event_creation( self, client, user_token, user, group, event_data ): """ Given user and group, And event data. When create event, Then response contains event info, And event is created. """ response = client.post( '/api/events', event_data, **user_token ) assert response.data == { **event_data, 'id': 1, 'description': '', 'created_by': user.username, 'start_date': datetime_to_drf(event_data['start_date']), 'end_date': None, } assert Event.objects.filter(title=event_data['title']).exists()
def test_alter_notification(self, client, user_token, sent_notification, notification_data): """ Given notification, And altered notification data, When update notification, Then notification is updated. """ notification_data['send_once'] = False encoded_data, content_type = encode_data(notification_data) response = client.put(f'/api/notifications/{sent_notification.pk}', encoded_data, content_type=content_type, **user_token) notification = Notification.objects.get(pk=sent_notification.pk) assert not notification.send_once assert response.data == { 'id': notification.pk, 'text': notification_data['text'], 'frequency': notification_data['frequency'], 'send_once': notification_data['send_once'], 'groups': notification_data['groups'], 'created_by': 'potykion', 'until': datetime_to_drf(notification.until) }
def test_event_comment_creation( self, client, user_token, event, user ): """ Given event and user, And comment text, When create comment for given event, Then response contains comment data, And comment is created. """ comment_text = 'sample text' response = client.post( '/api/event_comments', { 'event': event.id, 'text': comment_text }, **user_token ) commment = EventComment.objects.get(text=comment_text) assert response.data == { 'id': 1, 'created_by': user.username, 'text': comment_text, 'created': datetime_to_drf(commment.created) }
def test_event_update( self, client, user_token, event, user, group ): """ Given event, And new event data When update event data, Then response contains updated event, And event is updated. """ start_date = timezone.now() + timedelta(days=2) end_date = start_date + timedelta(hours=2) new_event_data = { 'title': event.title, 'place': 'Ne wkola', 'description': event.description, 'participation_groups': [], 'start_date': start_date, 'end_date': end_date, } encoded_data, content_type = encode_data(new_event_data) response = client.put( f'/api/events/{event.pk}', encoded_data, **user_token, content_type=content_type ) assert response.data == { 'id': 1, 'title': event.title, 'place': 'Ne wkola', 'description': event.description, 'created_by': user.username, 'participation_groups': [], 'start_date': datetime_to_drf(start_date), 'end_date': datetime_to_drf(end_date) }
def test_events_csv_view(self, client, user_token, event): """ Given events, When fetch events in CSV format, Then response contains events in CSV format. """ response = client.get('/api/events/csv', **user_token) event_start_date = datetime_to_drf(event.start_date) or '' response_content = response.content.decode('utf-8-sig') expected = f'''created_by,description,end_date,id,participation_groups.0,place,start_date,title\r potykion,Чисто собраться,,1,1,Школка,{event_start_date},Линейка\r ''' assert response_content.strip() == expected.strip()
def test_create_notification(self, client, user_token, notification_data): """ Given notification data, When create notification with given data, Then notification is created, And response contains notification data. """ response = client.post('/api/notifications', notification_data, **user_token) notification = Notification.objects.get() assert response.data == { 'id': notification.pk, 'text': notification_data['text'], 'frequency': notification_data['frequency'], 'send_once': notification_data['send_once'], 'groups': notification_data['groups'], 'created_by': 'potykion', 'until': datetime_to_drf(notification.until) }