@@ -45,3 +45,57 @@ func TestCredentialExpirationEnv(t *testing.T) {
45
45
}
46
46
}
47
47
}
48
+
49
+ // TestStackedCredentialContexts tests creating, using, listing, showing, and deleting credentials when there are multiple contexts.
50
+ func TestStackedCredentialContexts (t * testing.T ) {
51
+ // First, test credential creation. We will create a credential called testcred in two different contexts called one and two.
52
+ _ , err := RunScript ("scripts/cred_stacked.gpt" , "--sub-tool" , "testcred_one" , "--credential-context" , "one,two" )
53
+ require .NoError (t , err )
54
+
55
+ _ , err = RunScript ("scripts/cred_stacked.gpt" , "--sub-tool" , "testcred_two" , "--credential-context" , "two" )
56
+ require .NoError (t , err )
57
+
58
+ // Next, we try running the testcred_one tool. It should print the value of "testcred" in whichever context it finds the cred first.
59
+ out , err := RunScript ("scripts/cred_stacked.gpt" , "--sub-tool" , "testcred_one" , "--credential-context" , "one,two" )
60
+ require .NoError (t , err )
61
+ require .Contains (t , out , "one" )
62
+ require .NotContains (t , out , "two" )
63
+
64
+ out , err = RunScript ("scripts/cred_stacked.gpt" , "--sub-tool" , "testcred_one" , "--credential-context" , "two,one" )
65
+ require .NoError (t , err )
66
+ require .Contains (t , out , "two" )
67
+ require .NotContains (t , out , "one" )
68
+
69
+ // Next, list credentials and specify both contexts. We should get the credential from the first specified context.
70
+ out , err = GPTScriptExec ("--credential-context" , "one,two" , "cred" )
71
+ require .NoError (t , err )
72
+ require .Contains (t , out , "one" )
73
+ require .NotContains (t , out , "two" )
74
+
75
+ out , err = GPTScriptExec ("--credential-context" , "two,one" , "cred" )
76
+ require .NoError (t , err )
77
+ require .Contains (t , out , "two" )
78
+ require .NotContains (t , out , "one" )
79
+
80
+ // Next, try showing the credentials.
81
+ out , err = GPTScriptExec ("--credential-context" , "one,two" , "cred" , "show" , "testcred" )
82
+ require .NoError (t , err )
83
+ require .Contains (t , out , "one" )
84
+ require .NotContains (t , out , "two" )
85
+
86
+ out , err = GPTScriptExec ("--credential-context" , "two,one" , "cred" , "show" , "testcred" )
87
+ require .NoError (t , err )
88
+ require .Contains (t , out , "two" )
89
+ require .NotContains (t , out , "one" )
90
+
91
+ // Make sure we get an error if we try to delete a credential with multiple contexts specified.
92
+ _ , err = GPTScriptExec ("--credential-context" , "one,two" , "cred" , "delete" , "testcred" )
93
+ require .Error (t , err )
94
+
95
+ // Now actually delete the credentials.
96
+ _ , err = GPTScriptExec ("--credential-context" , "one" , "cred" , "delete" , "testcred" )
97
+ require .NoError (t , err )
98
+
99
+ _ , err = GPTScriptExec ("--credential-context" , "two" , "cred" , "delete" , "testcred" )
100
+ require .NoError (t , err )
101
+ }
0 commit comments