Skip to content

Commit

Permalink
Merge pull request #194 from chromaui/andrew/ap-4963-add-extra-basic-…
Browse files Browse the repository at this point in the history
…authentication-tests-in-e2e-to-ensure-we

Add extra Basic Authentication tests in E2E
  • Loading branch information
andrewortwein authored Sep 4, 2024
2 parents e629f2b + f4b5c86 commit d13a446
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
9 changes: 9 additions & 0 deletions packages/cypress/tests/cypress/e2e/auth.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
it('can succeed with basic authentication using locally defined credentials', () => {
cy.visit('/protected', {
auth: {
username: 'user',
password: 'secret',
},
});
cy.contains('I AM PROTECTED!!!').should('be.visible');
});
4 changes: 2 additions & 2 deletions packages/playwright/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export default defineConfig({
use: {
baseURL: 'http://localhost:3000',
httpCredentials: {
username: 'admin',
password: 'supersecret',
username: 'user',
password: 'secret',
},
},
projects: [
Expand Down
22 changes: 20 additions & 2 deletions packages/playwright/tests/auth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { test, expect } from '../src';

test.describe(() => {
test('can login', async ({ page }) => {
await page.goto('/auth');
test('can succeed with basic authentication using globally-defined credentials', async ({
page,
}) => {
await page.goto('/protected');

await expect(page.getByText('I AM PROTECTED!!!')).toBeVisible();
});
});

test.describe(() => {
test.use({
extraHTTPHeaders: {
Authorization: `Basic ${btoa('admin:supersecret')}`,
},
});

test('can succeed with basic authentication using a locally-defined header that overrides globally-defined credentials', async ({
page,
}) => {
await page.goto('/admin');

await expect(page.getByText('I AM PROTECTED!!!')).toBeVisible();
});
Expand Down
16 changes: 14 additions & 2 deletions test-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ const htmlIntro = `<!doctype html><html>`;
const htmlOutro = `</html>`;

app.use(
'/auth',
'/protected',
basicAuth({
users: { user: 'secret' },
challenge: true,
})
);

app.use(
'/admin',
basicAuth({
users: { admin: 'supersecret' },
challenge: true,
Expand All @@ -36,7 +44,11 @@ app.get('/img', (req, res) => {
}
});

app.get('/auth', (req, res) => {
app.get('/protected', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/auth/index.html'));
});

app.get('/admin', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/auth/index.html'));
});

Expand Down

0 comments on commit d13a446

Please sign in to comment.