Skip to content

Commit

Permalink
test: extract common registration helpers to library
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Mar 7, 2022
1 parent b4c4fd2 commit 5c1f11b
Show file tree
Hide file tree
Showing 7 changed files with 446 additions and 258 deletions.
345 changes: 345 additions & 0 deletions internal/registrationhelpers/helpers.go

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions internal/registrationhelpers/stub/basic.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"traits": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email",
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
},
"webauthn": {
"identifier": true
}
}
}
}
}
}
},
"additionalProperties": false
}
35 changes: 35 additions & 0 deletions internal/registrationhelpers/stub/multifield.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"traits": {
"type": "object",
"properties": {
"foobar": {
"type": "string",
"minLength": 2
},
"username": {
"type": "string",
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
},
"webauthn": {
"identifier": true
}
}
}
}
},
"required": [
"foobar",
"username"
]
}
},
"additionalProperties": false
}
9 changes: 9 additions & 0 deletions internal/testhelpers/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testhelpers

import (
"encoding/base64"
"testing"

"github.com/ory/kratos/driver/config"
Expand All @@ -24,3 +25,11 @@ func SetDefaultIdentitySchema(conf *config.Config, url string) {
{ID: "default", URL: url},
})
}

// SetDefaultIdentitySchemaFromRaw allows setting the default identity schema from a raw JSON string.
func SetDefaultIdentitySchemaFromRaw(conf *config.Config, schema []byte) {
conf.MustSet(config.ViperKeyDefaultIdentitySchemaID, "default")
conf.MustSet(config.ViperKeyIdentitySchemas, config.Schemas{
{ID: "default", URL: "base64://" + base64.URLEncoding.EncodeToString(schema)},
})
}
7 changes: 7 additions & 0 deletions internal/testhelpers/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testhelpers

import "github.com/ory/x/randx"

func RandomEmail() string {
return randx.MustString(16, randx.Alpha) + "@ory.sh"
}
9 changes: 7 additions & 2 deletions selfservice/strategy/password/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package password_test
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"fmt"
"github.com/ory/kratos/internal/registrationhelpers"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -39,6 +41,9 @@ import (
"github.com/ory/kratos/x"
)

//go:embed stub/login.schema.json
var loginSchema []byte

func TestCompleteLogin(t *testing.T) {
conf, reg := internal.NewFastRegistryWithMocks(t)
conf.MustSet(config.ViperKeySelfServiceStrategyConfig+"."+string(identity.CredentialsTypePassword),
Expand All @@ -54,11 +59,11 @@ func TestCompleteLogin(t *testing.T) {
conf.MustSet(config.ViperKeySelfServiceErrorUI, errTS.URL+"/error-ts")
conf.MustSet(config.ViperKeySelfServiceLoginUI, uiTS.URL+"/login-ts")

testhelpers.SetDefaultIdentitySchema(conf, "file://./stub/login.schema.json")
testhelpers.SetDefaultIdentitySchemaFromRaw(conf, loginSchema)
conf.MustSet(config.ViperKeySecretsDefault, []string{"not-a-secure-session-key"})

ensureFieldsExist := func(t *testing.T, body []byte) {
checkFormContent(t, body, "identifier",
registrationhelpers.CheckFormContent(t, body, "identifier",
"password",
"csrf_token")
}
Expand Down
Loading

0 comments on commit 5c1f11b

Please sign in to comment.