Compare commits

..

3 commits

Author SHA1 Message Date
550ca0aacc improve Parser tests 2024-12-05 15:36:43 +01:00
12005618a9 add Properties tests, handle bookmarks and likes 2024-12-05 15:36:18 +01:00
7b19e42f57 improve token response handling 2024-12-05 15:35:56 +01:00
6 changed files with 175 additions and 17 deletions

View file

@ -1,3 +1,9 @@
defmodule PlugIndie.Post do
defstruct [:type, :title, :content]
defstruct [
:bookmark_of,
:content,
:like_of,
:title,
:type
]
end

View file

@ -13,6 +13,22 @@ defmodule PlugIndie.Properties do
content: content
}}
:bookmark ->
{:ok,
%PlugIndie.Post{
type: type,
title: title,
content: content,
bookmark_of: get_bookmarked_url(properties)
}}
:like ->
{:ok,
%PlugIndie.Post{
type: type,
like_of: get_liked_url(properties)
}}
:unknown ->
{:error, :parse_error}
end

View file

@ -11,7 +11,9 @@ defmodule PlugIndie.Token do
) do
case do_verify_token(access_token, token_endpoint, user_agent) do
{:ok, %{status: 200, body: body}} ->
verify_token_response(body, required_scope, supported_scopes, own_hostname)
body
|> map_keys_to_string()
|> verify_token_response(required_scope, supported_scopes, own_hostname)
{:ok, %{status: status}} ->
{:error, :request_error, status}
@ -45,19 +47,28 @@ defmodule PlugIndie.Token do
Tesla.get(client, token_endpoint)
end
defp verify_token_response(
%{
me: host_uri,
scope: scope,
client_id: client_id,
issued_at: _issued_at,
issued_by: _issued_by,
nonce: _nonce
},
required_scope,
supported_scopes,
own_hostname
) do
def verify_token_response(
%{
"me" => host_uri,
"scope" => scope,
"client_id" => client_id,
"issued_at" => _issued_at,
"issued_by" => _issued_by,
"nonce" => _nonce
},
required_scope,
supported_scopes,
own_hostname
) do
# {%{
# "client_id" => "https://indiepass.app/",
# "issued_at" => 1_733_382_601,
# "issued_by" => "https://tokens.indieauth.com/token",
# "me" => "https://blog.inhji.de/",
# "nonce" => 358_618_865,
# "scope" => "create update delete media read follow channels mute block"
# }, "create", ["create", "media"], "inhji.de"}
Logger.info("Host-URI: '#{host_uri}'")
Logger.info("ClientId: '#{client_id}'")
Logger.info("Scopes: '#{scope}'")
@ -72,6 +83,8 @@ defmodule PlugIndie.Token do
end
end
def verify_token_response(_, _, _, _), do: {:error, "verify_token_response", "bad request"}
defp verify_hostname_match(host_uri, own_hostname) do
hostnames_match? = get_hostname(host_uri) == own_hostname
@ -107,4 +120,8 @@ defmodule PlugIndie.Token do
{:error, "verify_scope_support", "scope '#{required_scope}' was not requested"}
end
end
defp map_keys_to_string(map) do
for {key, val} <- map, into: %{}, do: {to_string(key), val}
end
end

View file

@ -7,11 +7,36 @@ defmodule ParserTest do
params = %{
"type" => ["h-entry"],
"properties" => %{
"content" => "Hello World!"
"name" => ["First Note"],
"content" => ["Hello World!"],
"category" => ["Foo", "Bar"]
}
}
assert {:ok, "entry", %{"content" => "Hello World!"}} =
assert {:ok, "entry",
%{
"content" => ["Hello World!"],
"category" => ["Foo", "Bar"],
"name" => ["First Note"]
}} ==
parse_create_body("application/json", params)
end
test "parse_create_body with content-type form" do
params = %{
"h" => "entry",
"name" => "First Note",
"content" => "Hello World!",
"category" => ["Foo", "Bar"]
}
# TODO: fix content type
assert {:ok, "entry",
%{
"content" => ["Hello World!"],
"category" => ["Foo", "Bar"],
"name" => ["First Note"]
}} ==
parse_create_body("application/form", params)
end
end

43
test/properties_test.exs Normal file
View file

@ -0,0 +1,43 @@
defmodule PropertiesTest do
use ExUnit.Case
doctest PlugIndie.Properties
import PlugIndie.Properties, only: [parse: 1]
test "parse/1 identifies a note" do
props = %{
"content" => ["Hello World!"]
}
assert {:ok,
%PlugIndie.Post{
type: :note,
content: "Hello World!"
}} == parse(props)
end
test "parse/1 identifies a bookmark" do
props = %{
"content" => ["Hello World!"],
"bookmark-of" => ["https://100r.co"]
}
assert {:ok,
%PlugIndie.Post{
type: :bookmark,
content: "Hello World!",
bookmark_of: "https://100r.co"
}} == parse(props)
end
test "parse/1 identifies a like" do
props = %{
"like-of" => ["https://100r.co"]
}
assert {:ok,
%PlugIndie.Post{
type: :like,
like_of: "https://100r.co"
}} == parse(props)
end
end

51
test/token_test.exs Normal file
View file

@ -0,0 +1,51 @@
defmodule TokenTest do
use ExUnit.Case
describe "verify_token_response/4" do
test "with atom keyed map" do
body = %{
:client_id => "https://indiepass.app/",
:issued_at => 1_733_382_601,
:issued_by => "https://tokens.indieauth.com/token",
:me => "https://blog.inhji.de/",
:nonce => 358_618_865,
:scope => "create update delete media read follow channels mute block"
}
required_scope = "create"
supported_scopes = ["create", "media"]
hostname = "blog.inhji.de"
assert {:error, _, _} =
PlugIndie.Token.verify_token_response(
body,
required_scope,
supported_scopes,
hostname
)
end
test "with string keyed map" do
body = %{
"client_id" => "https://indiepass.app/",
"issued_at" => 1_733_382_601,
"issued_by" => "https://tokens.indieauth.com/token",
"me" => "https://blog.inhji.de/",
"nonce" => 358_618_865,
"scope" => "create update delete media read follow channels mute block"
}
required_scope = "create"
supported_scopes = ["create", "media"]
hostname = "blog.inhji.de"
assert PlugIndie.Token.verify_token_response(
body,
required_scope,
supported_scopes,
hostname
) ==
:ok
end
end
end