Skip to content

Plugin E2E: Allow overriding grafanaAPICredentials - #930

Merged
sunker merged 8 commits into
mainfrom
plugin-e2e/api-admin-user
May 30, 2024
Merged

sunker merged 8 commits into
mainfrom
plugin-e2e/api-admin-user

Conversation

@sunker

@sunker sunker commented May 28, 2024 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

In a few places in the code, plugin-e2e uses the Grafana API to administrate users, data sources etc. Before this PR, the Grafana API was always called on behalf of the admin:admin user (the default server admin account) as it has all the necessary permissions. This is working fine in most cases, but in the test environment for the SLO app, this account doesn't exist so they're getting 401 errors when setting up users (more details on how to create users here).

With this PR, the Grafana API is still called on behalf of the admin:admin user by default. However, consumers now have the option to override this user by providing a grafanaAPIUser in the playwright config. grafanaAPIUser is a Playwright option, meaning it can be defined on a global level and on a project level. Also, when specifying the user to use when running the e2e test (not the same thing as the grafanaAPIUser), it's now possible to set skipCreateUser prop. This gives the consumer the option to not create the user in the database.

This is an example where one user is being used when calling the Grafana API and another one is being used when running the e2e tests.

export default defineConfig({
    ...
 testDir: './tests',
   use: {
     baseURL: 'http://localhost:3000',
     grafanaAPIUser: {
       user: 'server-admin',
       password: 'server-admin',
     },
    },
    projects: [
    {
      name: 'auth',
      testDir: pluginE2eAuth,
      testMatch: [/.*\.js/],
      use: {
        user: {
          user: 'slo',
          password: 'slo',
          skipCreateUser: true, // this user already exist in the db, so no need to create it during test setup
        },
      },
    },
    {
      name: 'run-tests',
      use: {
        ...devices['Desktop Chrome'],
        storageState: 'playwright/.auth/slo.json',
      },
      dependencies: ['auth'],
    }
  ],
});

Which issue(s) this PR fixes:

Fixes #934

Special notes for your reviewer:

📦 Published PR as canary version: Canary Versions

✨ Test out this PR locally via:

npm install @grafana/plugin-e2e@1.3.0-canary.930.a85b4bb.0
# or 
yarn add @grafana/plugin-e2e@1.3.0-canary.930.a85b4bb.0

@github-actions

github-actions Bot commented May 28, 2024 •

Copy link
Copy Markdown
Contributor

Hello! 👋 This repository uses Auto for releasing packages using PR labels.

✨ This PR can be merged and will trigger a new minor release.
NOTE: When merging a PR with the release label please avoid merging another PR. For further information see here.

@sunker sunker changed the title Plugin E2E: Allow consumer to specify user when interacting with the Grafana API Plugin E2E: Allow consumer to specify custom user when interacting with the Grafana API May 28, 2024
@sunker
sunker force-pushed the plugin-e2e/api-admin-user branch from 3fd6964 to 799875f Compare May 28, 2024 14:19
@sunker
sunker requested a review from mckn May 28, 2024 14:20
@sunker
sunker marked this pull request as ready for review May 28, 2024 14:20
@sunker
sunker requested a review from a team May 28, 2024 14:20
await expect(annotationEditPage.getByGrafanaSelector('Select option')).toContainText(REDSHIFT_SCHEMAS);
await expect(annotationEditPage.getByGrafanaSelector(selectors.components.Select.option)).toContainText(
REDSHIFT_SCHEMAS
);

@sunker sunker May 28, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (and the next file) is unrelated, but needed to get others tests to pass as this selector was recently changed in the main branch of Grafana.

Comment thread packages/plugin-e2e/src/types.ts Outdated
/**
* Set this to true if the user already exist in the database. If omitted or set to false, the user will be created.
*/
skipCreateUser?: boolean;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prop name is a bit weird - would have been better to reverse the condition and call this createUser, but that would have been a breaking change.

@sunker sunker self-assigned this May 29, 2024

@jackw jackw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

Left a couple of nits. Could we add something to the docs about this?


/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
grafanaAPIUser: {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Besides a user account are there other ways to auth with the API? Maybe grafanaAPIAuth or grafanaAPICredentials would work rather than mentioning user?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, 100% agree


setup('authenticate', async ({ login, createUser, user }) => {
if (user) {
if (user && !user.skipCreateUser) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'm understanding the skipCreateUser. Doesn't createUser already handle cases where the user already exists?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that's right, user is only created if it doesn't exist, so this is not entirely necessary. I guess I thought this would make this more clear from an end-user standpoint, but that's not the case so I'll remote this prop. Thanks for feedback.

@sunker sunker changed the title Plugin E2E: Allow consumer to specify custom user when interacting with the Grafana API Plugin E2E: Allow overriding grafanaAPICredentials May 30, 2024
@sunker
sunker merged commit 627f7ec into main May 30, 2024
@sunker
sunker deleted the plugin-e2e/api-admin-user branch May 30, 2024 13:11
@grafana-plugins-platform-bot

Copy link
Copy Markdown
Contributor

🚀 PR was released in @grafana/plugin-e2e@1.3.0 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Feat: Make it possible to switch admin client user

2 participants