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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
package gothic_test
import (
"fmt"
"html"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gorilla/sessions"
"github.com/markbates/goth"
. "github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/faux"
"github.com/stretchr/testify/assert"
)
type ProviderStore struct {
Store map[*http.Request]*sessions.Session
}
func NewProviderStore() *ProviderStore {
return &ProviderStore{map[*http.Request]*sessions.Session{}}
}
func (p ProviderStore) Get(r *http.Request, name string) (*sessions.Session, error) {
s := p.Store[r]
if s == nil {
s, err := p.New(r, name)
return s, err
}
return s, nil
}
func (p ProviderStore) New(r *http.Request, name string) (*sessions.Session, error) {
s := sessions.NewSession(p, name)
s.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 30,
}
p.Store[r] = s
return s, nil
}
func (p ProviderStore) Save(r *http.Request, w http.ResponseWriter, s *sessions.Session) error {
p.Store[r] = s
return nil
}
var fauxProvider goth.Provider
func init() {
Store = NewProviderStore()
fauxProvider = &faux.Provider{}
goth.UseProviders(fauxProvider)
}
func Test_BeginAuthHandler(t *testing.T) {
a := assert.New(t)
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/auth?provider=faux", nil)
a.NoError(err)
BeginAuthHandler(res, req)
sess, err := Store.Get(req, "faux"+SessionName)
if err != nil {
t.Fatalf("error getting faux Gothic session: %v", err)
}
sessStr, ok := sess.Values["faux"].(string)
if !ok {
t.Fatalf("Gothic session not stored as marshalled string; was %T (value %v)",
sess.Values["faux"], sess.Values["faux"])
}
gothSession, err := fauxProvider.UnmarshalSession(sessStr)
if err != nil {
t.Fatalf("error unmarshalling faux Gothic session: %v", err)
}
au, _ := gothSession.GetAuthURL()
a.Equal(http.StatusTemporaryRedirect, res.Code)
a.Contains(res.Body.String(),
fmt.Sprintf(`<a href="%s">Temporary Redirect</a>`, html.EscapeString(au)))
}
func Test_GetAuthURL(t *testing.T) {
a := assert.New(t)
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/auth?provider=faux", nil)
a.NoError(err)
u, err := GetAuthURL(res, req)
a.NoError(err)
// Check that we get the correct auth URL with a state parameter
parsed, err := url.Parse(u)
a.NoError(err)
a.Equal("http", parsed.Scheme)
a.Equal("example.com", parsed.Host)
q := parsed.Query()
a.Contains(q, "client_id")
a.Equal("code", q.Get("response_type"))
a.NotZero(q, "state")
// Check that if we run GetAuthURL on another request, that request's
// auth URL has a different state from the previous one.
req2, err := http.NewRequest("GET", "/auth?provider=faux", nil)
a.NoError(err)
url2, err := GetAuthURL(httptest.NewRecorder(), req2)
a.NoError(err)
parsed2, err := url.Parse(url2)
a.NoError(err)
a.NotEqual(parsed.Query().Get("state"), parsed2.Query().Get("state"))
}
func Test_CompleteUserAuth(t *testing.T) {
a := assert.New(t)
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/auth/callback?provider=faux", nil)
a.NoError(err)
sess := faux.Session{Name: "Homer Simpson", Email: "homer@example.com"}
session, _ := Store.Get(req, "faux"+SessionName)
session.Values["faux"] = sess.Marshal()
err = session.Save(req, res)
a.NoError(err)
user, err := CompleteUserAuth(res, req)
a.NoError(err)
a.Equal(user.Name, "Homer Simpson")
a.Equal(user.Email, "homer@example.com")
}
func Test_Logout(t *testing.T) {
a := assert.New(t)
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/auth/callback?provider=faux", nil)
a.NoError(err)
sess := faux.Session{Name: "Homer Simpson", Email: "homer@example.com"}
session, _ := Store.Get(req, "faux"+SessionName)
session.Values["faux"] = sess.Marshal()
err = session.Save(req, res)
a.NoError(err)
user, err := CompleteUserAuth(res, req)
a.NoError(err)
a.Equal(user.Name, "Homer Simpson")
a.Equal(user.Email, "homer@example.com")
err = Logout(res, req)
a.NoError(err)
session, _ = Store.Get(req, "faux"+SessionName)
a.Equal(session.Values, make(map[interface{}]interface{}))
a.Equal(session.Options.MaxAge, -1)
}
func Test_SetState(t *testing.T) {
a := assert.New(t)
req, _ := http.NewRequest("GET", "/auth?state=state", nil)
a.Equal(SetState(req), "state")
}
func Test_GetState(t *testing.T) {
a := assert.New(t)
req, _ := http.NewRequest("GET", "/auth?state=state", nil)
a.Equal(GetState(req), "state")
}
func Test_StateValidation(t *testing.T) {
a := assert.New(t)
Store = NewProviderStore()
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/auth?provider=faux&state=state_REAL", nil)
a.NoError(err)
BeginAuthHandler(res, req)
session, _ := Store.Get(req, "faux"+SessionName)
// Assert that matching states will return a nil error
req, err = http.NewRequest("GET", "/auth/callback?provider=faux&state=state_REAL", nil)
session.Save(req, res)
_, err = CompleteUserAuth(res, req)
a.NoError(err)
// Assert that mismatched states will return an error
req, err = http.NewRequest("GET", "/auth/callback?provider=faux&state=state_FAKE", nil)
session.Save(req, res)
_, err = CompleteUserAuth(res, req)
a.Error(err)
}
|