github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/storage_test.go (about) 1 package teams 2 3 import ( 4 "testing" 5 6 "github.com/keybase/client/go/kbtest" 7 "github.com/keybase/client/go/libkb" 8 "github.com/keybase/client/go/protocol/keybase1" 9 storage "github.com/keybase/client/go/teams/storage" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func getStorageFromG(g *libkb.GlobalContext) *storage.Storage { 14 tl := g.GetTeamLoader().(*TeamLoader) 15 return tl.storage 16 } 17 18 // Storage can get from memory 19 func TestStorageMem(t *testing.T) { 20 tc := SetupTest(t, "team", 1) 21 defer tc.Cleanup() 22 23 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 24 require.NoError(t, err) 25 26 for _, public := range []bool{false, true} { 27 teamID := NewSubteamID(false /*public*/) 28 st := getStorageFromG(tc.G) 29 mctx := libkb.NewMetaContextForTest(tc) 30 obj := keybase1.TeamData{ 31 Chain: keybase1.TeamSigChainState{ 32 Id: teamID, 33 Public: public, 34 }, 35 } 36 37 res, _, _ := st.Get(mctx, teamID, public) 38 require.Nil(t, res) 39 st.Put(mctx, &obj) 40 res, _, _ = st.Get(mctx, teamID, public) 41 require.NotNil(t, res, "cache miss") 42 require.True(t, res == &obj, "should be the same obj from mem") 43 } 44 } 45 46 // Storage can get from disk. 47 func TestStorageDisk(t *testing.T) { 48 tc := SetupTest(t, "team", 1) 49 defer tc.Cleanup() 50 51 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 52 require.NoError(t, err) 53 54 for _, public := range []bool{false, true} { 55 teamID := NewSubteamID(false /*public*/) 56 st := getStorageFromG(tc.G) 57 mctx := libkb.NewMetaContextForTest(tc) 58 obj := keybase1.TeamData{ 59 Chain: keybase1.TeamSigChainState{ 60 Id: teamID, 61 Public: public, 62 }, 63 } 64 65 res, _, _ := st.Get(mctx, teamID, public) 66 require.Nil(t, res) 67 st.Put(mctx, &obj) 68 t.Logf("throwing out mem storage") 69 st.ClearMem() 70 res, _, _ = st.Get(mctx, teamID, public) 71 require.NotNil(t, res, "cache miss") 72 require.False(t, res == &obj, "should be the a different object read from disk") 73 require.Equal(t, teamID, res.Chain.Id) 74 require.Equal(t, public, res.Chain.Public) 75 } 76 } 77 78 // Switching users should render other user's cache inaccessible. 79 func TestStorageLogout(t *testing.T) { 80 tc := SetupTest(t, "team", 1) 81 defer tc.Cleanup() 82 83 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 84 require.NoError(t, err) 85 86 for _, public := range []bool{false, true} { 87 teamID := NewSubteamID(false /*public*/) 88 st := getStorageFromG(tc.G) 89 obj := keybase1.TeamData{ 90 Chain: keybase1.TeamSigChainState{ 91 Id: teamID, 92 Public: public, 93 }, 94 } 95 mctx := libkb.NewMetaContextForTest(tc) 96 st.Put(mctx, &obj) 97 res, _, _ := st.Get(mctx, teamID, public) 98 require.NotNil(t, res, "cache miss") 99 require.True(t, res == &obj, "should be the same obj from mem") 100 101 t.Logf("logout") 102 err = tc.Logout() 103 require.NoError(t, err) 104 105 require.Equal(t, 0, st.MemSize(), "mem cache still populated") 106 107 res, _, _ = st.Get(mctx, teamID, public) 108 require.Nil(t, res, "got from cache, but should be gone") 109 110 t.Logf("login as someone else") 111 _, err = kbtest.CreateAndSignupFakeUser("team", tc.G) 112 require.NoError(t, err) 113 114 res, _, _ = st.Get(mctx, teamID, public) 115 require.Nil(t, res, "got from cache, but should be gone") 116 } 117 } 118 119 func TestStorageUpdate(t *testing.T) { 120 tc := SetupTest(t, "team", 1) 121 defer tc.Cleanup() 122 123 for _, public := range []bool{false, true} { 124 _, err := kbtest.CreateAndSignupFakeUser("team", tc.G) 125 require.NoError(t, err) 126 127 teamID := NewSubteamID(false /*public*/) 128 st := getStorageFromG(tc.G) 129 130 t.Logf("store 1") 131 team := &keybase1.TeamData{ 132 Chain: keybase1.TeamSigChainState{ 133 Id: teamID, 134 Public: public, 135 }, 136 CachedAt: keybase1.ToTime(tc.G.Clock().Now()), 137 } 138 mctx := libkb.NewMetaContextForTest(tc) 139 st.Put(mctx, team) 140 141 t.Logf("get 1") 142 team, _, _ = st.Get(mctx, teamID, public) 143 require.NotNil(t, team) 144 145 t.Logf("store updated") 146 t.Logf("cache pre-set cachedAt:%v", team.CachedAt.Time()) 147 newTime := keybase1.ToTime(tc.G.Clock().Now().Add(freshnessLimit * -2)).Time() 148 require.False(t, newTime.Equal(team.CachedAt.Time())) 149 team.CachedAt = keybase1.ToTime(newTime) 150 t.Logf("cache post-set cachedAt:%v", team.CachedAt.Time()) 151 require.True(t, newTime.Equal(team.CachedAt.Time()), "%v != %v", newTime, team.CachedAt.Time()) 152 153 t.Logf("get updated") 154 team, _, _ = st.Get(mctx, teamID, public) 155 require.NotNil(t, team) 156 157 require.True(t, newTime.Equal(team.CachedAt.Time())) 158 } 159 }