- Add JWT token generation and validation - Implement bcrypt password hashing - Create auth service with register/login/refresh/logout - Add app passkey generation and management - Implement protected routes and auth middleware - Add comprehensive tests for new functionality
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/dhao2001/mygo/internal/app"
|
|
"github.com/dhao2001/mygo/internal/config"
|
|
"github.com/dhao2001/mygo/internal/service"
|
|
)
|
|
|
|
func TestVersionRoute(t *testing.T) {
|
|
cfg := &config.Config{
|
|
JWT: config.JWTConfig{
|
|
Secret: "test-secret",
|
|
AccessTTL: "15m",
|
|
RefreshTTL: "168h",
|
|
},
|
|
}
|
|
authService := service.NewAuthService(nil, nil, nil, nil, 15*time.Minute, 7*24*time.Hour)
|
|
webApp := app.NewWebApp(cfg, nil, nil, nil, nil, nil, authService)
|
|
router := NewRouter(webApp)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/version", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
|
}
|
|
|
|
if got := rec.Header().Get("Content-Type"); got != "application/json; charset=utf-8" {
|
|
t.Fatalf("content-type = %q, want %q", got, "application/json; charset=utf-8")
|
|
}
|
|
|
|
var body struct {
|
|
Version string `json:"version"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("unmarshal response: %v", err)
|
|
}
|
|
|
|
if body.Version != app.AppVersion {
|
|
t.Errorf("version = %q, want %q", body.Version, app.AppVersion)
|
|
}
|
|
}
|
|
|
|
func TestAddress(t *testing.T) {
|
|
got := Address(config.ServerConfig{Host: "127.0.0.1", Port: 8080})
|
|
want := "127.0.0.1:8080"
|
|
if got != want {
|
|
t.Errorf("Address() = %q, want %q", got, want)
|
|
}
|
|
}
|