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

Commit 228ce8b

Browse files
authored
Merge pull request #23 from davidzhao98/chore/auth_get_expiry
chore: add GetExpiry to auth interface
2 parents a92456d + 344f139 commit 228ce8b

File tree

8 files changed

+77
-3
lines changed

8 files changed

+77
-3
lines changed

auth/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type Auth interface {
5151
// the authorization header set with the proper token
5252
GetHeaders() (http.Header, error)
5353
GetURL() *url.URL
54+
// GetExpiry either returns the expiry time of an existing token, or a zero-valued
55+
// time.Time struct and an error if a token doesn't exist
56+
GetExpiry() (time.Time, error)
5457
}
5558

5659
// Refresh contains logic for refreshing a token against the API. Because

auth/sts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (a *STSAuth) GetToken(*os.File) (string, error) {
8585

8686
// GetExpiry returns the expiry time of the token if it already exists. Otherwise,
8787
// it returns a zero-valued time.Time struct and an error.
88-
func (a *STSAuth) GetExpiry(*os.File) (time.Time, error) {
88+
func (a *STSAuth) GetExpiry() (time.Time, error) {
8989
if len(a.token) > 0 {
9090
return a.expiry, nil
9191
}

auth/sts_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func TestGetExpiry(t *testing.T) {
179179
a.expiry = time.Now()
180180
a.token = "token"
181181
Convey("Should return an expiry time", func() {
182-
exp, err := a.GetExpiry(nil)
182+
exp, err := a.GetExpiry()
183183
So(exp, ShouldNotBeNil)
184184
So(err, ShouldBeNil)
185185
})
@@ -189,7 +189,7 @@ func TestGetExpiry(t *testing.T) {
189189
So(err, ShouldBeNil)
190190
So(a, ShouldNotBeNil)
191191
Convey("Should return an error", func() {
192-
exp, err := a.GetExpiry(nil)
192+
exp, err := a.GetExpiry()
193193
So(exp, ShouldBeZeroValue)
194194
So(err, ShouldNotBeNil)
195195
})

auth/token.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222
"net/url"
2323
"os"
24+
"time"
2425

2526
"github.com/Nike-Inc/cerberus-go-client/api"
2627
"github.com/Nike-Inc/cerberus-go-client/utils"
@@ -131,3 +132,8 @@ func (t *TokenAuth) GetHeaders() (http.Header, error) {
131132
func (t *TokenAuth) GetURL() *url.URL {
132133
return t.baseURL
133134
}
135+
136+
// Always return zero-valued time.Time struct and a non-nil error
137+
func (t *TokenAuth) GetExpiry() (time.Time, error) {
138+
return time.Time{}, fmt.Errorf("Expiry time not set")
139+
}

auth/token_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,27 @@ func TestGetURLToken(t *testing.T) {
262262
})
263263
})
264264
}
265+
266+
func TestGetExpiryToken(t *testing.T) {
267+
Convey("A valid TokenAuth", t, func() {
268+
tok, err := NewTokenAuth("https://test.example.com", "token")
269+
So(err, ShouldBeNil)
270+
So(tok, ShouldNotBeNil)
271+
Convey("Should return zero value expiry and non-nil error", func() {
272+
exp, err := tok.GetExpiry()
273+
So(exp, ShouldBeZeroValue)
274+
So(err, ShouldNotBeNil)
275+
})
276+
})
277+
Convey("A logged out TokenAuth", t, func() {
278+
tok, err := NewTokenAuth("https://test.example.com", "token")
279+
So(err, ShouldBeNil)
280+
So(tok, ShouldNotBeNil)
281+
tok.token = ""
282+
Convey("Should return zero value expiry and non-nil error", func() {
283+
exp, err := tok.GetExpiry()
284+
So(exp, ShouldBeZeroValue)
285+
So(err, ShouldNotBeNil)
286+
})
287+
})
288+
}

auth/user.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ func (u *UserAuth) GetToken(f *os.File) (string, error) {
8989
return u.token, nil
9090
}
9191

92+
// GetExpiry returns the expiry time of the token if it already exists. Otherwise,
93+
// it returns a zero-valued time.Time struct and an error.
94+
func (u *UserAuth) GetExpiry() (time.Time, error) {
95+
if len(u.token) > 0 {
96+
return u.expiry, nil
97+
}
98+
return time.Time{}, fmt.Errorf("Expiry time not set")
99+
}
100+
92101
// GetURL returns the URL used for Cerberus
93102
func (u *UserAuth) GetURL() *url.URL {
94103
return u.baseURL

auth/user_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,33 @@ func TestGetURLUser(t *testing.T) {
132132
})
133133
}
134134

135+
func TestGetExpiryUser(t *testing.T) {
136+
Convey("A valid client", t, func() {
137+
c, err := NewUserAuth("http://example.com", "user", "pass")
138+
So(c, ShouldNotBeNil)
139+
So(err, ShouldBeNil)
140+
c.token = "token"
141+
c.expiry = time.Now()
142+
Convey("Should return an expiry time", func() {
143+
exp, err := c.GetExpiry()
144+
So(exp, ShouldNotBeNil)
145+
So(err, ShouldBeNil)
146+
})
147+
})
148+
Convey("An invalid client", t, func() {
149+
c, err := NewUserAuth("http://example.com", "user", "pass")
150+
So(c, ShouldNotBeNil)
151+
So(err, ShouldBeNil)
152+
c.token = ""
153+
c.expiry = time.Now()
154+
Convey("Should return an error", func() {
155+
exp, err := c.GetExpiry()
156+
So(exp, ShouldBeZeroValue)
157+
So(err, ShouldNotBeNil)
158+
})
159+
})
160+
}
161+
135162
func WithServer(status api.AuthStatus, returnCode int, token, expectedPath, expectedMethod string, expectedHeaders map[string]string, f func(ts *httptest.Server)) func() {
136163
return func() {
137164
Convey("http requests should be correct", func(c C) {

cerberus/cerberus_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/url"
2626
"os"
2727
"testing"
28+
"time"
2829

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

90+
func (m *MockAuth) GetExpiry() (time.Time, error) {
91+
return time.Now(), nil
92+
}
93+
8994
func TestNewCerberusClient(t *testing.T) {
9095
Convey("Valid setup arguments", t, func() {
9196
m := GenerateMockAuth("http://example.com", "a-cool-token", false, false)

0 commit comments

Comments
 (0)