@@ -3,13 +3,14 @@ package auditlogs
33import (
44 "context"
55 "encoding/json"
6- "github.com/workos/workos-go/v6/pkg/workos_errors"
76 "net/http"
87 "net/http/httptest"
98 "testing"
109 "time"
1110
1211 "github.com/stretchr/testify/require"
12+ "github.com/workos/workos-go/v6/pkg/retryablehttp"
13+ "github.com/workos/workos-go/v6/pkg/workos_errors"
1314)
1415
1516var event = CreateEventOpts {
@@ -283,3 +284,129 @@ func TestGetExports(t *testing.T) {
283284type defaultTestHandler struct {
284285 header * http.Header
285286}
287+
288+ func TestCreateEvent_AutoGeneratesIdempotencyKey (t * testing.T ) {
289+ // Test that when IdempotencyKey is empty, SDK auto-generates one
290+
291+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
292+ idempotencyKey := r .Header .Get ("Idempotency-Key" )
293+
294+ // Assert idempotency key was sent
295+ require .NotEmpty (t , idempotencyKey , "Expected Idempotency-Key header to be present" )
296+
297+ // Assert it has the workos-go prefix and UUID format (10 chars for "workos-go-" + 36 for UUID)
298+ require .Equal (t , 46 , len (idempotencyKey ), "Expected 'workos-go-' prefix + UUID format (46 characters total)" )
299+ require .Contains (t , idempotencyKey , "workos-go-" , "Expected idempotency key to start with 'workos-go-'" )
300+
301+ w .WriteHeader (http .StatusOK )
302+ }))
303+ defer server .Close ()
304+
305+ client := & Client {
306+ APIKey : "test_key" ,
307+ EventsEndpoint : server .URL ,
308+ HTTPClient : server .Client (),
309+ }
310+
311+ err := client .CreateEvent (context .Background (), CreateEventOpts {
312+ OrganizationID : "org_123" ,
313+ Event : Event {
314+ Action : "test.action" ,
315+ Actor : Actor {ID : "user_123" , Type : "user" , Name : "Test User" },
316+ Targets : []Target {
317+ {ID : "target_123" , Type : "test" , Name : "Test Target" },
318+ },
319+ Context : Context {
320+ Location : "127.0.0.1" ,
321+ UserAgent : "test" ,
322+ },
323+ },
324+ // Note: NOT providing IdempotencyKey
325+ })
326+
327+ require .NoError (t , err )
328+ }
329+
330+ func TestCreateEvent_UsesProvidedIdempotencyKey (t * testing.T ) {
331+ // Test that when user provides IdempotencyKey, SDK uses it
332+
333+ expectedKey := "user-provided-key-12345"
334+
335+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
336+ idempotencyKey := r .Header .Get ("Idempotency-Key" )
337+
338+ require .Equal (t , expectedKey , idempotencyKey , "Expected provided idempotency key to be used" )
339+
340+ w .WriteHeader (http .StatusOK )
341+ }))
342+ defer server .Close ()
343+
344+ client := & Client {
345+ APIKey : "test_key" ,
346+ EventsEndpoint : server .URL ,
347+ HTTPClient : server .Client (),
348+ }
349+
350+ err := client .CreateEvent (context .Background (), CreateEventOpts {
351+ OrganizationID : "org_123" ,
352+ Event : Event {
353+ Action : "test.action" ,
354+ Actor : Actor {ID : "user_123" , Type : "user" , Name : "Test User" },
355+ Targets : []Target {
356+ {ID : "target_123" , Type : "test" , Name : "Test Target" },
357+ },
358+ Context : Context {
359+ Location : "127.0.0.1" ,
360+ UserAgent : "test" ,
361+ },
362+ },
363+ IdempotencyKey : expectedKey , // User provides their own key
364+ })
365+
366+ require .NoError (t , err )
367+ }
368+
369+ func TestCreateEvent_RetriesOn5xxErrors (t * testing.T ) {
370+ // Test that SDK retries on 5xx errors
371+
372+ attempts := 0
373+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
374+ attempts ++
375+ if attempts < 3 {
376+ // First 2 attempts fail with 500
377+ w .Header ().Set ("Content-Type" , "application/json" )
378+ w .WriteHeader (http .StatusInternalServerError )
379+ w .Write ([]byte (`{"message": "Internal server error"}` ))
380+ } else {
381+ // Third attempt succeeds
382+ w .WriteHeader (http .StatusOK )
383+ }
384+ }))
385+ defer server .Close ()
386+
387+ client := & Client {
388+ APIKey : "test_key" ,
389+ EventsEndpoint : server .URL ,
390+ HTTPClient : & retryablehttp.HttpClient {
391+ Client : http.Client {Timeout : 10 * time .Second },
392+ },
393+ }
394+
395+ err := client .CreateEvent (context .Background (), CreateEventOpts {
396+ OrganizationID : "org_123" ,
397+ Event : Event {
398+ Action : "test.action" ,
399+ Actor : Actor {ID : "user_123" , Type : "user" , Name : "Test User" },
400+ Targets : []Target {
401+ {ID : "target_123" , Type : "test" , Name : "Test Target" },
402+ },
403+ Context : Context {
404+ Location : "127.0.0.1" ,
405+ UserAgent : "test" ,
406+ },
407+ },
408+ })
409+
410+ require .NoError (t , err , "CreateEvent should succeed after retries" )
411+ require .Equal (t , 3 , attempts , "Expected 3 attempts" )
412+ }
0 commit comments