Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ jobs:
- name: test
run: mix test

- name: atom
env:
MIX_ENV: test
run: mix run test/support/atom.exs

- name: credo
env:
MIX_ENV: test
Expand Down
29 changes: 12 additions & 17 deletions lib/web_push/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@ defmodule WebPushEx.Subscription do
"""
@spec from_json(String.t()) :: t()
def from_json(json) do
{decoded, :ok, ""} = :json.decode(json, :ok, %{object_push: &object_push/3})

struct!(__MODULE__, decoded)
end

@spec object_push(String.t(), :json.decode_value(), :ok) :: list()
defp object_push(key, value, acc) do
key = String.to_existing_atom(key)

value =
if key == :endpoint and is_binary(value) do
URI.parse(value)
else
value
end

[{key, value} | acc]
with decoded <- :json.decode(json),
endpoint <- Map.fetch!(decoded, "endpoint"),
uri <- URI.parse(endpoint),
keys <- Map.fetch!(decoded, "keys") do
%__MODULE__{
endpoint: uri,
keys: %{
p256dh: Map.fetch!(keys, "p256dh"),
auth: Map.fetch!(keys, "auth")
}
}
end
end
end
9 changes: 9 additions & 0 deletions test/support/atom.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
WebPushEx.Subscription.from_json("""
{
"endpoint": "https://push.example.com/123",
"keys": {
"p256dh": "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
"auth": "BTBZMqHH6r4Tts7J_aSIgg"
}
}
""")
8 changes: 3 additions & 5 deletions test/support/web_push_ex_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ defmodule WebPushExFixtures do
WebPushSubscription fixture with default `p256dh` and `auth` values from the
RFC example.
"""
def web_push_subscription_fixture(attrs \\ %{}) do
attrs
|> Enum.into(%{
def web_push_subscription_fixture do
%WebPushEx.Subscription{
endpoint: URI.parse("https://push.example.com/123"),
keys: %{
p256dh:
"BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
auth: "BTBZMqHH6r4Tts7J_aSIgg"
}
})
|> then(&struct!(WebPushEx.Subscription, &1))
}
end

@doc """
Expand Down
50 changes: 45 additions & 5 deletions test/web_push_ex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,58 @@ defmodule WebPushExTest do
assert observed.keys == expected.keys
end

test "raises with invalid keys" do
assert_raise ArgumentError, fn ->
test "raises KeyError with invalid keys" do
# auth key
assert_raise KeyError, fn ->
WebPushEx.Subscription.from_json("""
{
"end": "https://push.example.com/123",
"key": {
"p256": "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
"endpoint": "https://push.example.com/123",
"keys": {
"p256dh": "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
"au": "BTBZMqHH6r4Tts7J_aSIgg"
}
}
""")
end

# p256dh key
assert_raise KeyError, fn ->
WebPushEx.Subscription.from_json("""
{
"endpoint": "https://push.example.com/123",
"keys": {
"p256": "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
"auth": "BTBZMqHH6r4Tts7J_aSIgg"
}
}
""")
end

# keys key
assert_raise KeyError, fn ->
WebPushEx.Subscription.from_json("""
{
"endpoint": "https://push.example.com/123",
"key": {
"p256dh": "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
"auth": "BTBZMqHH6r4Tts7J_aSIgg"
}
}
""")
end

# endpoint key
assert_raise KeyError, fn ->
WebPushEx.Subscription.from_json("""
{
"end": "https://push.example.com/123",
"keys": {
"p256dh": "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
"auth": "BTBZMqHH6r4Tts7J_aSIgg"
}
}
""")
end
end
end
end