Merge branch 'mastodon-notification-endpoints' into 'develop'
authorlambda <pleromagit@rogerbraun.net>
Sun, 12 Nov 2017 11:08:06 +0000 (11:08 +0000)
committerlambda <pleromagit@rogerbraun.net>
Sun, 12 Nov 2017 11:08:06 +0000 (11:08 +0000)
MastoAPI: Add notification endpoints get, clear and dismiss.

Closes #42

See merge request pleroma/pleroma!13

16 files changed:
lib/pleroma/application.ex
lib/pleroma/upload.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/endpoint.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/mastodon_api/mastodon_socket.ex [new file with mode: 0644]
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
lib/pleroma/web/oauth/oauth_controller.ex
lib/pleroma/web/router.ex
lib/pleroma/web/streamer.ex [new file with mode: 0644]
lib/transports.ex [new file with mode: 0644]
test/upload_test.exs
test/web/mastodon_api/account_view_test.exs
test/web/mastodon_api/mastodon_api_controller_test.exs
test/web/mastodon_api/status_view_test.exs

index 1f0a055681cab7698c8f390d95cfd27ea44fb981..5422cbc28c0ca16cfc81cd0c848693cc524888b9 100644 (file)
@@ -19,7 +19,8 @@ defmodule Pleroma.Application do
                          ttl_interval: 1000,
                          limit: 2500
                        ]]),
-      worker(Pleroma.Web.Federator, [])
+      worker(Pleroma.Web.Federator, []),
+      worker(Pleroma.Web.Streamer, [])
     ]
 
     # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
index 2717377a37f4f6d1c1379fb80f6e225563742601..3567c6c8893388c76e8a90d4c3a2136375ac6785 100644 (file)
@@ -8,11 +8,18 @@ defmodule Pleroma.Upload do
     result_file = Path.join(upload_folder, file.filename)
     File.cp!(file.path, result_file)
 
+    # fix content type on some image uploads
+    content_type = if file.content_type == "application/octet-stream" do
+      get_content_type(file.path)
+    else
+      file.content_type
+    end
+
     %{
       "type" => "Image",
       "url" => [%{
         "type" => "Link",
-        "mediaType" => file.content_type,
+        "mediaType" => content_type,
         "href" => url_for(Path.join(uuid, :cow_uri.urlencode(file.filename)))
       }],
       "name" => file.filename,
@@ -53,4 +60,34 @@ defmodule Pleroma.Upload do
   defp url_for(file) do
     "#{Web.base_url()}/media/#{file}"
   end
+
+  def get_content_type(file) do
+    match = File.open(file, [:read], fn(f) ->
+      case IO.binread(f, 8) do
+        <<0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a>> ->
+          "image/png"
+        <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
+          "image/gif"
+        <<0xff, 0xd8, 0xff, _, _, _, _, _>> ->
+          "image/jpeg"
+        <<0x1a, 0x45, 0xdf, 0xa3, _, _, _, _>> ->
+          "video/webm"
+        <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
+          "video/mp4"
+        <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
+          "audio/mpeg"
+        <<0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
+          "audio/ogg"
+        <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
+          "audio/wav"
+        _ ->
+          "application/octet-stream"
+      end
+    end)
+
+    case match do
+      {:ok, type} -> type
+      _e -> "application/octet-stream"
+    end
+  end
 end
index 1624c6545b31e0af5fd1a7683431a3c1eb13a751..35536a1e41cb5530886a10fbb8a6ced31c873c97 100644 (file)
@@ -22,6 +22,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional),
          {:ok, activity} <- insert(create_data, local),
          :ok <- maybe_federate(activity) do
+      if activity.data["type"] == "Create" and Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do
+        Pleroma.Web.Streamer.stream("public", activity)
+      end
       {:ok, activity}
     end
   end
index a1b4108cd6e6f99fc2a3cf8abffc70889b83babc..dc1ba2a05c165fa5524321aa97c20b52bd0a992e 100644 (file)
@@ -2,6 +2,7 @@ defmodule Pleroma.Web.Endpoint do
   use Phoenix.Endpoint, otp_app: :pleroma
 
   socket "/socket", Pleroma.Web.UserSocket
+  socket "/api/v1", Pleroma.Web.MastodonAPI.MastodonSocket
 
   # Serve at "/" the static files from "priv/static" directory.
   #
index d95b18315fefb04f5cbb9092ecc0fbf1c1e4f0a0..9f50dc59615a2f49b7625a5476ca03bd418ec224 100644 (file)
@@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.TwitterAPI.TwitterAPI
-  alias Pleroma.Web.CommonAPI
+  alias Pleroma.Web.{CommonAPI, OStatus}
   import Ecto.Query
   import Logger
 
@@ -23,6 +23,57 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     end
   end
 
+  def update_credentials(%{assigns: %{user: user}} = conn, params) do
+    params = if bio = params["note"] do
+      Map.put(params, "bio", bio)
+    else
+      params
+    end
+
+    params = if name = params["display_name"] do
+      Map.put(params, "name", name)
+    else
+      params
+    end
+
+    user = if avatar = params["avatar"] do
+      with %Plug.Upload{} <- avatar,
+           {:ok, object} <- ActivityPub.upload(avatar),
+           change = Ecto.Changeset.change(user, %{avatar: object.data}),
+           {:ok, user} = Repo.update(change) do
+        user
+      else
+        _e -> user
+      end
+    else
+      user
+    end
+
+    user = if banner = params["header"] do
+      with %Plug.Upload{} <- banner,
+           {:ok, object} <- ActivityPub.upload(banner),
+           new_info <- Map.put(user.info, "banner", object.data),
+           change <- User.info_changeset(user, %{info: new_info}),
+           {:ok, user} <- Repo.update(change) do
+        user
+      else
+        _e -> user
+      end
+    else
+      user
+    end
+
+    with changeset <- User.update_changeset(user, params),
+         {:ok, user} <- Repo.update(changeset) do
+      json conn, AccountView.render("account.json", %{user: user})
+    else
+      _e ->
+        conn
+        |> put_status(403)
+        |> json(%{error: "Invalid request"})
+    end
+  end
+
   def verify_credentials(%{assigns: %{user: user}} = conn, params) do
     account = AccountView.render("account.json", %{user: user})
     json(conn, account)
@@ -373,11 +424,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
     accounts = User.search(query, params["resolve"] == "true")
 
+    fetched = if Regex.match?(~r/https?:/, query) do
+      with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
+        activities
+      else
+        _e -> []
+      end
+    end || []
+
     q = from a in Activity,
       where: fragment("?->>'type' = 'Create'", a.data),
       where: fragment("to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)", a.data, ^query),
       limit: 20
-    statuses = Repo.all(q)
+    statuses = Repo.all(q) ++ fetched
 
     res = %{
       "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex
new file mode 100644 (file)
index 0000000..f9c8cec
--- /dev/null
@@ -0,0 +1,34 @@
+defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
+  use Phoenix.Socket
+
+  transport :streaming, Phoenix.Transports.WebSocket.Raw,
+    timeout: :infinity # We never receive data.
+
+  def connect(params, socket) do
+    if params["stream"] == "public" do
+      socket = socket
+      |> assign(:topic, params["stream"])
+      Pleroma.Web.Streamer.add_socket(params["stream"], socket)
+      {:ok, socket}
+    else
+      :error
+    end
+  end
+
+  def id(socket), do: nil
+
+  def handle(:text, message, state) do
+    IO.inspect message
+    #| :ok
+    #| state
+    #| {:text, message}
+    #| {:text, message, state}
+    #| {:close, "Goodbye!"}
+    {:text, message}
+  end
+
+  def handle(:closed, reason, %{socket: socket}) do
+    topic = socket.assigns[:topic]
+    Pleroma.Web.Streamer.remove_socket(topic, socket)
+  end
+end
index cf97ab746858900880060f6fd2f6b197fbdcd004..16322cf219ea884654da118f32ec9ef991299a66 100644 (file)
@@ -18,7 +18,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     header = image_url(user.info["banner"]) || "https://placehold.it/700x335"
 
     %{
-      id: user.id,
+      id: to_string(user.id),
       username: hd(String.split(user.nickname, "@")),
       acct: user.nickname,
       display_name: user.name,
@@ -43,7 +43,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
   def render("mention.json", %{user: user}) do
     %{
-      id: user.id,
+      id: to_string(user.id),
       acct: user.nickname,
       username: hd(String.split(user.nickname, "@")),
       url: user.ap_id
@@ -52,7 +52,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
   def render("relationship.json", %{user: user, target: target}) do
     %{
-      id: target.id,
+      id: to_string(target.id),
       following: User.following?(user, target),
       followed_by: User.following?(target, user),
       blocking: User.blocks?(user, target),
index 09a2ca404a39515e2276ad32024551362838c02e..d97b2acb4c502d712c475416893b9bc17e8c95ac 100644 (file)
@@ -45,7 +45,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         name: "Web",
         website: nil
       },
-      language: nil
+      language: nil,
+      emojis: []
     }
   end
 
index 841df8c32d24d1a5f4b96bf2706f568fd4e61441..e8483dec061ee0338678c13fdaca9c4c553eb34d 100644 (file)
@@ -25,7 +25,8 @@ defmodule Pleroma.Web.OAuth.OAuthController do
           auth: auth
         }
       else
-        url = "#{redirect_uri}?code=#{auth.token}"
+        connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?"
+        url = "#{redirect_uri}#{connector}code=#{auth.token}"
         url = if params["state"] do
           url <> "&state=#{params["state"]}"
         else
index efd37ede20821aac0b9c580adb8e8777c7b794fc..637c300c8ca11f25a3a08e229df0fbfa71ab1add 100644 (file)
@@ -53,6 +53,7 @@ defmodule Pleroma.Web.Router do
   scope "/api/v1", Pleroma.Web.MastodonAPI do
     pipe_through :authenticated_api
 
+    patch "/accounts/update_credentials", MastodonAPIController, :update_credentials
     get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
     get "/accounts/relationships", MastodonAPIController, :relationships
     get "/accounts/search", MastodonAPIController, :account_search
diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex
new file mode 100644 (file)
index 0000000..3a7b917
--- /dev/null
@@ -0,0 +1,76 @@
+defmodule Pleroma.Web.Streamer do
+  use GenServer
+  require Logger
+  import Plug.Conn
+
+  def start_link do
+    spawn(fn ->
+      Process.sleep(1000 * 30) # 30 seconds
+      GenServer.cast(__MODULE__, %{action: :ping})
+    end)
+    GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
+  end
+
+  def add_socket(topic, socket) do
+    GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
+  end
+
+  def remove_socket(topic, socket) do
+    GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
+  end
+
+  def stream(topic, item) do
+    GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
+  end
+
+  def handle_cast(%{action: :ping}, topics) do
+    Map.values(topics)
+    |> List.flatten
+    |> Enum.each(fn (socket) ->
+      Logger.debug("Sending keepalive ping")
+      send socket.transport_pid, {:text, ""}
+    end)
+    spawn(fn ->
+      Process.sleep(1000 * 30) # 30 seconds
+      GenServer.cast(__MODULE__, %{action: :ping})
+    end)
+    {:noreply, topics}
+  end
+
+  def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
+    Logger.debug("Trying to push to #{topic}")
+    Logger.debug("Pushing item to #{topic}")
+    Enum.each(topics[topic] || [], fn (socket) ->
+      json = %{
+        event: "update",
+        payload: Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: item) |> Poison.encode!
+      } |> Poison.encode!
+
+      send socket.transport_pid, {:text, json}
+    end)
+    {:noreply, topics}
+  end
+
+  def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
+    sockets_for_topic = sockets[topic] || []
+    sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
+    sockets = Map.put(sockets, topic, sockets_for_topic)
+    Logger.debug("Got new conn for #{topic}")
+    IO.inspect(sockets)
+    {:noreply, sockets}
+  end
+
+  def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
+    sockets_for_topic = sockets[topic] || []
+    sockets_for_topic = List.delete(sockets_for_topic, socket)
+    sockets = Map.put(sockets, topic, sockets_for_topic)
+    Logger.debug("Removed conn for #{topic}")
+    IO.inspect(sockets)
+    {:noreply, sockets}
+  end
+
+  def handle_cast(m, state) do
+    IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}")
+    {:noreply, state}
+  end
+end
diff --git a/lib/transports.ex b/lib/transports.ex
new file mode 100644 (file)
index 0000000..5600a4f
--- /dev/null
@@ -0,0 +1,77 @@
+defmodule Phoenix.Transports.WebSocket.Raw do
+  import Plug.Conn, only: [
+    fetch_query_params: 1,
+    send_resp: 3
+  ]
+  alias Phoenix.Socket.Transport
+
+  def default_config do
+    [
+      timeout: 60_000,
+      transport_log: false,
+      cowboy: Phoenix.Endpoint.CowboyWebSocket
+    ]
+  end
+
+  def init(%Plug.Conn{method: "GET"} = conn, {endpoint, handler, transport}) do
+    {_, opts} = handler.__transport__(transport)
+
+    conn = conn
+    |> fetch_query_params
+    |> Transport.transport_log(opts[:transport_log])
+    |> Transport.force_ssl(handler, endpoint, opts)
+    |> Transport.check_origin(handler, endpoint, opts)
+
+    case conn do
+      %{halted: false} = conn ->
+        case Transport.connect(endpoint, handler, transport, __MODULE__, nil, conn.params) do
+          {:ok, socket} ->
+            {:ok, conn, {__MODULE__, {socket, opts}}}
+          :error ->
+            send_resp(conn, :forbidden, "")
+            {:error, conn}
+        end
+      _ ->
+        {:error, conn}
+    end
+  end
+
+  def init(conn, _) do
+    send_resp(conn, :bad_request, "")
+    {:error, conn}
+  end
+
+  def ws_init({socket, config}) do
+    Process.flag(:trap_exit, true)
+    {:ok, %{socket: socket}, config[:timeout]}
+  end
+
+  def ws_handle(op, data, state) do
+    state.socket.handler
+    |> apply(:handle, [op, data, state])
+    |> case do
+      {op, data} ->
+        {:reply, {op, data}, state}
+      {op, data, state} ->
+        {:reply, {op, data}, state}
+      %{} = state ->
+        {:ok, state}
+      _ ->
+        {:ok, state}
+    end
+  end
+
+  def ws_info({op, data} = tuple, state) do
+    {:reply, tuple, state}
+  end
+
+  def ws_info(_tuple, state), do: {:ok, state}
+
+  def ws_close(state) do
+    ws_handle(:closed, :normal, state)
+  end
+
+  def ws_terminate(reason, state) do
+    ws_handle(:closed, reason, state)
+  end
+end
index 71041e83c4ab0d55913f17ce66c13ef8622858ea..f90c4d7137e3686c48b7ad939172e73b8c67481f 100644 (file)
@@ -9,5 +9,17 @@ defmodule Pleroma.UploadTest do
       assert data["name"] == "an [image.jpg"
       assert List.first(data["url"])["href"] == "http://localhost:4001/media/#{data["uuid"]}/an%20%5Bimage.jpg"
     end
+
+    test "fixes an incorrect content type" do
+      file = %Plug.Upload{content_type: "application/octet-stream", path: Path.absname("test/fixtures/image.jpg"), filename: "an [image.jpg"}
+      data = Upload.store(file)
+      assert hd(data["url"])["mediaType"] == "image/jpeg"
+    end
+
+    test "does not modify a valid content type" do
+      file = %Plug.Upload{content_type: "image/png", path: Path.absname("test/fixtures/image.jpg"), filename: "an [image.jpg"}
+      data = Upload.store(file)
+      assert hd(data["url"])["mediaType"] == "image/png"
+    end
   end
 end
index c62cb4f36265c9f47e172d28f09d65bc31149f3d..eccfe0b366f4cf1d877d705dbbf21a274488fb97 100644 (file)
@@ -8,7 +8,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     user = insert(:user, %{info: %{"note_count" => 5, "follower_count" => 3}, nickname: "shp@shitposter.club", inserted_at: ~N[2017-08-15 15:47:06.597036]})
 
     expected = %{
-      id: user.id,
+      id: to_string(user.id),
       username: "shp",
       acct: user.nickname,
       display_name: user.name,
@@ -37,7 +37,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     user = insert(:user)
 
     expected = %{
-      id: user.id,
+      id: to_string(user.id),
       acct: user.nickname,
       username: user.nickname,
       url: user.ap_id
@@ -54,7 +54,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     {:ok, user} = User.block(user, other_user)
 
     expected = %{
-      id: other_user.id,
+      id: to_string(other_user.id),
       following: true,
       followed_by: false,
       blocking: true,
index e876b0af44b6cc375d9960c09d8dd619bbbe9ca6..f506d56a132f0c11c5d6f4aa978f81fbeb08aede 100644 (file)
@@ -81,7 +81,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> get("/api/v1/accounts/verify_credentials")
 
     assert %{"id" => id} = json_response(conn, 200)
-    assert id == user.id
+    assert id == to_string(user.id)
   end
 
   test "get a status", %{conn: conn} do
@@ -263,7 +263,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
       assert [relationship] = json_response(conn, 200)
 
-      assert other_user.id == relationship["id"]
+      assert to_string(other_user.id) == relationship["id"]
     end
   end
 
@@ -274,7 +274,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> get("/api/v1/accounts/#{user.id}")
 
     assert %{"id" => id} = json_response(conn, 200)
-    assert id == user.id
+    assert id == to_string(user.id)
 
     conn = build_conn()
     |> get("/api/v1/accounts/-1")
@@ -319,7 +319,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> get("/api/v1/accounts/#{other_user.id}/followers")
 
     assert [%{"id" => id}] = json_response(conn, 200)
-    assert id = user.id
+    assert id == to_string(user.id)
   end
 
   test "getting following", %{conn: conn} do
@@ -331,7 +331,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> get("/api/v1/accounts/#{user.id}/following")
 
     assert [%{"id" => id}] = json_response(conn, 200)
-    assert id = other_user.id
+    assert id == to_string(other_user.id)
   end
 
   test "following / unfollowing a user", %{conn: conn} do
@@ -357,7 +357,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> post("/api/v1/follows", %{"uri" => other_user.nickname})
 
     assert %{"id" => id} = json_response(conn, 200)
-    assert id == other_user.id
+    assert id == to_string(other_user.id)
   end
 
   test "blocking / unblocking a user", %{conn: conn} do
@@ -388,7 +388,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> assign(:user, user)
     |> get("/api/v1/blocks")
 
-    other_user_id = other_user.id
+    other_user_id = to_string(other_user.id)
     assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
   end
 
@@ -403,7 +403,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}")
 
       assert %{"id" => id} = json_response(conn, 200)
-      assert id == other_user.id
+      assert id == to_string(other_user.id)
     end)
   end
 
@@ -430,7 +430,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     |> get("/api/v1/accounts/search", %{"q" => "2hu"})
 
     assert [account] = json_response(conn, 200)
-    assert account["id"] == user_three.id
+    assert account["id"] == to_string(user_three.id)
   end
 
   test "search", %{conn: conn} do
@@ -447,7 +447,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert results = json_response(conn, 200)
 
     [account] = results["accounts"]
-    assert account["id"] == user_three.id
+    assert account["id"] == to_string(user_three.id)
 
     assert results["hashtags"] == []
 
@@ -455,6 +455,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert status["id"] == to_string(activity.id)
   end
 
+  test "search fetches remote statuses", %{conn: conn} do
+    conn = conn
+    |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
+    assert results = json_response(conn, 200)
+
+    [status] = results["statuses"]
+    assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
+  end
+
   test "search fetches remote accounts", %{conn: conn} do
     conn = conn
     |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})
@@ -480,4 +489,54 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert [status] = json_response(conn, 200)
     assert status["id"] == to_string(activity.id)
   end
+
+  describe "updating credentials" do
+    test "updates the user's bio" do
+      user = insert(:user)
+
+      conn = conn
+      |> assign(:user, user)
+      |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"})
+
+      assert user = json_response(conn, 200)
+      assert user["note"] == "I drink #cofe"
+    end
+
+    test "updates the user's name" do
+      user = insert(:user)
+
+      conn = conn
+      |> assign(:user, user)
+      |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"})
+
+      assert user = json_response(conn, 200)
+      assert user["display_name"] == "markorepairs"
+    end
+
+    test "updates the user's avatar" do
+      user = insert(:user)
+
+      new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
+
+      conn = conn
+      |> assign(:user, user)
+      |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar})
+
+      assert user = json_response(conn, 200)
+      assert user["avatar"] != "https://placehold.it/48x48"
+    end
+
+    test "updates the user's banner" do
+      user = insert(:user)
+
+      new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
+
+      conn = conn
+      |> assign(:user, user)
+      |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header})
+
+      assert user = json_response(conn, 200)
+      assert user["header"] != "https://placehold.it/700x335"
+    end
+  end
 end
index 69d86ea8235155d54ab5887378e025bde30b9cf7..601e551a9596218de1e33688b2dd2099ac0716e2 100644 (file)
@@ -103,5 +103,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
 
     assert represented[:id] == to_string(reblog.id)
     assert represented[:reblog][:id] == to_string(activity.id)
+    assert represented[:emojis] == []
   end
 end