Skip to content

Commit

Permalink
all: improve test exports (ory#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Jun 14, 2017
1 parent d91305b commit b40e879
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 95 deletions.
4 changes: 0 additions & 4 deletions client/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
"github.com/ory/dockertest"
"github.com/ory/fosite"
"github.com/ory/herodot"
. "github.com/ory/hydra/client"
Expand Down Expand Up @@ -57,9 +56,6 @@ func init() {
}
}

var resources []*dockertest.Resource
var pool *dockertest.Pool

func TestMain(m *testing.M) {
connectToPG()
connectToMySQL()
Expand Down
16 changes: 3 additions & 13 deletions client/manager_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"testing"
"time"

"github.com/ory/fosite"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -52,23 +51,18 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
TermsOfServiceURI: "foo",
}

err = m.CreateClient(c)
assert.NoError(t, err)
assert.NoError(t, m.CreateClient(c))
if err == nil {
compare(t, c, k)
}

err = m.CreateClient(&Client{
assert.NoError(t, m.CreateClient(&Client{
ID: "2-1234",
Name: "name",
Secret: "secret",
RedirectURIs: []string{"http://redirect"},
TermsOfServiceURI: "foo",
})
assert.NoError(t, err)

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

d, err := m.GetClient(nil, "1234")
assert.NoError(t, err)
Expand All @@ -90,7 +84,6 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
TermsOfServiceURI: "bar",
})
assert.NoError(t, err)
time.Sleep(100 * time.Millisecond)

nc, err := m.GetConcreteClient("2-1234")
assert.NoError(t, err)
Expand All @@ -107,9 +100,6 @@ func TestHelperCreateGetDeleteClient(k string, m Storage) func(t *testing.T) {
err = m.DeleteClient("1234")
assert.NoError(t, err)

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

_, err = m.GetClient(nil, "1234")
assert.NotNil(t, err)
}
Expand Down
74 changes: 3 additions & 71 deletions oauth2/fosite_store_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package oauth2

import (
"context"
"fmt"
"os"
"testing"
"time"

"github.com/Sirupsen/logrus"
"github.com/ory/fosite"
"github.com/ory/hydra/client"
"github.com/ory/hydra/integration"
"github.com/ory/hydra/pkg"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var clientManagers = map[string]pkg.FositeStorer{}
Expand Down Expand Up @@ -77,82 +72,19 @@ func TestCreateGetDeleteAccessTokenSession(t *testing.T) {
}

func TestCreateGetDeleteOpenIDConnectSession(t *testing.T) {
ctx := context.Background()
for k, m := range clientManagers {
t.Run(fmt.Sprintf("case=%s", k), func(t *testing.T) {
_, err := m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{})
assert.NotNil(t, err)

err = m.CreateOpenIDConnectSession(ctx, "4321", &defaultRequest)
require.Nil(t, err)

res, err := m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{Session: &fosite.DefaultSession{}})
require.Nil(t, err)
AssertObjectKeysEqual(t, &defaultRequest, res, "Scopes", "GrantedScopes", "Form", "Session")

err = m.DeleteOpenIDConnectSession(ctx, "4321")
require.Nil(t, err)

time.Sleep(100 * time.Millisecond)

_, err = m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{})
assert.NotNil(t, err)
})
t.Run(fmt.Sprintf("case=%s", k), TestHelperCreateGetDeleteOpenIDConnectSession(m))
}
}

func TestCreateGetDeleteRefreshTokenSession(t *testing.T) {
ctx := context.Background()
for k, m := range clientManagers {
t.Run(fmt.Sprintf("case=%s", k), func(t *testing.T) {
_, err := m.GetRefreshTokenSession(ctx, "4321", &fosite.DefaultSession{})
assert.NotNil(t, err)

err = m.CreateRefreshTokenSession(ctx, "4321", &defaultRequest)
require.Nil(t, err)

res, err := m.GetRefreshTokenSession(ctx, "4321", &fosite.DefaultSession{})
require.Nil(t, err)
AssertObjectKeysEqual(t, &defaultRequest, res, "Scopes", "GrantedScopes", "Form", "Session")

err = m.DeleteRefreshTokenSession(ctx, "4321")
require.Nil(t, err)

time.Sleep(100 * time.Millisecond)

_, err = m.GetRefreshTokenSession(ctx, "4321", &fosite.DefaultSession{})
assert.NotNil(t, err)
})
t.Run(fmt.Sprintf("case=%s", k), TestHelperCreateGetDeleteRefreshTokenSession(m))
}
}

func TestRevokeRefreshToken(t *testing.T) {
ctx := context.Background()
id := uuid.New()
for k, m := range clientManagers {
t.Run(fmt.Sprintf("case=%s", k), func(t *testing.T) {
_, err := m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
assert.NotNil(t, err)

err = m.CreateRefreshTokenSession(ctx, "1111", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second)})
require.Nil(t, err)

err = m.CreateRefreshTokenSession(ctx, "1122", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second)})
require.Nil(t, err)

_, err = m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
require.Nil(t, err)

err = m.RevokeRefreshToken(ctx, id)
require.Nil(t, err)

time.Sleep(100 * time.Millisecond)

_, err = m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
assert.NotNil(t, err)

_, err = m.GetRefreshTokenSession(ctx, "1122", &fosite.DefaultSession{})
assert.NotNil(t, err)
})
t.Run(fmt.Sprintf("case=%s", k), TestHelperRevokeRefreshToken(m))
}
}
78 changes: 75 additions & 3 deletions oauth2/fosite_store_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"testing"

"net/url"
"time"

"github.com/ory/fosite"
"github.com/ory/hydra/client"
"github.com/ory/hydra/pkg"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"time"
"github.com/ory/hydra/client"
)

var defaultRequest = fosite.Request{
Expand All @@ -22,6 +24,76 @@ var defaultRequest = fosite.Request{
Session: &fosite.DefaultSession{Subject: "bar"},
}

func TestHelperCreateGetDeleteOpenIDConnectSession(m pkg.FositeStorer) func(t *testing.T) {
return func(t *testing.T) {
ctx := context.Background()
_, err := m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{})
assert.NotNil(t, err)

err = m.CreateOpenIDConnectSession(ctx, "4321", &defaultRequest)
require.NoError(t, err)

res, err := m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{Session: &fosite.DefaultSession{}})
require.NoError(t, err)
AssertObjectKeysEqual(t, &defaultRequest, res, "Scopes", "GrantedScopes", "Form", "Session")

err = m.DeleteOpenIDConnectSession(ctx, "4321")
require.NoError(t, err)

_, err = m.GetOpenIDConnectSession(ctx, "4321", &fosite.Request{})
assert.NotNil(t, err)
}
}

func TestHelperCreateGetDeleteRefreshTokenSession(m pkg.FositeStorer) func(t *testing.T) {
return func(t *testing.T) {
ctx := context.Background()
_, err := m.GetRefreshTokenSession(ctx, "4321", &fosite.DefaultSession{})
assert.NotNil(t, err)

err = m.CreateRefreshTokenSession(ctx, "4321", &defaultRequest)
require.NoError(t, err)

res, err := m.GetRefreshTokenSession(ctx, "4321", &fosite.DefaultSession{})
require.NoError(t, err)
AssertObjectKeysEqual(t, &defaultRequest, res, "Scopes", "GrantedScopes", "Form", "Session")

err = m.DeleteRefreshTokenSession(ctx, "4321")
require.NoError(t, err)

_, err = m.GetRefreshTokenSession(ctx, "4321", &fosite.DefaultSession{})
assert.NotNil(t, err)
}

}
func TestHelperRevokeRefreshToken(m pkg.FositeStorer) func(t *testing.T) {
return func(t *testing.T) {
ctx := context.Background()
id := uuid.New()
_, err := m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
assert.NotNil(t, err)

err = m.CreateRefreshTokenSession(ctx, "1111", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second)})
require.NoError(t, err)

err = m.CreateRefreshTokenSession(ctx, "1122", &fosite.Request{ID: id, Client: &client.Client{ID: "foobar"}, RequestedAt: time.Now().Round(time.Second)})
require.NoError(t, err)

_, err = m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
require.NoError(t, err)

err = m.RevokeRefreshToken(ctx, id)
require.NoError(t, err)

_, err = m.GetRefreshTokenSession(ctx, "1111", &fosite.DefaultSession{})
assert.NotNil(t, err)

_, err = m.GetRefreshTokenSession(ctx, "1122", &fosite.DefaultSession{})
assert.NotNil(t, err)

}

}
func TestHelperCreateGetDeleteAuthorizeCodes(m pkg.FositeStorer) func(t *testing.T) {
return func(t *testing.T) {
ctx := context.Background()
Expand Down
9 changes: 5 additions & 4 deletions oauth2/oauth2_auth_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (

"net/http/cookiejar"

"net/url"
"strings"

"github.com/dgrijalva/jwt-go"
"github.com/julienschmidt/httprouter"
ejwt "github.com/ory/fosite/token/jwt"
"github.com/ory/hydra/jwk"
. "github.com/ory/hydra/oauth2"
"github.com/ory/hydra/pkg"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"github.com/stretchr/testify/assert"
"net/url"
"strings"
)

func TestAuthCode(t *testing.T) {
Expand Down Expand Up @@ -95,4 +96,4 @@ func testRefresh(t *testing.T, token *oauth2.Token) (*http.Response, error) {
req.SetBasicAuth(oauthClientConfig.ClientID, oauthClientConfig.ClientSecret)

return http.DefaultClient.Do(req)
}
}

0 comments on commit b40e879

Please sign in to comment.