Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville committed Sep 16, 2024
1 parent 92e81dd commit d2442f4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
54 changes: 54 additions & 0 deletions integration/cred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,57 @@ func TestCredentialExpirationEnv(t *testing.T) {
}
}
}

// TestStackedCredentialContexts tests creating, using, listing, showing, and deleting credentials when there are multiple contexts.
func TestStackedCredentialContexts(t *testing.T) {
// First, test credential creation. We will create a credential called testcred in two different contexts called one and two.
_, err := RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_one", "--credential-context", "one,two")
require.NoError(t, err)

_, err = RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_two", "--credential-context", "two")
require.NoError(t, err)

// Next, we try running the testcred_one tool. It should print the value of "testcred" in whichever context it finds the cred first.
out, err := RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_one", "--credential-context", "one,two")
require.NoError(t, err)
require.Contains(t, out, "one")
require.NotContains(t, out, "two")

out, err = RunScript("scripts/cred_stacked.gpt", "--sub-tool", "testcred_one", "--credential-context", "two,one")
require.NoError(t, err)
require.Contains(t, out, "two")
require.NotContains(t, out, "one")

// Next, list credentials and specify both contexts. We should get the credential from the first specified context.
out, err = GPTScriptExec("--credential-context", "one,two", "cred")
require.NoError(t, err)
require.Contains(t, out, "one")
require.NotContains(t, out, "two")

out, err = GPTScriptExec("--credential-context", "two,one", "cred")
require.NoError(t, err)
require.Contains(t, out, "two")
require.NotContains(t, out, "one")

// Next, try showing the credentials.
out, err = GPTScriptExec("--credential-context", "one,two", "cred", "show", "testcred")
require.NoError(t, err)
require.Contains(t, out, "one")
require.NotContains(t, out, "two")

out, err = GPTScriptExec("--credential-context", "two,one", "cred", "show", "testcred")
require.NoError(t, err)
require.Contains(t, out, "two")
require.NotContains(t, out, "one")

// Make sure we get an error if we try to delete a credential with multiple contexts specified.
_, err = GPTScriptExec("--credential-context", "one,two", "cred", "delete", "testcred")
require.Error(t, err)

// Now actually delete the credentials.
_, err = GPTScriptExec("--credential-context", "one", "cred", "delete", "testcred")
require.NoError(t, err)

_, err = GPTScriptExec("--credential-context", "two", "cred", "delete", "testcred")
require.NoError(t, err)
}
36 changes: 36 additions & 0 deletions integration/scripts/cred_stacked.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: testcred_one
credential: cred_one as testcred

#!python3

import os

print(os.environ.get("VALUE"))

---
name: testcred_two
credential: cred_two as testcred

#!python3

import os

print(os.environ.get("VALUE"))

---
name: cred_one

#!python3

import json

print(json.dumps({"env": {"VALUE": "one"}}))

---
name: cred_two

#!python3

import json

print(json.dumps({"env": {"VALUE": "two"}}))

0 comments on commit d2442f4

Please sign in to comment.