v3_auth_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "testing"
  17. "time"
  18. "golang.org/x/net/context"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. // TestV3AuthEmptyUserGet ensures that a get with an empty user will return an empty user error.
  24. func TestV3AuthEmptyUserGet(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  27. defer clus.Terminate(t)
  28. ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
  29. defer cancel()
  30. api := toGRPC(clus.Client(0))
  31. auth := api.Auth
  32. if _, err := auth.UserAdd(ctx, &pb.AuthUserAddRequest{Name: "root", Password: "123"}); err != nil {
  33. t.Fatal(err)
  34. }
  35. if _, err := auth.RoleAdd(ctx, &pb.AuthRoleAddRequest{Name: "root"}); err != nil {
  36. t.Fatal(err)
  37. }
  38. if _, err := auth.UserGrantRole(ctx, &pb.AuthUserGrantRoleRequest{User: "root", Role: "root"}); err != nil {
  39. t.Fatal(err)
  40. }
  41. if _, err := auth.AuthEnable(ctx, &pb.AuthEnableRequest{}); err != nil {
  42. t.Fatal(err)
  43. }
  44. _, err := api.KV.Range(ctx, &pb.RangeRequest{Key: []byte("abc")})
  45. if !eqErrGRPC(err, rpctypes.ErrUserEmpty) {
  46. t.Fatalf("got %v, expected %v", err, rpctypes.ErrUserEmpty)
  47. }
  48. }