Skip to content

Commit e269620

Browse files
alnrclaude
andcommitted
test: e2e coverage for get identity by external ID
Import an identity with an external_id and look it up via 'ory get identity --external-id', asserting the round trip against the Ory Network. Also covers the not-found path. Runs against staging with ORY_RATE_LIMIT_HEADER set. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 028fdb0 commit e269620

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright © 2023 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package identity_test
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"github.com/tidwall/gjson"
14+
15+
"github.com/ory/cli/cmd/cloudx/testhelpers"
16+
"github.com/ory/x/randx"
17+
)
18+
19+
func TestGetIdentityByExternalID(t *testing.T) {
20+
t.Parallel()
21+
22+
// The external_id column is VARCHAR(64), so keep this well under that limit.
23+
externalID := "customer-" + randx.MustString(16, randx.AlphaLowerNum)
24+
email := testhelpers.FakeEmail()
25+
26+
importPath := filepath.Join(t.TempDir(), "import.json")
27+
require.NoError(t, os.WriteFile(importPath, []byte(`{
28+
"schema_id": "preset://username",
29+
"traits": {
30+
"username": "`+email+`"
31+
},
32+
"external_id": "`+externalID+`"
33+
}`), 0o600))
34+
35+
stdout, stderr, err := defaultCmd.Exec(nil, "import", "identities", "--format", "json", "--project", defaultProject.Id, importPath)
36+
require.NoError(t, err, stderr)
37+
imported := gjson.Parse(stdout)
38+
require.True(t, gjson.Valid(stdout))
39+
userID := imported.Get("id").String()
40+
require.NotEmpty(t, userID)
41+
// the import must persist the external ID
42+
assert.Equal(t, externalID, imported.Get("external_id").String(), stdout)
43+
44+
t.Run("is able to get identity by external ID", func(t *testing.T) {
45+
stdout, stderr, err := defaultCmd.Exec(nil, "get", "identity", "--external-id", "--format", "json", "--project", defaultProject.Id, externalID)
46+
require.NoError(t, err, stderr)
47+
out := gjson.Parse(stdout)
48+
assert.True(t, gjson.Valid(stdout))
49+
assert.Equal(t, userID, out.Get("id").String(), stdout)
50+
assert.Equal(t, externalID, out.Get("external_id").String(), stdout)
51+
})
52+
53+
t.Run("fails to get identity by unknown external ID", func(t *testing.T) {
54+
_, _, err := defaultCmd.Exec(nil, "get", "identity", "--external-id", "--project", defaultProject.Id, "unknown-"+randx.MustString(16, randx.AlphaLowerNum))
55+
require.Error(t, err)
56+
})
57+
}

0 commit comments

Comments
 (0)