Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from davidzhao98/chore/auth_get_expiry
Browse files Browse the repository at this point in the history
chore: add GetExpiry to auth interface
  • Loading branch information
fieldju authored Jul 23, 2019
2 parents a92456d + 344f139 commit 228ce8b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 3 deletions.
3 changes: 3 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Auth interface {
// the authorization header set with the proper token
GetHeaders() (http.Header, error)
GetURL() *url.URL
// GetExpiry either returns the expiry time of an existing token, or a zero-valued
// time.Time struct and an error if a token doesn't exist
GetExpiry() (time.Time, error)
}

// Refresh contains logic for refreshing a token against the API. Because
Expand Down
2 changes: 1 addition & 1 deletion auth/sts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (a *STSAuth) GetToken(*os.File) (string, error) {

// GetExpiry returns the expiry time of the token if it already exists. Otherwise,
// it returns a zero-valued time.Time struct and an error.
func (a *STSAuth) GetExpiry(*os.File) (time.Time, error) {
func (a *STSAuth) GetExpiry() (time.Time, error) {
if len(a.token) > 0 {
return a.expiry, nil
}
Expand Down
4 changes: 2 additions & 2 deletions auth/sts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestGetExpiry(t *testing.T) {
a.expiry = time.Now()
a.token = "token"
Convey("Should return an expiry time", func() {
exp, err := a.GetExpiry(nil)
exp, err := a.GetExpiry()
So(exp, ShouldNotBeNil)
So(err, ShouldBeNil)
})
Expand All @@ -189,7 +189,7 @@ func TestGetExpiry(t *testing.T) {
So(err, ShouldBeNil)
So(a, ShouldNotBeNil)
Convey("Should return an error", func() {
exp, err := a.GetExpiry(nil)
exp, err := a.GetExpiry()
So(exp, ShouldBeZeroValue)
So(err, ShouldNotBeNil)
})
Expand Down
6 changes: 6 additions & 0 deletions auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/url"
"os"
"time"

"github.com/Nike-Inc/cerberus-go-client/api"
"github.com/Nike-Inc/cerberus-go-client/utils"
Expand Down Expand Up @@ -131,3 +132,8 @@ func (t *TokenAuth) GetHeaders() (http.Header, error) {
func (t *TokenAuth) GetURL() *url.URL {
return t.baseURL
}

// Always return zero-valued time.Time struct and a non-nil error
func (t *TokenAuth) GetExpiry() (time.Time, error) {
return time.Time{}, fmt.Errorf("Expiry time not set")
}
24 changes: 24 additions & 0 deletions auth/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,27 @@ func TestGetURLToken(t *testing.T) {
})
})
}

func TestGetExpiryToken(t *testing.T) {
Convey("A valid TokenAuth", t, func() {
tok, err := NewTokenAuth("https://test.example.com", "token")
So(err, ShouldBeNil)
So(tok, ShouldNotBeNil)
Convey("Should return zero value expiry and non-nil error", func() {
exp, err := tok.GetExpiry()
So(exp, ShouldBeZeroValue)
So(err, ShouldNotBeNil)
})
})
Convey("A logged out TokenAuth", t, func() {
tok, err := NewTokenAuth("https://test.example.com", "token")
So(err, ShouldBeNil)
So(tok, ShouldNotBeNil)
tok.token = ""
Convey("Should return zero value expiry and non-nil error", func() {
exp, err := tok.GetExpiry()
So(exp, ShouldBeZeroValue)
So(err, ShouldNotBeNil)
})
})
}
9 changes: 9 additions & 0 deletions auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func (u *UserAuth) GetToken(f *os.File) (string, error) {
return u.token, nil
}

// GetExpiry returns the expiry time of the token if it already exists. Otherwise,
// it returns a zero-valued time.Time struct and an error.
func (u *UserAuth) GetExpiry() (time.Time, error) {
if len(u.token) > 0 {
return u.expiry, nil
}
return time.Time{}, fmt.Errorf("Expiry time not set")
}

// GetURL returns the URL used for Cerberus
func (u *UserAuth) GetURL() *url.URL {
return u.baseURL
Expand Down
27 changes: 27 additions & 0 deletions auth/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ func TestGetURLUser(t *testing.T) {
})
}

func TestGetExpiryUser(t *testing.T) {
Convey("A valid client", t, func() {
c, err := NewUserAuth("http://example.com", "user", "pass")
So(c, ShouldNotBeNil)
So(err, ShouldBeNil)
c.token = "token"
c.expiry = time.Now()
Convey("Should return an expiry time", func() {
exp, err := c.GetExpiry()
So(exp, ShouldNotBeNil)
So(err, ShouldBeNil)
})
})
Convey("An invalid client", t, func() {
c, err := NewUserAuth("http://example.com", "user", "pass")
So(c, ShouldNotBeNil)
So(err, ShouldBeNil)
c.token = ""
c.expiry = time.Now()
Convey("Should return an error", func() {
exp, err := c.GetExpiry()
So(exp, ShouldBeZeroValue)
So(err, ShouldNotBeNil)
})
})
}

func WithServer(status api.AuthStatus, returnCode int, token, expectedPath, expectedMethod string, expectedHeaders map[string]string, f func(ts *httptest.Server)) func() {
return func() {
Convey("http requests should be correct", func(c C) {
Expand Down
5 changes: 5 additions & 0 deletions cerberus/cerberus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/url"
"os"
"testing"
"time"

"github.com/Nike-Inc/cerberus-go-client/api"
. "github.com/smartystreets/goconvey/convey"
Expand Down Expand Up @@ -86,6 +87,10 @@ func (m *MockAuth) GetURL() *url.URL {
return m.baseURL
}

func (m *MockAuth) GetExpiry() (time.Time, error) {
return time.Now(), nil
}

func TestNewCerberusClient(t *testing.T) {
Convey("Valid setup arguments", t, func() {
m := GenerateMockAuth("http://example.com", "a-cool-token", false, false)
Expand Down

0 comments on commit 228ce8b

Please sign in to comment.