Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix assertions #683

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ require (
github.com/pborman/uuid v1.2.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.7.1
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
@@ -65,6 +66,8 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/tools v0.1.1 // indirect
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -675,10 +675,13 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/subosito/gotenv v1.1.1/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.0.4/go.mod h1:bURseu1nuBkFpIES5cz6zBtjmYeOQmEESshn7VpF15Y=
github.com/tidwall/sjson v1.1.5/go.mod h1:VuJzsZnTowhSxWdOgsAnb886i4AjEyTkk7tNtsL7EYE=
52 changes: 35 additions & 17 deletions integration/client_credentials_grant_test.go
Original file line number Diff line number Diff line change
@@ -24,8 +24,12 @@ package integration_test
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/stretchr/testify/assert"
@@ -45,6 +49,20 @@ func TestClientCredentialsFlow(t *testing.T) {
}
}

func introspect(t *testing.T, ts *httptest.Server, token string, p interface{}, username, password string) {
req, err := http.NewRequest("POST", ts.URL+"/introspect", strings.NewReader(url.Values{"token": {token}}.Encode()))
require.NoError(t, err)
req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, r.StatusCode, "%s", body)
require.NoError(t, json.Unmarshal(body, p))
}

func runClientCredentialsGrantTest(t *testing.T, strategy oauth2.AccessTokenStrategy) {
f := compose.Compose(new(fosite.Config), fositeStore, strategy, compose.OAuth2ClientCredentialsGrantFactory, compose.OAuth2TokenIntrospectionFactory)
ts := mockServer(t, f, &fosite.DefaultSession{})
@@ -55,7 +73,7 @@ func runClientCredentialsGrantTest(t *testing.T, strategy oauth2.AccessTokenStra
description string
setup func()
err bool
check func(t *testing.T, r *http.Response)
check func(t *testing.T, token *goauth.Token)
params url.Values
}{
{
@@ -78,28 +96,23 @@ func runClientCredentialsGrantTest(t *testing.T, strategy oauth2.AccessTokenStra
description: "should pass",
setup: func() {
},
check: func(t *testing.T, r *http.Response) {
var b fosite.AccessRequest
b.Client = new(fosite.DefaultClient)
b.Session = new(defaultSession)
require.NoError(t, json.NewDecoder(r.Body).Decode(&b))
assert.EqualValues(t, fosite.Arguments{"https://www.ory.sh/api"}, b.RequestedAudience)
assert.EqualValues(t, fosite.Arguments{"https://www.ory.sh/api"}, b.GrantedAudience)
assert.EqualValues(t, "my-client", b.Session.(*defaultSession).Subject)
check: func(t *testing.T, token *goauth.Token) {
var j json.RawMessage
introspect(t, ts, token.AccessToken, &j, oauthClient.ClientID, oauthClient.ClientSecret)
assert.Equal(t, oauthClient.ClientID, gjson.GetBytes(j, "client_id").String())
assert.Equal(t, "fosite", gjson.GetBytes(j, "scope").String())
},
},
{
description: "should pass",
setup: func() {
},
check: func(t *testing.T, r *http.Response) {
var b fosite.AccessRequest
b.Client = new(fosite.DefaultClient)
b.Session = new(defaultSession)
require.NoError(t, json.NewDecoder(r.Body).Decode(&b))
assert.EqualValues(t, fosite.Arguments{}, b.RequestedAudience)
assert.EqualValues(t, fosite.Arguments{}, b.GrantedAudience)
assert.EqualValues(t, "my-client", b.Session.(*defaultSession).Subject)
check: func(t *testing.T, token *goauth.Token) {
var j json.RawMessage
introspect(t, ts, token.AccessToken, &j, oauthClient.ClientID, oauthClient.ClientSecret)
introspect(t, ts, token.AccessToken, &j, oauthClient.ClientID, oauthClient.ClientSecret)
assert.Equal(t, oauthClient.ClientID, gjson.GetBytes(j, "client_id").String())
assert.Equal(t, "fosite", gjson.GetBytes(j, "scope").String())
},
},
} {
@@ -112,6 +125,11 @@ func runClientCredentialsGrantTest(t *testing.T, strategy oauth2.AccessTokenStra
if !c.err {
assert.NotEmpty(t, token.AccessToken, "(%d) %s\n%s", k, c.description, token)
}

if c.check != nil {
c.check(t, token)
}

t.Logf("Passed test case %d", k)
})
}