Exemplo n.º 1
0
	. "github.com/onsi/gomega"
	"github.com/strajah/strajah-token-go/lib/createToken"
	"github.com/strajah/strajah-token-go/lib/settings"
	"testing"
)

var _ = Describe("Token creation", func() {
	It("Should generate a token", func() {
		settings := settings.Settings{
			CipherKey: "0123456789ABCDEF",
		}

		data := createToken.DataToBeTokenized{
			UserId: "user-123",
		}
		token, _ := createToken.Create(settings, &data)

		Expect(token).ToNot(BeNil())
	})

	It("Should not allow token creation if cipher key is different than 16, 24, or 32 bytes", func() {
		data := createToken.DataToBeTokenized{
			UserId: "User-12345",
		}

		settings := settings.Settings{CipherKey: "12345678"} //length = 8
		token, err := createToken.Create(settings, &data)
		Expect(token).To(Equal(""))
		Expect(err).ToNot(BeNil())

		settings.CipherKey = "12345678123456781234" //length = 20
Exemplo n.º 2
0
	"testing"
)

var _ = Describe("Decode tokens", func() {
	It("Generated token must be decoded back to get original data", func() {
		userId := "user-123"

		settings := settings.Settings{
			CipherKey: "0123456789ABCDEF",
		}

		data := createToken.DataToBeTokenized{
			UserId: userId,
		}

		token, _ := createToken.Create(settings, &data)

		tokenSet, _ := decodeToken.Decode(settings, token)
		Expect(tokenSet.UserId).To(Equal(userId))
	})

	It("Should return an error when trying to decode with invalid cipher key", func() {
		data := createToken.DataToBeTokenized{
			UserId: "userId",
		}

		invalidSettings := settings.Settings{
			CipherKey: "4321456789ABCDEF",
		}

		validSettings := settings.Settings{