diff options
author | lambadalambda <gitgud@rogerbraun.net> | 2017-09-11 15:16:49 -0400 |
---|---|---|
committer | lambadalambda <gitgud@rogerbraun.net> | 2017-09-11 15:16:49 -0400 |
commit | 2b21c05105d550d09d85807246be696a1aed4b32 (patch) | |
tree | d389818eff185ca584449f2fb47ad7c1254fa3af /test/web/mastodon_api | |
parent | 95aa6a3c651fed9810889d3446f2a1d710efb55e (diff) | |
parent | f0d41a3abf3e584c90c96644f73d533ea0680237 (diff) | |
download | pleroma-2b21c05105d550d09d85807246be696a1aed4b32.tar.gz pleroma-2b21c05105d550d09d85807246be696a1aed4b32.zip |
Merge branch 'oauth2' into 'develop'
Mastodon API
See merge request !27
Diffstat (limited to 'test/web/mastodon_api')
-rw-r--r-- | test/web/mastodon_api/account_view_test.exs | 42 | ||||
-rw-r--r-- | test/web/mastodon_api/mastodon_api_controller_test.exs | 184 | ||||
-rw-r--r-- | test/web/mastodon_api/status_view_test.exs | 77 |
3 files changed, 303 insertions, 0 deletions
diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs new file mode 100644 index 000000000..59fac6d95 --- /dev/null +++ b/test/web/mastodon_api/account_view_test.exs @@ -0,0 +1,42 @@ +defmodule Pleroma.Web.MastodonAPI.AccountViewTest do + use Pleroma.DataCase + import Pleroma.Factory + alias Pleroma.Web.MastodonAPI.AccountView + + test "Represent a user account" do + user = insert(:user, %{info: %{"note_count" => 5, "follower_count" => 3}}) + + expected = %{ + id: user.id, + username: user.nickname, + acct: user.nickname, + display_name: user.name, + locked: false, + created_at: user.inserted_at, + followers_count: 3, + following_count: 0, + statuses_count: 5, + note: user.bio, + url: user.ap_id, + avatar: "https://placehold.it/48x48", + avatar_static: "https://placehold.it/48x48", + header: "https://placehold.it/700x335", + header_static: "https://placehold.it/700x335" + } + + assert expected == AccountView.render("account.json", %{user: user}) + end + + test "Represent a smaller mention" do + user = insert(:user) + + expected = %{ + id: user.id, + acct: user.nickname, + username: user.nickname, + url: user.ap_id + } + + assert expected == AccountView.render("mention.json", %{user: user}) + end +end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs new file mode 100644 index 000000000..e87430d3f --- /dev/null +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -0,0 +1,184 @@ +defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do + use Pleroma.Web.ConnCase + + alias Pleroma.Web.TwitterAPI.TwitterAPI + alias Pleroma.{Repo, User, Activity} + alias Pleroma.Web.{OStatus, CommonAPI} + + import Pleroma.Factory + + test "the home timeline", %{conn: conn} do + user = insert(:user) + following = insert(:user) + + {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"}) + + conn = conn + |> assign(:user, user) + |> get("/api/v1/timelines/home") + + assert length(json_response(conn, 200)) == 0 + + {:ok, user} = User.follow(user, following) + + conn = build_conn() + |> assign(:user, user) + |> get("/api/v1/timelines/home") + + assert [%{"content" => "test"}] = json_response(conn, 200) + end + + test "the public timeline", %{conn: conn} do + following = insert(:user) + + {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"}) + {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873") + + conn = conn + |> get("/api/v1/timelines/public") + + assert length(json_response(conn, 200)) == 2 + + conn = build_conn() + |> get("/api/v1/timelines/public", %{"local" => "True"}) + + assert [%{"content" => "test"}] = json_response(conn, 200) + end + + test "posting a status", %{conn: conn} do + user = insert(:user) + + conn = conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{"status" => "cofe"}) + + assert %{"content" => "cofe", "id" => id} = json_response(conn, 200) + assert Repo.get(Activity, id) + end + + test "replying to a status", %{conn: conn} do + user = insert(:user) + + {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"}) + + conn = conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id}) + + assert %{"content" => "xD", "id" => id} = json_response(conn, 200) + + activity = Repo.get(Activity, id) + + assert activity.data["context"] == replied_to.data["context"] + assert activity.data["object"]["inReplyToStatusId"] == replied_to.id + end + + test "verify_credentials", %{conn: conn} do + user = insert(:user) + + conn = conn + |> assign(:user, user) + |> get("/api/v1/accounts/verify_credentials") + + assert %{"id" => id} = json_response(conn, 200) + assert id == user.id + end + + test "get a status", %{conn: conn} do + activity = insert(:note_activity) + + conn = conn + |> get("/api/v1/statuses/#{activity.id}") + + assert %{"id" => id} = json_response(conn, 200) + assert id == activity.id + end + + describe "deleting a status" do + test "when you created it", %{conn: conn} do + activity = insert(:note_activity) + author = User.get_by_ap_id(activity.data["actor"]) + + conn = conn + |> assign(:user, author) + |> delete("/api/v1/statuses/#{activity.id}") + + assert %{} = json_response(conn, 200) + + assert Repo.get(Activity, activity.id) == nil + end + + test "when you didn't create it", %{conn: conn} do + activity = insert(:note_activity) + user = insert(:user) + + conn = conn + |> assign(:user, user) + |> delete("/api/v1/statuses/#{activity.id}") + + assert %{"error" => _} = json_response(conn, 403) + + assert Repo.get(Activity, activity.id) == activity + end + end + + describe "reblogging" do + test "reblogs and returns the reblogged status", %{conn: conn} do + activity = insert(:note_activity) + user = insert(:user) + + conn = conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/reblog") + + assert %{"id" => id, "reblogged" => true, "reblogs_count" => 1} = json_response(conn, 200) + assert activity.id == id + end + end + + describe "favoriting" do + test "favs a status and returns it", %{conn: conn} do + activity = insert(:note_activity) + user = insert(:user) + + conn = conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/favourite") + + assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200) + assert activity.id == id + end + end + + describe "unfavoriting" do + test "unfavorites a status and returns it", %{conn: conn} do + activity = insert(:note_activity) + user = insert(:user) + + {:ok, _, _} = CommonAPI.favorite(activity.id, user) + + conn = conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/unfavourite") + + assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = json_response(conn, 200) + assert activity.id == id + end + end + + describe "user timelines" do + test "gets a users statuses", %{conn: conn} do + _note = insert(:note_activity) + note_two = insert(:note_activity) + + user = User.get_by_ap_id(note_two.data["actor"]) + + conn = conn + |> get("/api/v1/accounts/#{user.id}/statuses") + + assert [%{"id" => id}] = json_response(conn, 200) + + assert id == note_two.id + end + end +end diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs new file mode 100644 index 000000000..a12fc8244 --- /dev/null +++ b/test/web/mastodon_api/status_view_test.exs @@ -0,0 +1,77 @@ +defmodule Pleroma.Web.MastodonAPI.StatusViewTest do + use Pleroma.DataCase + + alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} + alias Pleroma.{User, Object} + alias Pleroma.Web.OStatus + import Pleroma.Factory + + test "a note activity" do + note = insert(:note_activity) + user = User.get_cached_by_ap_id(note.data["actor"]) + + status = StatusView.render("status.json", %{activity: note}) + + expected = %{ + id: note.id, + uri: note.data["object"]["id"], + url: note.data["object"]["external_id"], + account: AccountView.render("account.json", %{user: user}), + in_reply_to_id: nil, + in_reply_to_account_id: nil, + reblog: nil, + content: HtmlSanitizeEx.basic_html(note.data["object"]["content"]), + created_at: note.data["object"]["published"], + reblogs_count: 0, + favourites_count: 0, + reblogged: false, + favourited: false, + muted: false, + sensitive: false, + spoiler_text: "", + visibility: "public", + media_attachments: [], + mentions: [], + tags: [], + application: nil, + language: nil + } + + assert status == expected + end + + test "contains mentions" do + incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml") + user = insert(:user, %{ap_id: "https://pleroma.soykaf.com/users/lain"}) + + {:ok, [activity]} = OStatus.handle_incoming(incoming) + + status = StatusView.render("status.json", %{activity: activity}) + + assert status.mentions == [AccountView.render("mention.json", %{user: user})] + end + + test "attachments" do + incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml") + object = %{ + "type" => "Image", + "url" => [ + %{ + "mediaType" => "image/png", + "href" => "someurl" + } + ], + "uuid" => 6 + } + + expected = %{ + id: 6, + type: "image", + url: "someurl", + remote_url: "someurl", + preview_url: "someurl" + } + + assert expected == StatusView.render("attachment.json", %{attachment: object}) + end +end |