test(integration): test settings form and settings cookie

This commit is contained in:
Benedikt Rötsch
2017-10-25 15:23:52 +02:00
committed by Benedikt Rötsch
parent 62129cc1e3
commit bc73f3eec7
2 changed files with 65 additions and 2 deletions

View File

@@ -27,6 +27,8 @@
"safe-json-stringify": "^1.0.4" "safe-json-stringify": "^1.0.4"
}, },
"devDependencies": { "devDependencies": {
"cheerio": "^1.0.0-rc.2",
"cookie": "^0.3.1",
"eslint": "^3.16.0", "eslint": "^3.16.0",
"eslint-config-standard": "^6.2.1", "eslint-config-standard": "^6.2.1",
"eslint-plugin-promise": "^3.4.2", "eslint-plugin-promise": "^3.4.2",

View File

@@ -1,12 +1,73 @@
const app = require('../../app') const app = require('../../app')
const request = require('supertest') const request = require('supertest')
const cheerio = require('cheerio')
const cookie = require('cookie')
const cookieParser = require('cookie-parser')
function getSettingsCookie (res) {
try {
const cookies = res.headers['set-cookie']
const settingsCookie = cookies.find((cookie) => cookie.startsWith('theExampleAppSettings='))
const parsedCookie = cookie.parse(settingsCookie)
return cookieParser.JSONCookie(parsedCookie.theExampleAppSettings)
} catch (err) {
throw new Error(`Settings cookie was not set or could not be parsed. ${err.message}`)
}
}
describe('settings', () => { describe('settings', () => {
test('it should render the settings page', () => { test('should render', () => {
return request(app).get('/settings') return request(app).get('/settings')
.expect(200) .expect(200)
.then((response) => { .then((response) => {
expect(response.text.match(/Connected to space “.+”/)).toBeTruthy() const $ = cheerio.load(response.text)
const title = $('main h1')
expect(title.text()).toBe('Settings')
const status = $('main .status-block.status-block--info')
expect(status.text()).toMatch(/Connected to space “.+”/)
const inputSpaceId = $('#input-space')
expect(inputSpaceId.val()).toBe(process.env.CF_SPACE)
const inputCda = $('#input-cda')
expect(inputCda.val()).toBe(process.env.CF_ACCESS_TOKEN)
const inputCpa = $('#input-cpa')
expect(inputCpa.val()).toBe(process.env.CF_PREVIEW_ACCESS_TOKEN)
const inputEditorialFeatures = $('#input-editorial-features')
expect(inputEditorialFeatures.prop('checked')).toBeFalsy()
})
})
test('should have the editorial features enabled when query parameter is set and set cookie for it', () => {
return request(app).get('/settings?enable_editorial_features')
.expect(200)
.expect((res) => {
const cookie = getSettingsCookie(res)
if (!cookie.editorialFeatures) {
throw new Error('Did not set cookie value for editorial features')
}
if (cookie.space !== process.env.CF_SPACE) {
throw new Error('Did not set correct cookie value for SpaceID')
}
if (cookie.cda !== process.env.CF_ACCESS_TOKEN) {
throw new Error('Did not set correct cookie value for CDA access token')
}
if (cookie.cpa !== process.env.CF_PREVIEW_ACCESS_TOKEN) {
throw new Error('Did not set correct cookie value for CPA access token')
}
})
.then((response) => {
const $ = cheerio.load(response.text)
const inputEditorialFeatures = $('#input-editorial-features')
expect(inputEditorialFeatures.prop('checked')).toBeTruthy()
}) })
}) })
}) })