119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
package qbt
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// fakeQBittorrent — минимальный стенд WebUI API: требует cookie SID, выдаёт
|
|
// его на /auth/login. Так проверяется и ленивый логин по 403.
|
|
func fakeQBittorrent(t *testing.T, info string) *httptest.Server {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/api/v2/auth/login", func(w http.ResponseWriter, r *http.Request) {
|
|
_ = r.ParseForm()
|
|
if r.PostForm.Get("username") != "admin" || r.PostForm.Get("password") != "secret" {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
return
|
|
}
|
|
http.SetCookie(w, &http.Cookie{Name: "SID", Value: "token", Path: "/"})
|
|
_, _ = w.Write([]byte("Ok."))
|
|
})
|
|
|
|
authed := func(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if c, err := r.Cookie("SID"); err != nil || c.Value != "token" {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
return
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|
|
|
|
mux.HandleFunc("/api/v2/torrents/add", authed(func(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if r.FormValue("category") != "jellybit" || !strings.Contains(r.FormValue("urls"), "magnet:") {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte("Ok."))
|
|
}))
|
|
|
|
mux.HandleFunc("/api/v2/torrents/info", authed(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Query().Get("category") != "jellybit" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(info))
|
|
}))
|
|
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func newClient(t *testing.T, url string) *Client {
|
|
t.Helper()
|
|
c, err := New(Config{URL: url, Username: "admin", Password: "secret"}, nil)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func TestAddPerformsLazyLogin(t *testing.T) {
|
|
srv := fakeQBittorrent(t, "[]")
|
|
c := newClient(t, srv.URL)
|
|
|
|
// Первый вызов без cookie → сервер вернёт 403 → клиент логинится и повторяет.
|
|
err := c.Add(context.Background(), AddRequest{
|
|
URLs: []string{"magnet:?xt=urn:btih:541adcff3b6dd5dba7088ea83317d9d6fac331d6"},
|
|
Category: "jellybit",
|
|
SavePath: "/srv/media/downloads",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Add: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTorrents(t *testing.T) {
|
|
const body = `[{"hash":"541adcff3b6dd5dba7088ea83317d9d6fac331d6","name":"Dune","state":"uploading","save_path":"/srv/media/downloads","content_path":"/srv/media/downloads/Dune","progress":1.0,"amount_left":0}]`
|
|
srv := fakeQBittorrent(t, body)
|
|
c := newClient(t, srv.URL)
|
|
|
|
ts, err := c.Torrents(context.Background(), "jellybit")
|
|
if err != nil {
|
|
t.Fatalf("Torrents: %v", err)
|
|
}
|
|
if len(ts) != 1 {
|
|
t.Fatalf("torrents = %d, want 1", len(ts))
|
|
}
|
|
got := ts[0]
|
|
if got.Hash != "541adcff3b6dd5dba7088ea83317d9d6fac331d6" || got.State != "uploading" {
|
|
t.Errorf("torrent = %+v", got)
|
|
}
|
|
if got.ContentPath != "/srv/media/downloads/Dune" {
|
|
t.Errorf("content_path = %q", got.ContentPath)
|
|
}
|
|
}
|
|
|
|
func TestLoginFailure(t *testing.T) {
|
|
srv := fakeQBittorrent(t, "[]")
|
|
c, err := New(Config{URL: srv.URL, Username: "admin", Password: "wrong"}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 403 → попытка логина с неверным паролем → снова 403 → ошибка.
|
|
if _, err := c.Torrents(context.Background(), "jellybit"); err == nil {
|
|
t.Error("ожидалась ошибка логина")
|
|
}
|
|
}
|