Files
mtusi2026lab3/student-tests/auth_tests/auth_handler_test.go
T

33 lines
1.1 KiB
Go

package auth_tests
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"highload/internal/transport/http/handler"
"highload/internal/transport/http/middleware"
)
func TestStorageAuthHandlerRejectsJSONBody(t *testing.T) {
f := newFixture(t)
user, password := f.user(t)
writes := f.redis.audit.writeCount()
auth := handler.NewAuthHandler(f.auth)
mid := middleware.NewMiddleware(f.auth, "auth-test")
request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", strings.NewReader("{}"))
request.SetBasicAuth(user.Username, password)
request.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
// Exercise the real handler independently of Router wiring.
// This component test does not replace the end-to-end login acceptance test.
mid.RequestID(http.HandlerFunc(auth.CreateSession)).ServeHTTP(recorder, request)
if recorder.Code != http.StatusBadRequest {
t.Errorf("login with JSON body must return 400; received %d", recorder.Code)
}
if f.redis.audit.writeCount() != writes {
t.Error("login with forbidden JSON body attempted to create a session")
}
}