def test_steal_attachments(self):
        """middleware validates if attachments are already assigned to other posts"""
        other_post = testutils.reply_thread(self.thread)

        attachments = [
            self.mock_attachment(post=other_post),
            self.mock_attachment(),
        ]

        middleware = AttachmentsMiddleware(
            request=RequestMock(
                {'attachments': [attachments[0].pk, attachments[1].pk]}),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()
        self.assertTrue(serializer.is_valid())
        middleware.save(serializer)

        # only unassociated attachment was associated with post
        self.assertEqual(self.post.update_fields, ['attachments_cache'])
        self.assertEqual(self.post.attachment_set.count(), 1)

        self.assertEqual(
            Attachment.objects.get(pk=attachments[0].pk).post, other_post)
        self.assertEqual(
            Attachment.objects.get(pk=attachments[1].pk).post, self.post)
    def test_edit_attachments(self):
        """middleware removes and adds attachments to post"""
        attachments = [
            self.mock_attachment(post=self.post),
            self.mock_attachment(post=self.post),
            self.mock_attachment(),
        ]

        middleware = AttachmentsMiddleware(
            request=RequestMock(
                {'attachments': [attachments[0].pk, attachments[2].pk]}),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()
        self.assertTrue(serializer.is_valid())
        middleware.save(serializer)

        # attachments were associated with post
        self.assertEqual(self.post.update_fields, ['attachments_cache'])
        self.assertEqual(self.post.attachment_set.count(), 2)

        attachments_filenames = [
            attachments[2].filename, attachments[0].filename
        ]
        self.assertEqual([a['filename'] for a in self.post.attachments_cache],
                         attachments_filenames)
    def test_steal_attachments(self):
        """middleware validates if attachments are already assigned to other posts"""
        other_post = testutils.reply_thread(self.thread)

        attachments = [
            self.mock_attachment(post=other_post),
            self.mock_attachment(),
        ]

        middleware = AttachmentsMiddleware(
            request=RequestMock({
                'attachments': [attachments[0].pk, attachments[1].pk]
            }),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()
        self.assertTrue(serializer.is_valid())
        middleware.save(serializer)

        # only unassociated attachment was associated with post
        self.assertEqual(self.post.update_fields, ['attachments_cache'])
        self.assertEqual(self.post.attachment_set.count(), 1)

        self.assertEqual(Attachment.objects.get(pk=attachments[0].pk).post, other_post)
        self.assertEqual(Attachment.objects.get(pk=attachments[1].pk).post, self.post)
    def test_get_new_attachments(self):
        """get_initial_attachments returns list of attachments already existing on post"""
        middleware = AttachmentsMiddleware(
            request=RequestMock(),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()

        attachments = serializer.get_new_attachments(middleware.user,
                                                     [1, 2, 3])
        self.assertEqual(attachments, [])

        attachment = self.mock_attachment()
        attachments = serializer.get_new_attachments(middleware.user,
                                                     [attachment.pk])
        self.assertEqual(attachments, [attachment])

        # only own orphaned attachments may be assigned to posts
        other_user_attachment = self.mock_attachment(user=False)
        attachments = serializer.get_new_attachments(
            middleware.user, [other_user_attachment.pk])
        self.assertEqual(attachments, [])
    def test_edit_attachments(self):
        """middleware removes and adds attachments to post"""
        attachments = [
            self.mock_attachment(post=self.post),
            self.mock_attachment(post=self.post),
            self.mock_attachment(),
        ]

        middleware = AttachmentsMiddleware(
            request=RequestMock({
                'attachments': [attachments[0].pk, attachments[2].pk]
            }),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()
        self.assertTrue(serializer.is_valid())
        middleware.save(serializer)

        # attachments were associated with post
        self.assertEqual(self.post.update_fields, ['attachments_cache'])
        self.assertEqual(self.post.attachment_set.count(), 2)

        attachments_filenames = [attachments[2].filename, attachments[0].filename]
        self.assertEqual([a['filename'] for a in self.post.attachments_cache],
                         attachments_filenames)
    def test_add_attachments(self):
        """middleware adds attachments to post"""
        attachments = [
            self.mock_attachment(),
            self.mock_attachment(),
        ]

        middleware = AttachmentsMiddleware(request=RequestMock(
            {'attachments': [a.pk for a in attachments]}),
                                           mode=PostingEndpoint.EDIT,
                                           user=self.user,
                                           post=self.post)

        serializer = middleware.get_serializer()
        self.assertTrue(serializer.is_valid())
        middleware.save(serializer)

        # attachments were associated with post
        self.assertEqual(self.post.update_fields, ['attachments_cache'])
        self.assertEqual(self.post.attachment_set.count(), 2)

        attachments_filenames = list(
            reversed([a.filename for a in attachments]))
        self.assertEqual([a['filename'] for a in self.post.attachments_cache],
                         attachments_filenames)
    def test_middleware_is_optional(self):
        """middleware is optional"""
        INPUTS = ({}, {'attachments': []})

        for test_input in INPUTS:
            middleware = AttachmentsMiddleware(request=RequestMock(test_input),
                                               mode=PostingEndpoint.START,
                                               user=self.user,
                                               post=self.post)

            serializer = middleware.get_serializer()
            self.assertTrue(serializer.is_valid())
    def test_middleware_is_optional(self):
        """middleware is optional"""
        INPUTS = [{}, {'attachments': []}]

        for test_input in INPUTS:
            middleware = AttachmentsMiddleware(
                request=RequestMock(test_input),
                mode=PostingEndpoint.START,
                user=self.user,
                post=self.post,
            )

            serializer = middleware.get_serializer()
            self.assertTrue(serializer.is_valid())
    def test_middleware_validates_ids(self):
        """middleware validates attachments ids"""
        INPUTS = ('none', ['a', 'b', 123],
                  range(settings.MISAGO_POST_ATTACHMENTS_LIMIT + 1))

        for test_input in INPUTS:
            middleware = AttachmentsMiddleware(request=RequestMock(
                {'attachments': test_input}),
                                               mode=PostingEndpoint.START,
                                               user=self.user,
                                               post=self.post)

            serializer = middleware.get_serializer()
            self.assertFalse(serializer.is_valid(),
                             "%r shouldn't validate" % test_input)
    def test_middleware_validates_ids(self):
        """middleware validates attachments ids"""
        INPUTS = ['none', ['a', 'b', 123], range(settings.MISAGO_POST_ATTACHMENTS_LIMIT + 1)]

        for test_input in INPUTS:
            middleware = AttachmentsMiddleware(
                request=RequestMock({
                    'attachments': test_input
                }),
                mode=PostingEndpoint.START,
                user=self.user,
                post=self.post,
            )

            serializer = middleware.get_serializer()
            self.assertFalse(serializer.is_valid(), "%r shouldn't validate" % test_input)
    def test_get_initial_attachments(self):
        """get_initial_attachments returns list of attachments already existing on post"""
        middleware = AttachmentsMiddleware(request=RequestMock(),
                                           mode=PostingEndpoint.EDIT,
                                           user=self.user,
                                           post=self.post)

        serializer = middleware.get_serializer()

        attachments = serializer.get_initial_attachments(
            middleware.mode, middleware.user, middleware.post)
        self.assertEqual(attachments, [])

        attachment = self.mock_attachment(post=self.post)
        attachments = serializer.get_initial_attachments(
            middleware.mode, middleware.user, middleware.post)
        self.assertEqual(attachments, [attachment])
    def test_get_initial_attachments(self):
        """get_initial_attachments returns list of attachments already existing on post"""
        middleware = AttachmentsMiddleware(
            request=RequestMock(),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()

        attachments = serializer.get_initial_attachments(
            middleware.mode, middleware.user, middleware.post
        )
        self.assertEqual(attachments, [])

        attachment = self.mock_attachment(post=self.post)
        attachments = serializer.get_initial_attachments(
            middleware.mode, middleware.user, middleware.post
        )
        self.assertEqual(attachments, [attachment])
    def test_get_new_attachments(self):
        """get_initial_attachments returns list of attachments already existing on post"""
        middleware = AttachmentsMiddleware(
            request=RequestMock(),
            mode=PostingEndpoint.EDIT,
            user=self.user,
            post=self.post,
        )

        serializer = middleware.get_serializer()

        attachments = serializer.get_new_attachments(middleware.user, [1, 2, 3])
        self.assertEqual(attachments, [])

        attachment = self.mock_attachment()
        attachments = serializer.get_new_attachments(middleware.user, [attachment.pk])
        self.assertEqual(attachments, [attachment])

        # only own orphaned attachments may be assigned to posts
        other_user_attachment = self.mock_attachment(user=False)
        attachments = serializer.get_new_attachments(middleware.user, [other_user_attachment.pk])
        self.assertEqual(attachments, [])