def test_not_valid_slug(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') input_data = { 'slug': 'test-slug-id-12abc34' } serializer = RoomDetailSerializer(instance=room_1, data=input_data, partial=True) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def test_create_with_existing_slug(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') input_data = { 'title': 'New test title', 'slug': 'test-slug' } serializer = RoomDetailSerializer(data=input_data) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def test_create_with_members(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') user2 = user_model.objects.create(email='*****@*****.**') input_data = { 'title': 'New test title', 'slug': 'test-slug', 'members': [user2.id, ] } serializer = RoomDetailSerializer(data=input_data) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def test_add_members(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') user2 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') input_data = { 'members': [ user2.id ] } serializer = RoomDetailSerializer(instance=room_1, data=input_data, partial=True) with self.assertRaises(ValidationError): serializer.is_valid(raise_exception=True)
def test_read_only_fields(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') user2 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') room_1.members.add(user2) input_data = { 'owner': user2.id, 'members_num': 100, 'title': 'test-room-1' } serializer = RoomDetailSerializer(instance=room_1, data=input_data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() serializer_data = serializer.data serializer_data['created_at'] = str(room_1.created_at) expected_data = { 'title': 'test-room-1', 'slug': 'test-slug', 'owner': user1.id, 'created_at': str(room_1.created_at), 'members_num': 2, 'members': [ user2.id ] } self.assertEqual(serializer_data, expected_data)
def test_retrieve_auth_owner(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') url = reverse('room-detail', kwargs={'slug': room_1.slug}) self.client.force_login(user1) response = self.client.get(url) serializer = RoomDetailSerializer(room_1) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, serializer.data)
def test_delete_slug(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') input_data = { 'slug': '' } serializer = RoomDetailSerializer(instance=room_1, data=input_data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() room_1_uuid = room_1.slug.split('-')[-1] self.assertEqual(serializer.data['slug'], f'test-room-id-{room_1_uuid}')
def test_delete_members(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') user2 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') room_1.members.add(user2) input_data = { 'members': [] } serializer = RoomDetailSerializer(instance=room_1, data=input_data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() self.assertEqual(serializer.data['members'], [])
def test_ok(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') user2 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room', slug='test-slug') room_1.members.add(user2) expected_data = { 'title': 'test-room', 'slug': 'test-slug', 'owner': user1.id, 'created_at': str(room_1.created_at), 'members_num': 2, 'members': [ user2.id ] } serializer_data = RoomDetailSerializer(room_1).data serializer_data['created_at'] = str(room_1.created_at) self.assertEqual(serializer_data, expected_data)
def test_update_title_with_autogen_slug(self): user_model = get_user_model() user1 = user_model.objects.create(email='*****@*****.**') room_1 = Room.objects.create(owner=user1, title='test-room') room_1_uuid = room_1.slug.split('-')[-1] input_data = { 'title': 'New test title', 'slug': f'test-room-id-{room_1_uuid}' } serializer = RoomDetailSerializer(instance=room_1, data=input_data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() self.assertEqual(serializer.data['title'], 'New test title') self.assertEqual(serializer.data['slug'], f'test-room-id-{room_1_uuid}')