Merge branch 'develop' into fix/csp-for-captcha
[akkoma] / test / upload_test.exs
index 32c6977d1a05c5a4c5590142941bc3f209b1dda9..b06b54487e152dc397e3127930e481cefbaedace 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.UploadTest do
@@ -54,6 +54,7 @@ defmodule Pleroma.UploadTest do
                 %{
                   "name" => "image.jpg",
                   "type" => "Document",
+                  "mediaType" => "image/jpeg",
                   "url" => [
                     %{
                       "href" => "http://localhost:4001/media/post-process-file.jpg",
@@ -106,7 +107,8 @@ defmodule Pleroma.UploadTest do
   describe "Storing a file with the Local uploader" do
     setup [:ensure_local_uploader]
 
-    test "returns a media url" do
+    test "does not allow descriptions longer than the post limit" do
+      clear_config([:instance, :description_limit], 2)
       File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
 
       file = %Plug.Upload{
@@ -115,16 +117,10 @@ defmodule Pleroma.UploadTest do
         filename: "image.jpg"
       }
 
-      {:ok, data} = Upload.store(file)
-
-      assert %{"url" => [%{"href" => url}]} = data
-
-      assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
+      {:error, :description_too_long} = Upload.store(file, description: "123")
     end
 
-    test "returns a media url with configured base_url" do
-      base_url = "https://cache.pleroma.social"
-
+    test "returns a media url" do
       File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
 
       file = %Plug.Upload{
@@ -133,11 +129,11 @@ defmodule Pleroma.UploadTest do
         filename: "image.jpg"
       }
 
-      {:ok, data} = Upload.store(file, base_url: base_url)
+      {:ok, data} = Upload.store(file)
 
       assert %{"url" => [%{"href" => url}]} = data
 
-      assert String.starts_with?(url, base_url <> "/media/")
+      assert String.starts_with?(url, Pleroma.Web.base_url() <> "/media/")
     end
 
     test "copies the file to the configured folder with deduping" do
@@ -266,4 +262,26 @@ defmodule Pleroma.UploadTest do
                "%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
     end
   end
+
+  describe "Setting a custom base_url for uploaded media" do
+    setup do: clear_config([Pleroma.Upload, :base_url], "https://cache.pleroma.social")
+
+    test "returns a media url with configured base_url" do
+      base_url = Pleroma.Config.get([Pleroma.Upload, :base_url])
+
+      File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
+
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image_tmp.jpg"),
+        filename: "image.jpg"
+      }
+
+      {:ok, data} = Upload.store(file, base_url: base_url)
+
+      assert %{"url" => [%{"href" => url}]} = data
+
+      refute String.starts_with?(url, base_url <> "/media/")
+    end
+  end
 end