github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/lib/registry_test.go (about) 1 package lib 2 3 import ( 4 "context" 5 "io/ioutil" 6 "path/filepath" 7 "testing" 8 9 "github.com/ghodss/yaml" 10 11 testkeys "github.com/qri-io/qri/auth/key/test" 12 "github.com/qri-io/qri/config" 13 "github.com/qri-io/qri/profile" 14 "github.com/qri-io/qri/registry/regserver" 15 repotest "github.com/qri-io/qri/repo/test" 16 ) 17 18 // Test that running prove sets the profileID for the user 19 func TestProveProfileKey(t *testing.T) { 20 tr := newTestRunner(t) 21 defer tr.Delete() 22 23 ctx, cancel := context.WithCancel(context.Background()) 24 reg, cleanup, err := regserver.NewTempRegistry(ctx, "temp_registry", "", repotest.NewTestCrypto()) 25 if err != nil { 26 t.Fatal(err) 27 } 28 defer cleanup() 29 defer cancel() 30 31 // Create a mock registry, point our test runner to its URL 32 regClient, _ := regserver.NewMockServerRegistry(*reg) 33 tr.Instance.registry = regClient 34 35 // Get an example peer, and add it to the local profile store 36 keyData := testkeys.GetKeyData(2) 37 pro := &profile.Profile{ 38 Peername: "test_peer", 39 PubKey: keyData.PrivKey.GetPublic(), 40 PrivKey: keyData.PrivKey, 41 ID: profile.IDFromPeerID(keyData.PeerID), 42 } 43 repo := tr.Instance.Repo() 44 pstore := repo.Profiles() 45 err = pstore.SetOwner(ctx, pro) 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 // Set path for config file so that it will serialize to disk 51 configPath := filepath.Join(tr.TmpDir, "qri", "config.yaml") 52 tr.Instance.GetConfig().SetPath(configPath) 53 54 // Call the endpoint to prove our account 55 methods := tr.Instance.Registry() 56 p := RegistryProfile{ 57 Username: pro.Peername, 58 Email: "test_peer@qri.io", 59 Password: "hunter2", 60 } 61 if err = methods.ProveProfileKey(tr.Ctx, &RegistryProfileParams{Profile: &p}); err != nil { 62 t.Error(err) 63 } 64 65 // Peer 3 is used by the mock regserver, it is now used by this peer 66 expectProfileID := profile.IDFromPeerID(testkeys.GetKeyData(3).PeerID) 67 if pro.ID != expectProfileID { 68 t.Errorf("bad profileID for peer after prove. expect: %s, got: %s", expectProfileID, pro.ID) 69 } 70 71 // Read config to ensure it contains the expected values 72 contents, err := ioutil.ReadFile(configPath) 73 if err != nil { 74 t.Fatal(err) 75 } 76 cfgData := config.Config{} 77 err = yaml.Unmarshal(contents, &cfgData) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 // Key created by the test runner (this current node's profile) is testKey[0], it stays the 83 // same after running prove 84 if cfgData.Profile.KeyID != testkeys.GetKeyData(0).EncodedPeerID { 85 t.Errorf("profile's keyID should be testKey[0]") 86 } 87 // ProfileID given to us by registry, by running prove, is testKey[3] 88 if cfgData.Profile.ID != testkeys.GetKeyData(3).EncodedPeerID { 89 t.Errorf("profile's profileID given by prove command should be testKey[3]") 90 } 91 }