/
advanceddev
/
license-server
Обзор
Документация
Войти
/
advanceddev
/
license-server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
Безопасность
master
internal/handler/license_handler_test.go
190 строк
5 KB
advanceddev
fix: security & reliability hardening for rate limiter, auth, and audit
10 авг 2026, 23:52
Не верифицирован
10 авг 2026, 23:52
aef6073
Код
Авторство
О чём код?
package handler import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/google/uuid" "gitverse.ru/advanceddev/license-server/internal/domain" ) func TestValidate_Success(t *testing.T) { partnerID := uuid.MustParse("550e8400-e29b-41d4-a716-446655440000") licenseKey := "LCSR-test-key-1234" keyHash := domain.HashLicenseKey(licenseKey) repo := newMockRepo() repo.byHash[keyHash] = &domain.License{ ID: uuid.New(), KeyHash: keyHash, PartnerID: partnerID, BrandName: "Test Brand", ExpiresAt: time.Now().Add(365 * 24 * time.Hour), } h := NewLicenseHandler(repo, nil, nil, nil, nil) body := `{"license_key": "` + licenseKey + `"}` req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusOK { t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String()) } var resp domain.ValidateResponse json.Unmarshal(w.Body.Bytes(), &resp) if !resp.Valid { t.Errorf("expected valid=true, got false: %s", resp.Reason) } if resp.Partner == nil { t.Fatal("expected partner config, got nil") } if resp.Partner.BrandName != "Test Brand" { t.Errorf("expected brand 'Test Brand', got %q", resp.Partner.BrandName) } if resp.Partner.PartnerID != partnerID { t.Errorf("expected partner_id %s, got %s", partnerID, resp.Partner.PartnerID) } } func TestValidate_LicenseNotFound(t *testing.T) { repo := newMockRepo() h := NewLicenseHandler(repo, nil, nil, nil, nil) body := `{"license_key": "nonexistent-key"}` req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusOK { t.Fatalf("expected 200, got %d", w.Code) } var resp domain.ValidateResponse json.Unmarshal(w.Body.Bytes(), &resp) if resp.Valid { t.Error("expected valid=false for nonexistent key") } if resp.Reason != "license not found" { t.Errorf("expected reason 'license not found', got %q", resp.Reason) } } func TestValidate_LicenseExpired(t *testing.T) { licenseKey := "LCSR-expired-key" keyHash := domain.HashLicenseKey(licenseKey) repo := newMockRepo() repo.byHash[keyHash] = &domain.License{ ID: uuid.New(), KeyHash: keyHash, PartnerID: uuid.New(), BrandName: "Expired Brand", ExpiresAt: time.Now().Add(-1 * time.Hour), // истекла час назад } h := NewLicenseHandler(repo, nil, nil, nil, nil) body := `{"license_key": "` + licenseKey + `"}` req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusOK { t.Fatalf("expected 200, got %d", w.Code) } var resp domain.ValidateResponse json.Unmarshal(w.Body.Bytes(), &resp) if resp.Valid { t.Error("expected valid=false for expired license") } if resp.Reason != "license expired" { t.Errorf("expected reason 'license expired', got %q", resp.Reason) } if resp.ExpiresAt == nil { t.Error("expected expires_at in response") } } func TestValidate_MissingLicenseKey(t *testing.T) { repo := newMockRepo() h := NewLicenseHandler(repo, nil, nil, nil, nil) body := `{"instance_id": "test"}` req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusUnprocessableEntity { t.Fatalf("expected 422, got %d: %s", w.Code, w.Body.String()) } } func TestValidate_UnknownField(t *testing.T) { repo := newMockRepo() h := NewLicenseHandler(repo, nil, nil, nil, nil) body := `{"license_key": "test", "unknown_field": "bad"}` req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String()) } } func TestValidate_InvalidJSON(t *testing.T) { repo := newMockRepo() h := NewLicenseHandler(repo, nil, nil, nil, nil) body := `{malformed json` req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader(body)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String()) } } func TestValidate_EmptyBody(t *testing.T) { repo := newMockRepo() h := NewLicenseHandler(repo, nil, nil, nil, nil) req := httptest.NewRequest(http.MethodPost, "/api/v1/license/validate", strings.NewReader("")) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() h.Validate(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("expected 400 for empty body, got %d: %s", w.Code, w.Body.String()) } } var _ = context.Background