mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-30 18:29:21 +08:00
Refactored tests under web package
Refactored all tests under lib/web to use testify instead of gocheck. Switched backend for most tests to memory.
This commit is contained in:
committed by
Russell Jones
parent
94b8a198df
commit
22e81dcddb
+34
-34
@@ -20,73 +20,73 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gravitational/trace"
|
||||
"gopkg.in/check.v1"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type StaticSuite struct {
|
||||
}
|
||||
func TestLocalFS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var _ = check.Suite(&StaticSuite{})
|
||||
|
||||
func (s *StaticSuite) TestLocalFS(c *check.C) {
|
||||
fs, err := NewDebugFileSystem("../../webassets/teleport")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(fs, check.NotNil)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, fs)
|
||||
|
||||
f, err := fs.Open("/index.html")
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
bytes, err := io.ReadAll(f)
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
|
||||
html := string(bytes[:])
|
||||
c.Assert(f.Close(), check.IsNil)
|
||||
c.Assert(strings.Contains(html, `<script src="/web/config.js"></script>`), check.Equals, true)
|
||||
c.Assert(strings.Contains(html, `content="{{ .XCSRF }}"`), check.Equals, true)
|
||||
require.NoError(t, f.Close())
|
||||
require.Equal(t, strings.Contains(html, `<script src="/web/config.js"></script>`), true)
|
||||
require.Equal(t, strings.Contains(html, `content="{{ .XCSRF }}"`), true)
|
||||
}
|
||||
|
||||
func (s *StaticSuite) TestZipFS(c *check.C) {
|
||||
func TestZipFS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fs, err := readZipArchiveAt("../../fixtures/assets.zip")
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(fs, check.NotNil)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, fs)
|
||||
|
||||
// test simple full read:
|
||||
f, err := fs.Open("/index.html")
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
bytes, err := io.ReadAll(f)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(len(bytes), check.Equals, 813)
|
||||
c.Assert(f.Close(), check.IsNil)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(bytes), 813)
|
||||
require.NoError(t, f.Close())
|
||||
|
||||
// seek + read
|
||||
f, err = fs.Open("/index.html")
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
defer f.Close()
|
||||
|
||||
n, err := f.Seek(10, io.SeekStart)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(n, check.Equals, int64(10))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, n, int64(10))
|
||||
|
||||
bytes, err = io.ReadAll(f)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(len(bytes), check.Equals, 803)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(bytes), 803)
|
||||
|
||||
n, err = f.Seek(-50, io.SeekEnd)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(n, check.Equals, int64(763))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, n, int64(763))
|
||||
bytes, err = io.ReadAll(f)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(len(bytes), check.Equals, 50)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(bytes), 50)
|
||||
|
||||
_, err = f.Seek(-50, io.SeekEnd)
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
n, err = f.Seek(-50, io.SeekCurrent)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(n, check.Equals, int64(713))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, n, int64(713))
|
||||
bytes, err = io.ReadAll(f)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(len(bytes), check.Equals, 100)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(bytes), 100)
|
||||
}
|
||||
|
||||
func readZipArchiveAt(path string) (ResourceMap, error) {
|
||||
|
||||
@@ -27,7 +27,6 @@ import (
|
||||
"github.com/gravitational/teleport/api/types"
|
||||
"github.com/gravitational/teleport/lib/auth"
|
||||
"github.com/gravitational/teleport/lib/backend"
|
||||
"github.com/gravitational/teleport/lib/backend/lite"
|
||||
"github.com/gravitational/teleport/lib/backend/memory"
|
||||
"github.com/gravitational/teleport/lib/reversetunnel"
|
||||
"github.com/gravitational/teleport/lib/services"
|
||||
@@ -77,8 +76,8 @@ func BenchmarkGetClusterDetails(b *testing.B) {
|
||||
bk, err = memory.New(memory.Config{})
|
||||
require.NoError(b, err)
|
||||
} else {
|
||||
bk, err = lite.NewWithConfig(context.TODO(), lite.Config{
|
||||
Path: b.TempDir(),
|
||||
bk, err = memory.New(memory.Config{
|
||||
Context: ctx,
|
||||
})
|
||||
require.NoError(b, err)
|
||||
}
|
||||
|
||||
@@ -1,36 +1,35 @@
|
||||
// Copyright 2021 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.
|
||||
/*
|
||||
Copyright 2022 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 ui
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/gravitational/teleport/api/client/proto"
|
||||
apidefaults "github.com/gravitational/teleport/api/defaults"
|
||||
"github.com/gravitational/teleport/api/types"
|
||||
"github.com/gravitational/teleport/lib/services"
|
||||
"gopkg.in/check.v1"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type UserContextSuite struct{}
|
||||
func TestNewUserContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var _ = check.Suite(&UserContextSuite{})
|
||||
|
||||
func TestUserContext(t *testing.T) { check.TestingT(t) }
|
||||
|
||||
func (s *UserContextSuite) TestNewUserContext(c *check.C) {
|
||||
user := &types.UserV2{
|
||||
Metadata: types.Metadata{
|
||||
Name: "root",
|
||||
@@ -79,55 +78,58 @@ func (s *UserContextSuite) TestNewUserContext(c *check.C) {
|
||||
|
||||
roleSet := []types.Role{role1, role2}
|
||||
userContext, err := NewUserContext(user, roleSet, proto.Features{}, true)
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
|
||||
allowed := access{true, true, true, true, true}
|
||||
denied := access{false, false, false, false, false}
|
||||
|
||||
// test user name and acl
|
||||
c.Assert(userContext.Name, check.Equals, "root")
|
||||
c.Assert(userContext.ACL.AuthConnectors, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.TrustedClusters, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.AppServers, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.DBServers, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.KubeServers, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Events, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.RecordedSessions, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Roles, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Users, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Tokens, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Nodes, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.AccessRequests, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Desktops, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.WindowsLogins, check.DeepEquals, []string{"a", "b", "d"})
|
||||
c.Assert(userContext.AccessStrategy, check.DeepEquals, accessStrategy{
|
||||
require.Equal(t, userContext.Name, "root")
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.AuthConnectors, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.TrustedClusters, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.AppServers, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.DBServers, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.KubeServers, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Events, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.RecordedSessions, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Roles, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Users, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Tokens, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Nodes, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.AccessRequests, denied))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Desktops, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.WindowsLogins, []string{"a", "b", "d"}))
|
||||
require.Empty(t, cmp.Diff(userContext.AccessStrategy, accessStrategy{
|
||||
Type: types.RequestStrategyOptional,
|
||||
Prompt: "",
|
||||
})
|
||||
c.Assert(userContext.ACL.Billing, check.DeepEquals, denied)
|
||||
c.Assert(userContext.ACL.Clipboard, check.Equals, true)
|
||||
c.Assert(userContext.ACL.DesktopSessionRecording, check.Equals, true)
|
||||
}))
|
||||
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Billing, denied))
|
||||
require.Equal(t, userContext.ACL.Clipboard, true)
|
||||
require.Equal(t, userContext.ACL.DesktopSessionRecording, true)
|
||||
|
||||
// test local auth type
|
||||
c.Assert(userContext.AuthType, check.Equals, authLocal)
|
||||
require.Equal(t, userContext.AuthType, authLocal)
|
||||
|
||||
// test sso auth type
|
||||
user.Spec.GithubIdentities = []types.ExternalIdentity{{ConnectorID: "foo", Username: "bar"}}
|
||||
userContext, err = NewUserContext(user, roleSet, proto.Features{}, true)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(userContext.AuthType, check.Equals, authSSO)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, userContext.AuthType, authSSO)
|
||||
|
||||
userContext, err = NewUserContext(user, roleSet, proto.Features{Cloud: true}, true)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(userContext.ACL.Billing, check.DeepEquals, access{true, true, false, false, false})
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Billing, access{true, true, false, false, false}))
|
||||
|
||||
// test that desktopRecordingEnabled being false overrides the roleSet.RecordDesktopSession() returning true
|
||||
userContext, err = NewUserContext(user, roleSet, proto.Features{}, false)
|
||||
c.Assert(err, check.IsNil)
|
||||
c.Assert(userContext.ACL.DesktopSessionRecording, check.Equals, false)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, userContext.ACL.DesktopSessionRecording, false)
|
||||
}
|
||||
|
||||
func (s *UserContextSuite) TestNewUserContextCloud(c *check.C) {
|
||||
func TestNewUserContextCloud(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
user := &types.UserV2{
|
||||
Metadata: types.Metadata{
|
||||
Name: "root",
|
||||
@@ -151,30 +153,31 @@ func (s *UserContextSuite) TestNewUserContextCloud(c *check.C) {
|
||||
allowed := access{true, true, true, true, true}
|
||||
|
||||
userContext, err := NewUserContext(user, roleSet, proto.Features{Cloud: true}, true)
|
||||
c.Assert(err, check.IsNil)
|
||||
require.NoError(t, err)
|
||||
|
||||
c.Assert(userContext.Name, check.Equals, "root")
|
||||
c.Assert(userContext.ACL.AuthConnectors, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.TrustedClusters, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.AppServers, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.DBServers, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.KubeServers, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.Events, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.RecordedSessions, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.Roles, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.Users, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.Tokens, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.Nodes, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.AccessRequests, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.WindowsLogins, check.DeepEquals, []string{"a", "b"})
|
||||
c.Assert(userContext.AccessStrategy, check.DeepEquals, accessStrategy{
|
||||
require.Equal(t, userContext.Name, "root")
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.AuthConnectors, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.TrustedClusters, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.AppServers, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.DBServers, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.KubeServers, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Events, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.RecordedSessions, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Roles, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Users, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Tokens, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Nodes, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.AccessRequests, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.WindowsLogins, []string{"a", "b"}))
|
||||
require.Empty(t, cmp.Diff(userContext.AccessStrategy, accessStrategy{
|
||||
Type: types.RequestStrategyOptional,
|
||||
Prompt: "",
|
||||
})
|
||||
c.Assert(userContext.ACL.Clipboard, check.Equals, true)
|
||||
c.Assert(userContext.ACL.DesktopSessionRecording, check.Equals, true)
|
||||
}))
|
||||
|
||||
require.Equal(t, userContext.ACL.Clipboard, true)
|
||||
require.Equal(t, userContext.ACL.DesktopSessionRecording, true)
|
||||
|
||||
// cloud-specific asserts
|
||||
c.Assert(userContext.ACL.Billing, check.DeepEquals, allowed)
|
||||
c.Assert(userContext.ACL.Desktops, check.DeepEquals, allowed)
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Billing, allowed))
|
||||
require.Empty(t, cmp.Diff(userContext.ACL.Desktops, allowed))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user