Skip to content

Commit 0601e07

Browse files
authored
add new recipe showing basic authentication in action (#655)
1 parent c12787f commit 0601e07

File tree

13 files changed

+156
-0
lines changed

13 files changed

+156
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Recipe | Description
4545

4646
Recipe | Description
4747
--- | ---
48+
[Basic Auth](./examples/logging-in__basic-auth) | Log in using Basic Authentication
4849
[Single Sign On](./examples/logging-in__single-sign-on) | Log in across multiple servers or providers
4950
[HTML Web Forms](./examples/logging-in__html-web-forms) | Log in with a basic HTML form
5051
[XHR Web Forms](./examples/logging-in__xhr-web-forms) | Log in using an XHR

circle.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ jobs:
237237
<<: *defaults
238238
logging-in__using-app-code:
239239
<<: *defaults
240+
logging-in__basic-auth:
241+
<<: *defaults
240242
preprocessors__grep:
241243
<<: *defaults
242244
# in this particular example we want to grep tests
@@ -440,6 +442,9 @@ all_jobs: &all_jobs
440442
- fundamentals__cy-events:
441443
requires:
442444
- build
445+
- logging-in__basic-auth:
446+
requires:
447+
- build
443448
- logging-in__csrf-tokens:
444449
requires:
445450
- build
@@ -634,6 +639,7 @@ all_jobs: &all_jobs
634639
- logging-in__xhr-web-forms
635640
- logging-in__jwt
636641
- logging-in__using-app-code
642+
- logging-in__basic-auth
637643
- preprocessors__grep
638644
- preprocessors__flow-browserify
639645
- preprocessors__typescript-browserify
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# logging-in__basic-auth
2+
Shows how to visit the page protected by the [Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication)
3+
4+
The static site from [public folder](./public) is protected by the basic authentication, see [server.js](./server.js).
5+
6+
The [cypress/integration/spec.js](./cypress/integration/spec.js) shows how to pass the username and the password when calling [cy.visit](https://on.cypress.io/visit) and [cy.request](https://on.cypress.io/request) commands.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"baseUrl": "http://localhost:7065",
3+
"fixturesFolder": false,
4+
"pluginsFile": false,
5+
"supportFile": false,
6+
"viewportHeight": 200,
7+
"viewportWidth": 200
8+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/// <reference types="cypress" />
2+
3+
describe('Logging In - Basic Auth', function () {
4+
// we can use these values to log in
5+
const username = 'jane.lane'
6+
const password = 'password123'
7+
8+
context('cy.request', () => {
9+
// https://on.cypress.io/request
10+
11+
it('without authorization gets 401', () => {
12+
cy.request({
13+
url: '/',
14+
failOnStatusCode: false,
15+
}).its('status').should('equal', 401)
16+
})
17+
18+
it('with authorization', () => {
19+
cy.request({
20+
url: '/',
21+
auth: {
22+
username, password,
23+
},
24+
}).its('status').should('equal', 200)
25+
})
26+
27+
it('can post', () => {
28+
cy.request({
29+
url: '/echo',
30+
method: 'POST',
31+
auth: {
32+
username, password,
33+
},
34+
body: {
35+
text: 'ping!',
36+
},
37+
}).then((response) => {
38+
expect(response.status, 'status').to.equal(200)
39+
expect(response.body).to.deep.equal({
40+
text: 'ping!',
41+
})
42+
})
43+
})
44+
})
45+
46+
context('cy.visit', () => {
47+
// https://on.cypress.io/visit
48+
49+
it('loads the page using basic auth', () => {
50+
cy.visit('/', {
51+
auth: {
52+
username,
53+
password,
54+
},
55+
})
56+
57+
// confirm that all static resources have loaded
58+
cy.get('#app-message').should('not.be.empty')
59+
cy.log('app.js loaded')
60+
61+
cy.contains('h1', 'Red').should('have.css', 'color', 'rgb(255, 0, 0)')
62+
cy.log('app.css loaded')
63+
})
64+
})
65+
})
Loading
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "logging-in__basic-auth",
3+
"version": "1.0.0",
4+
"description": "",
5+
"private": true,
6+
"scripts": {
7+
"cypress:open": "../../node_modules/.bin/cypress open",
8+
"cypress:run": "../../node_modules/.bin/cypress run",
9+
"cypress:run:chrome": "../../node_modules/.bin/cypress run --browser chrome",
10+
"cypress:run:firefox": "../../node_modules/.bin/cypress run --browser firefox",
11+
"cypress:run:record": "../../node_modules/.bin/cypress run --record",
12+
"dev": "../../node_modules/.bin/start-test 7065 cypress:open",
13+
"start": "node server.js --port 7065",
14+
"test:ci": "../../node_modules/.bin/start-test 7065 cypress:run",
15+
"test:ci:chrome": "../../node_modules/.bin/start-test 7065 cypress:run:chrome",
16+
"test:ci:firefox": "../../node_modules/.bin/start-test 7065 cypress:run:firefox",
17+
"test:ci:record": "../../node_modules/.bin/start-test 7065 cypress:run:record"
18+
}
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* global document */
2+
document.getElementById('app-message').innerText = 'app.js loaded'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<head>
2+
<link rel="stylesheet" type="text/css" href="app.css" />
3+
</head>
4+
<body>
5+
<h1>Red</h1>
6+
<div id="app-message"></div>
7+
<script src="app.js"></script>
8+
</body>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable no-console */
2+
const minimist = require('minimist')
3+
const morgan = require('morgan')
4+
const express = require('express')
5+
const bodyParser = require('body-parser')
6+
const basicAuth = require('express-basic-auth')
7+
8+
const app = express()
9+
10+
// get port from passed in args from scripts/start.js
11+
const port = minimist(process.argv.slice(2)).port
12+
13+
// an endpoint replying to HEAD requests
14+
// used by start-server-and-test utility to know when the server is ready
15+
app.head('/', (req, res) => res.send(''))
16+
17+
app.use(morgan('dev'))
18+
// protect all resources with basic authentication
19+
app.use(basicAuth({
20+
users: { 'jane.lane': 'password123' },
21+
}))
22+
23+
app.post('/echo', bodyParser.json(), (req, res) => {
24+
console.log('/echo received', req.body)
25+
res.json(req.body)
26+
})
27+
28+
app.use(express.static('public'))
29+
app.listen(port)

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"enzyme-adapter-react-16": "1.1.1",
5050
"execa": "2.0.4",
5151
"express": "4.16.2",
52+
"express-basic-auth": "1.2.0",
5253
"express-jwt": "5.3.1",
5354
"express-session": "1.15.6",
5455
"graphql": "14.5.8",

0 commit comments

Comments
 (0)