mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
Feature: Get size by api for ptp btn and ggn (#66)
* chore: add package * feat: get size by api for ptp and btn * feat: download and parse torrent if not api * feat: bypass tls check and load meta from file * fix: no invite command needed for btn * feat: add ggn api * feat: imrpove logging * feat: build request url * feat: improve err logging
This commit is contained in:
parent
d2aa7c1e7e
commit
2ea2293745
32 changed files with 2181 additions and 99 deletions
175
pkg/btn/btn_test.go
Normal file
175
pkg/btn/btn_test.go
Normal file
|
@ -0,0 +1,175 @@
|
|||
package btn
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func TestAPI(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
ts := httptest.NewServer(mux)
|
||||
defer ts.Close()
|
||||
|
||||
key := "mock-key"
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// request validation logic
|
||||
//apiKey := r.Header.Get("ApiKey")
|
||||
//if apiKey != key {
|
||||
// w.WriteHeader(http.StatusUnauthorized)
|
||||
// w.Write(nil)
|
||||
// return
|
||||
//}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := ioutil.ReadFile("testdata/btn_get_user_info.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
})
|
||||
|
||||
type fields struct {
|
||||
Url string
|
||||
APIKey string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want bool
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test_user",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
},
|
||||
want: true,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewClient(tt.fields.Url, tt.fields.APIKey)
|
||||
|
||||
got, err := c.TestAPI()
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_GetTorrentByID(t *testing.T) {
|
||||
// disable logger
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
ts := httptest.NewServer(mux)
|
||||
defer ts.Close()
|
||||
|
||||
key := "mock-key"
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("Expected 'POST' reqeust, got '%v'", r.Method)
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Errorf("expected error to be nil got %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(string(data), "1555073") {
|
||||
//t.Errorf(
|
||||
// `response body "%s" does not contain "1555073"`,
|
||||
// string(data),
|
||||
//)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(string(data), key) {
|
||||
jsonPayload, _ := ioutil.ReadFile("testdata/btn_bad_creds.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write(jsonPayload)
|
||||
return
|
||||
}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := ioutil.ReadFile("testdata/btn_get_torrent_by_id.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
})
|
||||
|
||||
type fields struct {
|
||||
Url string
|
||||
APIKey string
|
||||
}
|
||||
type args struct {
|
||||
torrentID string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want *domain.TorrentBasic
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "btn_get_torrent_by_id",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
},
|
||||
args: args{torrentID: "1555073"},
|
||||
want: &domain.TorrentBasic{
|
||||
Id: "",
|
||||
TorrentId: "1555073",
|
||||
InfoHash: "56CD94119F6BF7FC294A92D7A4099C3D1815C907",
|
||||
Size: "3288852849",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "btn_get_torrent_by_id_not_found",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
},
|
||||
args: args{torrentID: "9555073"},
|
||||
want: nil,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
c := NewClient(tt.fields.Url, tt.fields.APIKey)
|
||||
|
||||
got, err := c.GetTorrentByID(tt.args.torrentID)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue