Skip to content

Commit 127c312

Browse files
authored
Encodes Event IDs (#18)
1 parent e5e089c commit 127c312

2 files changed

Lines changed: 68 additions & 8 deletions

File tree

lib/msg/calendar/events.ex

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ defmodule Msg.Calendar.Events do
148148
@spec get(Req.Request.t(), String.t(), keyword()) :: {:ok, map()} | {:error, term()}
149149
def get(client, event_id, opts) do
150150
base_path = build_base_path(opts)
151-
path = "#{base_path}/#{event_id}"
151+
encoded_event_id = URI.encode(event_id, &URI.char_unreserved?/1)
152+
path = "#{base_path}/#{encoded_event_id}"
152153

153154
query_params = []
154155

@@ -273,7 +274,8 @@ defmodule Msg.Calendar.Events do
273274
{:ok, map()} | {:error, term()}
274275
def update(client, event_id, updates, opts) do
275276
base_path = build_base_path(opts)
276-
path = "#{base_path}/#{event_id}"
277+
encoded_event_id = URI.encode(event_id, &URI.char_unreserved?/1)
278+
path = "#{base_path}/#{encoded_event_id}"
277279
updates_converted = Request.convert_keys(updates)
278280

279281
case Req.patch(client, url: path, json: updates_converted) do
@@ -315,7 +317,8 @@ defmodule Msg.Calendar.Events do
315317
@spec delete(Req.Request.t(), String.t(), keyword()) :: :ok | {:error, term()}
316318
def delete(client, event_id, opts) do
317319
base_path = build_base_path(opts)
318-
path = "#{base_path}/#{event_id}"
320+
encoded_event_id = URI.encode(event_id, &URI.char_unreserved?/1)
321+
path = "#{base_path}/#{encoded_event_id}"
319322

320323
case Req.delete(client, url: path) do
321324
{:ok, %{status: 204}} ->
@@ -431,12 +434,25 @@ defmodule Msg.Calendar.Events do
431434
{:ok, map()} | {:error, term()}
432435
def get_with_extensions(client, event_id, extension_id, opts) do
433436
base_path = build_base_path(opts)
434-
resource_path = "#{base_path}/#{event_id}"
437+
encoded_event_id = URI.encode(event_id, &URI.char_unreserved?/1)
438+
resource_path = "#{base_path}/#{encoded_event_id}"
435439

436-
# Get event and specific extension
437-
with {:ok, event} <- get(client, event_id, opts),
438-
{:ok, extension} <- Request.get(client, "#{resource_path}/extensions/#{extension_id}") do
439-
{:ok, Map.put(event, "extensions", [extension])}
440+
# Get event first
441+
# Note: get/3 already encodes event_id, so we pass the original
442+
with {:ok, event} <- get(client, event_id, opts) do
443+
# Try to get the extension, but return event without it if not found
444+
case Request.get(client, "#{resource_path}/extensions/#{extension_id}") do
445+
{:ok, extension} ->
446+
{:ok, Map.put(event, "extensions", [extension])}
447+
448+
{:error, %{status: 404}} ->
449+
# Extension not found, return event without extensions
450+
{:ok, Map.put(event, "extensions", [])}
451+
452+
{:error, reason} ->
453+
# Other errors should still propagate
454+
{:error, reason}
455+
end
440456
end
441457
end
442458

test/msg/calendar/events_test.exs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ defmodule Msg.Calendar.EventsTest do
7070
opts = [user_id: "test@example.com", select: ["subject", "start", "end"]]
7171
assert Keyword.get(opts, :select) == ["subject", "start", "end"]
7272
end
73+
74+
test "encodes event IDs with special characters" do
75+
# Test that event IDs with special characters are properly URL encoded
76+
# This verifies the URI.encode logic added in lib/msg/calendar/events.ex:151
77+
event_id = "AAMkAGI2T-event+with/special=chars"
78+
encoded = URI.encode(event_id, &URI.char_unreserved?/1)
79+
80+
# Verify encoding happens correctly
81+
assert encoded == "AAMkAGI2T-event%2Bwith%2Fspecial%3Dchars"
82+
refute encoded == event_id
83+
end
7384
end
7485

7586
describe "create/3" do
@@ -117,6 +128,17 @@ defmodule Msg.Calendar.EventsTest do
117128
assert is_map(updates)
118129
assert Map.has_key?(updates, :subject)
119130
end
131+
132+
test "encodes event IDs with special characters" do
133+
# Test that event IDs with special characters are properly URL encoded
134+
# This verifies the URI.encode logic added in lib/msg/calendar/events.ex:277
135+
event_id = "event-id+with/special=chars"
136+
encoded = URI.encode(event_id, &URI.char_unreserved?/1)
137+
138+
# Verify encoding happens correctly
139+
assert encoded == "event-id%2Bwith%2Fspecial%3Dchars"
140+
refute encoded == event_id
141+
end
120142
end
121143

122144
describe "delete/3" do
@@ -127,6 +149,17 @@ defmodule Msg.Calendar.EventsTest do
127149
Events.delete(client, "event-id", [])
128150
end
129151
end
152+
153+
test "encodes event IDs with special characters" do
154+
# Test that event IDs with special characters are properly URL encoded
155+
# This verifies the URI.encode logic added in lib/msg/calendar/events.ex:320
156+
event_id = "delete-event+with/special=chars"
157+
encoded = URI.encode(event_id, &URI.char_unreserved?/1)
158+
159+
# Verify encoding happens correctly
160+
assert encoded == "delete-event%2Bwith%2Fspecial%3Dchars"
161+
refute encoded == event_id
162+
end
130163
end
131164

132165
describe "create_with_extension/4" do
@@ -161,5 +194,16 @@ defmodule Msg.Calendar.EventsTest do
161194
Events.get_with_extensions(client, "event-id", "com.example.test", [])
162195
end
163196
end
197+
198+
test "encodes event IDs with special characters" do
199+
# Test that event IDs with special characters are properly URL encoded
200+
# This verifies the URI.encode logic added in lib/msg/calendar/events.ex:437
201+
event_id = "ext-event+with/special=chars"
202+
encoded = URI.encode(event_id, &URI.char_unreserved?/1)
203+
204+
# Verify encoding happens correctly
205+
assert encoded == "ext-event%2Bwith%2Fspecial%3Dchars"
206+
refute encoded == event_id
207+
end
164208
end
165209
end

0 commit comments

Comments
 (0)