Skip to content

Commit

Permalink
test: add test for registration request
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 10, 2020
1 parent ce9133b commit 79ed63c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
8 changes: 3 additions & 5 deletions selfservice/flow/registration/request.go
Expand Up @@ -73,11 +73,9 @@ func NewRequest(exp time.Duration, csrf string, r *http.Request) *Request {
source := urlx.Copy(r.URL)
source.Host = r.Host

if len(source.Scheme) == 0 {
source.Scheme = "http"
if r.TLS != nil {
source.Scheme = "https"
}
source.Scheme = "http"
if r.TLS != nil {
source.Scheme = "https"
}

return &Request{
Expand Down
32 changes: 32 additions & 0 deletions selfservice/flow/registration/request_test.go
@@ -1,13 +1,17 @@
package registration_test

import (
"crypto/tls"
"net/http"
"testing"
"time"

"github.com/bxcodec/faker/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/x/urlx"

"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/x"
)
Expand All @@ -28,6 +32,34 @@ func TestFakeRequestData(t *testing.T) {
}
}

func TestNewRequest(t *testing.T) {
t.Run("case=0", func(t *testing.T) {
r := registration.NewRequest(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("/"),
Host: "ory.sh", TLS: &tls.ConnectionState{},
})
assert.Equal(t, r.IssuedAt, r.ExpiresAt)
// assert.Equal(t, flow.TypeBrowser, r.Type)
assert.Equal(t, "https://ory.sh/", r.RequestURL)
})

t.Run("case=1", func(t *testing.T) {
r := registration.NewRequest(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("/"),
Host: "ory.sh"})
assert.Equal(t, r.IssuedAt, r.ExpiresAt)
// assert.Equal(t, flow.TypeBrowser, r.Type)
assert.Equal(t, "http://ory.sh/", r.RequestURL)
})

t.Run("case=2", func(t *testing.T) {
r := registration.NewRequest(0, "csrf", &http.Request{
URL: urlx.ParseOrPanic("https://ory.sh/"),
Host: "ory.sh"})
assert.Equal(t, "http://ory.sh/", r.RequestURL)
})
}

func TestRequest(t *testing.T) {
r := &registration.Request{ID: x.NewUUID()}
assert.Equal(t, r.ID, r.GetID())
Expand Down

0 comments on commit 79ed63c

Please sign in to comment.