@@ -306,6 +306,90 @@ func TestSignInPageDirectAccessRedirectsToRoot(t *testing.T) {
306306 }
307307}
308308
309+type ValidateTokenTest struct {
310+ opts *Options
311+ proxy *OauthProxy
312+ backend *httptest.Server
313+ response_code int
314+}
315+
316+func NewValidateTokenTest() *ValidateTokenTest {
317+ var vt_test ValidateTokenTest
318+
319+ vt_test.opts = NewOptions()
320+ vt_test.opts.Upstreams = append(vt_test.opts.Upstreams, "unused")
321+ vt_test.opts.CookieSecret = "foobar"
322+ vt_test.opts.ClientID = "bazquux"
323+ vt_test.opts.ClientSecret = "xyzzyplugh"
324+ vt_test.opts.Validate()
325+
326+ vt_test.backend = httptest.NewServer(
327+ http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
328+ switch r.URL.Path {
329+ case "/oauth/tokeninfo":
330+ w.WriteHeader(vt_test.response_code)
331+ w.Write([]byte("only code matters; contents disregarded"))
332+ default:
333+ w.WriteHeader(500)
334+ w.Write([]byte("unknown URL"))
335+ }
336+ }))
337+ backend_url, _ := url.Parse(vt_test.backend.URL)
338+ vt_test.opts.provider.Data().ValidateUrl = &url.URL{
339+ Scheme: "http",
340+ Host: backend_url.Host,
341+ Path: "/oauth/tokeninfo",
342+ }
343+ vt_test.response_code = 200
344+
345+ vt_test.proxy = NewOauthProxy(vt_test.opts, func(email string) bool {
346+ return true
347+ })
348+ return &vt_test
349+}
350+
351+func (vt_test *ValidateTokenTest) Close() {
352+ vt_test.backend.Close()
353+}
354+
355+func TestValidateTokenEmptyToken(t *testing.T) {
356+ vt_test := NewValidateTokenTest()
357+ defer vt_test.Close()
358+
359+ assert.Equal(t, false, vt_test.proxy.ValidateToken(""))
360+}
361+
362+func TestValidateTokenEmptyValidateUrl(t *testing.T) {
363+ vt_test := NewValidateTokenTest()
364+ defer vt_test.Close()
365+
366+ vt_test.proxy.oauthValidateUrl = nil
367+ assert.Equal(t, false, vt_test.proxy.ValidateToken("foobar"))
368+}
369+
370+func TestValidateTokenRequestNetworkFailure(t *testing.T) {
371+ vt_test := NewValidateTokenTest()
372+ // Close immediately to simulate a network failure
373+ vt_test.Close()
374+
375+ assert.Equal(t, false, vt_test.proxy.ValidateToken("foobar"))
376+}
377+
378+func TestValidateTokenExpiredToken(t *testing.T) {
379+ vt_test := NewValidateTokenTest()
380+ defer vt_test.Close()
381+
382+ vt_test.response_code = 401
383+ assert.Equal(t, false, vt_test.proxy.ValidateToken("foobar"))
384+}
385+
386+func TestValidateTokenValidToken(t *testing.T) {
387+ vt_test := NewValidateTokenTest()
388+ defer vt_test.Close()
389+
390+ assert.Equal(t, true, vt_test.proxy.ValidateToken("foobar"))
391+}
392+
309393type ProcessCookieTest struct {
310394 opts *Options
311395 proxy *OauthProxy