Skip to content

Commit

Permalink
all: export test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeneas Rekkas (arekkas) committed Jun 13, 2017
1 parent b5cb0c6 commit 1fee2b3
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 214 deletions.
14 changes: 1 addition & 13 deletions client/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import (
. "github.com/ory/hydra/client"
"github.com/ory/hydra/compose"
"github.com/ory/hydra/integration"
"github.com/ory/hydra/pkg"
"github.com/ory/ladon"
"github.com/stretchr/testify/assert"
)

var clientManagers = map[string]Storage{}
Expand Down Expand Up @@ -103,18 +101,8 @@ func TestAuthenticateClient(t *testing.T) {
Clients: map[string]Client{},
Hasher: &fosite.BCrypt{},
}
mem.CreateClient(&Client{
ID: "1234",
Secret: "secret",
RedirectURIs: []string{"http://redirect"},
})

c, err := mem.Authenticate("1234", []byte("secret1"))
pkg.AssertError(t, true, err)

c, err = mem.Authenticate("1234", []byte("secret"))
pkg.AssertError(t, false, err)
assert.Equal(t, "1234", c.ID)
TestHelperClientAuthenticate("", mem)(t)
}

func TestCreateGetDeleteClient(t *testing.T) {
Expand Down
41 changes: 31 additions & 10 deletions client/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package client

import (
"testing"
"github.com/stretchr/testify/assert"
"time"

"github.com/ory/fosite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHelperClientAutoGenerateKey(k string, m Storage) func(t *testing.T) {
Expand All @@ -14,9 +16,26 @@ func TestHelperClientAutoGenerateKey(k string, m Storage) func(t *testing.T) {
RedirectURIs: []string{"http://redirect"},
TermsOfServiceURI: "foo",
}
assert.Nil(t, m.CreateClient(c))
assert.NoError(t, m.CreateClient(c))
assert.NotEmpty(t, c.ID)
assert.Nil(t, m.DeleteClient(c.ID))
assert.NoError(t, m.DeleteClient(c.ID))
}
}

func TestHelperClientAuthenticate(k string, m Manager) func(t *testing.T) {
return func(t *testing.T) {
m.CreateClient(&Client{
ID: "1234321",
Secret: "secret",
RedirectURIs: []string{"http://redirect"},
})

c, err := m.Authenticate("1234321", []byte("secret1"))
require.NotNil(t, err)

c, err = m.Authenticate("1234321", []byte("secret"))
require.Error(t, err)
assert.Equal(t, "1234321", c.ID)
}
}

Expand All @@ -32,8 +51,9 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
RedirectURIs: []string{"http://redirect"},
TermsOfServiceURI: "foo",
}

err = m.CreateClient(c)
assert.Nil(t, err)
assert.NoError(t, err)
if err == nil {
compare(t, c, k)
}
Expand All @@ -45,19 +65,20 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
RedirectURIs: []string{"http://redirect"},
TermsOfServiceURI: "foo",
})
assert.Nil(t, err)
assert.NoError(t, err)

// RethinkDB delay
time.Sleep(100 * time.Millisecond)

d, err := m.GetClient(nil, "1234")
assert.Nil(t, err)
assert.NoError(t, err)

if err == nil {
compare(t, d, k)
}

ds, err := m.GetClients()
assert.Nil(t, err)
assert.NoError(t, err)
assert.Len(t, ds, 2)
assert.NotEqual(t, ds["1234"].ID, ds["2-1234"].ID)

Expand All @@ -68,11 +89,11 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
RedirectURIs: []string{"http://redirect/new"},
TermsOfServiceURI: "bar",
})
assert.Nil(t, err)
assert.NoError(t, err)
time.Sleep(100 * time.Millisecond)

nc, err := m.GetConcreteClient("2-1234")
assert.Nil(t, err)
assert.NoError(t, err)

if k != "http" {
// http always returns an empty secret
Expand All @@ -84,7 +105,7 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
assert.Zero(t, len(nc.Contacts))

err = m.DeleteClient("1234")
assert.Nil(t, err)
assert.NoError(t, err)

// RethinkDB delay
time.Sleep(100 * time.Millisecond)
Expand Down
74 changes: 6 additions & 68 deletions jwk/manager_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package jwk_test

import (
"crypto/rand"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -20,7 +18,6 @@ import (
. "github.com/ory/hydra/jwk"
"github.com/ory/hydra/pkg"
"github.com/ory/ladon"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -69,15 +66,7 @@ func init() {
managers["http"] = httpManager
}

func randomBytes(n int) ([]byte, error) {
bytes := make([]byte, n)
if _, err := io.ReadFull(rand.Reader, bytes); err != nil {
return []byte{}, errors.WithStack(err)
}
return bytes, nil
}

var encryptionKey, _ = randomBytes(32)
var encryptionKey, _ = RandomBytes(32)

func TestMain(m *testing.M) {
connectToPG()
Expand Down Expand Up @@ -132,48 +121,16 @@ func TestHTTPManagerPublicKeyGet(t *testing.T) {

func TestManagerKey(t *testing.T) {
ks, _ := testGenerator.Generate("")
priv := ks.Key("private")
pub := ks.Key("public")

for name, m := range managers {
t.Run(fmt.Sprintf("case=%s", name), func(t *testing.T) {
_, err := m.GetKey("faz", "baz")
assert.NotNil(t, err)

err = m.AddKey("faz", First(priv))
assert.Nil(t, err)

time.Sleep(time.Millisecond * 100)

got, err := m.GetKey("faz", "private")
assert.Nil(t, err)
assert.Equal(t, priv, got.Keys, "%s", name)

err = m.AddKey("faz", First(pub))
assert.Nil(t, err)

time.Sleep(time.Millisecond * 100)

got, err = m.GetKey("faz", "private")
assert.Nil(t, err)
assert.Equal(t, priv, got.Keys, "%s", name)

got, err = m.GetKey("faz", "public")
assert.Nil(t, err)
assert.Equal(t, pub, got.Keys, "%s", name)

err = m.DeleteKey("faz", "public")
assert.Nil(t, err)

time.Sleep(time.Millisecond * 100)

ks, err = m.GetKey("faz", "public")
assert.NotNil(t, err)
TestHelperManagerKey(m, ks)(t)
})
}

priv := ks.Key("private")
err := managers["http"].AddKey("nonono", First(priv))
pkg.AssertError(t, true, err, "%s")
assert.NotNil(t, err)
}

func TestManagerKeySet(t *testing.T) {
Expand All @@ -182,29 +139,10 @@ func TestManagerKeySet(t *testing.T) {

for name, m := range managers {
t.Run(fmt.Sprintf("case=%s", name), func(t *testing.T) {
_, err := m.GetKeySet("foo")
pkg.AssertError(t, true, err, name)

err = m.AddKeySet("bar", ks)
assert.Nil(t, err)

time.Sleep(time.Millisecond * 100)

got, err := m.GetKeySet("bar")
assert.Nil(t, err)
assert.Equal(t, ks.Key("public"), got.Key("public"), name)
assert.Equal(t, ks.Key("private"), got.Key("private"), name)

err = m.DeleteKeySet("bar")
assert.Nil(t, err)

time.Sleep(time.Millisecond * 100)

_, err = m.GetKeySet("bar")
assert.NotNil(t, err)
TestHelperManagerKeySet(m, ks)(t)
})
}

err := managers["http"].AddKeySet("nonono", ks)
pkg.AssertError(t, true, err, "%s")
assert.NotNil(t, err)
}
75 changes: 75 additions & 0 deletions jwk/manager_test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package jwk

import (
"crypto/rand"
"io"
"testing"

"github.com/ory/hydra/pkg"
"github.com/pkg/errors"
"github.com/square/go-jose"
"github.com/stretchr/testify/assert"
)

func RandomBytes(n int) ([]byte, error) {
bytes := make([]byte, n)
if _, err := io.ReadFull(rand.Reader, bytes); err != nil {
return []byte{}, errors.WithStack(err)
}
return bytes, nil
}

func TestHelperManagerKey(m Manager, keys *jose.JsonWebKeySet) func(t *testing.T) {
pub := keys.Key("public")
priv := keys.Key("private")

return func(t *testing.T) {
_, err := m.GetKey("faz", "baz")
assert.NotNil(t, err)

err = m.AddKey("faz", First(priv))
assert.Nil(t, err)

got, err := m.GetKey("faz", "private")
assert.Nil(t, err)
assert.Equal(t, priv, got.Keys)

err = m.AddKey("faz", First(pub))
assert.Nil(t, err)

got, err = m.GetKey("faz", "private")
assert.Nil(t, err)
assert.Equal(t, priv, got.Keys)

got, err = m.GetKey("faz", "public")
assert.Nil(t, err)
assert.Equal(t, pub, got.Keys)

err = m.DeleteKey("faz", "public")
assert.Nil(t, err)

_, err = m.GetKey("faz", "public")
assert.NotNil(t, err)
}
}

func TestHelperManagerKeySet(m Manager, keys *jose.JsonWebKeySet) func(t *testing.T) {
return func(t *testing.T) {
_, err := m.GetKeySet("foo")
pkg.AssertError(t, true, err)

err = m.AddKeySet("bar", keys)
assert.Nil(t, err)

got, err := m.GetKeySet("bar")
assert.Nil(t, err)
assert.Equal(t, keys.Key("public"), got.Key("public"))
assert.Equal(t, keys.Key("private"), got.Key("private"))

err = m.DeleteKeySet("bar")
assert.Nil(t, err)

_, err = m.GetKeySet("bar")
assert.NotNil(t, err)
}
}
Loading

0 comments on commit 1fee2b3

Please sign in to comment.