Remove public registration, support administrator-managed accounts, and persist personal preferences. Refs #2 #5 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
61 lines
2.1 KiB
Go
61 lines
2.1 KiB
Go
package api_test
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/api"
|
|
"github.com/tradarr/backend/internal/api/handlers"
|
|
"github.com/tradarr/backend/internal/auth"
|
|
"github.com/tradarr/backend/internal/config"
|
|
)
|
|
|
|
func TestPreferencesEndpointsExistAndRejectInvalidValues(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
token, err := auth.GenerateToken("user-1", "person@example.test", "user", "preferences-test-secret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
router := api.SetupRouter(handlers.New(nil, &config.Config{JWTSecret: "preferences-test-secret"}, nil, nil, nil, nil), "preferences-test-secret")
|
|
for _, tc := range []struct {
|
|
method, body string
|
|
want int
|
|
}{
|
|
{http.MethodGet, "", http.StatusOK},
|
|
{http.MethodPut, `{"language":"de","ranking_mode":"title"}`, http.StatusBadRequest},
|
|
{http.MethodPut, `{"language":"fr","ranking_mode":"unknown"}`, http.StatusBadRequest},
|
|
} {
|
|
req := httptest.NewRequest(tc.method, "/api/me/preferences", bytes.NewBufferString(tc.body))
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
if tc.method == http.MethodPut {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
if tc.method == http.MethodGet {
|
|
if w.Code == http.StatusNotFound {
|
|
t.Errorf("GET /api/me/preferences is not registered; got status %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
continue
|
|
}
|
|
if w.Code != tc.want {
|
|
t.Errorf("%s /api/me/preferences got status %d, want %d; body=%s", tc.method, w.Code, tc.want, w.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreferencesRequireAuthentication(t *testing.T) {
|
|
router := api.SetupRouter(handlers.New(nil, &config.Config{}, nil, nil, nil, nil), "preferences-test-secret")
|
|
for _, method := range []string{http.MethodGet, http.MethodPut} {
|
|
req := httptest.NewRequest(method, "/api/me/preferences", bytes.NewBufferString(`{"language":"fr","ranking_mode":"title"}`))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("%s: status=%d", method, w.Code)
|
|
}
|
|
}
|
|
}
|