diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-16 22:02:58 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-16 22:02:58 +0900 |
| commit | db87f85aa7055e597800481b8cc6d006c70bcc88 (patch) | |
| tree | 57630fde35a39e445c177a278cacf243b7fb0d52 /backend/account | |
| parent | 08c121c21a7e429e43e2d51fa4a3d8bd945c5d01 (diff) | |
| download | phperkaigi-2026-albatross-db87f85aa7055e597800481b8cc6d006c70bcc88.tar.gz phperkaigi-2026-albatross-db87f85aa7055e597800481b8cc6d006c70bcc88.tar.zst phperkaigi-2026-albatross-db87f85aa7055e597800481b8cc6d006c70bcc88.zip | |
test(backend): add unit tests for auth_middleware, fortee, processor, account, and more handlers
Cover previously untested code: SessionCookieMiddleware, context helpers,
downloadFile, addAcceptHeader, doProcessTaskRunTestcase, updateSubmissionAndGameState,
PostLogout, GetGames, PostGamePlayCode, GetGameWatchRanking, GetGameWatchLatestStates.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'backend/account')
| -rw-r--r-- | backend/account/icon_test.go | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/backend/account/icon_test.go b/backend/account/icon_test.go new file mode 100644 index 0000000..7f4ddbc --- /dev/null +++ b/backend/account/icon_test.go @@ -0,0 +1,94 @@ +package account + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func TestDownloadFile_Success(t *testing.T) { + expectedContent := "file content here" + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(expectedContent)) + })) + defer server.Close() + + tmpDir := t.TempDir() + filePath := filepath.Join(tmpDir, "subdir", "test.png") + + err := downloadFile(context.Background(), server.URL+"/icon.png", filePath) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + data, err := os.ReadFile(filePath) + if err != nil { + t.Fatalf("failed to read downloaded file: %v", err) + } + if string(data) != expectedContent { + t.Errorf("expected content %q, got %q", expectedContent, string(data)) + } +} + +func TestDownloadFile_NotFound(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + })) + defer server.Close() + + tmpDir := t.TempDir() + filePath := filepath.Join(tmpDir, "test.png") + + err := downloadFile(context.Background(), server.URL+"/missing.png", filePath) + if err == nil { + t.Error("expected error for 404 response") + } +} + +func TestDownloadFile_ServerError(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer server.Close() + + tmpDir := t.TempDir() + filePath := filepath.Join(tmpDir, "test.png") + + err := downloadFile(context.Background(), server.URL+"/error.png", filePath) + if err == nil { + t.Error("expected error for 500 response") + } +} + +func TestDownloadFile_InvalidURL(t *testing.T) { + tmpDir := t.TempDir() + filePath := filepath.Join(tmpDir, "test.png") + + err := downloadFile(context.Background(), "http://localhost:1/unreachable", filePath) + if err == nil { + t.Error("expected error for unreachable server") + } +} + +func TestDownloadFile_ContextCanceled(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("data")) + })) + defer server.Close() + + ctx, cancel := context.WithCancel(context.Background()) + cancel() // cancel immediately + + tmpDir := t.TempDir() + filePath := filepath.Join(tmpDir, "test.png") + + err := downloadFile(ctx, server.URL+"/icon.png", filePath) + if err == nil { + t.Error("expected error for canceled context") + } +} |
