Fix tests

This commit is contained in:
Saj Goonatilleke 2019-05-15 17:44:44 +10:00
parent 33403daf59
commit bdc39cee65
2 changed files with 62 additions and 28 deletions

62
main_test.go Normal file
View File

@ -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"))
})
})
})

View File

@ -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")
}
}