Skip to content

Commit

Permalink
Allow unauthenticated call to version method (#135)
Browse files Browse the repository at this point in the history
* allow unauthenticated call to version method

* add test case for grpc version endpoint
  • Loading branch information
jmorganca committed Jul 29, 2021
1 parent 1991fdf commit b8aa34d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/registry/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var publicMethods = map[string]bool{
"/v1.V1/ListSources": true,
"/v1.V1/Login": true,
"/v1.V1/Signup": true,
"/v1.V1/Version": true,
}

var tokenAuthMethods = map[string]bool{
Expand Down
36 changes: 36 additions & 0 deletions internal/registry/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/infrahq/infra/internal/generate"
"github.com/infrahq/infra/internal/registry/mocks"
v1 "github.com/infrahq/infra/internal/v1"
"github.com/infrahq/infra/internal/version"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -596,3 +598,37 @@ func TestSignupWithExistingAdmin(t *testing.T) {
assert.Equal(t, status.Code(err), codes.InvalidArgument)
assert.Equal(t, res, nil)
}

func TestVersion(t *testing.T) {
db, err := NewDB("file::memory:")
if err != nil {
t.Error(err)
}

server := &V1Server{db: db}

res, err := server.Version(context.Background(), &emptypb.Empty{})
assert.Equal(t, status.Code(err), codes.OK)
assert.Equal(t, res.Version, version.Version)
}

func TestVersionPublicAuth(t *testing.T) {
db, err := NewDB("file::memory:")
if err != nil {
t.Error(err)
}

server := &V1Server{db: db}

unaryInfo := &grpc.UnaryServerInfo{
FullMethod: "/v1.V1/Version",
}

unaryHandler := func(ctx context.Context, req interface{}) (interface{}, error) {
return server.Version(ctx, req.(*emptypb.Empty))
}

res, err := authInterceptor(db)(context.Background(), &emptypb.Empty{}, unaryInfo, unaryHandler)
assert.Equal(t, status.Code(err), codes.OK)
assert.Equal(t, res.(*v1.VersionResponse).Version, version.Version)
}

0 comments on commit b8aa34d

Please sign in to comment.