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
|
//go:build ignore
// +build ignore
package twitter
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 := twitterProvider()
a.Equal(provider.ClientKey, os.Getenv("TWITTER_KEY"))
a.Equal(provider.Secret, os.Getenv("TWITTER_SECRET"))
a.Equal(provider.CallbackURL, "/foo")
}
func Test_Implements_Provider(t *testing.T) {
t.Parallel()
a := assert.New(t)
a.Implements((*goth.Provider)(nil), twitterProvider())
}
func Test_BeginAuth(t *testing.T) {
t.Parallel()
a := assert.New(t)
mockTwitter(func(ts *httptest.Server) {
provider := twitterProvider()
session, err := provider.BeginAuth("state")
s := session.(*Session)
a.NoError(err)
a.Contains(s.AuthURL, "authorize?oauth_token=TOKEN")
a.Equal("TOKEN", s.RequestToken.Token)
a.Equal("SECRET", s.RequestToken.Secret)
})
mockTwitter(func(ts *httptest.Server) {
provider := twitterProviderAuthenticate()
session, err := provider.BeginAuth("state")
s := session.(*Session)
a.NoError(err)
a.Contains(s.AuthURL, "authenticate?oauth_token=TOKEN")
a.Equal("TOKEN", s.RequestToken.Token)
a.Equal("SECRET", s.RequestToken.Secret)
})
}
func Test_FetchUser(t *testing.T) {
t.Parallel()
a := assert.New(t)
mockTwitter(func(ts *httptest.Server) {
provider := twitterProvider()
session := Session{AccessToken: &oauth.AccessToken{Token: "TOKEN", Secret: "SECRET"}}
user, err := provider.FetchUser(&session)
a.NoError(err)
a.Equal("Homer", user.Name)
a.Equal("duffman", user.NickName)
a.Equal("Duff rules!!", user.Description)
a.Equal("http://example.com/image.jpg", user.AvatarURL)
a.Equal("1234", user.UserID)
a.Equal("Springfield", user.Location)
a.Equal("TOKEN", user.AccessToken)
a.Equal("duffman@springfield.com", user.Email)
})
}
func Test_SessionFromJSON(t *testing.T) {
t.Parallel()
a := assert.New(t)
provider := twitterProvider()
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 twitterProvider() *Provider {
return New(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), "/foo")
}
func twitterProviderAuthenticate() *Provider {
return NewAuthenticate(os.Getenv("TWITTER_KEY"), os.Getenv("TWITTER_SECRET"), "/foo")
}
func mockTwitter(f func(*httptest.Server)) {
p := pat.New()
p.Get("/oauth/request_token", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "oauth_token=TOKEN&oauth_token_secret=SECRET")
})
p.Get("/1.1/account/verify_credentials.json", func(res http.ResponseWriter, req *http.Request) {
data := map[string]string{
"name": "Homer",
"screen_name": "duffman",
"description": "Duff rules!!",
"profile_image_url": "http://example.com/image.jpg",
"id_str": "1234",
"location": "Springfield",
"email": "duffman@springfield.com",
}
json.NewEncoder(res).Encode(&data)
})
ts := httptest.NewServer(p)
defer ts.Close()
originalRequestURL := requestURL
originalEndpointProfile := endpointProfile
requestURL = ts.URL + "/oauth/request_token"
endpointProfile = ts.URL + "/1.1/account/verify_credentials.json"
f(ts)
requestURL = originalRequestURL
endpointProfile = originalEndpointProfile
}
|