summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/bbs/handler_test.exs83
-rw-r--r--test/conversation/participation_test.exs89
-rw-r--r--test/conversation_test.exs137
-rw-r--r--test/repo_test.exs44
-rw-r--r--test/support/factory.ex17
-rw-r--r--test/user_test.exs8
-rw-r--r--test/web/activity_pub/activity_pub_test.exs75
-rw-r--r--test/web/mastodon_api/mastodon_api_controller_test.exs59
-rw-r--r--test/web/oauth/oauth_controller_test.exs196
9 files changed, 696 insertions, 12 deletions
diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs
new file mode 100644
index 000000000..7d5d68d11
--- /dev/null
+++ b/test/bbs/handler_test.exs
@@ -0,0 +1,83 @@
+defmodule Pleroma.BBS.HandlerTest do
+ use Pleroma.DataCase
+ alias Pleroma.Activity
+ alias Pleroma.BBS.Handler
+ alias Pleroma.Object
+ alias Pleroma.Repo
+ alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
+
+ import ExUnit.CaptureIO
+ import Pleroma.Factory
+ import Ecto.Query
+
+ test "getting the home timeline" do
+ user = insert(:user)
+ followed = insert(:user)
+
+ {:ok, user} = User.follow(user, followed)
+
+ {:ok, _first} = CommonAPI.post(user, %{"status" => "hey"})
+ {:ok, _second} = CommonAPI.post(followed, %{"status" => "hello"})
+
+ output =
+ capture_io(fn ->
+ Handler.handle_command(%{user: user}, "home")
+ end)
+
+ assert output =~ user.nickname
+ assert output =~ followed.nickname
+
+ assert output =~ "hey"
+ assert output =~ "hello"
+ end
+
+ test "posting" do
+ user = insert(:user)
+
+ output =
+ capture_io(fn ->
+ Handler.handle_command(%{user: user}, "p this is a test post")
+ end)
+
+ assert output =~ "Posted"
+
+ activity =
+ Repo.one(
+ from(a in Activity,
+ where: fragment("?->>'type' = ?", a.data, "Create")
+ )
+ )
+
+ assert activity.actor == user.ap_id
+ object = Object.normalize(activity)
+ assert object.data["content"] == "this is a test post"
+ end
+
+ test "replying" do
+ user = insert(:user)
+ another_user = insert(:user)
+
+ {:ok, activity} = CommonAPI.post(another_user, %{"status" => "this is a test post"})
+
+ output =
+ capture_io(fn ->
+ Handler.handle_command(%{user: user}, "r #{activity.id} this is a reply")
+ end)
+
+ assert output =~ "Replied"
+
+ reply =
+ Repo.one(
+ from(a in Activity,
+ where: fragment("?->>'type' = ?", a.data, "Create"),
+ where: a.actor == ^user.ap_id
+ )
+ )
+
+ assert reply.actor == user.ap_id
+ object = Object.normalize(reply)
+ assert object.data["content"] == "this is a reply"
+ assert object.data["inReplyTo"] == activity.data["object"]
+ end
+end
diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs
new file mode 100644
index 000000000..568953b07
--- /dev/null
+++ b/test/conversation/participation_test.exs
@@ -0,0 +1,89 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Conversation.ParticipationTest do
+ use Pleroma.DataCase
+ import Pleroma.Factory
+ alias Pleroma.Conversation.Participation
+ alias Pleroma.Web.CommonAPI
+
+ test "it creates a participation for a conversation and a user" do
+ user = insert(:user)
+ conversation = insert(:conversation)
+
+ {:ok, %Participation{} = participation} =
+ Participation.create_for_user_and_conversation(user, conversation)
+
+ assert participation.user_id == user.id
+ assert participation.conversation_id == conversation.id
+
+ :timer.sleep(1000)
+ # Creating again returns the same participation
+ {:ok, %Participation{} = participation_two} =
+ Participation.create_for_user_and_conversation(user, conversation)
+
+ assert participation.id == participation_two.id
+ refute participation.updated_at == participation_two.updated_at
+ end
+
+ test "recreating an existing participations sets it to unread" do
+ participation = insert(:participation, %{read: true})
+
+ {:ok, participation} =
+ Participation.create_for_user_and_conversation(
+ participation.user,
+ participation.conversation
+ )
+
+ refute participation.read
+ end
+
+ test "it marks a participation as read" do
+ participation = insert(:participation, %{read: false})
+ {:ok, participation} = Participation.mark_as_read(participation)
+
+ assert participation.read
+ end
+
+ test "it marks a participation as unread" do
+ participation = insert(:participation, %{read: true})
+ {:ok, participation} = Participation.mark_as_unread(participation)
+
+ refute participation.read
+ end
+
+ test "gets all the participations for a user, ordered by updated at descending" do
+ user = insert(:user)
+ {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
+ :timer.sleep(1000)
+ {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
+ :timer.sleep(1000)
+
+ {:ok, activity_three} =
+ CommonAPI.post(user, %{
+ "status" => "x",
+ "visibility" => "direct",
+ "in_reply_to_status_id" => activity_one.id
+ })
+
+ assert [participation_one, participation_two] = Participation.for_user(user)
+
+ object2 = Pleroma.Object.normalize(activity_two)
+ object3 = Pleroma.Object.normalize(activity_three)
+
+ assert participation_one.conversation.ap_id == object3.data["context"]
+ assert participation_two.conversation.ap_id == object2.data["context"]
+
+ # Pagination
+ assert [participation_one] = Participation.for_user(user, %{"limit" => 1})
+
+ assert participation_one.conversation.ap_id == object3.data["context"]
+
+ # With last_activity_id
+ assert [participation_one] =
+ Participation.for_user_with_last_activity_id(user, %{"limit" => 1})
+
+ assert participation_one.last_activity_id == activity_three.id
+ end
+end
diff --git a/test/conversation_test.exs b/test/conversation_test.exs
new file mode 100644
index 000000000..f3300e7d1
--- /dev/null
+++ b/test/conversation_test.exs
@@ -0,0 +1,137 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ConversationTest do
+ use Pleroma.DataCase
+ alias Pleroma.Conversation
+ alias Pleroma.Web.CommonAPI
+
+ import Pleroma.Factory
+
+ test "it creates a conversation for given ap_id" do
+ assert {:ok, %Conversation{} = conversation} =
+ Conversation.create_for_ap_id("https://some_ap_id")
+
+ # Inserting again returns the same
+ assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
+ assert conversation_two.id == conversation.id
+ end
+
+ test "public posts don't create conversations" do
+ user = insert(:user)
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
+
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
+
+ conversation = Conversation.get_for_ap_id(context)
+
+ refute conversation
+ end
+
+ test "it creates or updates a conversation and participations for a given DM" do
+ har = insert(:user)
+ jafnhar = insert(:user, local: false)
+ tridi = insert(:user)
+
+ {:ok, activity} =
+ CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
+
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
+
+ conversation =
+ Conversation.get_for_ap_id(context)
+ |> Repo.preload(:participations)
+
+ assert conversation
+
+ assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end)
+
+ assert Enum.find(conversation.participations, fn %{user_id: user_id} ->
+ jafnhar.id == user_id
+ end)
+
+ {:ok, activity} =
+ CommonAPI.post(jafnhar, %{
+ "status" => "Hey @#{har.nickname}",
+ "visibility" => "direct",
+ "in_reply_to_status_id" => activity.id
+ })
+
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
+
+ conversation_two =
+ Conversation.get_for_ap_id(context)
+ |> Repo.preload(:participations)
+
+ assert conversation_two.id == conversation.id
+
+ assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
+ har.id == user_id
+ end)
+
+ assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
+ jafnhar.id == user_id
+ end)
+
+ {:ok, activity} =
+ CommonAPI.post(tridi, %{
+ "status" => "Hey @#{har.nickname}",
+ "visibility" => "direct",
+ "in_reply_to_status_id" => activity.id
+ })
+
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
+
+ conversation_three =
+ Conversation.get_for_ap_id(context)
+ |> Repo.preload([:participations, :users])
+
+ assert conversation_three.id == conversation.id
+
+ assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
+ har.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
+ jafnhar.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
+ tridi.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+ har.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+ jafnhar.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+ tridi.id == user_id
+ end)
+ end
+
+ test "create_or_bump_for returns the conversation with participations" do
+ har = insert(:user)
+ jafnhar = insert(:user, local: false)
+
+ {:ok, activity} =
+ CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
+
+ {:ok, conversation} = Conversation.create_or_bump_for(activity)
+
+ assert length(conversation.participations) == 2
+
+ {:ok, activity} =
+ CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"})
+
+ assert {:error, _} = Conversation.create_or_bump_for(activity)
+ end
+end
diff --git a/test/repo_test.exs b/test/repo_test.exs
new file mode 100644
index 000000000..5382289c7
--- /dev/null
+++ b/test/repo_test.exs
@@ -0,0 +1,44 @@
+defmodule Pleroma.RepoTest do
+ use Pleroma.DataCase
+ import Pleroma.Factory
+
+ describe "find_resource/1" do
+ test "returns user" do
+ user = insert(:user)
+ query = from(t in Pleroma.User, where: t.id == ^user.id)
+ assert Repo.find_resource(query) == {:ok, user}
+ end
+
+ test "returns not_found" do
+ query = from(t in Pleroma.User, where: t.id == ^"9gBuXNpD2NyDmmxxdw")
+ assert Repo.find_resource(query) == {:error, :not_found}
+ end
+ end
+
+ describe "get_assoc/2" do
+ test "get assoc from preloaded data" do
+ user = %Pleroma.User{name: "Agent Smith"}
+ token = %Pleroma.Web.OAuth.Token{insert(:oauth_token) | user: user}
+ assert Repo.get_assoc(token, :user) == {:ok, user}
+ end
+
+ test "get one-to-one assoc from repo" do
+ user = insert(:user, name: "Jimi Hendrix")
+ token = refresh_record(insert(:oauth_token, user: user))
+
+ assert Repo.get_assoc(token, :user) == {:ok, user}
+ end
+
+ test "get one-to-many assoc from repo" do
+ user = insert(:user)
+ notification = refresh_record(insert(:notification, user: user))
+
+ assert Repo.get_assoc(user, :notifications) == {:ok, [notification]}
+ end
+
+ test "return error if has not assoc " do
+ token = insert(:oauth_token, user: nil)
+ assert Repo.get_assoc(token, :user) == {:error, :not_found}
+ end
+ end
+end
diff --git a/test/support/factory.ex b/test/support/factory.ex
index ea59912cf..2a2954ad6 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -5,6 +5,23 @@
defmodule Pleroma.Factory do
use ExMachina.Ecto, repo: Pleroma.Repo
+ def participation_factory do
+ conversation = insert(:conversation)
+ user = insert(:user)
+
+ %Pleroma.Conversation.Participation{
+ conversation: conversation,
+ user: user,
+ read: false
+ }
+ end
+
+ def conversation_factory do
+ %Pleroma.Conversation{
+ ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
+ }
+ end
+
def user_factory do
user = %Pleroma.User{
name: sequence(:name, &"Test テスト User #{&1}"),
diff --git a/test/user_test.exs b/test/user_test.exs
index 6d21b56f7..adc77a264 100644
--- a/test/user_test.exs
+++ b/test/user_test.exs
@@ -829,10 +829,12 @@ defmodule Pleroma.UserTest do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
- {:ok, _} = User.delete_user_activities(user)
- # TODO: Remove favorites, repeats, delete activities.
- refute Activity.get_by_id(activity.id)
+ Ecto.Adapters.SQL.Sandbox.unboxed_run(Repo, fn ->
+ {:ok, _} = User.delete_user_activities(user)
+ # TODO: Remove favorites, repeats, delete activities.
+ refute Activity.get_by_id(activity.id)
+ end)
end
test ".delete deactivates a user, all follow relationships and all create activities" do
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs
index f8e987e58..1e056b7ee 100644
--- a/test/web/activity_pub/activity_pub_test.exs
+++ b/test/web/activity_pub/activity_pub_test.exs
@@ -22,6 +22,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
:ok
end
+ describe "streaming out participations" do
+ test "it streams them out" do
+ user = insert(:user)
+ {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
+
+ {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
+
+ participations =
+ conversation.participations
+ |> Repo.preload(:user)
+
+ with_mock Pleroma.Web.Streamer,
+ stream: fn _, _ -> nil end do
+ ActivityPub.stream_out_participations(conversation.participations)
+
+ Enum.each(participations, fn participation ->
+ assert called(Pleroma.Web.Streamer.stream("participation", participation))
+ end)
+ end
+ end
+ end
+
describe "fetching restricted by visibility" do
test "it restricts by the appropriate visibility" do
user = insert(:user)
@@ -130,9 +152,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end
test "doesn't drop activities with content being null" do
+ user = insert(:user)
+
data = %{
- "ok" => true,
+ "actor" => user.ap_id,
+ "to" => [],
"object" => %{
+ "actor" => user.ap_id,
+ "to" => [],
+ "type" => "Note",
"content" => nil
}
}
@@ -148,8 +176,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end
test "inserts a given map into the activity database, giving it an id if it has none." do
+ user = insert(:user)
+
data = %{
- "ok" => true
+ "actor" => user.ap_id,
+ "to" => [],
+ "object" => %{
+ "actor" => user.ap_id,
+ "to" => [],
+ "type" => "Note",
+ "content" => "hey"
+ }
}
{:ok, %Activity{} = activity} = ActivityPub.insert(data)
@@ -159,9 +196,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
given_id = "bla"
data = %{
- "ok" => true,
"id" => given_id,
- "context" => "blabla"
+ "actor" => user.ap_id,
+ "to" => [],
+ "context" => "blabla",
+ "object" => %{
+ "actor" => user.ap_id,
+ "to" => [],
+ "type" => "Note",
+ "content" => "hey"
+ }
}
{:ok, %Activity{} = activity} = ActivityPub.insert(data)
@@ -172,26 +216,39 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
end
test "adds a context when none is there" do
+ user = insert(:user)
+
data = %{
- "id" => "some_id",
+ "actor" => user.ap_id,
+ "to" => [],
"object" => %{
- "id" => "object_id"
+ "actor" => user.ap_id,
+ "to" => [],
+ "type" => "Note",
+ "content" => "hey"
}
}
{:ok, %Activity{} = activity} = ActivityPub.insert(data)
+ object = Pleroma.Object.normalize(activity)
assert is_binary(activity.data["context"])
- assert is_binary(activity.data["object"]["context"])
+ assert is_binary(object.data["context"])
assert activity.data["context_id"]
- assert activity.data["object"]["context_id"]
+ assert object.data["context_id"]
end
test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
+ user = insert(:user)
+
data = %{
+ "actor" => user.ap_id,
+ "to" => [],
"object" => %{
+ "actor" => user.ap_id,
+ "to" => [],
"type" => "Note",
- "ok" => true
+ "content" => "hey"
}
}
diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs
index 610aa486e..505e45010 100644
--- a/test/web/mastodon_api/mastodon_api_controller_test.exs
+++ b/test/web/mastodon_api/mastodon_api_controller_test.exs
@@ -300,6 +300,65 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert status["url"] != direct.data["id"]
end
+ test "Conversations", %{conn: conn} do
+ user_one = insert(:user)
+ user_two = insert(:user)
+
+ {:ok, user_two} = User.follow(user_two, user_one)
+
+ {:ok, direct} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, _follower_only} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}!",
+ "visibility" => "private"
+ })
+
+ res_conn =
+ conn
+ |> assign(:user, user_one)
+ |> get("/api/v1/conversations")
+
+ assert response = json_response(res_conn, 200)
+
+ assert [
+ %{
+ "id" => res_id,
+ "accounts" => res_accounts,
+ "last_status" => res_last_status,
+ "unread" => unread
+ }
+ ] = response
+
+ assert length(res_accounts) == 2
+ assert is_binary(res_id)
+ assert unread == true
+ assert res_last_status["id"] == direct.id
+
+ # Apparently undocumented API endpoint
+ res_conn =
+ conn
+ |> assign(:user, user_one)
+ |> post("/api/v1/conversations/#{res_id}/read")
+
+ assert response = json_response(res_conn, 200)
+ assert length(response["accounts"]) == 2
+ assert response["last_status"]["id"] == direct.id
+ assert response["unread"] == false
+
+ # (vanilla) Mastodon frontend behaviour
+ res_conn =
+ conn
+ |> assign(:user, user_one)
+ |> get("/api/v1/statuses/#{res_last_status["id"]}/context")
+
+ assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
+ end
+
test "doesn't include DMs from blocked users", %{conn: conn} do
blocker = insert(:user)
blocked = insert(:user)
diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs
index 6e96537ec..cb6836983 100644
--- a/test/web/oauth/oauth_controller_test.exs
+++ b/test/web/oauth/oauth_controller_test.exs
@@ -12,6 +12,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
alias Pleroma.Web.OAuth.Authorization
alias Pleroma.Web.OAuth.Token
+ @oauth_config_path [:oauth2, :issue_new_refresh_token]
@session_opts [
store: :cookie,
key: "_test",
@@ -714,4 +715,199 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
refute Map.has_key?(resp, "access_token")
end
end
+
+ describe "POST /oauth/token - refresh token" do
+ setup do
+ oauth_token_config = Pleroma.Config.get(@oauth_config_path)
+
+ on_exit(fn ->
+ Pleroma.Config.get(@oauth_config_path, oauth_token_config)
+ end)
+ end
+
+ test "issues a new access token with keep fresh token" do
+ Pleroma.Config.put(@oauth_config_path, true)
+ user = insert(:user)
+ app = insert(:oauth_app, scopes: ["read", "write"])
+
+ {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
+ {:ok, token} = Token.exchange_token(app, auth)
+
+ response =
+ build_conn()
+ |> post("/oauth/token", %{
+ "grant_type" => "refresh_token",
+ "refresh_token" => token.refresh_token,
+ "client_id" => app.client_id,
+ "client_secret" => app.client_secret
+ })
+ |> json_response(200)
+
+ ap_id = user.ap_id
+
+ assert match?(
+ %{
+ "scope" => "write",
+ "token_type" => "Bearer",
+ "expires_in" => 600,
+ "access_token" => _,
+ "refresh_token" => _,
+ "me" => ^ap_id
+ },
+ response
+ )
+
+ refute Repo.get_by(Token, token: token.token)
+ new_token = Repo.get_by(Token, token: response["access_token"])
+ assert new_token.refresh_token == token.refresh_token
+ assert new_token.scopes == auth.scopes
+ assert new_token.user_id == user.id
+ assert new_token.app_id == app.id
+ end
+
+ test "issues a new access token with new fresh token" do
+ Pleroma.Config.put(@oauth_config_path, false)
+ user = insert(:user)
+ app = insert(:oauth_app, scopes: ["read", "write"])
+
+ {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
+ {:ok, token} = Token.exchange_token(app, auth)
+
+ response =
+ build_conn()
+ |> post("/oauth/token", %{
+ "grant_type" => "refresh_token",
+ "refresh_token" => token.refresh_token,
+ "client_id" => app.client_id,
+ "client_secret" => app.client_secret
+ })
+ |> json_response(200)
+
+ ap_id = user.ap_id
+
+ assert match?(
+ %{
+ "scope" => "write",
+ "token_type" => "Bearer",
+ "expires_in" => 600,
+ "access_token" => _,
+ "refresh_token" => _,
+ "me" => ^ap_id
+ },
+ response
+ )
+
+ refute Repo.get_by(Token, token: token.token)
+ new_token = Repo.get_by(Token, token: response["access_token"])
+ refute new_token.refresh_token == token.refresh_token
+ assert new_token.scopes == auth.scopes
+ assert new_token.user_id == user.id
+ assert new_token.app_id == app.id
+ end
+
+ test "returns 400 if we try use access token" do
+ user = insert(:user)
+ app = insert(:oauth_app, scopes: ["read", "write"])
+
+ {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
+ {:ok, token} = Token.exchange_token(app, auth)
+
+ response =
+ build_conn()
+ |> post("/oauth/token", %{
+ "grant_type" => "refresh_token",
+ "refresh_token" => token.token,
+ "client_id" => app.client_id,
+ "client_secret" => app.client_secret
+ })
+ |> json_response(400)
+
+ assert %{"error" => "Invalid credentials"} == response
+ end
+
+ test "returns 400 if refresh_token invalid" do
+ app = insert(:oauth_app, scopes: ["read", "write"])
+
+ response =
+ build_conn()
+ |> post("/oauth/token", %{
+ "grant_type" => "refresh_token",
+ "refresh_token" => "token.refresh_token",
+ "client_id" => app.client_id,
+ "client_secret" => app.client_secret
+ })
+ |> json_response(400)
+
+ assert %{"error" => "Invalid credentials"} == response
+ end
+
+ test "issues a new token if token expired" do
+ user = insert(:user)
+ app = insert(:oauth_app, scopes: ["read", "write"])
+
+ {:ok, auth} = Authorization.create_authorization(app, user, ["write"])
+ {:ok, token} = Token.exchange_token(app, auth)
+
+ change =
+ Ecto.Changeset.change(
+ token,
+ %{valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -86_400 * 30)}
+ )
+
+ {:ok, access_token} = Repo.update(change)
+
+ response =
+ build_conn()
+ |> post("/oauth/token", %{
+ "grant_type" => "refresh_token",
+ "refresh_token" => access_token.refresh_token,
+ "client_id" => app.client_id,
+ "client_secret" => app.client_secret
+ })
+ |> json_response(200)
+
+ ap_id = user.ap_id
+
+ assert match?(
+ %{
+ "scope" => "write",
+ "token_type" => "Bearer",
+ "expires_in" => 600,
+ "access_token" => _,
+ "refresh_token" => _,
+ "me" => ^ap_id
+ },
+ response
+ )
+
+ refute Repo.get_by(Token, token: token.token)
+ token = Repo.get_by(Token, token: response["access_token"])
+ assert token
+ assert token.scopes == auth.scopes
+ assert token.user_id == user.id
+ assert token.app_id == app.id
+ end
+ end
+
+ describe "POST /oauth/token - bad request" do
+ test "returns 500" do
+ response =
+ build_conn()
+ |> post("/oauth/token", %{})
+ |> json_response(500)
+
+ assert %{"error" => "Bad request"} == response
+ end
+ end
+
+ describe "POST /oauth/revoke - bad request" do
+ test "returns 500" do
+ response =
+ build_conn()
+ |> post("/oauth/revoke", %{})
+ |> json_response(500)
+
+ assert %{"error" => "Bad request"} == response
+ end
+ end
end