|
| 1 | +package quickbooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/markbates/goth" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_New(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + a := assert.New(t) |
| 17 | + |
| 18 | + provider := New("client-id", "secret", "http://example.com/callback", nil, ScopeAccounting) |
| 19 | + a.Equal(provider.ClientId(), "client-id") |
| 20 | + a.Equal(provider.Secret(), "secret") |
| 21 | + a.Equal(provider.RedirectURL(), "http://example.com/callback") |
| 22 | + a.Equal(provider.Name(), "quickbooks") |
| 23 | +} |
| 24 | + |
| 25 | +func Test_Implements_Provider(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + a := assert.New(t) |
| 28 | + a.Implements((*goth.Provider)(nil), New("", "", "", nil)) |
| 29 | +} |
| 30 | + |
| 31 | +func Test_BeginAuth(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + a := assert.New(t) |
| 34 | + |
| 35 | + provider := New("client-id", "secret", "http://example.com/callback", nil, ScopeAccounting) |
| 36 | + session, err := provider.BeginAuth("test_state") |
| 37 | + s := session.(*Session) |
| 38 | + a.NoError(err) |
| 39 | + a.Contains(s.AuthURL, "appcenter.intuit.com/connect/oauth2") |
| 40 | + a.Contains(s.AuthURL, "client_id=client-id") |
| 41 | + a.Contains(s.AuthURL, "state=test_state") |
| 42 | + a.Contains(s.AuthURL, "scope=com.intuit.quickbooks.accounting") |
| 43 | +} |
| 44 | + |
| 45 | +func Test_FetchUser(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + a := assert.New(t) |
| 48 | + |
| 49 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 50 | + a.Equal(r.Header.Get("Authorization"), "Bearer access_token") |
| 51 | + w.Header().Set("Content-Type", "application/json") |
| 52 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 53 | + "sub": "user123", |
| 54 | + "email": "user@example.com", |
| 55 | + "email_verified": true, |
| 56 | + "name": "John Doe", |
| 57 | + "given_name": "John", |
| 58 | + "family_name": "Doe", |
| 59 | + }) |
| 60 | + })) |
| 61 | + defer ts.Close() |
| 62 | + |
| 63 | + provider := New("client-id", "secret", "http://example.com/callback", nil, ScopeAccounting) |
| 64 | + provider.userInfoURL = ts.URL |
| 65 | + session := &Session{ |
| 66 | + AccessToken: "access_token", |
| 67 | + ExpiresAt: time.Now().Add(time.Hour), |
| 68 | + } |
| 69 | + |
| 70 | + user, err := provider.FetchUser(session) |
| 71 | + a.NoError(err) |
| 72 | + a.Equal(user.UserID, "user123") |
| 73 | + a.Equal(user.Email, "user@example.com") |
| 74 | + a.Equal(user.Name, "John Doe") |
| 75 | + a.Equal(user.FirstName, "John") |
| 76 | + a.Equal(user.LastName, "Doe") |
| 77 | + a.Equal(user.AccessToken, "access_token") |
| 78 | +} |
| 79 | + |
| 80 | +func Test_RefreshToken(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + a := assert.New(t) |
| 83 | + |
| 84 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 85 | + a.Equal(r.Method, "POST") |
| 86 | + a.Equal(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") |
| 87 | + |
| 88 | + w.Header().Set("Content-Type", "application/json") |
| 89 | + json.NewEncoder(w).Encode(map[string]interface{}{ |
| 90 | + "access_token": "new_access_token", |
| 91 | + "token_type": "bearer", |
| 92 | + "expires_in": 3600, |
| 93 | + "refresh_token": "new_refresh_token", |
| 94 | + }) |
| 95 | + })) |
| 96 | + defer ts.Close() |
| 97 | + |
| 98 | + provider := New("client-id", "secret", "http://example.com/callback", nil, ScopeAccounting) |
| 99 | + provider.config.Endpoint.TokenURL = ts.URL |
| 100 | + |
| 101 | + token, err := provider.RefreshToken("refresh_token") |
| 102 | + a.NoError(err) |
| 103 | + a.NotNil(token) |
| 104 | + a.Equal(token.AccessToken, "new_access_token") |
| 105 | + a.Equal(token.RefreshToken, "new_refresh_token") |
| 106 | + a.True(token.Expiry.After(time.Now())) |
| 107 | +} |
| 108 | + |
| 109 | +func Test_RefreshTokenAvailable(t *testing.T) { |
| 110 | + t.Parallel() |
| 111 | + a := assert.New(t) |
| 112 | + |
| 113 | + provider := New("client-id", "secret", "http://example.com/callback", nil, ScopeAccounting) |
| 114 | + a.True(provider.RefreshTokenAvailable()) |
| 115 | +} |
0 commit comments