lbry.go/extras/jsonrpc/daemon_test.go

418 lines
10 KiB
Go
Raw Permalink Normal View History

2017-09-12 18:04:45 +02:00
package jsonrpc
import (
"encoding/json"
2019-01-17 21:38:45 +01:00
"fmt"
2019-05-28 18:29:14 +02:00
"os"
2019-01-17 21:38:45 +01:00
"strconv"
"strings"
2017-09-12 18:04:45 +02:00
"testing"
"time"
2019-01-17 21:38:45 +01:00
"github.com/shopspring/decimal"
"github.com/lbryio/lbry.go/extras/util"
2017-09-12 18:04:45 +02:00
)
func prettyPrint(i interface{}) {
s, _ := json.MarshalIndent(i, "", "\t")
fmt.Println(string(s))
}
func TestClient_AccountFund(t *testing.T) {
d := NewClient("")
accounts, err := d.AccountList()
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
account := (accounts.LBCRegtest)[0].ID
balanceString, err := d.AccountBalance(&account)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
balance, err := strconv.ParseFloat(string(*balanceString), 64)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
2019-06-12 02:54:18 +02:00
got, err := d.AccountFund(account, account, fmt.Sprintf("%f", balance/2.0), 40, false)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
2019-01-17 21:38:45 +01:00
func TestClient_AccountList(t *testing.T) {
d := NewClient("")
got, err := d.AccountList()
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
2019-01-17 21:38:45 +01:00
}
prettyPrint(*got)
2019-01-17 21:38:45 +01:00
}
func TestClient_SingleAccountList(t *testing.T) {
d := NewClient("")
createdAccount, err := d.AccountCreate("test"+fmt.Sprintf("%d", time.Now().Unix())+"@lbry.com", false)
if err != nil {
t.Fatal(err)
2019-05-28 18:29:14 +02:00
return
}
account, err := d.SingleAccountList(createdAccount.ID)
if err != nil {
t.Fatal(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*account)
}
2019-01-17 21:38:45 +01:00
func TestClient_AccountBalance(t *testing.T) {
d := NewClient("")
got, err := d.AccountBalance(nil)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
2019-01-17 21:38:45 +01:00
}
prettyPrint(*got)
2019-01-17 21:38:45 +01:00
}
func TestClient_AddressUnused(t *testing.T) {
d := NewClient("")
got, err := d.AddressUnused(nil)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_ChannelList(t *testing.T) {
d := NewClient("")
2019-02-22 15:54:52 +01:00
got, err := d.ChannelList(nil, 1, 50)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_StreamCreate(t *testing.T) {
2019-05-28 18:29:14 +02:00
_ = os.Setenv("BLOCKCHAIN_NAME", "lbrycrd_regtest")
d := NewClient("")
addressResponse, err := d.AddressUnused(nil)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
address := string(*addressResponse)
2019-05-28 18:29:14 +02:00
got, err := d.StreamCreate("test"+fmt.Sprintf("%d", time.Now().Unix()), "/home/niko/Downloads/IMG_20171012_205120.jpg", 14.37, StreamCreateOptions{
ClaimCreateOptions: ClaimCreateOptions{
Title: util.PtrToString("This is a Test Title" + fmt.Sprintf("%d", time.Now().Unix())),
Description: util.PtrToString("My Special Description"),
Tags: []string{"nsfw", "test"},
Languages: []string{"en-US", "fr-CH"},
Locations: []Location{{
Country: util.PtrToString("CH"),
State: util.PtrToString("Ticino"),
City: util.PtrToString("Lugano"),
PostalCode: util.PtrToString("6900"),
Latitude: nil,
Longitude: nil,
}},
ThumbnailURL: util.PtrToString("https://scrn.storni.info/2019-01-18_16-37-39-098537783.png"),
AccountID: nil,
ClaimAddress: &address,
Preview: nil,
},
Fee: &Fee{
FeeCurrency: "LBC",
FeeAmount: decimal.NewFromFloat(1.0),
FeeAddress: &address,
},
Author: util.PtrToString("Niko"),
License: util.PtrToString("FREE"),
LicenseURL: nil,
ReleaseTime: nil,
Duration: nil,
Preview: nil,
AllowDuplicateName: nil,
ChannelName: nil,
ChannelID: util.PtrToString("dd0b559b4f17a7af984f173efb5438534eb6ab27"),
ChannelAccountID: nil,
})
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_ChannelCreate(t *testing.T) {
d := NewClient("")
got, err := d.ChannelCreate("@Test"+fmt.Sprintf("%d", time.Now().Unix()), 13.37, ChannelCreateOptions{
ClaimCreateOptions: ClaimCreateOptions{
Title: util.PtrToString("Mess with the channels"),
Description: util.PtrToString("And you'll get what you deserve"),
Tags: []string{"we", "got", "tags"},
Languages: []string{"en-US"},
Locations: []Location{{
Country: util.PtrToString("CH"),
State: util.PtrToString("Ticino"),
City: util.PtrToString("Lugano"),
}},
ThumbnailURL: util.PtrToString("https://scrn.storni.info/2019-04-12_15-43-25-001592625.png"),
},
Email: util.PtrToString("niko@lbry.com"),
WebsiteURL: util.PtrToString("https://lbry.com"),
CoverURL: util.PtrToString("https://scrn.storni.info/2019-04-12_15-43-25-001592625.png"),
})
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_ChannelUpdate(t *testing.T) {
d := NewClient("")
2019-05-28 18:29:14 +02:00
got, err := d.ChannelUpdate("2a8b6d061c5ecb2515f1dd7e04729e9fafac660d", ChannelUpdateOptions{
ClearLanguages: util.PtrToBool(true),
ClearLocations: util.PtrToBool(true),
ClearTags: util.PtrToBool(true),
ChannelCreateOptions: ChannelCreateOptions{
ClaimCreateOptions: ClaimCreateOptions{
Title: util.PtrToString("Mess with the channels"),
Description: util.PtrToString("And you'll get what you deserve"),
Tags: []string{"we", "got", "more", "tags"},
Languages: []string{"en-US"},
Locations: []Location{{
Country: util.PtrToString("CH"),
State: util.PtrToString("Ticino"),
City: util.PtrToString("Lugano"),
}},
ThumbnailURL: util.PtrToString("https://scrn.storni.info/2019-04-12_15-43-25-001592625.png"),
},
Email: util.PtrToString("niko@lbry.com"),
WebsiteURL: util.PtrToString("https://lbry.com"),
CoverURL: util.PtrToString("https://scrn.storni.info/2019-04-12_15-43-25-001592625.png"),
}})
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_ChannelAbandon(t *testing.T) {
d := NewClient("")
channelName := "@TestToDelete" + fmt.Sprintf("%d", time.Now().Unix())
channelResponse, err := d.ChannelCreate(channelName, 13.37, ChannelCreateOptions{
ClaimCreateOptions: ClaimCreateOptions{
Title: util.PtrToString("Mess with the channels"),
Description: util.PtrToString("And you'll get what you deserve"),
Tags: []string{"we", "got", "tags"},
Languages: []string{"en-US"},
Locations: []Location{{
Country: util.PtrToString("CH"),
State: util.PtrToString("Ticino"),
City: util.PtrToString("Lugano"),
}},
ThumbnailURL: util.PtrToString("https://scrn.storni.info/2019-04-12_15-43-25-001592625.png"),
},
Email: util.PtrToString("niko@lbry.com"),
WebsiteURL: util.PtrToString("https://lbry.com"),
CoverURL: util.PtrToString("https://scrn.storni.info/2019-04-12_15-43-25-001592625.png"),
})
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
txID := channelResponse.Outputs[0].Txid
nout := channelResponse.Outputs[0].Nout
time.Sleep(10 * time.Second)
got, err := d.ChannelAbandon(txID, nout, nil, false)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_AddressList(t *testing.T) {
d := NewClient("")
got, err := d.AddressList(nil)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_ClaimList(t *testing.T) {
2019-05-28 18:29:14 +02:00
_ = os.Setenv("BLOCKCHAIN_NAME", "lbrycrd_regtest")
d := NewClient("")
got, err := d.ClaimList(nil, 1, 10)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_ClaimSearch(t *testing.T) {
d := NewClient("")
2019-05-28 18:29:14 +02:00
got, err := d.ClaimSearch(nil, util.PtrToString("2a8b6d061c5ecb2515f1dd7e04729e9fafac660d"), nil, nil)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_Status(t *testing.T) {
d := NewClient("")
got, err := d.Status()
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_UTXOList(t *testing.T) {
d := NewClient("")
got, err := d.UTXOList(nil)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_Version(t *testing.T) {
d := NewClient("")
got, err := d.Version()
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_GetFile(t *testing.T) {
2019-05-28 19:27:04 +02:00
_ = os.Setenv("BLOCKCHAIN_NAME", "lbrycrd_regtest")
d := NewClient("")
2019-05-28 18:29:14 +02:00
got, err := d.Get("lbry://test1559058649")
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
func TestClient_FileList(t *testing.T) {
2019-05-28 18:29:14 +02:00
_ = os.Setenv("BLOCKCHAIN_NAME", "lbrycrd_regtest")
2019-02-22 15:54:52 +01:00
d := NewClient("")
got, err := d.FileList()
2019-02-22 15:54:52 +01:00
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
2019-02-22 15:54:52 +01:00
}
prettyPrint(*got)
}
func TestClient_Resolve(t *testing.T) {
2019-05-28 19:27:04 +02:00
_ = os.Setenv("BLOCKCHAIN_NAME", "lbrycrd_regtest")
d := NewClient("")
2019-05-28 18:29:14 +02:00
got, err := d.Resolve("test1559058649")
2019-02-22 15:54:52 +01:00
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
2019-02-22 15:54:52 +01:00
}
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
2019-02-22 15:54:52 +01:00
}
prettyPrint(*got)
2019-02-22 15:54:52 +01:00
}
2019-04-10 23:23:24 +02:00
func TestClient_AccountSet(t *testing.T) {
d := NewClient("")
accounts, err := d.AccountList()
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
account := (accounts.LBCRegtest)[0].ID
2019-06-12 22:18:03 +02:00
got, err := d.AccountSet(account, AccountSettings{ChangeMaxUses: util.PtrToInt(10000)})
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
prettyPrint(*got)
}
2019-05-21 20:17:22 +02:00
2019-04-10 23:23:24 +02:00
func TestClient_AccountCreate(t *testing.T) {
d := NewClient("")
2019-05-21 20:17:22 +02:00
name := "test" + fmt.Sprintf("%d", time.Now().Unix()) + "@lbry.com"
account, err := d.AccountCreate(name, false)
2019-04-10 23:23:24 +02:00
if err != nil {
2019-05-21 20:17:22 +02:00
t.Fatal(err)
2019-05-28 18:29:14 +02:00
return
2019-05-21 20:17:22 +02:00
}
if account.Name != name {
t.Errorf("account name mismatch, expected %q, got %q", name, account.Name)
2019-05-28 18:29:14 +02:00
return
2019-04-10 23:23:24 +02:00
}
prettyPrint(*account)
2019-04-10 23:23:24 +02:00
}
func TestClient_AccountRemove(t *testing.T) {
d := NewClient("")
createdAccount, err := d.AccountCreate("test"+fmt.Sprintf("%d", time.Now().Unix())+"@lbry.com", false)
if err != nil {
t.Fatal(err)
2019-05-28 18:29:14 +02:00
return
}
removedAccount, err := d.AccountRemove(createdAccount.ID)
if err != nil {
t.Error(err)
2019-05-28 18:29:14 +02:00
return
}
2019-05-22 19:58:42 +02:00
if removedAccount.ID != createdAccount.ID {
t.Error("accounts IDs mismatch")
}
2019-05-22 19:58:42 +02:00
account, err := d.SingleAccountList(createdAccount.ID)
2019-05-28 18:29:14 +02:00
if err != nil {
if strings.HasPrefix(err.Error(), "Error in daemon: Couldn't find account") {
prettyPrint(*removedAccount)
return
}
t.Error(err)
return
}
2019-05-28 18:29:14 +02:00
t.Error("account was not removed")
2019-05-22 19:58:42 +02:00
prettyPrint(*account)
}
2019-07-26 05:33:52 +02:00
func TestClient_ChannelExport(t *testing.T) {
d := NewClient("")
channelClaimID := "f1ad0d72eae6b471cfd72b7956768c0c191b22d7"
response, err := d.ChannelExport(channelClaimID, nil, nil)
if err != nil {
t.Error(err)
}
if response == nil || len(*response) == 0 {
t.Error("nothing returned!")
}
t.Log("Export:", *response)
}