Пример #1
0
func (s *AuthS) TestCachedLogin(c *C) {
	defer func(old string) {
		os.Setenv("HOME", old)
	}(os.Getenv("HOME"))
	os.Setenv("HOME", c.MkDir())

	auth := rietveld.NewAuth(s.ui, true, testServer.URL+"/lurl")

	testServer.Response(200, nil, "Auth=the-auth")

	headers := map[string]string{
		"Set-Cookie": "some=cookie",
		"location":   "http://example.com/marker",
	}
	testServer.Response(302, headers, "")

	err := auth.Login(s.rietveldURL, time.Time{})
	c.Assert(err, IsNil)
	c.Assert(s.ui.loginURL, Equals, testServer.URL+"/lurl")
	c.Assert(s.ui.prevUser, Equals, "")

	// Build a new Auth to make use of cached credentials.
	newauth := rietveld.NewAuth(s.ui, true, "")

	req, err := http.NewRequest("POST", "http://example.com", nil)
	c.Assert(err, IsNil)

	_, err = newauth.Sign(s.rietveldURL, req)
	c.Assert(err, IsNil)
	c.Assert(req.Header["Cookie"], DeepEquals, []string{"some=cookie"})

	// Login again to check usage of previousUser on credentials.
	testServer.Response(200, nil, "Auth=the-auth")
	testServer.Response(302, headers, "")
	err = newauth.Login(s.rietveldURL, time.Time{})
	c.Assert(err, IsNil)
	c.Assert(s.ui.loginURL, Equals, s.loginURL)
	c.Assert(s.ui.prevUser, Equals, "myuser")

	// Check that logging out removes cached credentials as well.
	path := os.ExpandEnv("$HOME/.goetveld_localhost:4444")
	stat, err := os.Stat(path)
	c.Assert(stat, NotNil)

	err = newauth.Logout(s.rietveldURL)
	c.Assert(err, IsNil)

	stat, err = os.Stat(path)
	c.Assert(stat, IsNil)

	req, err = http.NewRequest("POST", "http://example.com", nil)
	c.Assert(err, IsNil)

	_, err = newauth.Sign(s.rietveldURL, req)
	c.Assert(err, IsNil)
	c.Assert(req.Header["Cookie"], IsNil)
}
Пример #2
0
func (s *AuthS) SetUpTest(c *C) {
	rietveld.SetDebug(true)
	rietveld.SetLogger(c)

	s.ui = &dummyUI{}
	s.loginURL = testServer.URL + "/lurl"
	s.rietveldURL = testServer.URL + "/rurl"
	s.auth = rietveld.NewAuth(s.ui, false, s.loginURL)
}