Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: marcin mikołajczak <[email protected]>
  • Loading branch information
mkljczk committed Jun 1, 2024
1 parent 305a8d9 commit a7ba40b
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/pleroma/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Pleroma.Upload do
end

defp validate_description_limit(%{} = description) do
Enum.each(description, fn content ->
Enum.all?(description, fn {_, content} ->
String.length(content) <= Pleroma.Config.get([:instance, :description_limit])
end)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
|> fix_attachments()
|> CommonFixes.fix_quote_url()
|> Transmogrifier.fix_emoji()
|> Transmogrifier.fix_content_map()
|> CommonFixes.maybe_add_language()
|> CommonFixes.maybe_add_content_map()
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do

embedded_schema do
field(:name, :string)
field(:nameRendered, :string)
field(:nameMap, ObjectValidators.ContentLanguageMap)

embeds_one :replies, Replies, primary_key: false do
Expand All @@ -26,7 +25,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do

def changeset(struct, data) do
struct
|> cast(data, [:name, :nameRendered, :nameMap, :type])
|> cast(data, [:name, :nameMap, :type])
|> cast_embed(:replies, with: &replies_changeset/2)
|> validate_inclusion(:type, ["Note"])
|> validate_required([:name, :type])
Expand Down
5 changes: 5 additions & 0 deletions lib/pleroma/web/api_spec/operations/media_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ defmodule Pleroma.Web.ApiSpec.MediaOperation do
type: :string,
description: "A plain-text description of the media, for accessibility purposes."
}),
language: %Schema{
type: :string,
nullable: true,
description: "ISO 639 language code for this status."
},
focus: %Schema{
type: :string,
description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
Expand Down
6 changes: 4 additions & 2 deletions lib/pleroma/web/common_api/activity_draft.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do

defp language(%{status: status} = draft) when is_binary(status) do
detected_language =
LanguageDetector.detect(draft.status <> " " <> (draft.summary || draft.params[:summary]))
LanguageDetector.detect(
draft.status <> " " <> (draft.summary || draft.params[:summary] || draft.params[:name])
)

if MultiLanguage.good_locale_code?(detected_language) do
%__MODULE__{
Expand Down Expand Up @@ -535,7 +537,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do

defp get_source_map(%{status_map: %{} = status_map} = draft) do
%{
"content" => Map.get(status_map, draft.language),
"content" => Map.get(draft, :status),
"contentMap" => status_map
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
) do
with %Object{} = object <- Object.get_by_id(id),
:ok <- Object.authorize_access(object, user),
language = Map.get(body_params, :language, object["language"]),
language = Map.get(body_params, :language, object.data["language"]),
{_, true} <-
{:valid_locale, description_map == nil or MultiLanguage.good_locale_code?(language)},
{_, {:ok, %{}}} <- {:description_map, MultiLanguage.validate_map(description_map)},
Expand Down
18 changes: 16 additions & 2 deletions lib/pleroma/web/mastodon_api/controllers/status_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,14 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
} = conn,
_
) do
create(conn |> put_in([:private, :open_api_spex, :body_params, :status], ""), %{})
create(
put_in(
conn,
[Access.key(:private), Access.key(:open_api_spex), Access.key(:body_params), :status],
""
),
%{}
)
end

def create(
Expand All @@ -222,7 +229,14 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
} = conn,
_
) do
create(conn |> put_in([:private, :open_api_spex, :body_params, :status], ""), %{})
create(
put_in(
conn,
[Access.key(:private), Access.key(:open_api_spex), Access.key(:body_params), :status],
""
),
%{}
)
end

defp do_create(
Expand Down
2 changes: 1 addition & 1 deletion lib/pleroma/web/mastodon_api/views/poll_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule Pleroma.Web.MastodonAPI.PollView do
current_count = option["replies"]["totalItems"] || 0

{%{
title: option["nameRendered"] || name,
title: name,
title_map: option["nameMap"] || %{},
votes_count: current_count
}, current_count + count}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidatorTest
"nameMap" => name_map
}

assert %{valid?: true, changes: %{nameMap: ^name_map, nameRendered: _}} =
assert %{valid?: true, changes: %{nameMap: ^name_map, name: _}} =
QuestionOptionsValidator.changeset(%QuestionOptionsValidator{}, data)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
assert object.data["content"] == "Hi"
end

test "it works for incoming notices with a nil contentMap (firefish)" do
test "it works for incoming notices with a nil content (firefish)" do
data =
File.read!("test/fixtures/mastodon-post-activity-contentmap.json")
|> Jason.decode!()
Expand Down
3 changes: 2 additions & 1 deletion test/pleroma/web/common_api/activity_draft_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraftTest do
{:ok, draft} =
ActivityDraft.create(user, %{
status_map: %{"a" => "mew mew", "b" => "lol lol"},
spoiler_text_map: %{"a" => "mew", "b" => "lol"}
spoiler_text_map: %{"a" => "mew", "b" => "lol"},
language: "a"
})

assert %{
Expand Down
3 changes: 2 additions & 1 deletion test/pleroma/web/common_api/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
%{"a" => "bar", "c" => "2"}
],
expires_in: 600
}
},
language: "a"
})

assert %{"oneOf" => choices} = poll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/media", %{
"file" => image,
"description_map" => %{"a" => "mew", "b" => "lol"}
"description_map" => %{"a" => "mew", "b" => "lol"},
"language" => "a"
})
|> json_response_and_validate_schema(:ok)

Expand Down Expand Up @@ -119,7 +120,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v2/media", %{
"file" => image,
"description_map" => %{"a" => "mew", "b" => "lol"}
"description_map" => %{"a" => "mew", "b" => "lol"},
"language" => "a"
})
|> json_response_and_validate_schema(202)

Expand Down Expand Up @@ -261,7 +263,8 @@ defmodule Pleroma.Web.MastodonAPI.MediaControllerTest do
conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{
"description_map" => %{"a" => "test-media", "b" => "xxx"}
"description_map" => %{"a" => "test-media", "b" => "xxx"},
"language" => "a"
})
|> json_response_and_validate_schema(:ok)

Expand Down
1 change: 0 additions & 1 deletion test/pleroma/web/mastodon_api/views/poll_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
%{
"name" => "mew",
"nameMap" => %{"en" => "mew", "cmn" => "喵"},
"nameRendered" => "mew | 喵"
},
%{"name" => "mew mew", "nameMap" => %{"en" => "mew mew", "cmn" => "喵喵"}}
]
Expand Down
6 changes: 4 additions & 2 deletions test/pleroma/web/mastodon_api/views/status_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
content_map: %{"en" => "mew mew", "cmn" => "喵喵"},
spoiler_text: "mew",
spoiler_text_map: %{"en" => "mew", "cmn" => "喵"},
language: "mul",
pleroma: %{
content: %{"text/plain" => "mew mew"},
content_map: %{"text/plain" => %{"en" => "mew mew", "cmn" => "喵喵"}},
Expand All @@ -399,7 +398,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
"content" => "mew mew",
"contentMap" => %{"en" => "mew mew"},
"summary" => "mew",
"summaryMap" => %{"en" => "mew"}
"summaryMap" => %{"en" => "mew"},
"language" => "en"
}
)

Expand Down Expand Up @@ -686,6 +686,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
preview_url: "someurl",
text_url: "someurl",
description: nil,
description_map: %{},
pleroma: %{mime_type: "image/png"},
meta: %{original: %{width: 200, height: 100, aspect: 2}},
blurhash: "UJJ8X[xYW,%Jtq%NNFbXB5j]IVM|9GV=WHRn"
Expand Down Expand Up @@ -720,6 +721,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
expected = %{
blurhash: nil,
description: "they have played us for absolute fools.",
description_map: %{},
id: "1638338801",
pleroma: %{mime_type: "image/png", name: "fool.jpeg"},
preview_url: "someurl",
Expand Down

0 comments on commit a7ba40b

Please sign in to comment.