Add Twitter API verify_credentials endpoint.
This commit is contained in:
parent
980717fdce
commit
093fd1832d
@ -1,11 +1,25 @@
|
|||||||
defmodule Pleroma.Web.Router do
|
defmodule Pleroma.Web.Router do
|
||||||
use Pleroma.Web, :router
|
use Pleroma.Web, :router
|
||||||
|
|
||||||
|
alias Pleroma.{Repo, User}
|
||||||
|
|
||||||
|
def user_fetcher(username) do
|
||||||
|
{:ok, Repo.get_by(User, %{nickname: username})}
|
||||||
|
end
|
||||||
|
|
||||||
pipeline :api do
|
pipeline :api do
|
||||||
plug :accepts, ["json"]
|
plug :accepts, ["json"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
pipeline :authenticated_api do
|
||||||
|
plug :accepts, ["json"]
|
||||||
|
plug :fetch_session
|
||||||
|
plug Pleroma.Plugs.AuthenticationPlug, fetcher: &Pleroma.Web.Router.user_fetcher/1
|
||||||
|
end
|
||||||
|
|
||||||
scope "/api", Pleroma.Web do
|
scope "/api", Pleroma.Web do
|
||||||
pipe_through :api
|
pipe_through :authenticated_api
|
||||||
|
|
||||||
|
post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
18
lib/pleroma/web/twitter_api/twitter_api_controller.ex
Normal file
18
lib/pleroma/web/twitter_api/twitter_api_controller.ex
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||||
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
|
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
|
||||||
|
|
||||||
|
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
|
||||||
|
response = user |> UserRepresenter.to_json
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> json_reply(200, response)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp json_reply(conn, status, json) do
|
||||||
|
conn
|
||||||
|
|> put_resp_content_type("application/json")
|
||||||
|
|> send_resp(status, json)
|
||||||
|
end
|
||||||
|
end
|
38
test/web/twitter_api/twitter_api_controller_test.exs
Normal file
38
test/web/twitter_api/twitter_api_controller_test.exs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
||||||
|
use Pleroma.Web.ConnCase
|
||||||
|
alias Pleroma.{User, Repo}
|
||||||
|
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
|
||||||
|
|
||||||
|
describe "POST /api/account/verify_credentials" do
|
||||||
|
setup [:valid_user]
|
||||||
|
test "without valid credentials", %{conn: conn} do
|
||||||
|
conn = post conn, "/api/account/verify_credentials.json"
|
||||||
|
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "with credentials", %{conn: conn, user: user} do
|
||||||
|
conn = conn
|
||||||
|
|> with_credentials(user.nickname, "test")
|
||||||
|
|> post("/api/account/verify_credentials.json")
|
||||||
|
|
||||||
|
assert json_response(conn, 200) == UserRepresenter.to_map(user)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp valid_user(_context) do
|
||||||
|
user = %User{
|
||||||
|
email: "test@example.org",
|
||||||
|
name: "Test Name",
|
||||||
|
nickname: "testname",
|
||||||
|
password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
|
||||||
|
bio: "A tester."
|
||||||
|
}
|
||||||
|
user = Repo.insert!(user)
|
||||||
|
[user: user]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp with_credentials(conn, username, password) do
|
||||||
|
header_content = "Basic " <> Base.encode64("#{username}:#{password}")
|
||||||
|
put_req_header(conn, "authorization", header_content)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user