Skip to content

Commit

Permalink
fix test code
Browse files Browse the repository at this point in the history
  • Loading branch information
tanuki884 authored and t-katsumura committed Oct 3, 2022
1 parent 1ba622a commit 4f4d6f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
13 changes: 5 additions & 8 deletions pkg/cookies/cookies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ var _ = Describe("Cookie Tests", func() {

validName := "_oauth2_proxy"
validSecret := "secretthirtytwobytes+abcdefghijk"
domains := []string{"a.localhost"}
domains := []string{"www.cookies.test"}

now := time.Now()
expectedExpires, e := time.Parse("", "0001-01-01T00:00:00Z")
if e != nil {
fmt.Println(e)
}
var expectedExpires time.Time

DescribeTable("should return expected results",
func(in MakeCookieFromOptionsTableInput) {
Expand All @@ -112,8 +109,8 @@ var _ = Describe("Cookie Tests", func() {

Expect(MakeCookieFromOptions(req, in.name, in.value, &in.opts, in.expiration, in.now).Expires).To(Equal(in.expectedOutput))
},
Entry("normal cookie", MakeCookieFromOptionsTableInput{
host: "a.localhost",
Entry("persistent cookie", MakeCookieFromOptionsTableInput{
host: "www.cookies.test",
name: validName,
value: "1",
opts: options.Cookie{
Expand All @@ -132,7 +129,7 @@ var _ = Describe("Cookie Tests", func() {
expectedOutput: now.Add(15 * time.Minute),
}),
Entry("session cookie", MakeCookieFromOptionsTableInput{
host: "a.localhost",
host: "www.cookies.test",
name: validName,
value: "1",
opts: options.Cookie{
Expand Down
12 changes: 7 additions & 5 deletions pkg/encryption/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -107,21 +108,22 @@ func TestValidate(t *testing.T) {
seed := "0123456789abcdef"
key := "cookie-name"
value := base64.URLEncoding.EncodeToString([]byte("I am soooo encoded"))
epoch := "123456789"
epoch := int64(123456789)
epochStr := strconv.FormatInt(epoch, 10)

sha256sig, err := cookieSignature(sha256.New, seed, key, value, epoch)
sha256sig, err := cookieSignature(sha256.New, seed, key, value, epochStr)
assert.NoError(t, err)

cookie := &http.Cookie{
Name: key,
Value: value + "|" + epoch + "|" + sha256sig,
Value: value + "|" + epochStr + "|" + sha256sig,
}

validValue, timestamp, ok := Validate(cookie, seed, 0)
assert.True(t, ok)
assert.Equal(t, timestamp, time.Unix(epoch, 0))

expectedValue, err := base64.URLEncoding.DecodeString(value)
assert.NoError(t, err)
assert.Equal(t, validValue, expectedValue)
assert.Equal(t, timestamp, time.Time(time.Date(1973, time.November, 29, 21, 33, 9, 0, time.Local)))
assert.True(t, ok)
}

0 comments on commit 4f4d6f5

Please sign in to comment.