|
| 1 | +package tenant |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func fixedEnv(m map[string]string) func(string) string { |
| 9 | + return func(k string) string { return m[k] } |
| 10 | +} |
| 11 | + |
| 12 | +func TestResolve_LocalHasNoTenant(t *testing.T) { |
| 13 | + id := Resolve(false, "ignored", Deps{ |
| 14 | + Getenv: fixedEnv(map[string]string{"DEVSTACK_TENANT": "bob"}), |
| 15 | + OSUser: func() (string, error) { return "bob", nil }, |
| 16 | + }) |
| 17 | + if id.IsTenant() || id.Name != "" { |
| 18 | + t.Fatalf("local backend must have no tenant, got %q", id.Name) |
| 19 | + } |
| 20 | + // Qualify is a no-op locally → backward-compatible names. |
| 21 | + if got := id.Qualify("app", "_"); got != "app" { |
| 22 | + t.Errorf("local Qualify = %q, want app", got) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestResolve_RemotePrecedence(t *testing.T) { |
| 27 | + cases := []struct { |
| 28 | + name string |
| 29 | + env map[string]string |
| 30 | + configured string |
| 31 | + osUser string |
| 32 | + osErr error |
| 33 | + want string |
| 34 | + }{ |
| 35 | + {"env wins", map[string]string{"DEVSTACK_TENANT": "Alice.Dev"}, "cfg", "root", nil, "alice-dev"}, |
| 36 | + {"configured next", nil, "Team-One", "root", nil, "team-one"}, |
| 37 | + {"os user fallback", nil, "", "Gustavo.Bertoi", nil, "gustavo-bertoi"}, |
| 38 | + {"nameless fallback", nil, "", "", errors.New("no user"), "user"}, |
| 39 | + } |
| 40 | + for _, tc := range cases { |
| 41 | + t.Run(tc.name, func(t *testing.T) { |
| 42 | + id := Resolve(true, tc.configured, Deps{ |
| 43 | + Getenv: fixedEnv(tc.env), |
| 44 | + OSUser: func() (string, error) { return tc.osUser, tc.osErr }, |
| 45 | + }) |
| 46 | + if id.Name != tc.want { |
| 47 | + t.Errorf("Resolve = %q, want %q", id.Name, tc.want) |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestResolve_JunkConfiguredFallsToUser(t *testing.T) { |
| 54 | + // A configured value that sanitizes to empty must not yield an empty tenant. |
| 55 | + id := Resolve(true, "***", Deps{ |
| 56 | + Getenv: fixedEnv(nil), |
| 57 | + OSUser: func() (string, error) { return "", errors.New("none") }, |
| 58 | + }) |
| 59 | + if id.Name != "user" { |
| 60 | + t.Errorf("junk configured → %q, want user", id.Name) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestQualify_RemoteNamespaces(t *testing.T) { |
| 65 | + id := Identity{Name: "alice"} |
| 66 | + if got := id.Qualify("app", "_"); got != "alice_app" { |
| 67 | + t.Errorf("sql Qualify = %q, want alice_app", got) |
| 68 | + } |
| 69 | + if got := id.Qualify("uploads", "-"); got != "alice-uploads" { |
| 70 | + t.Errorf("bucket Qualify = %q, want alice-uploads", got) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestSanitize(t *testing.T) { |
| 75 | + cases := map[string]string{ |
| 76 | + "Gustavo.Bertoi": "gustavo-bertoi", |
| 77 | + "UPPER_snake": "upper-snake", |
| 78 | + " trim.me ": "trim-me", |
| 79 | + "a@@@b---c": "a-b-c", |
| 80 | + "---leading": "leading", |
| 81 | + "trailing---": "trailing", |
| 82 | + "only***symbols": "only-symbols", |
| 83 | + "***": "", |
| 84 | + "good123": "good123", |
| 85 | + "日本語user": "user", // non-ascii dropped, leaving "user" |
| 86 | + } |
| 87 | + for in, want := range cases { |
| 88 | + if got := Sanitize(in); got != want { |
| 89 | + t.Errorf("Sanitize(%q) = %q, want %q", in, got, want) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestSanitize_LengthCapped(t *testing.T) { |
| 95 | + long := "abcdefghijklmnopqrstuvwxyz0123456789abcdefghij" // 45 chars |
| 96 | + got := Sanitize(long) |
| 97 | + if len(got) > maxLen { |
| 98 | + t.Errorf("Sanitize length = %d, want <= %d", len(got), maxLen) |
| 99 | + } |
| 100 | +} |
0 commit comments