Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    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}')
Beispiel #5
0
    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)
Beispiel #6
0
    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'], [])
Beispiel #7
0
    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)
Beispiel #8
0
    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}')