Files
teleport/tool/tctl/common/lock_command_test.go
T
Jeff Pihach 1513294b7b Add CreatedBy and CreatedOn fields to locks. (#22667)
* Add CreatedBy and CreatedOn fields to locks.

* Move the setting of the CreatedBy and CreatedOn for locks to the server instead of the client.

* Add tests for the tctl resource lock import and for the tctl lock create command.

* Use clockwork for time in lock creation tests.

* Make fake clock optional and use the golang 0 date.

* Change CreatedOn to CreatedAt.

* Allow a custom time to be passed through as a fake clock.

* Actually accept a fake clock with useFakeClock.

* Pass Clock through as a configuration value when initializing Teleport for the tests.

* Test cleanup

* Set CreatedBy and CreatedAt values in test mocks.

* Fix failing test from enabling clock passthrough to auth.
2023-03-13 16:14:24 +00:00

82 lines
2.2 KiB
Go

/*
Copyright 2023 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/config"
)
func TestLocks(t *testing.T) {
dynAddr := newDynamicServiceAddr(t)
fileConfig := &config.FileConfig{
Global: config.Global{
DataDir: t.TempDir(),
},
Proxy: config.Proxy{
Service: config.Service{
EnabledFlag: "true",
},
WebAddr: dynAddr.webAddr,
TunAddr: dynAddr.tunnelAddr,
},
Auth: config.Auth{
Service: config.Service{
EnabledFlag: "true",
ListenAddress: dynAddr.authAddr,
},
},
}
timeNow := time.Now().UTC()
fakeClock := clockwork.NewFakeClockAt(timeNow)
makeAndRunTestAuthServer(t, withFileConfig(fileConfig), withFileDescriptors(dynAddr.descriptors), withFakeClock(fakeClock))
t.Run("create", func(t *testing.T) {
err := runLockCommand(t, fileConfig, []string{"--user=bad@actor", "--message=Come see me"})
require.NoError(t, err)
var out []*types.LockV2
buf, err := runResourceCommand(t, fileConfig, []string{"get", types.KindLock, "--format=json"})
require.NoError(t, err)
mustDecodeJSON(t, buf, &out)
expected, err := types.NewLock("test-lock", types.LockSpecV2{
Target: types.LockTarget{
User: "bad@actor",
},
Message: "Come see me",
})
require.NoError(t, err)
expected.SetCreatedBy(string(types.RoleAdmin))
require.NoError(t, err)
expected.SetCreatedAt(timeNow)
require.Empty(t, cmp.Diff([]*types.LockV2{expected.(*types.LockV2)}, out,
cmpopts.IgnoreFields(types.LockV2{}, "Metadata")))
})
}