Files
cosmos-explorer/test/sql/resourceToken.spec.ts
jawelton74 7014981807 Scale the number of test accounts used for SQL tests to one per shard. (#2468)
* Scale the number of test accounts used for SQL tests to one per shard.

* Set PLAYWRIGHT_SHARD_INDEX environment variable in CI workflow.

* Add log statement for the shared index and selected account.

* Remove console log.

* Fix order of accounts so that shard index maps to same account index.

* Try to fix the SQL account scope in ci.yml

* Get tokens for all accounts and use the shard index to pick which one.

* Set tokens without loop.

* Handcode the token use in tests.

* Fix database creation.

* Add debug for rbac token issues.

* Common function for retrieving NoSQL token.

* Disable eslint rule for noconsole temporarily.

* Move getNoSqlRbacToken to separate file.

* Fix ref to new function.

* mock Resource Graph API — fires on auto-subscription selection to populate account dropdown

* Code tidy-up.

* Fix build errors.

---------

Co-authored-by: Bikram Choudhury <bchoudhury@microsoft.com>
2026-04-30 14:17:32 -07:00

60 lines
2.4 KiB
TypeScript

import { expect, test } from "@playwright/test";
import { CosmosDBManagementClient } from "@azure/arm-cosmosdb";
import { CosmosClient, PermissionMode } from "@azure/cosmos";
import {
DataExplorer,
TestAccount,
generateUniqueName,
getAccountName,
getAzureCLICredentials,
resourceGroupName,
subscriptionId,
} from "../fx";
import { getNoSqlRbacToken } from "../NoSqlTestSetup";
test("SQL account using Resource token", async ({ page }) => {
const nosqlAccountRbacToken = getNoSqlRbacToken() ?? "";
test.skip(nosqlAccountRbacToken.length > 0, "Resource tokens not supported when using data plane RBAC.");
const credentials = getAzureCLICredentials();
const armClient = new CosmosDBManagementClient(credentials, subscriptionId);
const accountName = getAccountName(TestAccount.SQL);
const account = await armClient.databaseAccounts.get(resourceGroupName, accountName);
const keys = await armClient.databaseAccounts.listKeys(resourceGroupName, accountName);
const dbId = generateUniqueName("db");
const collectionId = "testcollection";
const client = new CosmosClient({
endpoint: account.documentEndpoint!,
key: keys.primaryMasterKey,
});
const { database } = await client.databases.createIfNotExists({ id: dbId });
const { container } = await database.containers.createIfNotExists({ id: collectionId });
const { user } = await database.users.upsert({ id: "testUser" });
const { resource: containerPermission } = await user.permissions.upsert({
id: "partitionLevelPermission",
permissionMode: PermissionMode.All,
resource: container.url,
});
await expect(containerPermission).toBeDefined();
const resourceTokenConnectionString = `AccountEndpoint=${account.documentEndpoint};DatabaseId=${
database.id
};CollectionId=${container.id};${containerPermission!._token}`;
await page.goto("https://localhost:1234/hostedExplorer.html");
const switchConnectionLink = page.getByTestId("Link:SwitchConnectionType");
await switchConnectionLink.waitFor();
await switchConnectionLink.click();
await page.getByPlaceholder("Please enter a connection string").fill(resourceTokenConnectionString);
await page.getByRole("button", { name: "Connect" }).click();
const explorer = await DataExplorer.waitForExplorer(page);
const collectionNode = explorer.treeNode(`${collectionId}`);
await collectionNode.element.waitFor();
await expect(collectionNode.element).toBeAttached();
await database.delete();
});