Fix tests
This commit is contained in:
parent
33403daf59
commit
bdc39cee65
|
@ -0,0 +1,62 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "main")
|
||||
}
|
||||
|
||||
var _ = Describe("parseCookie", func() {
|
||||
var (
|
||||
signed, secret string
|
||||
|
||||
parsedUsername string
|
||||
parsedGroup string
|
||||
parseError error
|
||||
)
|
||||
|
||||
JustBeforeEach(func() {
|
||||
parsedUsername, parsedGroup, parseError = parseCookie(signed, secret)
|
||||
})
|
||||
|
||||
Context("when verifying with an invalid secret", func() {
|
||||
BeforeEach(func() {
|
||||
secret = "secretbar"
|
||||
signed = signCookie("user,group", "secretfoo")
|
||||
})
|
||||
|
||||
It("fails", func() {
|
||||
Expect(parseError).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Context("when verifying with an invalid payload", func() {
|
||||
BeforeEach(func() {
|
||||
secret = "mysecret"
|
||||
signed = signCookie("user,group", secret) + "garbage"
|
||||
})
|
||||
|
||||
It("fails", func() {
|
||||
Expect(parseError).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
Context("when verifying with a valid payload and secret", func() {
|
||||
BeforeEach(func() {
|
||||
secret = "mysecret"
|
||||
signed = signCookie("user,group", secret)
|
||||
})
|
||||
|
||||
It("returns a user and group", func() {
|
||||
Expect(parseError).To(Succeed())
|
||||
Expect(parsedUsername).To(Equal("user"))
|
||||
Expect(parsedGroup).To(Equal("group"))
|
||||
})
|
||||
})
|
||||
})
|
|
@ -1,28 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCookieValidation(t *testing.T) {
|
||||
|
||||
signed := signCookie("hi, there", "mysecret")
|
||||
|
||||
parsed, err := parseCookie(signed, "bob")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Expecting an error when decrypting with wrong key")
|
||||
}
|
||||
|
||||
parsed, err = parseCookie(signed+"a", "mysecret")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("Expecting an error when decrypting with wrong payload")
|
||||
}
|
||||
|
||||
parsed, err = parseCookie(signed, "mysecret")
|
||||
|
||||
if err != nil || parsed != "hi, there" {
|
||||
t.Fatal("Expecting a correctly validated cookie")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue