def test_download_shared_document(self):
        doc = setup_document(self.org, self.user)
        doc_share = setup_document_share(doc)

        response = self.client.get(f"/app/share/{doc_share.pid}/download")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["content-type"], "application/octet-stream")
    def test_cant_view_expired_shared_document(self):
        doc = setup_document(self.org, self.user)
        doc_share = setup_document_share(doc,
                                         expire_at=datetime.datetime.now() -
                                         datetime.timedelta(hours=1))

        response = self.client.get(f"/app/share/{doc_share.pid}")
        self.assertEqual(response.status_code, 404)
    def test_cant_get_shares_for_not_owned_doc(self):
        org_bis = setup_org(name=tv.ORG_NAME_2, slug=tv.ORG_SLUG_2)
        admin_bis = setup_admin(org_bis, tv.ADMIN2_EMAIL)
        doc_bis = setup_document(org_bis, admin_bis)
        share_doc_bis = setup_document_share(doc_bis)

        client_get = self.client.get(
            f"/app/api/v1/documents/{share_doc_bis.pid}/share", format="json"
        )

        self.assertEqual(client_get.status_code, status.HTTP_404_NOT_FOUND)
    def test_get_document_share_links(self):
        share_doc = setup_document_share(self.doc)

        client_get = self.client.get(
            f"/app/api/v1/documents/{self.doc.pid}/share", format="json"
        )

        self.assertEqual(client_get.status_code, status.HTTP_200_OK)
        client_doc_shares = client_get.data
        self.assertEqual(client_doc_shares["count"], 1)
        self.assertEqual(client_doc_shares["results"][0]["pid"], str(share_doc.pid))
    def test_cant_view_unshared_document(self):
        doc = setup_document(self.org, self.user)
        doc_share = setup_document_share(doc)

        response = self.client.get(f"/app/share/{doc_share.pid}")
        self.assertEqual(response.status_code, 200)

        doc_share.delete()

        response = self.client.get(f"/app/share/{doc_share.pid}")
        self.assertEqual(response.status_code, 404)
    def test_unshare_document(self):
        share_doc = setup_document_share(self.doc)

        client_delete = self.client.delete(
            f"/app/api/v1/documents/{self.doc.pid}/share/{share_doc.pid}",
            format="json",
        )

        self.assertEqual(client_delete.status_code, status.HTTP_204_NO_CONTENT)

        with self.assertRaises(core.models.FTLDocumentSharing.DoesNotExist):
            FTLDocumentSharing.objects.get(pid=share_doc.pid)
    def test_update_document_share_link(self):
        share_doc = setup_document_share(self.doc)

        client_patch = self.client.patch(
            f"/app/api/v1/documents/{self.doc.pid}/share/{share_doc.pid}",
            {"note": "fakeNote", "expire_at": "2019-11-18T00:42:42.242424Z"},
            format="json",
        )

        self.assertEqual(client_patch.status_code, status.HTTP_200_OK)
        client_doc_shares = client_patch.data
        self.assertEqual(client_doc_shares["pid"], str(share_doc.pid))
        self.assertEqual(client_doc_shares["note"], "fakeNote")
        self.assertEqual(
            client_doc_shares["expire_at"], "2019-11-18T01:42:42.242424+01:00"
        )
    def setUp(self, **kwargs):
        # orgs, admin, users are already created
        super().setUp()
        self.admin_org = setup_org(name="admin-org", slug="admin-org")
        self.admin = setup_admin(self.admin_org)
        self.user1_org = setup_org(name=tv.ORG_NAME_1, slug=tv.ORG_SLUG_1)
        self.user1 = setup_user(self.user1_org,
                                email=tv.USER1_EMAIL,
                                password=tv.USER1_PASS)
        self.user2_org = setup_org(name=tv.ORG_NAME_2, slug=tv.ORG_SLUG_2)
        self.user2 = setup_user(self.user2_org,
                                email=tv.USER2_EMAIL,
                                password=tv.USER2_PASS)

        # admin, user1 and user2 have added documents, some documents are shared
        self.admin_resources = {}
        self.admin_resources["doc1"] = setup_document(
            self.admin_org,
            ftl_user=self.admin,
            binary=setup_temporary_file().name)
        self.admin_resources["doc2"] = setup_document(
            self.admin_org,
            ftl_user=self.admin,
            binary=setup_temporary_file().name,
        )
        self.admin_resources["doc3"] = setup_document(
            self.admin_org,
            ftl_user=self.admin,
            binary=setup_temporary_file().name,
        )
        self.admin_resources["share_links"] = [
            setup_document_share(self.admin_resources["doc3"])
        ]

        self.user1_resources = {}
        self.user1_resources["doc1"] = setup_document(
            self.user1_org,
            ftl_user=self.user1,
            binary=setup_temporary_file().name)
        self.user1_resources["doc2"] = setup_document(
            self.user1_org,
            ftl_user=self.user1,
            binary=setup_temporary_file().name,
        )
        self.user1_resources["doc3"] = setup_document(
            self.user1_org,
            ftl_user=self.user1,
            binary=setup_temporary_file().name,
        )
        self.user1_resources["share_links"] = [
            setup_document_share(self.user1_resources["doc2"])
        ]

        self.user2_resources = {}
        self.user2_resources["doc1"] = setup_document(self.user2_org,
                                                      ftl_user=self.user2)
        self.user2_resources["doc2"] = setup_document(
            self.user2_org,
            ftl_user=self.user2,
        )
        self.user2_resources["doc3"] = setup_document(
            self.user2_org,
            ftl_user=self.user2,
        )
        self.user2_resources["share_links"] = [
            setup_document_share(self.user2_resources["doc1"])
        ]
    def test_only_office_viewer(self):
        doc = setup_document(
            self.org,
            self.user,
            title=tv.DOCUMENT_DOCX_TITLE,
            note=tv.DOCUMENT_DOCX_NOTE,
            text_content=tv.DOCUMENT_DOCX_CONTENT,
            binary=tv.DOCUMENT_DOCX_BINARY_PATH,
            file_type=
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        )
        doc_share = setup_document_share(doc)

        response = self.client.get(f"/app/share/{doc_share.pid}")

        self.assertContains(response,
                            "onlyoffice-embed-container",
                            status_code=200)

        self.assertIn("only_office_supported_ext", response.context)
        self.assertIn("only_office_config", response.context)

        self.assertIn("document", response.context["only_office_config"])
        self.assertIn("editorConfig", response.context["only_office_config"])
        self.assertIn("token", response.context["only_office_config"])

        self.assertIn("url",
                      response.context["only_office_config"]["document"])
        self.assertIn("fileType",
                      response.context["only_office_config"]["document"])
        self.assertIn("key",
                      response.context["only_office_config"]["document"])
        self.assertIn("title",
                      response.context["only_office_config"]["document"])
        self.assertIn("permissions",
                      response.context["only_office_config"]["document"])
        self.assertDictEqual(
            {
                "comment": False,
                "copy": True,
                "download": True,
                "edit": False,
                "fillForms": False,
                "modifyContentControl": False,
                "modifyFilter": False,
                "print": True,
                "review": False,
            },
            response.context["only_office_config"]["document"]["permissions"],
        )

        self.assertIn("lang",
                      response.context["only_office_config"]["editorConfig"])
        self.assertIn("mode",
                      response.context["only_office_config"]["editorConfig"])
        self.assertIn("customization",
                      response.context["only_office_config"]["editorConfig"])
        self.assertDictEqual(
            {
                "autosave": False,
                "chat": False,
                "commentAuthorOnly": False,
                "comments": False,
                "compactHeader": False,
                "compactToolbar": False,
                "compatibleFeatures": False,
                "help": True,
                "hideRightMenu": False,
                "mentionShare": False,
                "plugins": False,
                "reviewDisplay": "original",
                "showReviewChanges": False,
                "spellcheck": False,
                "toolbarNoTabs": False,
                "unit": "cm",
                "zoom": -2,
            },
            response.context["only_office_config"]["editorConfig"]
            ["customization"],
        )