Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Nov 29, 2023
1 parent ff3dfdf commit b05b597
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 109 deletions.
221 changes: 113 additions & 108 deletions oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,126 +8,131 @@ import (
)

func TestAuthCodeURL(t *testing.T) {
f := func(cfg Config, state string, want string) {
client := NewClient(http.DefaultClient, cfg)

url := client.AuthCodeURL(state)
mustEqual(t, url, want)
}

f(
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth",
TokenURL: "",
testCases := []struct {
cfg Config
state string
want string
}{
{
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth",
TokenURL: "",
},
"test-state",
`server:1234/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
},
"test-state",
`server:1234/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
)

f(
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth?foo=bar",
TokenURL: "",
{
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth?foo=bar",
TokenURL: "",
},
"test-state",
`server:1234/auth?foo=bar&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
},
"test-state",
`server:1234/auth?foo=bar&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
)

f(
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: []string{"scope1", "scope2"},
AuthURL: "server:1234/auth",
TokenURL: "",
{
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: []string{"scope1", "scope2"},
AuthURL: "server:1234/auth",
TokenURL: "",
},
"test-state",
`server:1234/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=test-state`,
},
"test-state",
`server:1234/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=test-state`,
)

f(
Config{
ClientID: "CLIENT_ID",
AuthURL: "server:1234/auth-url",
TokenURL: "",
{
Config{
ClientID: "CLIENT_ID",
AuthURL: "server:1234/auth-url",
TokenURL: "",
},
"",
`server:1234/auth-url?client_id=CLIENT_ID&response_type=code`,
},
"",
`server:1234/auth-url?client_id=CLIENT_ID&response_type=code`,
)
}

func TestAuthCodeURLWithParams(t *testing.T) {
f := func(cfg Config, state string, params url.Values, want string) {
client := NewClient(http.DefaultClient, cfg)
}

url := client.AuthCodeURLWithParams(state, params)
mustEqual(t, url, want)
for _, tc := range testCases {
client := NewClient(http.DefaultClient, tc.cfg)
url := client.AuthCodeURL(tc.state)
mustEqual(t, url, tc.want)
}
}

f(
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth",
TokenURL: "",
func TestAuthCodeURLWithParams(t *testing.T) {
testCases := []struct {
cfg Config
state string
params url.Values
want string
}{
{
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth",
TokenURL: "",
},
"test-state",
nil,
`server:1234/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
},
"test-state",
nil,
`server:1234/auth?client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
)

f(
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth?foo=bar",
TokenURL: "",
{
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: nil,
AuthURL: "server:1234/auth?foo=bar",
TokenURL: "",
},
"test-state",
nil,
`server:1234/auth?foo=bar&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
},
"test-state",
nil,
`server:1234/auth?foo=bar&client_id=CLIENT_ID&redirect_uri=REDIRECT_URL&response_type=code&state=test-state`,
)

f(
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: []string{"scope1", "scope2"},
AuthURL: "server:1234/auth",
TokenURL: "",
{
Config{
ClientID: "CLIENT_ID",
ClientSecret: "CLIENT_SECRET",
RedirectURL: "REDIRECT_URL",
Scopes: []string{"scope1", "scope2"},
AuthURL: "server:1234/auth",
TokenURL: "",
},
"test-state",
url.Values{
"access_type": []string{"anything"},
"param1": []string{"value1"},
},
`server:1234/auth?access_type=anything&client_id=CLIENT_ID&param1=value1&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=test-state`,
},
"test-state",
url.Values{
"access_type": []string{"anything"},
"param1": []string{"value1"},
{
Config{
ClientID: "CLIENT_ID",
AuthURL: "server:1234/auth-url",
TokenURL: "",
},
"",
nil,
`server:1234/auth-url?client_id=CLIENT_ID&response_type=code`,
},
`server:1234/auth?access_type=anything&client_id=CLIENT_ID&param1=value1&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=test-state`,
)
}

f(
Config{
ClientID: "CLIENT_ID",
AuthURL: "server:1234/auth-url",
TokenURL: "",
},
"",
nil,
`server:1234/auth-url?client_id=CLIENT_ID&response_type=code`,
)
for _, tc := range testCases {
client := NewClient(http.DefaultClient, tc.cfg)
url := client.AuthCodeURLWithParams(tc.state, tc.params)
mustEqual(t, url, tc.want)
}
}

func mustOk(tb testing.TB, err error) {
Expand Down
2 changes: 1 addition & 1 deletion token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestTokenExtra(t *testing.T) {
{wantKey, "abc", "abc"},
{wantKey, 123, 123},
{wantKey, "", ""},
{wantKey, "def", nil},
{"other-key", "def", nil},
}

for _, tc := range testCases {
Expand Down

0 comments on commit b05b597

Please sign in to comment.