Skip to content

Commit 72ceac7

Browse files
authored
test(api): confirm validation methods (#366)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 94afba1 commit 72ceac7

File tree

1 file changed

+303
-5
lines changed

1 file changed

+303
-5
lines changed

internal/api/api_test.go

Lines changed: 303 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ func waitForServer(apiBaseURL string) {
125125

126126
for range maxRetries {
127127
resp, err := http.Get(fmt.Sprintf("%s/healthcheck", apiBaseURL))
128-
if resp == nil {
129-
continue
130-
}
131-
if err == nil && resp.StatusCode == http.StatusOK {
132-
return // Server is ready
128+
if err == nil && resp != nil {
129+
resp.Body.Close()
130+
if resp.StatusCode == http.StatusOK {
131+
return // Server is ready
132+
}
133133
}
134134
time.Sleep(retryInterval)
135135
}
@@ -356,3 +356,301 @@ func TestCreateWalletReturnsMnemonic(t *testing.T) {
356356
}
357357
}
358358
}
359+
360+
func TestWalletCreateMethodNotAllowed(t *testing.T) {
361+
// Start the API
362+
apiBaseURL, _, cleanup := startAPI(t)
363+
defer cleanup()
364+
365+
// Test POST method (should fail)
366+
resp, err := http.Post(
367+
fmt.Sprintf("%s/api/wallet/create", apiBaseURL),
368+
"application/json",
369+
nil,
370+
)
371+
assert.NoError(t, err)
372+
assert.NotNil(t, resp)
373+
defer resp.Body.Close()
374+
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
375+
}
376+
377+
func TestWalletRestoreMethodNotAllowed(t *testing.T) {
378+
// Start the API
379+
apiBaseURL, _, cleanup := startAPI(t)
380+
defer cleanup()
381+
382+
// Test GET method (should fail)
383+
resp, err := http.Get(fmt.Sprintf("%s/api/wallet/restore", apiBaseURL))
384+
assert.NoError(t, err)
385+
assert.NotNil(t, resp)
386+
defer resp.Body.Close()
387+
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
388+
}
389+
390+
func TestHealthcheckEndpoint(t *testing.T) {
391+
// Start the API
392+
apiBaseURL, _, cleanup := startAPI(t)
393+
defer cleanup()
394+
395+
// Test the /healthcheck endpoint
396+
resp, err := http.Get(fmt.Sprintf("%s/healthcheck", apiBaseURL))
397+
assert.NoError(t, err, "failed to call healthcheck endpoint")
398+
assert.NotNil(t, resp)
399+
defer resp.Body.Close()
400+
assert.Equal(t, http.StatusOK, resp.StatusCode)
401+
402+
// Check response body
403+
body, err := io.ReadAll(resp.Body)
404+
assert.NoError(t, err)
405+
assert.Equal(t, `{"healthy": true}`, string(body))
406+
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
407+
}
408+
409+
func TestSwaggerEndpoint(t *testing.T) {
410+
// Start the API
411+
apiBaseURL, _, cleanup := startAPI(t)
412+
defer cleanup()
413+
414+
// Test the /swagger/ endpoint
415+
resp, err := http.Get(fmt.Sprintf("%s/swagger/", apiBaseURL))
416+
assert.NoError(t, err, "failed to call swagger endpoint")
417+
assert.NotNil(t, resp)
418+
defer resp.Body.Close()
419+
assert.Equal(t, http.StatusOK, resp.StatusCode)
420+
421+
// Swagger should return HTML content
422+
body, err := io.ReadAll(resp.Body)
423+
assert.NoError(t, err)
424+
assert.Contains(
425+
t,
426+
string(body),
427+
"swagger",
428+
"response should contain swagger content",
429+
)
430+
}
431+
432+
func TestHealthcheckMethodNotAllowed(t *testing.T) {
433+
// Start the API
434+
apiBaseURL, _, cleanup := startAPI(t)
435+
defer cleanup()
436+
437+
// Test POST method (should fail)
438+
resp, err := http.Post(
439+
fmt.Sprintf("%s/healthcheck", apiBaseURL),
440+
"application/json",
441+
nil,
442+
)
443+
assert.NoError(t, err)
444+
assert.NotNil(t, resp)
445+
defer resp.Body.Close()
446+
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
447+
}
448+
449+
func TestWalletListEndpointGCPNotConfigured(t *testing.T) {
450+
// Start the API
451+
apiBaseURL, _, cleanup := startAPI(t)
452+
defer cleanup()
453+
454+
// Test the /api/wallet/list endpoint
455+
resp, err := http.Get(fmt.Sprintf("%s/api/wallet/list", apiBaseURL))
456+
assert.NoError(t, err, "failed to call wallet list endpoint")
457+
assert.NotNil(t, resp)
458+
defer resp.Body.Close()
459+
460+
// GCP routes are only registered if GCP is configured
461+
// Since test setup doesn't configure GCP, expect 404
462+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
463+
}
464+
465+
func TestWalletListMethodGCPNotConfigured(t *testing.T) {
466+
// Start the API
467+
apiBaseURL, _, cleanup := startAPI(t)
468+
defer cleanup()
469+
470+
// Test POST method (should return 404 since GCP not configured)
471+
resp, err := http.Post(
472+
fmt.Sprintf("%s/api/wallet/list", apiBaseURL),
473+
"application/json",
474+
nil,
475+
)
476+
assert.NoError(t, err)
477+
assert.NotNil(t, resp)
478+
defer resp.Body.Close()
479+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
480+
}
481+
482+
func TestWalletGetEndpointGCPNotConfigured(t *testing.T) {
483+
t.Run("Invalid JSON", func(t *testing.T) {
484+
// Start the API
485+
apiBaseURL, _, cleanup := startAPI(t)
486+
defer cleanup()
487+
488+
// Test with invalid JSON (should return 404 since GCP not configured)
489+
resp, err := http.Post(
490+
fmt.Sprintf("%s/api/wallet/get", apiBaseURL),
491+
"application/json",
492+
strings.NewReader("invalid json"),
493+
)
494+
assert.NoError(t, err)
495+
assert.NotNil(t, resp)
496+
defer resp.Body.Close()
497+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
498+
})
499+
500+
t.Run("Validation Error", func(t *testing.T) {
501+
// Start the API
502+
apiBaseURL, _, cleanup := startAPI(t)
503+
defer cleanup()
504+
505+
// Test with empty request (should return 404 since GCP not configured)
506+
req := WalletGetRequest{}
507+
reqBody, _ := json.Marshal(req)
508+
509+
resp, err := http.Post(
510+
fmt.Sprintf("%s/api/wallet/get", apiBaseURL),
511+
"application/json",
512+
bytes.NewReader(reqBody),
513+
)
514+
assert.NoError(t, err)
515+
assert.NotNil(t, resp)
516+
defer resp.Body.Close()
517+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
518+
})
519+
520+
t.Run("Nonexistent Wallet", func(t *testing.T) {
521+
// Start the API
522+
apiBaseURL, _, cleanup := startAPI(t)
523+
defer cleanup()
524+
525+
// Test with valid request but nonexistent wallet (should return 404 since GCP not configured)
526+
req := WalletGetRequest{Name: "nonexistent-wallet"}
527+
reqBody, _ := json.Marshal(req)
528+
529+
resp, err := http.Post(
530+
fmt.Sprintf("%s/api/wallet/get", apiBaseURL),
531+
"application/json",
532+
bytes.NewReader(reqBody),
533+
)
534+
assert.NoError(t, err)
535+
assert.NotNil(t, resp)
536+
defer resp.Body.Close()
537+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
538+
})
539+
}
540+
541+
func TestWalletGetMethodGCPNotConfigured(t *testing.T) {
542+
// Start the API
543+
apiBaseURL, _, cleanup := startAPI(t)
544+
defer cleanup()
545+
546+
// Test GET method (should return 404 since GCP not configured)
547+
resp, err := http.Get(fmt.Sprintf("%s/api/wallet/get", apiBaseURL))
548+
assert.NoError(t, err)
549+
assert.NotNil(t, resp)
550+
defer resp.Body.Close()
551+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
552+
}
553+
554+
func TestWalletDeleteEndpointGCPNotConfigured(t *testing.T) {
555+
t.Run("Invalid JSON", func(t *testing.T) {
556+
// Start the API
557+
apiBaseURL, _, cleanup := startAPI(t)
558+
defer cleanup()
559+
560+
// Test with invalid JSON (should return 404 since GCP not configured)
561+
resp, err := http.Post(
562+
fmt.Sprintf("%s/api/wallet/delete", apiBaseURL),
563+
"application/json",
564+
strings.NewReader("invalid json"),
565+
)
566+
assert.NoError(t, err)
567+
assert.NotNil(t, resp)
568+
defer resp.Body.Close()
569+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
570+
})
571+
572+
t.Run("Validation Error", func(t *testing.T) {
573+
// Start the API
574+
apiBaseURL, _, cleanup := startAPI(t)
575+
defer cleanup()
576+
577+
// Test with empty request (should return 404 since GCP not configured)
578+
req := WalletDeleteRequest{}
579+
reqBody, _ := json.Marshal(req)
580+
581+
resp, err := http.Post(
582+
fmt.Sprintf("%s/api/wallet/delete", apiBaseURL),
583+
"application/json",
584+
bytes.NewReader(reqBody),
585+
)
586+
assert.NoError(t, err)
587+
assert.NotNil(t, resp)
588+
defer resp.Body.Close()
589+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
590+
})
591+
}
592+
593+
func TestWalletDeleteMethodGCPNotConfigured(t *testing.T) {
594+
// Start the API
595+
apiBaseURL, _, cleanup := startAPI(t)
596+
defer cleanup()
597+
598+
// Test GET method (should return 404 since GCP not configured)
599+
resp, err := http.Get(fmt.Sprintf("%s/api/wallet/delete", apiBaseURL))
600+
assert.NoError(t, err)
601+
assert.NotNil(t, resp)
602+
defer resp.Body.Close()
603+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
604+
}
605+
606+
func TestWalletUpdateEndpointGCPNotConfigured(t *testing.T) {
607+
t.Run("Invalid JSON", func(t *testing.T) {
608+
// Start the API
609+
apiBaseURL, _, cleanup := startAPI(t)
610+
defer cleanup()
611+
612+
// Test with invalid JSON (should return 404 since GCP not configured)
613+
resp, err := http.Post(
614+
fmt.Sprintf("%s/api/wallet/update", apiBaseURL),
615+
"application/json",
616+
strings.NewReader("invalid json"),
617+
)
618+
assert.NoError(t, err)
619+
assert.NotNil(t, resp)
620+
defer resp.Body.Close()
621+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
622+
})
623+
624+
t.Run("Validation Error", func(t *testing.T) {
625+
// Start the API
626+
apiBaseURL, _, cleanup := startAPI(t)
627+
defer cleanup()
628+
629+
// Test with empty request (should return 404 since GCP not configured)
630+
req := WalletUpdateRequest{}
631+
reqBody, _ := json.Marshal(req)
632+
633+
resp, err := http.Post(
634+
fmt.Sprintf("%s/api/wallet/update", apiBaseURL),
635+
"application/json",
636+
bytes.NewReader(reqBody),
637+
)
638+
assert.NoError(t, err)
639+
assert.NotNil(t, resp)
640+
defer resp.Body.Close()
641+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
642+
})
643+
}
644+
645+
func TestWalletUpdateMethodGCPNotConfigured(t *testing.T) {
646+
// Start the API
647+
apiBaseURL, _, cleanup := startAPI(t)
648+
defer cleanup()
649+
650+
// Test GET method (should return 404 since GCP not configured)
651+
resp, err := http.Get(fmt.Sprintf("%s/api/wallet/update", apiBaseURL))
652+
assert.NoError(t, err)
653+
assert.NotNil(t, resp)
654+
defer resp.Body.Close()
655+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
656+
}

0 commit comments

Comments
 (0)