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
Show file tree
Hide file tree
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
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
52 changes: 35 additions & 17 deletions integration/client_credentials_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{})
Expand All @@ -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
}{
{
Expand All @@ -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())
},
},
} {
Expand All @@ -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)
})
}
Expand Down