github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/untrack_test.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package engine 5 6 import ( 7 "testing" 8 9 "github.com/keybase/client/go/libkb" 10 keybase1 "github.com/keybase/client/go/protocol/keybase1" 11 ) 12 13 func runUntrack(tc libkb.TestContext, fu *FakeUser, username string, sigVersion libkb.SigVersion) error { 14 arg := UntrackEngineArg{ 15 Username: libkb.NewNormalizedUsername(username), 16 SigVersion: sigVersion, 17 } 18 uis := libkb.UIs{ 19 LogUI: tc.G.UI.GetLogUI(), 20 SecretUI: fu.NewSecretUI(), 21 } 22 eng := NewUntrackEngine(tc.G, &arg) 23 m := libkb.NewMetaContextForTest(tc).WithUIs(uis) 24 return RunEngine2(m, eng) 25 } 26 27 func assertUntracked(tc libkb.TestContext, username string) { 28 me, err := libkb.LoadMe(libkb.NewLoadUserArg(tc.G)) 29 if err != nil { 30 tc.T.Fatal(err) 31 } 32 them, err := libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, username)) 33 if err != nil { 34 tc.T.Fatal(err) 35 } 36 37 m := NewMetaContextForTest(tc) 38 s, err := me.TrackChainLinkFor(m, them.GetNormalizedName(), them.GetUID()) 39 if err != nil { 40 tc.T.Fatal(err) 41 } 42 if s != nil { 43 tc.T.Fatal("expected not to get a tracking statement; but got one") 44 } 45 46 s, err = libkb.LocalTrackChainLinkFor(m, me.GetUID(), them.GetUID()) 47 if err != nil { 48 tc.T.Fatal(err) 49 } 50 if s != nil { 51 tc.T.Fatal("expected not to get a local tracking statement; but got one") 52 } 53 } 54 55 func untrackAlice(tc libkb.TestContext, fu *FakeUser, sigVersion libkb.SigVersion) { 56 err := runUntrack(tc, fu, "t_alice", sigVersion) 57 if err != nil { 58 tc.T.Fatal(err) 59 } 60 } 61 62 func untrackBob(tc libkb.TestContext, fu *FakeUser, sigVersion libkb.SigVersion) { 63 err := runUntrack(tc, fu, "t_bob", sigVersion) 64 if err != nil { 65 tc.T.Fatal(err) 66 } 67 } 68 69 func TestUntrack(t *testing.T) { 70 doWithSigChainVersions(func(sigVersion libkb.SigVersion) { 71 _testUntrack(t, sigVersion) 72 }) 73 } 74 func _testUntrack(t *testing.T, sigVersion libkb.SigVersion) { 75 tc := SetupEngineTest(t, "untrack") 76 defer tc.Cleanup() 77 fu := CreateAndSignupFakeUser(tc, "untrk") 78 79 // Local-tracked only. 80 sv := keybase1.SigVersion(sigVersion) 81 trackAliceWithOptions(tc, fu, keybase1.TrackOptions{LocalOnly: true, BypassConfirm: true, SigVersion: &sv}, fu.NewSecretUI()) 82 assertTracking(tc, "t_alice") 83 untrackAlice(tc, fu, sigVersion) 84 assertUntracked(tc, "t_alice") 85 86 // Remote-tracked only. 87 trackAliceWithOptions(tc, fu, keybase1.TrackOptions{LocalOnly: false, BypassConfirm: true, SigVersion: &sv}, fu.NewSecretUI()) 88 untrackAlice(tc, fu, sigVersion) 89 assertUntracked(tc, "t_alice") 90 91 // Both local- and remote-tracked. 92 trackAliceWithOptions(tc, fu, keybase1.TrackOptions{LocalOnly: true, BypassConfirm: true, SigVersion: &sv}, fu.NewSecretUI()) 93 trackAliceWithOptions(tc, fu, keybase1.TrackOptions{LocalOnly: false, BypassConfirm: true, SigVersion: &sv}, fu.NewSecretUI()) 94 untrackAlice(tc, fu, sigVersion) 95 assertUntracked(tc, "t_alice") 96 97 // Assert that we gracefully handle cases where there is nothing to untrack. 98 err := runUntrack(tc, fu, "t_alice", sigVersion) 99 if err == nil { 100 t.Fatal("expected untrack error; got no error") 101 } else if _, ok := err.(libkb.UntrackError); !ok { 102 t.Fatalf("expected an UntrackError; got %s", err) 103 } 104 105 err = runUntrack(tc, fu, "t_bob", sigVersion) 106 if err == nil { 107 t.Fatal("expected untrack error; got no error") 108 } else if _, ok := err.(libkb.UntrackError); !ok { 109 t.Fatalf("expected an UntrackError; got %s", err) 110 } 111 } 112 113 func TestUntrackRemoteOnly(t *testing.T) { 114 tc := SetupEngineTest(t, "untrack") 115 defer tc.Cleanup() 116 sigVersion := libkb.GetDefaultSigVersion(tc.G) 117 fu := CreateAndSignupFakeUser(tc, "untrk") 118 119 sv := keybase1.SigVersion(sigVersion) 120 trackAliceWithOptions(tc, fu, keybase1.TrackOptions{LocalOnly: false, BypassConfirm: true, SigVersion: &sv}, fu.NewSecretUI()) 121 untrackAlice(tc, fu, sigVersion) 122 assertUntracked(tc, "t_alice") 123 }