github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/config_test.go (about)

     1  package lib
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	"github.com/qri-io/qri/config"
    10  	testcfg "github.com/qri-io/qri/config/test"
    11  	"github.com/qri-io/qri/event"
    12  	"github.com/qri-io/qri/p2p"
    13  	testrepo "github.com/qri-io/qri/repo/test"
    14  )
    15  
    16  func TestGetConfig(t *testing.T) {
    17  	ctx, done := context.WithCancel(context.Background())
    18  	defer done()
    19  
    20  	cfg := testcfg.DefaultConfigForTesting()
    21  	mr, err := testrepo.NewTestRepo()
    22  	if err != nil {
    23  		t.Fatalf("error allocating test repo: %s", err)
    24  		return
    25  	}
    26  	node, err := p2p.NewQriNode(mr, cfg.P2P, event.NilBus, nil)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	inst := NewInstanceFromConfigAndNode(ctx, cfg, node)
    32  
    33  	p := &GetConfigParams{Field: "profile.id", Format: "json"}
    34  	res, err := inst.Config().GetConfig(ctx, p)
    35  	if err != nil {
    36  		t.Error(err.Error())
    37  	}
    38  	if !bytes.Equal(res, []byte(`"QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B"`)) {
    39  		t.Errorf("response mismatch. got %s", string(res))
    40  	}
    41  }
    42  
    43  func TestSaveConfigToFile(t *testing.T) {
    44  	ctx, done := context.WithCancel(context.Background())
    45  	defer done()
    46  
    47  	path, err := ioutil.TempDir("", "save_config_test")
    48  	if err != nil {
    49  		t.Fatal(err.Error())
    50  	}
    51  
    52  	cfgPath := path + "/config.yaml"
    53  	cfg := testcfg.DefaultConfigForTesting()
    54  	cfg.SetPath(cfgPath)
    55  	mr, err := testrepo.NewTestRepo()
    56  	if err != nil {
    57  		t.Fatalf("error allocating test repo: %s", err)
    58  		return
    59  	}
    60  	node, err := p2p.NewQriNode(mr, cfg.P2P, event.NilBus, nil)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	inst := NewInstanceFromConfigAndNode(ctx, cfg, node)
    66  
    67  	if _, err := inst.Config().SetConfig(ctx, cfg); err != nil {
    68  		t.Error(err.Error())
    69  	}
    70  }
    71  
    72  func TestSetConfig(t *testing.T) {
    73  	ctx, done := context.WithCancel(context.Background())
    74  	defer done()
    75  
    76  	cfg := testcfg.DefaultConfigForTesting()
    77  	mr, err := testrepo.NewTestRepo()
    78  	if err != nil {
    79  		t.Fatalf("error allocating test repo: %s", err)
    80  		return
    81  	}
    82  	node, err := p2p.NewQriNode(mr, cfg.P2P, event.NilBus, nil)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	inst := NewInstanceFromConfigAndNode(ctx, cfg, node)
    88  	m := inst.Config()
    89  
    90  	if _, err := m.SetConfig(ctx, &config.Config{}); err == nil {
    91  		t.Errorf("expected saving empty config to be invalid")
    92  	}
    93  
    94  	cfg.Profile.Twitter = "@qri_io"
    95  	if _, err := m.SetConfig(ctx, cfg); err != nil {
    96  		t.Error(err.Error())
    97  	}
    98  	p := &GetConfigParams{Field: "profile.twitter", Format: "json"}
    99  	res, err := m.GetConfig(ctx, p)
   100  	if err != nil {
   101  		t.Error(err.Error())
   102  	}
   103  	if !bytes.Equal(res, []byte(`"@qri_io"`)) {
   104  		t.Errorf("response mismatch. got %s", string(res))
   105  	}
   106  }