@@ -25,7 +25,8 @@ func TestDBStore(t *testing.T) {
25
25
RefreshToken : "myrefreshtoken" ,
26
26
}
27
27
28
- cfg , _ := config .ReadCLIConfig ("" )
28
+ cfg , err := config .ReadCLIConfig ("" )
29
+ require .NoError (t , err )
29
30
30
31
// Set up the store
31
32
store , err := NewDBStore (context .Background (), cfg , []string {credCtx })
@@ -60,3 +61,74 @@ func TestDBStore(t *testing.T) {
60
61
// Delete the credential
61
62
require .NoError (t , store .Remove (context .Background (), credential .ToolName ))
62
63
}
64
+
65
+ func TestDBStoreStackedContexts (t * testing.T ) {
66
+ const (
67
+ credCtx1 = "testing1"
68
+ credCtx2 = "testing2"
69
+ )
70
+
71
+ bytes := make ([]byte , 16 )
72
+ _ , err := rand .Read (bytes )
73
+ require .NoError (t , err )
74
+
75
+ credential1 := Credential {
76
+ Context : credCtx1 ,
77
+ ToolName : fmt .Sprintf ("%x" , bytes ),
78
+ Type : CredentialTypeTool ,
79
+ Env : map [string ]string {"ENV_VAR" : "value" },
80
+ }
81
+
82
+ credential2 := Credential {
83
+ Context : credCtx2 ,
84
+ ToolName : fmt .Sprintf ("%x" , bytes ),
85
+ Type : CredentialTypeTool ,
86
+ Env : map [string ]string {"ENV_VAR" : "value" },
87
+ }
88
+
89
+ cfg , err := config .ReadCLIConfig ("" )
90
+ require .NoError (t , err )
91
+
92
+ // Set up the stores
93
+ store1 , err := NewDBStore (context .Background (), cfg , []string {credCtx1 })
94
+ require .NoError (t , err )
95
+ store2 , err := NewDBStore (context .Background (), cfg , []string {credCtx2 })
96
+ require .NoError (t , err )
97
+
98
+ // Create both credentials
99
+ require .NoError (t , store1 .Add (context .Background (), credential1 ))
100
+ require .NoError (t , store2 .Add (context .Background (), credential2 ))
101
+
102
+ // Set up a store with both contexts
103
+ storeBoth , err := NewDBStore (context .Background (), cfg , []string {credCtx1 , credCtx2 })
104
+ require .NoError (t , err )
105
+
106
+ // Get the credential. We should get credential1.
107
+ cred , found , err := storeBoth .Get (context .Background (), credential1 .ToolName )
108
+ require .NoError (t , err )
109
+ require .True (t , found )
110
+ require .Equal (t , credential1 .ToolName , cred .ToolName )
111
+ require .Equal (t , credential1 .Context , cred .Context )
112
+ require .Equal (t , credential1 .Env , cred .Env )
113
+
114
+ // List credentials. We should only get credential1.
115
+ list , err := storeBoth .List (context .Background ())
116
+ require .NoError (t , err )
117
+
118
+ found = false
119
+ for _ , c := range list {
120
+ if c .ToolName == credential1 .ToolName {
121
+ require .Equal (t , credential1 .Env , c .Env )
122
+ require .Equal (t , credential1 .Context , c .Context )
123
+ found = true
124
+ break
125
+ } else {
126
+ require .Fail (t , "unexpected credential found" )
127
+ }
128
+ }
129
+ require .True (t , found )
130
+
131
+ // Delete both credentials
132
+ require .NoError (t , store1 .Remove (context .Background (), credential1 .ToolName ))
133
+ require .NoError (t , store2 .Remove (context .Background (), credential2 .ToolName ))
134
+ }
0 commit comments