@@ -2,44 +2,61 @@ package credentials
2
2
3
3
import (
4
4
"context"
5
+ "crypto/rand"
6
+ "fmt"
5
7
"testing"
6
8
7
9
"github.com/gptscript-ai/gptscript/pkg/config"
8
10
"github.com/stretchr/testify/require"
9
11
)
10
12
11
13
func 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
+
13
20
credential := Credential {
14
21
Context : credCtx ,
15
- ToolName : "mytestcred" ,
22
+ ToolName : fmt . Sprintf ( "%x" , bytes ) ,
16
23
Type : CredentialTypeTool ,
17
- Env : map [string ]string {"ASDF " : "yeet " },
24
+ Env : map [string ]string {"ENV_VAR " : "value " },
18
25
RefreshToken : "myrefreshtoken" ,
19
26
}
20
27
21
28
cfg , _ := config .ReadCLIConfig ("" )
22
29
30
+ // Set up the store
23
31
store , err := NewDBStore (context .Background (), cfg , []string {credCtx })
24
32
require .NoError (t , err )
25
33
34
+ // Create the credential
26
35
require .NoError (t , store .Add (context .Background (), credential ))
27
36
37
+ // Get the credential
28
38
cred , found , err := store .Get (context .Background (), credential .ToolName )
29
39
require .NoError (t , err )
30
40
require .True (t , found )
31
41
require .Equal (t , credential .Env , cred .Env )
32
42
require .Equal (t , credential .RefreshToken , cred .RefreshToken )
33
43
44
+ // List credentials and check for it
34
45
list , err := store .List (context .Background ())
35
46
require .NoError (t , err )
36
47
require .Greater (t , len (list ), 0 )
48
+
49
+ found = false
37
50
for _ , c := range list {
38
51
if c .Context == credCtx && c .ToolName == credential .ToolName {
39
52
require .Equal (t , credential .Env , c .Env )
40
53
require .Equal (t , credential .RefreshToken , c .RefreshToken )
54
+ found = true
55
+ break
41
56
}
42
57
}
58
+ require .True (t , found )
43
59
60
+ // Delete the credential
44
61
require .NoError (t , store .Remove (context .Background (), credential .ToolName ))
45
62
}
0 commit comments