prysm-pulse/shared/httputils/endpoint_test.go
Shay Zluf d77c298ec6
Support authorised access to web 3 providers (#8075)
* jwt access token impl

* use secret or jwt

* rename

* separate method for splitting auth

* usage update

* Update beacon-chain/flags/base.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/node/node.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* make things work

* removed unused code

* better, more flexible authorization

* move types and function to proper packages

* fix checking if endpoint is not set

* fix existing tests

* rename Endpoint field to Url

* Tests for HttpEndpoint

* better bearer auth

* tests for endpoint utils

* fix endpoint registration

* fix test build

* move endpoint parsing to powchain

* Revert "fix existing tests"

This reverts commit ceab192e6a78c106cf4e16a1bdf5399752a39890.

* fix field name in tests

* gzl

* add httputils dependency

* remove httputils dependency

* fix compilation issue in blockchain service test

* correct endpoint fallback function and tests

* gzl

* remove pointer from currHttpEndpoint

* allow whitespace in auth string

* endpoint equality

* correct one auth data Equals test case

* remove pointer receiver

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-04-15 11:02:02 +00:00

143 lines
3.4 KiB
Go

package httputils
import (
"testing"
"github.com/prysmaticlabs/prysm/shared/httputils/authorizationmethod"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestToHeaderValue(t *testing.T) {
t.Run("None", func(t *testing.T) {
data := &AuthorizationData{
Method: authorizationmethod.None,
Value: "foo",
}
header, err := data.ToHeaderValue()
require.NoError(t, err)
assert.Equal(t, "", header)
})
t.Run("Basic", func(t *testing.T) {
data := &AuthorizationData{
Method: authorizationmethod.Basic,
Value: "foo",
}
header, err := data.ToHeaderValue()
require.NoError(t, err)
assert.Equal(t, "Basic foo", header)
})
t.Run("Bearer", func(t *testing.T) {
data := &AuthorizationData{
Method: authorizationmethod.Bearer,
Value: "foo",
}
header, err := data.ToHeaderValue()
require.NoError(t, err)
assert.Equal(t, "Bearer foo", header)
})
t.Run("Unknown", func(t *testing.T) {
data := &AuthorizationData{
Method: 99,
Value: "foo",
}
_, err := data.ToHeaderValue()
require.NotNil(t, err)
})
}
func TestMethod(t *testing.T) {
t.Run("None", func(t *testing.T) {
method := Method("")
assert.Equal(t, authorizationmethod.None, method)
method = Method("foo")
assert.Equal(t, authorizationmethod.None, method)
})
t.Run("Basic", func(t *testing.T) {
method := Method("Basic")
assert.Equal(t, authorizationmethod.Basic, method)
})
t.Run("Basic different text case", func(t *testing.T) {
method := Method("bAsIc")
assert.Equal(t, authorizationmethod.Basic, method)
})
t.Run("Bearer", func(t *testing.T) {
method := Method("Bearer")
assert.Equal(t, authorizationmethod.Bearer, method)
})
t.Run("Bearer different text case", func(t *testing.T) {
method := Method("bEaReR")
assert.Equal(t, authorizationmethod.Bearer, method)
})
}
func TestEndpointEquals(t *testing.T) {
e := Endpoint{
Url: "Url",
Auth: AuthorizationData{
Method: authorizationmethod.Basic,
Value: "Basic username:password",
},
}
t.Run("equal", func(t *testing.T) {
other := Endpoint{
Url: "Url",
Auth: AuthorizationData{
Method: authorizationmethod.Basic,
Value: "Basic username:password",
},
}
assert.Equal(t, true, e.Equals(other))
})
t.Run("different URL", func(t *testing.T) {
other := Endpoint{
Url: "Different",
Auth: AuthorizationData{
Method: authorizationmethod.Basic,
Value: "Basic username:password",
},
}
assert.Equal(t, false, e.Equals(other))
})
t.Run("different auth data", func(t *testing.T) {
other := Endpoint{
Url: "Url",
Auth: AuthorizationData{
Method: authorizationmethod.Bearer,
Value: "Bearer token",
},
}
assert.Equal(t, false, e.Equals(other))
})
}
func TestAuthorizationDataEquals(t *testing.T) {
d := AuthorizationData{
Method: authorizationmethod.Basic,
Value: "username:password",
}
t.Run("equal", func(t *testing.T) {
other := AuthorizationData{
Method: authorizationmethod.Basic,
Value: "username:password",
}
assert.Equal(t, true, d.Equals(other))
})
t.Run("different method", func(t *testing.T) {
other := AuthorizationData{
Method: authorizationmethod.None,
Value: "username:password",
}
assert.Equal(t, false, d.Equals(other))
})
t.Run("different value", func(t *testing.T) {
other := AuthorizationData{
Method: authorizationmethod.Basic,
Value: "different:different",
}
assert.Equal(t, false, d.Equals(other))
})
}