Skip to content

Commit

Permalink
Add basic service testing
Browse files Browse the repository at this point in the history
  • Loading branch information
codingllama committed Dec 16, 2024
1 parent 17e8a94 commit 0fb1117
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/decision/decisionv1/decision_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Teleport
// Copyright (C) 2024 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package decisionv1_test

import (
"context"
"testing"

"github.com/gravitational/trace"
"github.com/stretchr/testify/assert"

decisionpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/decision/v1alpha1"
)

func TestDecisionService_notImplemented(t *testing.T) {
t.Parallel()

env := NewTestenv(t)
decisionClient := env.DecisionClient
ctx := context.Background()

t.Run("EvaluateSSHAccess", func(t *testing.T) {
_, err := decisionClient.EvaluateSSHAccess(ctx, &decisionpb.EvaluateSSHAccessRequest{})
assert.ErrorAs(t, err, new(*trace.NotImplementedError), "EvaluateSSHAccess error mismatch")
})

t.Run("EvaluateDatabaseAccess", func(t *testing.T) {
_, err := decisionClient.EvaluateDatabaseAccess(ctx, &decisionpb.EvaluateDatabaseAccessRequest{})
assert.ErrorAs(t, err, new(*trace.NotImplementedError), "EvaluateDatabaseAccess error mismatch")
})
}
74 changes: 74 additions & 0 deletions lib/decision/decisionv1/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Teleport
// Copyright (C) 2024 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package decisionv1_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/api/constants"
decisionpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/decision/v1alpha1"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/auth/authclient"
)

// Testenv is a test environment for decisionv1.Service.
type Testenv struct {
TestServer *auth.TestServer

// AuthAdminClient is an admin Auth client.
AuthAdminClient *authclient.Client

// DecisionClient is an admin decision client.
// Created from AuthAdminClient.
DecisionClient decisionpb.DecisionServiceClient
}

func NewTestenv(t *testing.T) *Testenv {
t.Helper()

testServer, err := auth.NewTestServer(auth.TestServerConfig{
Auth: auth.TestAuthServerConfig{
Dir: t.TempDir(),
AuthPreferenceSpec: &types.AuthPreferenceSpecV2{
SecondFactor: constants.SecondFactorOTP, // Required.
},
},
})
require.NoError(t, err, "NewTestServer failed")
t.Cleanup(func() {
assert.NoError(t,
testServer.Shutdown(context.Background()),
"testServer.Shutdown failed")
})

adminClient, err := testServer.NewClient(auth.TestAdmin())
require.NoError(t, err, "NewClient failed")
t.Cleanup(func() {
assert.NoError(t, adminClient.Close(), "adminClient.Close() failed")
})

return &Testenv{
TestServer: testServer,
AuthAdminClient: adminClient,
DecisionClient: adminClient.DecisionClient(),
}
}

0 comments on commit 0fb1117

Please sign in to comment.