51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package sonarr
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestCalendarUsesV3APIAndKeepsKeyInHeader(t *testing.T) {
|
||
|
|
var gotQuery string
|
||
|
|
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if r.URL.Path != "/api/v3/calendar" {
|
||
|
|
t.Errorf("path = %q", r.URL.Path)
|
||
|
|
}
|
||
|
|
if got := r.Header.Get("X-Api-Key"); got != "secret" {
|
||
|
|
t.Errorf("X-Api-Key = %q", got)
|
||
|
|
}
|
||
|
|
if r.URL.Query().Get("includeSeries") != "true" ||
|
||
|
|
r.URL.Query().Get("includeEpisodeFile") != "true" {
|
||
|
|
t.Errorf("missing include flags: %s", r.URL.RawQuery)
|
||
|
|
}
|
||
|
|
gotQuery = r.URL.RawQuery
|
||
|
|
w.Header().Set("Content-Type", "application/json")
|
||
|
|
_, _ = w.Write([]byte(`[{"id":7,"seriesId":2,"title":"Arrival","hasFile":true}]`))
|
||
|
|
}))
|
||
|
|
defer upstream.Close()
|
||
|
|
|
||
|
|
client := New(upstream.URL, "secret", time.Second)
|
||
|
|
episodes, err := client.Calendar(
|
||
|
|
context.Background(),
|
||
|
|
time.Date(2026, 7, 27, 0, 0, 0, 0, time.UTC),
|
||
|
|
time.Date(2026, 7, 28, 0, 0, 0, 0, time.UTC),
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(episodes) != 1 || episodes[0].ID != 7 || !episodes[0].HasFile {
|
||
|
|
t.Fatalf("unexpected episodes: %+v", episodes)
|
||
|
|
}
|
||
|
|
if gotQuery == "" || rHasAPIKey(gotQuery) {
|
||
|
|
t.Fatalf("API key leaked into query: %q", gotQuery)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func rHasAPIKey(query string) bool {
|
||
|
|
return strings.Contains(query, "secret")
|
||
|
|
}
|