storj/pkg/macaroon/macaroon_test.go
Bogdan Artemenko ecb81144a1 Macaroon Library (#1537)
* Added initial implementation of Macaroon Library.

* Implemented Unit tests.

* Added header.

* Serialization refactoring.

* Added description for Macaroon and Caveat struct.

* Removed NewNonce. Use NewSecret instead.

* change macaroon library to just use bytes directly

Change-Id: I0411203cb09244605d2ee49f9d9b9b1e2bf46c76

* linting

Change-Id: I0363c0e30b610966eb18ff8b3905d75c69541610
2019-05-01 08:40:56 -04:00

84 lines
1.9 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package macaroon_test
import (
"testing"
assert "github.com/stretchr/testify/require"
"storj.io/storj/pkg/macaroon"
)
func TestNilMacaroon(t *testing.T) {
mac, err := macaroon.NewUnrestricted(nil)
assert.NoError(t, err)
assert.NotNil(t, mac)
data := mac.Serialize()
assert.NotNil(t, data)
assert.NotEmpty(t, data)
mac2, err := macaroon.ParseMacaroon(data)
assert.NoError(t, err)
assert.NotNil(t, mac2)
assert.Equal(t, mac, mac2)
t.Run("Successful add Caveat", func(t *testing.T) {
mac, err = mac.AddFirstPartyCaveat([]byte("cav1"))
assert.NotNil(t, mac)
assert.NoError(t, err)
assert.Equal(t, len(mac.Caveats()), 1)
})
t.Run("Successful serialization", func(t *testing.T) {
data := mac.Serialize()
assert.NotNil(t, data)
assert.NotEmpty(t, data)
mac2, err := macaroon.ParseMacaroon(data)
assert.NotNil(t, mac2)
assert.NoError(t, err)
assert.Equal(t, mac, mac2)
})
}
func TestMacaroon(t *testing.T) {
secret, err := macaroon.NewSecret()
assert.NoError(t, err)
assert.NotNil(t, secret)
assert.Equal(t, len(secret), 32)
mac, err := macaroon.NewUnrestricted(secret)
assert.NoError(t, err)
assert.NotNil(t, mac)
nonce := mac.Head()
assert.NotNil(t, nonce)
assert.Equal(t, len(nonce), 32)
t.Run("Successful add Caveat", func(t *testing.T) {
mac, err = mac.AddFirstPartyCaveat([]byte("cav1"))
assert.NotNil(t, mac)
assert.NoError(t, err)
assert.Equal(t, len(mac.Caveats()), 1)
})
t.Run("Successful serialization", func(t *testing.T) {
data := mac.Serialize()
assert.NotNil(t, data)
assert.NotEmpty(t, data)
mac2, err := macaroon.ParseMacaroon(data)
assert.NotNil(t, mac2)
assert.NoError(t, err)
assert.Equal(t, mac, mac2)
})
t.Run("Successful Unpack", func(t *testing.T) {
ok := mac.Validate(secret)
assert.True(t, ok)
c := mac.Caveats()
assert.NotNil(t, c)
assert.NotEmpty(t, c)
})
}