diff --git a/examples/app-with-rbac/.config/Dockerfile b/examples/app-with-rbac/.config/Dockerfile index 2a8a2b1e9..54d3388f3 100644 --- a/examples/app-with-rbac/.config/Dockerfile +++ b/examples/app-with-rbac/.config/Dockerfile @@ -14,7 +14,7 @@ ENV DEV "${development}" # Do NOT enable these settings in a public facing / production grafana instance ENV GF_AUTH_ANONYMOUS_ORG_ROLE "Admin" ENV GF_AUTH_ANONYMOUS_ENABLED "true" -ENV GF_AUTH_BASIC_ENABLED "false" +ENV GF_AUTH_BASIC_ENABLED "true" # Set development mode so plugins can be loaded without the need to sign ENV GF_DEFAULT_APP_MODE "development" @@ -29,14 +29,14 @@ USER root # Installing supervisor and inotify-tools RUN if [ "${development}" = "true" ]; then \ if grep -i -q alpine /etc/issue; then \ - apk add supervisor inotify-tools git; \ + apk add supervisor inotify-tools git; \ elif grep -i -q ubuntu /etc/issue; then \ - DEBIAN_FRONTEND=noninteractive && \ - apt-get update && \ - apt-get install -y supervisor inotify-tools git && \ - rm -rf /var/lib/apt/lists/*; \ + DEBIAN_FRONTEND=noninteractive && \ + apt-get update && \ + apt-get install -y supervisor inotify-tools git && \ + rm -rf /var/lib/apt/lists/*; \ else \ - echo 'ERROR: Unsupported base image' && /bin/false; \ + echo 'ERROR: Unsupported base image' && /bin/false; \ fi \ fi diff --git a/examples/app-with-rbac/playwright.config.ts b/examples/app-with-rbac/playwright.config.ts index cc35fbcfe..d017dec8e 100644 --- a/examples/app-with-rbac/playwright.config.ts +++ b/examples/app-with-rbac/playwright.config.ts @@ -31,37 +31,46 @@ export default defineConfig({ /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', }, - /* Configure projects for major browsers */ projects: [ { - name: 'auth', + name: 'adminUserAuthenticates', testDir: pluginE2eAuth, testMatch: [/.*\.js/], }, { - name: 'run-tests', + name: 'run-tests-for-admin', + testDir: './tests/admin', use: { ...devices['Desktop Chrome'], + // @grafana/plugin-e2e writes the auth state to this file, + // the path should not be modified storageState: 'playwright/.auth/admin.json', }, - dependencies: ['auth'], + dependencies: ['adminUserAuthenticates'], + }, + { + name: 'createViewerUserAndAuthenticate', + testDir: pluginE2eAuth, + testMatch: [/.*\.js/], + use: { + user: { + user: 'viewer', + password: 'password', + role: 'Viewer', + }, + }, + }, + { + name: 'run-tests-for-viewer', + testDir: './tests/viewer', + use: { + ...devices['Desktop Chrome'], + // @grafana/plugin-e2e writes the auth state to this file, + // the path should not be modified + storageState: 'playwright/.auth/viewer.json', + }, + dependencies: ['createViewerUserAndAuthenticate'], }, - // { - // name: 'createAdminUserAndAuthenticate', - // testDir: pluginE2eAuth, - // testMatch: [/.*\.js/], - // }, - // { - // name: 'run-tests-for-admin', - // testDir: './tests', - // use: { - // ...devices['Desktop Chrome'], - // // @grafana/plugin-e2e writes the auth state to this file, - // // the path should not be modified - // storageState: 'playwright/.auth/admin.json', - // }, - // dependencies: ['createAdminUserAndAuthenticate'], - // }, ], }); diff --git a/examples/app-with-rbac/tests/appAuthentication.spec.ts b/examples/app-with-rbac/tests/admin/appAuthentication.spec.ts similarity index 52% rename from examples/app-with-rbac/tests/appAuthentication.spec.ts rename to examples/app-with-rbac/tests/admin/appAuthentication.spec.ts index a55da06aa..e015fb9be 100644 --- a/examples/app-with-rbac/tests/appAuthentication.spec.ts +++ b/examples/app-with-rbac/tests/admin/appAuthentication.spec.ts @@ -1,17 +1,15 @@ -import { test, expect } from './fixtures'; -import { ROUTES } from '../src/constants'; +import { test, expect } from '../fixtures'; +import { ROUTES } from '../../src/constants'; test.describe('navigating app', () => { test('Page patents should be visible for admin', async ({ gotoPage, page }) => { // wait for page to successfully render - await gotoPage('/hello'); - await expect(page).toHaveURL(/.*patents/); - await expect(page.getByText('Welcome')).toBeVisible(); + await gotoPage(`/${ROUTES.Patents}`); + await expect(page.getByText('Normally restricted to Administrators')).toBeVisible(); }); test('Page research docs should be visible for admin', async ({ gotoPage, page }) => { // wait for page to successfully render await gotoPage(`/${ROUTES.ResearchDocs}`); - await expect(page).toHaveURL(/.*research/); - await expect(page.getByText('Welcome')).toBeVisible(); + await expect(page.getByText('Normally accessible to anyone')).toBeVisible(); }); }); diff --git a/examples/app-with-rbac/tests/viewer/appAuthenticationViewer.spec.ts b/examples/app-with-rbac/tests/viewer/appAuthenticationViewer.spec.ts new file mode 100644 index 000000000..7873161d7 --- /dev/null +++ b/examples/app-with-rbac/tests/viewer/appAuthenticationViewer.spec.ts @@ -0,0 +1,15 @@ +import { test, expect } from '../fixtures'; +import { ROUTES } from '../../src/constants'; + +test.describe('navigating app for Viewer', () => { + test('Page patents should not be visible for viewer', async ({ gotoPage, page }) => { + // wait for page to successfully render + await gotoPage(`/${ROUTES.Patents}`); + await expect(page.getByText('Normally restricted to Administrators')).not.toBeVisible(); + }); + test('Page research docs should be visible for viewer', async ({ gotoPage, page }) => { + // wait for page to successfully render + await gotoPage(`/${ROUTES.ResearchDocs}`); + await expect(page.getByText('Normally accessible to anyone')).toBeVisible(); + }); +}); diff --git a/examples/app-with-service-account/.config/Dockerfile b/examples/app-with-service-account/.config/Dockerfile index 2a8a2b1e9..54d3388f3 100644 --- a/examples/app-with-service-account/.config/Dockerfile +++ b/examples/app-with-service-account/.config/Dockerfile @@ -14,7 +14,7 @@ ENV DEV "${development}" # Do NOT enable these settings in a public facing / production grafana instance ENV GF_AUTH_ANONYMOUS_ORG_ROLE "Admin" ENV GF_AUTH_ANONYMOUS_ENABLED "true" -ENV GF_AUTH_BASIC_ENABLED "false" +ENV GF_AUTH_BASIC_ENABLED "true" # Set development mode so plugins can be loaded without the need to sign ENV GF_DEFAULT_APP_MODE "development" @@ -29,14 +29,14 @@ USER root # Installing supervisor and inotify-tools RUN if [ "${development}" = "true" ]; then \ if grep -i -q alpine /etc/issue; then \ - apk add supervisor inotify-tools git; \ + apk add supervisor inotify-tools git; \ elif grep -i -q ubuntu /etc/issue; then \ - DEBIAN_FRONTEND=noninteractive && \ - apt-get update && \ - apt-get install -y supervisor inotify-tools git && \ - rm -rf /var/lib/apt/lists/*; \ + DEBIAN_FRONTEND=noninteractive && \ + apt-get update && \ + apt-get install -y supervisor inotify-tools git && \ + rm -rf /var/lib/apt/lists/*; \ else \ - echo 'ERROR: Unsupported base image' && /bin/false; \ + echo 'ERROR: Unsupported base image' && /bin/false; \ fi \ fi diff --git a/examples/app-with-service-account/provisioning/dashboards/dashboard.json b/examples/app-with-service-account/provisioning/dashboards/dashboard.json new file mode 100644 index 000000000..0d5357c89 --- /dev/null +++ b/examples/app-with-service-account/provisioning/dashboards/dashboard.json @@ -0,0 +1,133 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 0, + "links": [], + "liveNow": false, + "panels": [ + { + "datasource": { + "type": "grafana-testdata-datasource", + "uid": "PD8C576611E62080A" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unitScale": true + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, + "id": 1, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "single", + "sort": "none" + } + }, + "targets": [ + { + "datasource": { + "type": "grafana-testdata-datasource", + "uid": "PD8C576611E62080A" + }, + "refId": "A", + "scenarioId": "random_walk", + "seriesCount": 1 + } + ], + "title": "Panel Title", + "type": "timeseries" + } + ], + "refresh": "", + "schemaVersion": 39, + "tags": [], + "templating": { + "list": [] + }, + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": {}, + "timezone": "", + "title": "New dashboard", + "version": 0, + "weekStart": "" + } \ No newline at end of file diff --git a/examples/app-with-service-account/provisioning/dashboards/default.yaml b/examples/app-with-service-account/provisioning/dashboards/default.yaml new file mode 100644 index 000000000..840c71b55 --- /dev/null +++ b/examples/app-with-service-account/provisioning/dashboards/default.yaml @@ -0,0 +1,7 @@ +apiVersion: 1 + +providers: + - name: Default + type: file + options: + path: /etc/grafana/provisioning/dashboards \ No newline at end of file diff --git a/examples/app-with-service-account/provisioning/datasources/default.yaml b/examples/app-with-service-account/provisioning/datasources/default.yaml new file mode 100644 index 000000000..3def355ec --- /dev/null +++ b/examples/app-with-service-account/provisioning/datasources/default.yaml @@ -0,0 +1,6 @@ +apiVersion: 1 + +datasources: + - name: gdev-testdata + isDefault: true + type: testdata diff --git a/examples/app-with-service-account/provisioning/plugins/apps.yaml b/examples/app-with-service-account/provisioning/plugins/apps.yaml index 494122c7b..de5169ce2 100644 --- a/examples/app-with-service-account/provisioning/plugins/apps.yaml +++ b/examples/app-with-service-account/provisioning/plugins/apps.yaml @@ -2,4 +2,6 @@ apiVersion: 1 apps: - type: 'grafana-appwithserviceaccount-app' + org_id: 1 + org_name: Main Org. disabled: false diff --git a/examples/app-with-service-account/src/pages/PageOne.tsx b/examples/app-with-service-account/src/pages/PageOne.tsx index 30add7922..f963472a0 100644 --- a/examples/app-with-service-account/src/pages/PageOne.tsx +++ b/examples/app-with-service-account/src/pages/PageOne.tsx @@ -52,7 +52,7 @@ export function PageOne() { setApiResponse('Loading...'); let params = {}; params = { ...params, method: method }; - if (method === 'POST' || method === "PUT") { + if (method === 'POST' || method === 'PUT') { let parsedBody = JSON.parse(body); params = { ...params, body: JSON.stringify(parsedBody) }; } @@ -98,7 +98,9 @@ export function PageOne() {

Token used

{apiToken}

API Response

- +
+ +
); diff --git a/examples/app-with-service-account/tests/appConfig.spec.ts b/examples/app-with-service-account/tests/appConfig.spec.ts deleted file mode 100644 index 1e069e301..000000000 --- a/examples/app-with-service-account/tests/appConfig.spec.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { test, expect } from './fixtures'; - -test('should be possible to save app configuration', async ({ appConfigPage, page }) => { - const saveButton = page.getByRole('button', { name: /Save API settings/i }); - - // reset the configured secret - await page.getByRole('button', { name: /reset/i }).click(); - - // enter some valid values - await page.getByRole('textbox', { name: 'API Key' }).fill('secret-api-key'); - await page.getByRole('textbox', { name: 'API Url' }).clear(); - await page.getByRole('textbox', { name: 'API Url' }).fill('http://www.my-awsome-grafana-app.com/api'); - - // listen for the server response on the saved form - const saveResponse = appConfigPage.waitForSettingsResponse(); - - await saveButton.click(); - await expect(saveResponse).toBeOK(); -}); diff --git a/examples/app-with-service-account/tests/appNavigation.spec.ts b/examples/app-with-service-account/tests/appNavigation.spec.ts deleted file mode 100644 index 006223036..000000000 --- a/examples/app-with-service-account/tests/appNavigation.spec.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { test, expect } from './fixtures'; -import { ROUTES } from '../src/constants'; - -test.describe('navigating app', () => { - test('page one should render successfully', async ({ gotoPage, page }) => { - await gotoPage(`/${ROUTES.One}`); - await expect(page.getByText('This is page one.')).toBeVisible(); - }); - - test('page two should render successfully', async ({ gotoPage, page }) => { - await gotoPage(`/${ROUTES.Two}`); - await expect(page.getByText('This is page two.')).toBeVisible(); - }); - - test('page three should support an id parameter', async ({ gotoPage, page }) => { - await gotoPage(`/${ROUTES.Three}/123456`); - await expect(page.getByText('ID: 123456')).toBeVisible(); - }); - - test('page three should render sucessfully', async ({ gotoPage, page }) => { - // wait for page to successfully render - await gotoPage(`/${ROUTES.One}`); - await expect(page.getByText('This is page one.')).toBeVisible(); - - // navigating to page four with full width layout without sidebar menu - await page.getByText('Full-width page example').click(); - - // navigate back to page one - await page.getByRole('link', { name: 'Back', exact: true }).click(); - }); -}); diff --git a/examples/app-with-service-account/tests/appRequests.spec.ts b/examples/app-with-service-account/tests/appRequests.spec.ts new file mode 100644 index 000000000..fd7f1590e --- /dev/null +++ b/examples/app-with-service-account/tests/appRequests.spec.ts @@ -0,0 +1,11 @@ +import { test, expect } from './fixtures'; +import { ROUTES } from '../src/constants'; + +test.describe('app requests', () => { + test('GET request should be successfully', async ({ gotoPage, page }) => { + await gotoPage(`/${ROUTES.One}`); + await expect(page.getByTestId('data-testid pg-one-health')).toBeVisible(); + await page.getByRole('button', { name: 'Request' }).click(); + await expect(page.getByTestId('json-format-response')).toContainText('New dashboard'); + }); +}); diff --git a/examples/app-with-service-account/tests/auth.setup.js b/examples/app-with-service-account/tests/auth.setup.js new file mode 100644 index 000000000..e69de29bb diff --git a/examples/app-with-service-account/tests/fixtures.ts b/examples/app-with-service-account/tests/fixtures.ts index 89410d0e3..7b8562bc1 100644 --- a/examples/app-with-service-account/tests/fixtures.ts +++ b/examples/app-with-service-account/tests/fixtures.ts @@ -21,45 +21,6 @@ export const test = base.extend( }) ); }, - // Use the same storage state for all tests in this worker. - storageState: ({ workerStorageState }, use) => use(workerStorageState), - - // Authenticate once per worker with a worker-scoped fixture. - workerStorageState: [ - async ({}, use) => { - // Use parallelIndex as a unique identifier for each worker. - const id = test.info().parallelIndex; - const fileName = path.resolve(test.info().project.outputDir, `.auth/${id}.json`); - - if (fs.existsSync(fileName)) { - // Reuse existing authentication state if any. - await use(fileName); - return; - } - - // Important: make sure we authenticate in a clean environment by unsetting storage state. - const context = await request.newContext({ storageState: undefined }); - - // Acquire a unique account, for example create a new one. - // Alternatively, you can have a list of precreated accounts for testing. - // Make sure that accounts are unique, so that multiple team members - // can run tests at the same time without interference. - const account = await acquireAccount(id); - - // Send authentication request. Replace with your own. - await context.post('https://github.com/login', { - form: { - user: 'user', - password: 'password', - }, - }); - - await context.storageState({ path: fileName }); - await context.dispose(); - await use(fileName); - }, - { scope: 'worker' }, - ], }); export { expect } from '@grafana/plugin-e2e';