|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "golang.org/x/oauth2" |
| 9 | +) |
| 10 | + |
| 11 | +// Tests |
| 12 | + |
| 13 | +func TestGenericOAuthName(t *testing.T) { |
| 14 | + p := GenericOAuth{} |
| 15 | + assert.Equal(t, "generic-oauth", p.Name()) |
| 16 | +} |
| 17 | + |
| 18 | +func TestGenericOAuthSetup(t *testing.T) { |
| 19 | + assert := assert.New(t) |
| 20 | + p := GenericOAuth{} |
| 21 | + |
| 22 | + // Check validation |
| 23 | + err := p.Setup() |
| 24 | + if assert.Error(err) { |
| 25 | + assert.Equal("providers.generic-oauth.auth-url, providers.generic-oauth.token-url, providers.generic-oauth.user-url, providers.generic-oauth.client-id, providers.generic-oauth.client-secret must be set", err.Error()) |
| 26 | + } |
| 27 | + |
| 28 | + // Check setup |
| 29 | + p = GenericOAuth{ |
| 30 | + AuthURL: "https://provider.com/oauth2/auth", |
| 31 | + TokenURL: "https://provider.com/oauth2/token", |
| 32 | + UserURL: "https://provider.com/oauth2/user", |
| 33 | + ClientID: "id", |
| 34 | + ClientSecret: "secret", |
| 35 | + } |
| 36 | + err = p.Setup() |
| 37 | + assert.Nil(err) |
| 38 | +} |
| 39 | + |
| 40 | +func TestGenericOAuthGetLoginURL(t *testing.T) { |
| 41 | + assert := assert.New(t) |
| 42 | + p := GenericOAuth{ |
| 43 | + AuthURL: "https://provider.com/oauth2/auth", |
| 44 | + TokenURL: "https://provider.com/oauth2/token", |
| 45 | + UserURL: "https://provider.com/oauth2/user", |
| 46 | + ClientID: "idtest", |
| 47 | + ClientSecret: "secret", |
| 48 | + Scopes: []string{"scopetest"}, |
| 49 | + } |
| 50 | + err := p.Setup() |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + // Check url |
| 56 | + uri, err := url.Parse(p.GetLoginURL("http://example.com/_oauth", "state")) |
| 57 | + assert.Nil(err) |
| 58 | + assert.Equal("https", uri.Scheme) |
| 59 | + assert.Equal("provider.com", uri.Host) |
| 60 | + assert.Equal("/oauth2/auth", uri.Path) |
| 61 | + |
| 62 | + // Check query string |
| 63 | + qs := uri.Query() |
| 64 | + expectedQs := url.Values{ |
| 65 | + "client_id": []string{"idtest"}, |
| 66 | + "redirect_uri": []string{"http://example.com/_oauth"}, |
| 67 | + "response_type": []string{"code"}, |
| 68 | + "scope": []string{"scopetest"}, |
| 69 | + "state": []string{"state"}, |
| 70 | + } |
| 71 | + assert.Equal(expectedQs, qs) |
| 72 | +} |
| 73 | + |
| 74 | +func TestGenericOAuthExchangeCode(t *testing.T) { |
| 75 | + assert := assert.New(t) |
| 76 | + |
| 77 | + // Setup server |
| 78 | + expected := url.Values{ |
| 79 | + "client_id": []string{"idtest"}, |
| 80 | + "client_secret": []string{"sectest"}, |
| 81 | + "code": []string{"code"}, |
| 82 | + "grant_type": []string{"authorization_code"}, |
| 83 | + "redirect_uri": []string{"http://example.com/_oauth"}, |
| 84 | + } |
| 85 | + server, serverURL := NewOAuthServer(t, map[string]string{ |
| 86 | + "token": expected.Encode(), |
| 87 | + }) |
| 88 | + defer server.Close() |
| 89 | + |
| 90 | + // Setup provider |
| 91 | + p := GenericOAuth{ |
| 92 | + AuthURL: "https://provider.com/oauth2/auth", |
| 93 | + TokenURL: serverURL.String() + "/token", |
| 94 | + UserURL: "https://provider.com/oauth2/user", |
| 95 | + ClientID: "idtest", |
| 96 | + ClientSecret: "sectest", |
| 97 | + } |
| 98 | + err := p.Setup() |
| 99 | + if err != nil { |
| 100 | + t.Fatal(err) |
| 101 | + } |
| 102 | + |
| 103 | + // We force AuthStyleInParams to prevent the test failure when the |
| 104 | + // AuthStyleInHeader is attempted |
| 105 | + p.Config.Endpoint.AuthStyle = oauth2.AuthStyleInParams |
| 106 | + |
| 107 | + token, err := p.ExchangeCode("http://example.com/_oauth", "code") |
| 108 | + assert.Nil(err) |
| 109 | + assert.Equal("123456789", token) |
| 110 | +} |
| 111 | + |
| 112 | +func TestGenericOAuthGetUser(t *testing.T) { |
| 113 | + assert := assert.New(t) |
| 114 | + |
| 115 | + // Setup server |
| 116 | + server, serverURL := NewOAuthServer(t, nil) |
| 117 | + defer server.Close() |
| 118 | + |
| 119 | + // Setup provider |
| 120 | + p := GenericOAuth{ |
| 121 | + AuthURL: "https://provider.com/oauth2/auth", |
| 122 | + TokenURL: "https://provider.com/oauth2/token", |
| 123 | + UserURL: serverURL.String() + "/userinfo", |
| 124 | + ClientID: "idtest", |
| 125 | + ClientSecret: "sectest", |
| 126 | + } |
| 127 | + err := p.Setup() |
| 128 | + if err != nil { |
| 129 | + t.Fatal(err) |
| 130 | + } |
| 131 | + |
| 132 | + // We force AuthStyleInParams to prevent the test failure when the |
| 133 | + // AuthStyleInHeader is attempted |
| 134 | + p.Config.Endpoint.AuthStyle = oauth2.AuthStyleInParams |
| 135 | + |
| 136 | + user, err := p.GetUser("123456789") |
| 137 | + assert.Nil(err) |
| 138 | + |
| 139 | + assert.Equal("example@example.com", user.Email) |
| 140 | +} |
0 commit comments