Files
archived-next-auth/test/integration/github.js
Iain Collins 3b40335202 Add full end-to-end integration tests
Full end-to-end integration tests for Twitter (OAuth 1) and GitHub (OAuth 2) using Puppeteer and Mocha.

This replaces Cypress tests due to issues with Cypress not being able to run tests against external URLs, which we need for our integration tests.

The integration test runner is hosted outside of GitHub Actions (it cannot be hosted by GitHub or on AWS due to IP access controls placed on sign in by providers like Twitter and GitHub) and so the integration tests may not pass if the test runner is offline. If this happens, tests can be re-run later when the test runner is available.

See Pull Request #641 for details.
2020-09-08 12:41:30 +01:00

72 lines
2.1 KiB
JavaScript

require('dotenv').config()
const assert = require('assert')
const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
const BASE_URL = 'http://localhost:3000'
const CALLBACK_URL = `${BASE_URL}/test`
const {
NEXTAUTH_GITHUB_USERNAME: USERNAME,
NEXTAUTH_GITHUB_PASSWORD: PASSWORD
} = process.env
describe('GitHub (OAuth 2.0 flow)', function () {
this.timeout(1000 * 60)
let browser,page
before(async () => {
browser = await puppeteer.launch({ headless: true })
page = await browser.newPage()
return Promise.resolve()
})
it('should show button on sign in page', async function () {
page.setDefaultTimeout(1000 * 60)
await page.goto(`${BASE_URL}/api/auth/signin?callbackUrl=${encodeURIComponent(CALLBACK_URL)}`)
await page.waitForSelector(`form[action="${BASE_URL}/api/auth/signin/github"] button`)
await page.click(`form[action="${BASE_URL}/api/auth/signin/github"] button`)
return Promise.resolve()
})
it('should be redirected to provider', async function () {
// Enter username
await page.waitForSelector('input[name="login"]')
await page.click('input[name="login"]')
await page.keyboard.type(USERNAME)
// Enter password
await page.waitForSelector('input[name="password"]')
await page.click('input[name="password"]')
await page.keyboard.type(PASSWORD)
return Promise.resolve()
})
it('should be able to sign in with provider', async function () {
// Click submit
await page.click('form[action="/session"] [type="submit"]')
return Promise.resolve()
})
it('should be returned to callback URL', async function () {
// Wait for page to return to callback URL
await page.waitForSelector('#nextauth-test-page')
// Check we are at the correct callback URL
assert.equal(page.url(), CALLBACK_URL)
return Promise.resolve()
})
it('should be signed in', async function () {
// Check we are signed in
await page.waitForSelector('#nextauth-signed-in')
return Promise.resolve()
})
after(async () => {
await browser.close()
return Promise.resolve()
})
})