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

     1  package config_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/qri-io/qri/config"
     9  	testcfg "github.com/qri-io/qri/config/test"
    10  	"github.com/qri-io/qri/dsref"
    11  )
    12  
    13  func TestProfileValidate(t *testing.T) {
    14  	err := testcfg.DefaultProfileForTesting().Validate()
    15  	if err != nil {
    16  		t.Errorf("error validating default profile: %s", err)
    17  	}
    18  }
    19  
    20  func TestProfileCopy(t *testing.T) {
    21  	cases := []struct {
    22  		profile *config.ProfilePod
    23  	}{
    24  		{&config.ProfilePod{Type: "peer"}},
    25  		{&config.ProfilePod{Name: "user", Color: "blue"}},
    26  		{&config.ProfilePod{Type: "peer", Name: "user", Color: "blue"}},
    27  	}
    28  	for i, c := range cases {
    29  		cpy := c.profile.Copy()
    30  		if !reflect.DeepEqual(cpy, c.profile) {
    31  			t.Errorf("ProfilePod Copy test case %v, profile structs are not equal: \ncopy: %v, \noriginal: %v", i, cpy, c.profile)
    32  			continue
    33  		}
    34  	}
    35  }
    36  
    37  func TestProfileCopyPeerIDs(t *testing.T) {
    38  	// build off DefaultProfile so we can test that the profile Copy
    39  	// actually copies over correctly (ie, deeply)
    40  	p := testcfg.DefaultProfileForTesting()
    41  	p.PeerIDs = []string{"1", "2", "3"}
    42  
    43  	cpy := p.Copy()
    44  	if !reflect.DeepEqual(cpy, p) {
    45  		t.Errorf("ProfilePodCopyPeerIDs, structs are not equal: \ncopy: %v, \noriginal: %v", cpy, p)
    46  	}
    47  	cpy.PeerIDs[0] = ""
    48  	if reflect.DeepEqual(cpy, p) {
    49  		t.Errorf("ProfilePodCopyPeerIDs, editing one profile struct should not affect the other: \ncopy: %v, \noriginal: %v", cpy, p)
    50  	}
    51  }
    52  
    53  func TestProfileSetField(t *testing.T) {
    54  	p := config.ProfilePod{
    55  		Created: time.Now(),
    56  		Updated: time.Now(),
    57  		Type:    "peer",
    58  	}
    59  	p.SetField("email", "user@example.com")
    60  
    61  	expect := config.ProfilePod{
    62  		Created: p.Created,
    63  		Updated: p.Updated,
    64  		Type:    "peer",
    65  		Email:   "user@example.com",
    66  	}
    67  	if !reflect.DeepEqual(p, expect) {
    68  		t.Errorf("ProfilePod SetField email, structs are not equal: \nactual: %v, \nexpect: %v", p, expect)
    69  	}
    70  
    71  	p.SetField("name", "user")
    72  	expect = config.ProfilePod{
    73  		Created: p.Created,
    74  		Updated: p.Updated,
    75  		Type:    "peer",
    76  		Email:   "user@example.com",
    77  		Name:    "user",
    78  	}
    79  	if !reflect.DeepEqual(p, expect) {
    80  		t.Errorf("ProfilePod SetField name, structs are not equal: \nactual: %v, \nexpect: %v", p, expect)
    81  	}
    82  
    83  	p.SetField("email", "me@example.com")
    84  	expect = config.ProfilePod{
    85  		Created: p.Created,
    86  		Updated: p.Updated,
    87  		Type:    "peer",
    88  		Email:   "me@example.com",
    89  		Name:    "user",
    90  	}
    91  	if !reflect.DeepEqual(p, expect) {
    92  		t.Errorf("ProfilePod SetField email again, structs are not equal: \nactual: %v, \nexpect: %v", p, expect)
    93  	}
    94  }
    95  
    96  func TestBadPeername(t *testing.T) {
    97  	pro := config.ProfilePod{
    98  		Type:     "peer",
    99  		Peername: "bad=name",
   100  	}
   101  
   102  	err := pro.Validate()
   103  	if err == nil {
   104  		t.Fatal("expected error, did not get one")
   105  	}
   106  	expectErr := dsref.ErrDescribeValidUsername.Error()
   107  	if err.Error() != expectErr {
   108  		t.Errorf("error mismatch, expect: %s, got: %s", expectErr, err.Error())
   109  	}
   110  }