1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
//go:build ignore
// +build ignore
package xero
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/gorilla/pat"
"github.com/markbates/goth"
"github.com/mrjones/oauth"
"github.com/stretchr/testify/assert"
)
func Test_New(t *testing.T) {
t.Parallel()
a := assert.New(t)
provider := xeroProvider()
a.Equal(provider.ClientKey, os.Getenv("XERO_KEY"))
a.Equal(provider.Secret, os.Getenv("XERO_SECRET"))
a.Equal(provider.CallbackURL, "/foo")
}
func Test_Implements_Provider(t *testing.T) {
t.Parallel()
a := assert.New(t)
a.Implements((*goth.Provider)(nil), xeroProvider())
}
func Test_BeginAuth(t *testing.T) {
t.Parallel()
a := assert.New(t)
mockXero(func(ts *httptest.Server) {
provider := xeroProvider()
session, err := provider.BeginAuth("state")
if err != nil {
a.Error(err, nil)
}
s := session.(*Session)
a.NoError(err)
a.Contains(s.AuthURL, "Authorize")
a.Equal("TOKEN", s.RequestToken.Token)
a.Equal("SECRET", s.RequestToken.Secret)
})
}
func Test_FetchUser(t *testing.T) {
t.Parallel()
a := assert.New(t)
mockXero(func(ts *httptest.Server) {
provider := xeroProvider()
session := Session{AccessToken: &oauth.AccessToken{Token: "TOKEN", Secret: "SECRET"}}
user, err := provider.FetchUser(&session)
if err != nil {
a.Error(err, nil)
}
a.NoError(err)
a.Equal("Vanderlay Industries", user.Name)
a.Equal("Vanderlay Industries", user.NickName)
a.Equal("COMPANY", user.Description)
a.Equal("111-11", user.UserID)
a.Equal("NZ", user.Location)
a.Empty(user.Email)
})
}
func Test_SessionFromJSON(t *testing.T) {
t.Parallel()
a := assert.New(t)
provider := xeroProvider()
s, err := provider.UnmarshalSession(`{"AuthURL":"http://com/auth_url","AccessToken":{"Token":"1234567890","Secret":"secret!!","AdditionalData":{}},"RequestToken":{"Token":"0987654321","Secret":"!!secret"}}`)
a.NoError(err)
session := s.(*Session)
a.Equal(session.AuthURL, "http://com/auth_url")
a.Equal(session.AccessToken.Token, "1234567890")
a.Equal(session.AccessToken.Secret, "secret!!")
a.Equal(session.RequestToken.Token, "0987654321")
a.Equal(session.RequestToken.Secret, "!!secret")
}
func xeroProvider() *Provider {
return New(os.Getenv("XERO_KEY"), os.Getenv("XERO_SECRET"), "/foo")
}
func mockXero(f func(*httptest.Server)) {
p := pat.New()
p.Get("/oauth/RequestToken", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "oauth_token=TOKEN&oauth_token_secret=SECRET")
})
p.Get("/oauth/Authorize", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "DO NOT USE THIS ENDPOINT")
})
p.Get("/oauth/AccessToken", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "oauth_token=TOKEN&oauth_token_secret=SECRET")
})
p.Get("/api.xro/2.0/Organisation", func(res http.ResponseWriter, req *http.Request) {
apiResponse := APIResponse{
Organisations: []Organisation{
{"Vanderlay Industries", "Vanderlay Industries", "COMPANY", "NZ", "111-11"},
},
}
js, err := json.Marshal(apiResponse)
if err != nil {
fmt.Fprint(res, "Json did not Marshal")
}
res.Write(js)
})
ts := httptest.NewServer(p)
defer ts.Close()
originalRequestURL := requestURL
originalEndpointProfile := endpointProfile
originalAuthorizeURL := authorizeURL
originalAccessTokenURL := tokenURL
requestURL = ts.URL + "/oauth/RequestToken"
endpointProfile = ts.URL + "/api.xro/2.0/"
authorizeURL = ts.URL + "/oauth/Authorize"
tokenURL = ts.URL + "/oauth/AccessToken"
f(ts)
requestURL = originalRequestURL
endpointProfile = originalEndpointProfile
authorizeURL = originalAuthorizeURL
tokenURL = originalAccessTokenURL
}
|