@@ -2,44 +2,61 @@ package credentials
22
33import (
44 "context"
5+ "crypto/rand"
6+ "fmt"
57 "testing"
68
79 "github.com/gptscript-ai/gptscript/pkg/config"
810 "github.com/stretchr/testify/require"
911)
1012
1113func TestDBStore (t * testing.T ) {
12- const credCtx = "default"
14+ const credCtx = "testing"
15+
16+ bytes := make ([]byte , 16 )
17+ _ , err := rand .Read (bytes )
18+ require .NoError (t , err )
19+
1320 credential := Credential {
1421 Context : credCtx ,
15- ToolName : "mytestcred" ,
22+ ToolName : fmt . Sprintf ( "%x" , bytes ) ,
1623 Type : CredentialTypeTool ,
17- Env : map [string ]string {"ASDF " : "yeet " },
24+ Env : map [string ]string {"ENV_VAR " : "value " },
1825 RefreshToken : "myrefreshtoken" ,
1926 }
2027
2128 cfg , _ := config .ReadCLIConfig ("" )
2229
30+ // Set up the store
2331 store , err := NewDBStore (context .Background (), cfg , []string {credCtx })
2432 require .NoError (t , err )
2533
34+ // Create the credential
2635 require .NoError (t , store .Add (context .Background (), credential ))
2736
37+ // Get the credential
2838 cred , found , err := store .Get (context .Background (), credential .ToolName )
2939 require .NoError (t , err )
3040 require .True (t , found )
3141 require .Equal (t , credential .Env , cred .Env )
3242 require .Equal (t , credential .RefreshToken , cred .RefreshToken )
3343
44+ // List credentials and check for it
3445 list , err := store .List (context .Background ())
3546 require .NoError (t , err )
3647 require .Greater (t , len (list ), 0 )
48+
49+ found = false
3750 for _ , c := range list {
3851 if c .Context == credCtx && c .ToolName == credential .ToolName {
3952 require .Equal (t , credential .Env , c .Env )
4053 require .Equal (t , credential .RefreshToken , c .RefreshToken )
54+ found = true
55+ break
4156 }
4257 }
58+ require .True (t , found )
4359
60+ // Delete the credential
4461 require .NoError (t , store .Remove (context .Background (), credential .ToolName ))
4562}
0 commit comments