//go:build manual

package teller_test

import (
	"crypto/tls"
	"io"
	"net/http"
	"testing"
	"time"

	"gogs.inhome.blapointe.com/ana-ledger/src/bank/teller"
)

func TestIntegration(t *testing.T) {
	teller.Tokens = "test_token_bfu2cyvq3il6o"

	//curl --cert certificate.pem --cert-key private_key.pem   --auth test_token_bfu2cyvq3il6o: https://api.teller.io/accounts
	cert, err := tls.LoadX509KeyPair("./certificate.pem", "./private_key.pem")
	if err != nil {
		t.Fatal(err)
	}

	c := &http.Client{
		Timeout: time.Second,
		Transport: &http.Transport{
			DisableKeepAlives: true,
			TLSClientConfig:   &tls.Config{Certificates: []tls.Certificate{cert}},
		},
	}
	//curl --cert certificate.pem --cert-key private_key.pem   --auth test_token_bfu2cyvq3il6o: https://api.teller.io/accounts

	for _, url := range []string{
		"https://api.teller.io/accounts",
		"https://api.teller.io/accounts/acc_pdvv4810fi9hmrcn6g000/transactions",
	} {
		url := url
		t.Run(url, func(t *testing.T) {
			req, err := http.NewRequest(http.MethodGet, url, nil)
			if err != nil {
				t.Fatal(err)
			}
			req.SetBasicAuth("test_token_bfu2cyvq3il6o", "")

			resp, err := c.Do(req)
			if err != nil {
				t.Fatal(err)
			}
			defer resp.Body.Close()

			body, _ := io.ReadAll(resp.Body)
			if code := resp.StatusCode; code >= 300 {
				t.Fatalf("(%d) %s", code, body)
			}

			t.Logf("(%d) %s", resp.StatusCode, body)
		})
	}
}
