|
| 1 | +package tags |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/databricks/databricks-sdk-go/qa" |
| 8 | +) |
| 9 | + |
| 10 | +// These tests verify that the governed-tag key is percent-encoded when it is |
| 11 | +// sent as a URL *path* parameter, so a hierarchical key containing "/" (e.g. |
| 12 | +// "Field/Shared Technical Services") routes as a single path segment rather |
| 13 | +// than being split into multiple segments (which resolves to no endpoint). |
| 14 | +// The qa fixture matches on the raw request URI, so a passing match proves the |
| 15 | +// "/" was encoded to %2F. |
| 16 | + |
| 17 | +func TestGetTagPolicyEncodesPathKey(t *testing.T) { |
| 18 | + requestMocks := qa.HTTPFixtures{ |
| 19 | + { |
| 20 | + Method: "GET", |
| 21 | + Resource: "/api/2.1/tag-policies/Field%2FShared%20Technical%20Services?", |
| 22 | + Response: TagPolicy{TagKey: "Field/Shared Technical Services"}, |
| 23 | + }, |
| 24 | + } |
| 25 | + client, server := requestMocks.Client(t) |
| 26 | + defer server.Close() |
| 27 | + api := &TagPoliciesAPI{tagPoliciesImpl: tagPoliciesImpl{client: client}} |
| 28 | + |
| 29 | + resp, err := api.GetTagPolicy(context.Background(), GetTagPolicyRequest{ |
| 30 | + TagKey: "Field/Shared Technical Services", |
| 31 | + }) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("GetTagPolicy returned error: %v", err) |
| 34 | + } |
| 35 | + if resp.TagKey != "Field/Shared Technical Services" { |
| 36 | + t.Fatalf("unexpected tag_key in response: %q", resp.TagKey) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestDeleteTagPolicyEncodesPathKey(t *testing.T) { |
| 41 | + requestMocks := qa.HTTPFixtures{ |
| 42 | + { |
| 43 | + Method: "DELETE", |
| 44 | + Resource: "/api/2.1/tag-policies/Field%2FShared%20Technical%20Services?", |
| 45 | + Response: map[string]any{}, |
| 46 | + }, |
| 47 | + } |
| 48 | + client, server := requestMocks.Client(t) |
| 49 | + defer server.Close() |
| 50 | + api := &TagPoliciesAPI{tagPoliciesImpl: tagPoliciesImpl{client: client}} |
| 51 | + |
| 52 | + err := api.DeleteTagPolicy(context.Background(), DeleteTagPolicyRequest{ |
| 53 | + TagKey: "Field/Shared Technical Services", |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("DeleteTagPolicy returned error: %v", err) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestGetTagAssignmentEncodesPathKey(t *testing.T) { |
| 61 | + requestMocks := qa.HTTPFixtures{ |
| 62 | + { |
| 63 | + Method: "GET", |
| 64 | + Resource: "/api/2.0/entity-tag-assignments/dashboards/123/tags/team%2Fdata-eng?", |
| 65 | + Response: TagAssignment{}, |
| 66 | + }, |
| 67 | + } |
| 68 | + client, server := requestMocks.Client(t) |
| 69 | + defer server.Close() |
| 70 | + api := &WorkspaceEntityTagAssignmentsAPI{workspaceEntityTagAssignmentsImpl: workspaceEntityTagAssignmentsImpl{client: client}} |
| 71 | + |
| 72 | + _, err := api.GetTagAssignment(context.Background(), GetTagAssignmentRequest{ |
| 73 | + EntityType: "dashboards", |
| 74 | + EntityId: "123", |
| 75 | + TagKey: "team/data-eng", |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("GetTagAssignment returned error: %v", err) |
| 79 | + } |
| 80 | +} |
0 commit comments