Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin E2E: Allow overriding grafanaAPICredentials #930

Merged
merged 8 commits into from
May 30, 2024

Conversation

sunker
Copy link
Contributor

@sunker sunker commented May 28, 2024

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/[email protected]
# or 
yarn add @grafana/[email protected]

Copy link

github-actions bot commented May 28, 2024

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 added minor Increment the minor version when merged release Create a release when this pr is merged labels 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 as a code owner 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
);
Copy link
Contributor Author

@sunker sunker May 28, 2024

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.

/**
* 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
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
Copy link
Collaborator

@jackw jackw left a comment

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?

@@ -25,6 +25,10 @@ export default defineConfig<PluginOptions>({

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
grafanaAPIUser: {
Copy link
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
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

@@ -1,7 +1,7 @@
import { test as setup } from '../';

setup('authenticate', async ({ login, createUser, user }) => {
if (user) {
if (user && !user.skipCreateUser) {
Copy link
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
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
18 checks passed
@sunker sunker deleted the plugin-e2e/api-admin-user branch May 30, 2024 13:11
@grafana-plugins-platform-bot
Copy link

🚀 PR was released in @grafana/[email protected] 🚀

@grafana-plugins-platform-bot grafana-plugins-platform-bot bot added the released This issue/pull request has been released. label May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Increment the minor version when merged release Create a release when this pr is merged released This issue/pull request has been released.
Projects
Development

Successfully merging this pull request may close these issues.

Feat: Make it possible to switch admin client user
2 participants