Move Mock Authenticator into mocks package

Signed-off-by: Michael de Sa <mjdesa@gmail.com>
pull/5028/head
Jared Scheib 2017-10-26 15:46:06 -07:00 committed by Michael de Sa
parent 7665fc1e67
commit 7ffbf5dd98
2 changed files with 52 additions and 35 deletions

51
mocks/auth.go Normal file
View File

@ -0,0 +1,51 @@
package mocks
import (
"context"
"net/http"
"github.com/influxdata/chronograf/oauth2"
)
// Authenticator implements a OAuth2 authenticator
type Authenticator struct {
Principal oauth2.Principal
ValidateErr error
ExtendErr error
Serialized string
}
// Validate returns Principal associated with authenticated and authorized
// entity if successful.
func (a *Authenticator) Validate(context.Context, *http.Request) (oauth2.Principal, error) {
return a.Principal, a.ValidateErr
}
// Extend will extend the lifetime of a already validated Principal
func (a *Authenticator) Extend(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) (oauth2.Principal, error) {
cookie := http.Cookie{}
http.SetCookie(w, &cookie)
return a.Principal, a.ExtendErr
}
// Authorize will grant privileges to a Principal
func (a *Authenticator) Authorize(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) error {
cookie := http.Cookie{}
http.SetCookie(w, &cookie)
return nil
}
// Expire revokes privileges from a Principal
func (a *Authenticator) Expire(http.ResponseWriter) {}
// ValidAuthorization returns the Principal
func (a *Authenticator) ValidAuthorization(ctx context.Context, serializedAuthorization string) (oauth2.Principal, error) {
return oauth2.Principal{}, nil
}
// Serialize the serialized values stored on the Authenticator
func (a *Authenticator) Serialize(context.Context, oauth2.Principal) (string, error) {
return a.Serialized, nil
}

View File

@ -15,40 +15,6 @@ import (
"github.com/influxdata/chronograf/server"
)
type MockAuthenticator struct {
Principal oauth2.Principal
ValidateErr error
ExtendErr error
Serialized string
}
func (m *MockAuthenticator) Validate(context.Context, *http.Request) (oauth2.Principal, error) {
return m.Principal, m.ValidateErr
}
func (m *MockAuthenticator) Extend(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) (oauth2.Principal, error) {
cookie := http.Cookie{}
http.SetCookie(w, &cookie)
return m.Principal, m.ExtendErr
}
func (m *MockAuthenticator) Authorize(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) error {
cookie := http.Cookie{}
http.SetCookie(w, &cookie)
return nil
}
func (m *MockAuthenticator) Expire(http.ResponseWriter) {}
func (m *MockAuthenticator) ValidAuthorization(ctx context.Context, serializedAuthorization string) (oauth2.Principal, error) {
return oauth2.Principal{}, nil
}
func (m *MockAuthenticator) Serialize(context.Context, oauth2.Principal) (string, error) {
return m.Serialized, nil
}
func TestAuthorizedToken(t *testing.T) {
var tests = []struct {
Desc string
@ -82,7 +48,7 @@ func TestAuthorizedToken(t *testing.T) {
req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
a := &MockAuthenticator{
a := &mocks.Authenticator{
Principal: test.Principal,
ValidateErr: test.ValidateErr,
}