Skip to content

Commit

Permalink
test(e2e): wait for initial network requests (ory#2242)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Feb 21, 2022
1 parent 533f065 commit 3b8b610
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 245 deletions.
19 changes: 19 additions & 0 deletions driver/config/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"crypto/sha256"
"fmt"
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/knadh/koanf/parsers/json"
)

func NewConfigHashHandler(c Provider, router *httprouter.Router) {
router.GET("/health/config", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
bytes, _ := c.Config(r.Context()).Source().Marshal(json.Parser())
sum := sha256.Sum256(bytes)
w.Header().Set("Content-Type", "text/plain")
_, _ = fmt.Fprintf(w, "%x", sum)
})
}
46 changes: 46 additions & 0 deletions driver/config/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package config_test

import (
"io/ioutil"
"net/http/httptest"
"testing"

"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/internal"
)

func TestNewConfigHashHandler(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
router := httprouter.New()
config.NewConfigHashHandler(reg, router)
ts := httptest.NewServer(router)
t.Cleanup(ts.Close)
res, err := ts.Client().Get(ts.URL + "/health/config")
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, 200, res.StatusCode)
first, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)

res, err = ts.Client().Get(ts.URL + "/health/config")
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, 200, res.StatusCode)
second, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
assert.Equal(t, first, second)

require.NoError(t, conf.Set(config.ViperKeySessionDomain, "foobar"))

res, err = ts.Client().Get(ts.URL + "/health/config")
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, 200, res.StatusCode)
second, err = ioutil.ReadAll(res.Body)
require.NoError(t, err)
assert.NotEqual(t, first, second)
}
2 changes: 2 additions & 0 deletions driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func (m *RegistryDefault) RegisterAdminRoutes(ctx context.Context, router *x.Rou
m.HealthHandler(ctx).SetHealthRoutes(router.Router, true)
m.HealthHandler(ctx).SetVersionRoutes(router.Router)
m.MetricsHandler().SetRoutes(router.Router)

config.NewConfigHashHandler(m, router.Router)
}

func (m *RegistryDefault) RegisterRoutes(ctx context.Context, public *x.RouterPublic, admin *x.RouterAdmin) {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.log
kratos.*.yml
proxy.json

cypress/downloads
2 changes: 1 addition & 1 deletion test/e2e/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"videosFolder": "cypress/videos",
"screenshotsFolder": "cypress/screenshots",
"retries": {
"runMode": 3,
"runMode": 6,
"openMode": 1
},
"timeouts": {
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/cypress/integration/profiles/mfa/mix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { routes as react } from '../../../helpers/react'
import { routes as express } from '../../../helpers/express'

context('2FA with various methods', () => {
before(() => {
cy.task('resetCRI', {})
})
after(() => {
cy.task('resetCRI', {})
})
;[
{
login: react.login,
Expand All @@ -24,7 +30,6 @@ context('2FA with various methods', () => {
before(() => {
cy.useConfigProfile(profile)
cy.proxy(app)
cy.task('resetCRI')
})
let email = gen.email()
let password = gen.password()
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/cypress/integration/profiles/mfa/webauthn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { routes as express } from '../../../helpers/express'

context('2FA WebAuthn', () => {
before(() => {
cy.task('resetCRI')
cy.task('resetCRI', {})
})
after(() => {
cy.task('resetCRI', {})
})
;[
{
Expand Down
9 changes: 7 additions & 2 deletions test/e2e/cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ module.exports = (on) => {
// Reset chrome remote interface for clean state
async resetCRI() {
if (criClient) {
await criClient.close()
const c = criClient
criClient = null
await c.close()
}

return Promise.resolve(true)
},
// Execute CRI command
async sendCRI(args) {
criClient = criClient || (await CRI({ port: criPort }))
if (!criClient) {
criClient = await CRI({ port: criPort })
}

return criClient.send(args.query, args.opts)
}
})
Expand Down
45 changes: 32 additions & 13 deletions test/e2e/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,48 @@ const mergeFields = (form, fields) => {
return { ...result, ...fields }
}

function checkConfigVersion(previous, tries = 0) {
cy.wait(10)
cy.request('GET', KRATOS_ADMIN + '/health/config').then(({ body }) => {
if (previous !== body) {
return
} else if (tries > 5) {
console.warn(
'Config version did not change after 5 tries, maybe the changes did not have an effect?'
)
return
}
cy.wait(50)
checkConfigVersion(previous, tries + 1)
})
}

const updateConfigFile = (cb: (arg: any) => any) => {
cy.readFile(configFile).then((contents) => {
cy.writeFile(configFile, YAML.stringify(cb(YAML.parse(contents))))
cy.wait(500)
cy.request('GET', KRATOS_ADMIN + '/health/config').then(({ body }) => {
cy.readFile(configFile).then((contents) => {
cy.writeFile(configFile, YAML.stringify(cb(YAML.parse(contents))))
cy.wait(500)
})
checkConfigVersion(body)
})
}

Cypress.Commands.add('useConfigProfile', (profile: string) => {
console.log('Switching config profile to:', profile)
cy.readFile(`kratos.${profile}.yml`).then((contents) =>
cy.writeFile(configFile, contents)
)
cy.wait(500)
cy.request('GET', KRATOS_ADMIN + '/health/config').then(({ body }) => {
console.log('Switching config profile to:', profile)
cy.readFile(`kratos.${profile}.yml`).then((contents) =>
cy.writeFile(configFile, contents)
)
checkConfigVersion(body)
})
})

Cypress.Commands.add('proxy', (app: string) => {
console.log('Switching proxy profile to:', app)
cy.writeFile(`proxy.json`, `"${app}"`)
cy.readFile(`proxy.json`).should('eq', app)
cy.wait(200)
cy.visit(APP_URL + '/')
cy.get(`[data-testid="app-${app}"]`).should('exist')
cy.wait(500)
cy.request(APP_URL + '/')
.its('body')
.should('contain', `data-testid="app-${app}"`)
})

Cypress.Commands.add('shortPrivilegedSessionTime', ({} = {}) => {
Expand Down
Loading

0 comments on commit 3b8b610

Please sign in to comment.