Skip to content

Commit

Permalink
cleanup and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunker committed Jun 19, 2024
1 parent 8e3fbaf commit ba42a9c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 deletions.
11 changes: 2 additions & 9 deletions packages/plugin-e2e/src/auth/auth.setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { test as setup } from '../';
import { createAdminClientStorageState } from '../fixtures/grafanaAPIClient';
import { GrafanaAPIClient } from '../models/GrafanaAPIClient';

setup('authenticate', async ({ login, user, grafanaAPICredentials, browser }) => {
const context = await browser.newContext();
await createAdminClientStorageState(context.request, grafanaAPICredentials);
const adminClient = new GrafanaAPIClient(context.request);

// there's no need to create the server admin user
setup('authenticate', async ({ login, createUser, user, grafanaAPICredentials }) => {
if (user && (user.user !== grafanaAPICredentials.user || user.password !== grafanaAPICredentials.password)) {
await adminClient.createUser(user);
await createUser();
}
await login();
});
10 changes: 9 additions & 1 deletion packages/plugin-e2e/src/fixtures/grafanaAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ export const createAdminClientStorageState = async (request: APIRequestContext,
await request.storageState({ path: adminClientStorageState });
};

export const grafanaAPIClient: GrafanaAPIClientFixture = async ({ browser }, use) => {
export const grafanaAPIClient: GrafanaAPIClientFixture = async ({ browser, grafanaAPICredentials }, use) => {
const context = await browser.newContext({ storageState: undefined });
const loginReq = await context.request.post('/login', { data: grafanaAPICredentials });
if (!loginReq.ok()) {
console.log(
`Could not login to grafana using credentials '${
grafanaAPICredentials?.user
}'. Find information on how user can be managed in the plugin-e2e docs: https://grafana.com/developers/plugin-tools/e2e-test-a-plugin/use-authentication#managing-users : ${await loginReq.text()}`
);
}
await context.request.storageState({ path: adminClientStorageState });
await use(new GrafanaAPIClient(context.request));
};
9 changes: 3 additions & 6 deletions packages/plugin-e2e/src/models/GrafanaAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class GrafanaAPIClient {

async getUserIdByUsername(userName: string) {
const getUserIdByUserNameReq = await this.request.get(`/api/users/lookup?loginOrEmail=${userName}`);
await expect(getUserIdByUserNameReq.ok()).toBeTruthy();
const json = await getUserIdByUserNameReq.json();
return json.id;
}
Expand Down Expand Up @@ -38,11 +37,9 @@ export class GrafanaAPIClient {
const updateRoleReq = await this.request.patch(`/api/org/users/${userId}`, {
data: { role: user.role },
});
const updateRoleReqText = await updateRoleReq.text();
await expect(
updateRoleReq.ok(),
`Could not assign role '${user.role}' to user '${user.user}': ${updateRoleReqText}`
).toBeTruthy();
if (!updateRoleReq.ok()) {
throw new Error(`Could not assign role '${user.role}' to user '${user.user}': ${await updateRoleReq.text()}`);
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-e2e/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ export type PluginFixture = {
/**
* Client that allows you to use certain endpoints in the Grafana http API.
*
* Note that this client doesn't call the Grafana HTTP API on behalf of the logged in user -
* it uses the {@link types.grafanaAPICredentials} credentials. These defaults to admin:admin, but you may override this
The GrafanaAPIClient doesn't call the Grafana HTTP API on behalf of the logged in user -
* it uses the {@link types.grafanaAPICredentials} credentials. grafanaAPICredentials defaults to admin:admin, but you may override this
* by specifying grafanaAPICredentials in the playwright config options.
*
* This fixture reuses storage state in all tests, so don't forget to add a dependency to the auth setup project in the playwright config.
* See https://grafana.com/developers/plugin-tools/e2e-test-a-plugin/use-authentication for more info.
* Note that storage state for the admin client is not persisted throughout the test suite. For every test where the grafanaAPICredentials fixtures is used,
* new storage state is created.
*/

grafanaAPIClient: GrafanaAPIClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from '../../../src';

test.use({ grafanaAPICredentials: { user: 'test', password: 'test' } });
test('should throw error when credentials are invalid', async ({ grafanaAPIClient }) => {
await expect(grafanaAPIClient.createUser({ user: 'testuser1', password: 'pass' })).rejects.toThrowError(
/Could not create user 'testuser1'.*/
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from '../../../src';

test('should be possible to create user when credentials are valid', async ({ grafanaAPIClient }) => {
await expect(grafanaAPIClient.createUser({ user: 'testuser1', password: 'pass' })).resolves.toBeUndefined();
await expect(await grafanaAPIClient.getUserIdByUsername('testuser1')).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from '../../../src';

test.use({ grafanaAPICredentials: { user: 'test', password: 'test' } });
test('should throw error when credentials are invalid', async ({ grafanaAPIClient }) => {
await expect(grafanaAPIClient.createUser({ user: 'testuser1', password: 'pass' })).rejects.toThrowError(
/Could not create user 'testuser1'.*/
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from '../../../src';

test('using grafanaAPIClient should not change storage state for logged in user', async ({
grafanaAPIClient,
page,
}) => {
// assert one can create user on behalf of the admin credentials
await expect(grafanaAPIClient.createUser({ user: 'testuser1', password: 'pass' })).resolves.toBeUndefined();
await expect(await grafanaAPIClient.getUserIdByUsername('testuser1')).toBeTruthy();

// but logged in user should still only have viewer persmissions
await page.goto('/');
const homePageTitle = await page.title();
await page.goto('/datasources', { waitUntil: 'networkidle' });
expect(await page.title()).toEqual(homePageTitle);
});

0 comments on commit ba42a9c

Please sign in to comment.