You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1224 lines
34 KiB

  1. // Copyright 2019 Google LLC
  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. // https://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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package admin
  16. import (
  17. emptypb "github.com/golang/protobuf/ptypes/empty"
  18. adminpb "google.golang.org/genproto/googleapis/iam/admin/v1"
  19. iampb "google.golang.org/genproto/googleapis/iam/v1"
  20. )
  21. import (
  22. "context"
  23. "flag"
  24. "fmt"
  25. "io"
  26. "log"
  27. "net"
  28. "os"
  29. "strings"
  30. "testing"
  31. "github.com/golang/protobuf/proto"
  32. "github.com/golang/protobuf/ptypes"
  33. "google.golang.org/api/option"
  34. status "google.golang.org/genproto/googleapis/rpc/status"
  35. "google.golang.org/grpc"
  36. "google.golang.org/grpc/codes"
  37. "google.golang.org/grpc/metadata"
  38. gstatus "google.golang.org/grpc/status"
  39. )
  40. var _ = io.EOF
  41. var _ = ptypes.MarshalAny
  42. var _ status.Status
  43. type mockIamServer struct {
  44. // Embed for forward compatibility.
  45. // Tests will keep working if more methods are added
  46. // in the future.
  47. adminpb.IAMServer
  48. reqs []proto.Message
  49. // If set, all calls return this error.
  50. err error
  51. // responses to return if err == nil
  52. resps []proto.Message
  53. }
  54. func (s *mockIamServer) ListServiceAccounts(ctx context.Context, req *adminpb.ListServiceAccountsRequest) (*adminpb.ListServiceAccountsResponse, error) {
  55. md, _ := metadata.FromIncomingContext(ctx)
  56. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  57. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  58. }
  59. s.reqs = append(s.reqs, req)
  60. if s.err != nil {
  61. return nil, s.err
  62. }
  63. return s.resps[0].(*adminpb.ListServiceAccountsResponse), nil
  64. }
  65. func (s *mockIamServer) GetServiceAccount(ctx context.Context, req *adminpb.GetServiceAccountRequest) (*adminpb.ServiceAccount, error) {
  66. md, _ := metadata.FromIncomingContext(ctx)
  67. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  68. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  69. }
  70. s.reqs = append(s.reqs, req)
  71. if s.err != nil {
  72. return nil, s.err
  73. }
  74. return s.resps[0].(*adminpb.ServiceAccount), nil
  75. }
  76. func (s *mockIamServer) CreateServiceAccount(ctx context.Context, req *adminpb.CreateServiceAccountRequest) (*adminpb.ServiceAccount, error) {
  77. md, _ := metadata.FromIncomingContext(ctx)
  78. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  79. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  80. }
  81. s.reqs = append(s.reqs, req)
  82. if s.err != nil {
  83. return nil, s.err
  84. }
  85. return s.resps[0].(*adminpb.ServiceAccount), nil
  86. }
  87. func (s *mockIamServer) UpdateServiceAccount(ctx context.Context, req *adminpb.ServiceAccount) (*adminpb.ServiceAccount, error) {
  88. md, _ := metadata.FromIncomingContext(ctx)
  89. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  90. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  91. }
  92. s.reqs = append(s.reqs, req)
  93. if s.err != nil {
  94. return nil, s.err
  95. }
  96. return s.resps[0].(*adminpb.ServiceAccount), nil
  97. }
  98. func (s *mockIamServer) DeleteServiceAccount(ctx context.Context, req *adminpb.DeleteServiceAccountRequest) (*emptypb.Empty, error) {
  99. md, _ := metadata.FromIncomingContext(ctx)
  100. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  101. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  102. }
  103. s.reqs = append(s.reqs, req)
  104. if s.err != nil {
  105. return nil, s.err
  106. }
  107. return s.resps[0].(*emptypb.Empty), nil
  108. }
  109. func (s *mockIamServer) ListServiceAccountKeys(ctx context.Context, req *adminpb.ListServiceAccountKeysRequest) (*adminpb.ListServiceAccountKeysResponse, error) {
  110. md, _ := metadata.FromIncomingContext(ctx)
  111. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  112. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  113. }
  114. s.reqs = append(s.reqs, req)
  115. if s.err != nil {
  116. return nil, s.err
  117. }
  118. return s.resps[0].(*adminpb.ListServiceAccountKeysResponse), nil
  119. }
  120. func (s *mockIamServer) GetServiceAccountKey(ctx context.Context, req *adminpb.GetServiceAccountKeyRequest) (*adminpb.ServiceAccountKey, error) {
  121. md, _ := metadata.FromIncomingContext(ctx)
  122. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  123. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  124. }
  125. s.reqs = append(s.reqs, req)
  126. if s.err != nil {
  127. return nil, s.err
  128. }
  129. return s.resps[0].(*adminpb.ServiceAccountKey), nil
  130. }
  131. func (s *mockIamServer) CreateServiceAccountKey(ctx context.Context, req *adminpb.CreateServiceAccountKeyRequest) (*adminpb.ServiceAccountKey, error) {
  132. md, _ := metadata.FromIncomingContext(ctx)
  133. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  134. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  135. }
  136. s.reqs = append(s.reqs, req)
  137. if s.err != nil {
  138. return nil, s.err
  139. }
  140. return s.resps[0].(*adminpb.ServiceAccountKey), nil
  141. }
  142. func (s *mockIamServer) DeleteServiceAccountKey(ctx context.Context, req *adminpb.DeleteServiceAccountKeyRequest) (*emptypb.Empty, error) {
  143. md, _ := metadata.FromIncomingContext(ctx)
  144. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  145. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  146. }
  147. s.reqs = append(s.reqs, req)
  148. if s.err != nil {
  149. return nil, s.err
  150. }
  151. return s.resps[0].(*emptypb.Empty), nil
  152. }
  153. func (s *mockIamServer) SignBlob(ctx context.Context, req *adminpb.SignBlobRequest) (*adminpb.SignBlobResponse, error) {
  154. md, _ := metadata.FromIncomingContext(ctx)
  155. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  156. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  157. }
  158. s.reqs = append(s.reqs, req)
  159. if s.err != nil {
  160. return nil, s.err
  161. }
  162. return s.resps[0].(*adminpb.SignBlobResponse), nil
  163. }
  164. func (s *mockIamServer) SignJwt(ctx context.Context, req *adminpb.SignJwtRequest) (*adminpb.SignJwtResponse, error) {
  165. md, _ := metadata.FromIncomingContext(ctx)
  166. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  167. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  168. }
  169. s.reqs = append(s.reqs, req)
  170. if s.err != nil {
  171. return nil, s.err
  172. }
  173. return s.resps[0].(*adminpb.SignJwtResponse), nil
  174. }
  175. func (s *mockIamServer) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest) (*iampb.Policy, error) {
  176. md, _ := metadata.FromIncomingContext(ctx)
  177. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  178. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  179. }
  180. s.reqs = append(s.reqs, req)
  181. if s.err != nil {
  182. return nil, s.err
  183. }
  184. return s.resps[0].(*iampb.Policy), nil
  185. }
  186. func (s *mockIamServer) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest) (*iampb.Policy, error) {
  187. md, _ := metadata.FromIncomingContext(ctx)
  188. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  189. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  190. }
  191. s.reqs = append(s.reqs, req)
  192. if s.err != nil {
  193. return nil, s.err
  194. }
  195. return s.resps[0].(*iampb.Policy), nil
  196. }
  197. func (s *mockIamServer) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest) (*iampb.TestIamPermissionsResponse, error) {
  198. md, _ := metadata.FromIncomingContext(ctx)
  199. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  200. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  201. }
  202. s.reqs = append(s.reqs, req)
  203. if s.err != nil {
  204. return nil, s.err
  205. }
  206. return s.resps[0].(*iampb.TestIamPermissionsResponse), nil
  207. }
  208. func (s *mockIamServer) QueryGrantableRoles(ctx context.Context, req *adminpb.QueryGrantableRolesRequest) (*adminpb.QueryGrantableRolesResponse, error) {
  209. md, _ := metadata.FromIncomingContext(ctx)
  210. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  211. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  212. }
  213. s.reqs = append(s.reqs, req)
  214. if s.err != nil {
  215. return nil, s.err
  216. }
  217. return s.resps[0].(*adminpb.QueryGrantableRolesResponse), nil
  218. }
  219. // clientOpt is the option tests should use to connect to the test server.
  220. // It is initialized by TestMain.
  221. var clientOpt option.ClientOption
  222. var (
  223. mockIam mockIamServer
  224. )
  225. func TestMain(m *testing.M) {
  226. flag.Parse()
  227. serv := grpc.NewServer()
  228. adminpb.RegisterIAMServer(serv, &mockIam)
  229. lis, err := net.Listen("tcp", "localhost:0")
  230. if err != nil {
  231. log.Fatal(err)
  232. }
  233. go serv.Serve(lis)
  234. conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
  235. if err != nil {
  236. log.Fatal(err)
  237. }
  238. clientOpt = option.WithGRPCConn(conn)
  239. os.Exit(m.Run())
  240. }
  241. func TestIamListServiceAccounts(t *testing.T) {
  242. var nextPageToken string = ""
  243. var accountsElement *adminpb.ServiceAccount = &adminpb.ServiceAccount{}
  244. var accounts = []*adminpb.ServiceAccount{accountsElement}
  245. var expectedResponse = &adminpb.ListServiceAccountsResponse{
  246. NextPageToken: nextPageToken,
  247. Accounts: accounts,
  248. }
  249. mockIam.err = nil
  250. mockIam.reqs = nil
  251. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  252. var formattedName string = fmt.Sprintf("projects/%s", "[PROJECT]")
  253. var request = &adminpb.ListServiceAccountsRequest{
  254. Name: formattedName,
  255. }
  256. c, err := NewIamClient(context.Background(), clientOpt)
  257. if err != nil {
  258. t.Fatal(err)
  259. }
  260. resp, err := c.ListServiceAccounts(context.Background(), request).Next()
  261. if err != nil {
  262. t.Fatal(err)
  263. }
  264. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  265. t.Errorf("wrong request %q, want %q", got, want)
  266. }
  267. want := (interface{})(expectedResponse.Accounts[0])
  268. got := (interface{})(resp)
  269. var ok bool
  270. switch want := (want).(type) {
  271. case proto.Message:
  272. ok = proto.Equal(want, got.(proto.Message))
  273. default:
  274. ok = want == got
  275. }
  276. if !ok {
  277. t.Errorf("wrong response %q, want %q)", got, want)
  278. }
  279. }
  280. func TestIamListServiceAccountsError(t *testing.T) {
  281. errCode := codes.PermissionDenied
  282. mockIam.err = gstatus.Error(errCode, "test error")
  283. var formattedName string = fmt.Sprintf("projects/%s", "[PROJECT]")
  284. var request = &adminpb.ListServiceAccountsRequest{
  285. Name: formattedName,
  286. }
  287. c, err := NewIamClient(context.Background(), clientOpt)
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. resp, err := c.ListServiceAccounts(context.Background(), request).Next()
  292. if st, ok := gstatus.FromError(err); !ok {
  293. t.Errorf("got error %v, expected grpc error", err)
  294. } else if c := st.Code(); c != errCode {
  295. t.Errorf("got error code %q, want %q", c, errCode)
  296. }
  297. _ = resp
  298. }
  299. func TestIamGetServiceAccount(t *testing.T) {
  300. var name2 string = "name2-1052831874"
  301. var projectId string = "projectId-1969970175"
  302. var uniqueId string = "uniqueId-538310583"
  303. var email string = "email96619420"
  304. var displayName string = "displayName1615086568"
  305. var etag []byte = []byte("21")
  306. var oauth2ClientId string = "oauth2ClientId-1833466037"
  307. var expectedResponse = &adminpb.ServiceAccount{
  308. Name: name2,
  309. ProjectId: projectId,
  310. UniqueId: uniqueId,
  311. Email: email,
  312. DisplayName: displayName,
  313. Etag: etag,
  314. Oauth2ClientId: oauth2ClientId,
  315. }
  316. mockIam.err = nil
  317. mockIam.reqs = nil
  318. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  319. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  320. var request = &adminpb.GetServiceAccountRequest{
  321. Name: formattedName,
  322. }
  323. c, err := NewIamClient(context.Background(), clientOpt)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. resp, err := c.GetServiceAccount(context.Background(), request)
  328. if err != nil {
  329. t.Fatal(err)
  330. }
  331. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  332. t.Errorf("wrong request %q, want %q", got, want)
  333. }
  334. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  335. t.Errorf("wrong response %q, want %q)", got, want)
  336. }
  337. }
  338. func TestIamGetServiceAccountError(t *testing.T) {
  339. errCode := codes.PermissionDenied
  340. mockIam.err = gstatus.Error(errCode, "test error")
  341. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  342. var request = &adminpb.GetServiceAccountRequest{
  343. Name: formattedName,
  344. }
  345. c, err := NewIamClient(context.Background(), clientOpt)
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. resp, err := c.GetServiceAccount(context.Background(), request)
  350. if st, ok := gstatus.FromError(err); !ok {
  351. t.Errorf("got error %v, expected grpc error", err)
  352. } else if c := st.Code(); c != errCode {
  353. t.Errorf("got error code %q, want %q", c, errCode)
  354. }
  355. _ = resp
  356. }
  357. func TestIamCreateServiceAccount(t *testing.T) {
  358. var name2 string = "name2-1052831874"
  359. var projectId string = "projectId-1969970175"
  360. var uniqueId string = "uniqueId-538310583"
  361. var email string = "email96619420"
  362. var displayName string = "displayName1615086568"
  363. var etag []byte = []byte("21")
  364. var oauth2ClientId string = "oauth2ClientId-1833466037"
  365. var expectedResponse = &adminpb.ServiceAccount{
  366. Name: name2,
  367. ProjectId: projectId,
  368. UniqueId: uniqueId,
  369. Email: email,
  370. DisplayName: displayName,
  371. Etag: etag,
  372. Oauth2ClientId: oauth2ClientId,
  373. }
  374. mockIam.err = nil
  375. mockIam.reqs = nil
  376. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  377. var formattedName string = fmt.Sprintf("projects/%s", "[PROJECT]")
  378. var accountId string = "accountId-803333011"
  379. var request = &adminpb.CreateServiceAccountRequest{
  380. Name: formattedName,
  381. AccountId: accountId,
  382. }
  383. c, err := NewIamClient(context.Background(), clientOpt)
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. resp, err := c.CreateServiceAccount(context.Background(), request)
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  392. t.Errorf("wrong request %q, want %q", got, want)
  393. }
  394. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  395. t.Errorf("wrong response %q, want %q)", got, want)
  396. }
  397. }
  398. func TestIamCreateServiceAccountError(t *testing.T) {
  399. errCode := codes.PermissionDenied
  400. mockIam.err = gstatus.Error(errCode, "test error")
  401. var formattedName string = fmt.Sprintf("projects/%s", "[PROJECT]")
  402. var accountId string = "accountId-803333011"
  403. var request = &adminpb.CreateServiceAccountRequest{
  404. Name: formattedName,
  405. AccountId: accountId,
  406. }
  407. c, err := NewIamClient(context.Background(), clientOpt)
  408. if err != nil {
  409. t.Fatal(err)
  410. }
  411. resp, err := c.CreateServiceAccount(context.Background(), request)
  412. if st, ok := gstatus.FromError(err); !ok {
  413. t.Errorf("got error %v, expected grpc error", err)
  414. } else if c := st.Code(); c != errCode {
  415. t.Errorf("got error code %q, want %q", c, errCode)
  416. }
  417. _ = resp
  418. }
  419. func TestIamUpdateServiceAccount(t *testing.T) {
  420. var name string = "name3373707"
  421. var projectId string = "projectId-1969970175"
  422. var uniqueId string = "uniqueId-538310583"
  423. var email string = "email96619420"
  424. var displayName string = "displayName1615086568"
  425. var etag2 []byte = []byte("-120")
  426. var oauth2ClientId string = "oauth2ClientId-1833466037"
  427. var expectedResponse = &adminpb.ServiceAccount{
  428. Name: name,
  429. ProjectId: projectId,
  430. UniqueId: uniqueId,
  431. Email: email,
  432. DisplayName: displayName,
  433. Etag: etag2,
  434. Oauth2ClientId: oauth2ClientId,
  435. }
  436. mockIam.err = nil
  437. mockIam.reqs = nil
  438. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  439. var etag []byte = []byte("21")
  440. var request = &adminpb.ServiceAccount{
  441. Etag: etag,
  442. }
  443. c, err := NewIamClient(context.Background(), clientOpt)
  444. if err != nil {
  445. t.Fatal(err)
  446. }
  447. resp, err := c.UpdateServiceAccount(context.Background(), request)
  448. if err != nil {
  449. t.Fatal(err)
  450. }
  451. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  452. t.Errorf("wrong request %q, want %q", got, want)
  453. }
  454. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  455. t.Errorf("wrong response %q, want %q)", got, want)
  456. }
  457. }
  458. func TestIamUpdateServiceAccountError(t *testing.T) {
  459. errCode := codes.PermissionDenied
  460. mockIam.err = gstatus.Error(errCode, "test error")
  461. var etag []byte = []byte("21")
  462. var request = &adminpb.ServiceAccount{
  463. Etag: etag,
  464. }
  465. c, err := NewIamClient(context.Background(), clientOpt)
  466. if err != nil {
  467. t.Fatal(err)
  468. }
  469. resp, err := c.UpdateServiceAccount(context.Background(), request)
  470. if st, ok := gstatus.FromError(err); !ok {
  471. t.Errorf("got error %v, expected grpc error", err)
  472. } else if c := st.Code(); c != errCode {
  473. t.Errorf("got error code %q, want %q", c, errCode)
  474. }
  475. _ = resp
  476. }
  477. func TestIamDeleteServiceAccount(t *testing.T) {
  478. var expectedResponse *emptypb.Empty = &emptypb.Empty{}
  479. mockIam.err = nil
  480. mockIam.reqs = nil
  481. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  482. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  483. var request = &adminpb.DeleteServiceAccountRequest{
  484. Name: formattedName,
  485. }
  486. c, err := NewIamClient(context.Background(), clientOpt)
  487. if err != nil {
  488. t.Fatal(err)
  489. }
  490. err = c.DeleteServiceAccount(context.Background(), request)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  495. t.Errorf("wrong request %q, want %q", got, want)
  496. }
  497. }
  498. func TestIamDeleteServiceAccountError(t *testing.T) {
  499. errCode := codes.PermissionDenied
  500. mockIam.err = gstatus.Error(errCode, "test error")
  501. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  502. var request = &adminpb.DeleteServiceAccountRequest{
  503. Name: formattedName,
  504. }
  505. c, err := NewIamClient(context.Background(), clientOpt)
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. err = c.DeleteServiceAccount(context.Background(), request)
  510. if st, ok := gstatus.FromError(err); !ok {
  511. t.Errorf("got error %v, expected grpc error", err)
  512. } else if c := st.Code(); c != errCode {
  513. t.Errorf("got error code %q, want %q", c, errCode)
  514. }
  515. }
  516. func TestIamListServiceAccountKeys(t *testing.T) {
  517. var expectedResponse *adminpb.ListServiceAccountKeysResponse = &adminpb.ListServiceAccountKeysResponse{}
  518. mockIam.err = nil
  519. mockIam.reqs = nil
  520. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  521. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  522. var request = &adminpb.ListServiceAccountKeysRequest{
  523. Name: formattedName,
  524. }
  525. c, err := NewIamClient(context.Background(), clientOpt)
  526. if err != nil {
  527. t.Fatal(err)
  528. }
  529. resp, err := c.ListServiceAccountKeys(context.Background(), request)
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  534. t.Errorf("wrong request %q, want %q", got, want)
  535. }
  536. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  537. t.Errorf("wrong response %q, want %q)", got, want)
  538. }
  539. }
  540. func TestIamListServiceAccountKeysError(t *testing.T) {
  541. errCode := codes.PermissionDenied
  542. mockIam.err = gstatus.Error(errCode, "test error")
  543. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  544. var request = &adminpb.ListServiceAccountKeysRequest{
  545. Name: formattedName,
  546. }
  547. c, err := NewIamClient(context.Background(), clientOpt)
  548. if err != nil {
  549. t.Fatal(err)
  550. }
  551. resp, err := c.ListServiceAccountKeys(context.Background(), request)
  552. if st, ok := gstatus.FromError(err); !ok {
  553. t.Errorf("got error %v, expected grpc error", err)
  554. } else if c := st.Code(); c != errCode {
  555. t.Errorf("got error code %q, want %q", c, errCode)
  556. }
  557. _ = resp
  558. }
  559. func TestIamGetServiceAccountKey(t *testing.T) {
  560. var name2 string = "name2-1052831874"
  561. var privateKeyData []byte = []byte("-58")
  562. var publicKeyData []byte = []byte("-96")
  563. var expectedResponse = &adminpb.ServiceAccountKey{
  564. Name: name2,
  565. PrivateKeyData: privateKeyData,
  566. PublicKeyData: publicKeyData,
  567. }
  568. mockIam.err = nil
  569. mockIam.reqs = nil
  570. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  571. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s/keys/%s", "[PROJECT]", "[SERVICE_ACCOUNT]", "[KEY]")
  572. var request = &adminpb.GetServiceAccountKeyRequest{
  573. Name: formattedName,
  574. }
  575. c, err := NewIamClient(context.Background(), clientOpt)
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. resp, err := c.GetServiceAccountKey(context.Background(), request)
  580. if err != nil {
  581. t.Fatal(err)
  582. }
  583. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  584. t.Errorf("wrong request %q, want %q", got, want)
  585. }
  586. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  587. t.Errorf("wrong response %q, want %q)", got, want)
  588. }
  589. }
  590. func TestIamGetServiceAccountKeyError(t *testing.T) {
  591. errCode := codes.PermissionDenied
  592. mockIam.err = gstatus.Error(errCode, "test error")
  593. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s/keys/%s", "[PROJECT]", "[SERVICE_ACCOUNT]", "[KEY]")
  594. var request = &adminpb.GetServiceAccountKeyRequest{
  595. Name: formattedName,
  596. }
  597. c, err := NewIamClient(context.Background(), clientOpt)
  598. if err != nil {
  599. t.Fatal(err)
  600. }
  601. resp, err := c.GetServiceAccountKey(context.Background(), request)
  602. if st, ok := gstatus.FromError(err); !ok {
  603. t.Errorf("got error %v, expected grpc error", err)
  604. } else if c := st.Code(); c != errCode {
  605. t.Errorf("got error code %q, want %q", c, errCode)
  606. }
  607. _ = resp
  608. }
  609. func TestIamCreateServiceAccountKey(t *testing.T) {
  610. var name2 string = "name2-1052831874"
  611. var privateKeyData []byte = []byte("-58")
  612. var publicKeyData []byte = []byte("-96")
  613. var expectedResponse = &adminpb.ServiceAccountKey{
  614. Name: name2,
  615. PrivateKeyData: privateKeyData,
  616. PublicKeyData: publicKeyData,
  617. }
  618. mockIam.err = nil
  619. mockIam.reqs = nil
  620. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  621. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  622. var request = &adminpb.CreateServiceAccountKeyRequest{
  623. Name: formattedName,
  624. }
  625. c, err := NewIamClient(context.Background(), clientOpt)
  626. if err != nil {
  627. t.Fatal(err)
  628. }
  629. resp, err := c.CreateServiceAccountKey(context.Background(), request)
  630. if err != nil {
  631. t.Fatal(err)
  632. }
  633. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  634. t.Errorf("wrong request %q, want %q", got, want)
  635. }
  636. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  637. t.Errorf("wrong response %q, want %q)", got, want)
  638. }
  639. }
  640. func TestIamCreateServiceAccountKeyError(t *testing.T) {
  641. errCode := codes.PermissionDenied
  642. mockIam.err = gstatus.Error(errCode, "test error")
  643. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  644. var request = &adminpb.CreateServiceAccountKeyRequest{
  645. Name: formattedName,
  646. }
  647. c, err := NewIamClient(context.Background(), clientOpt)
  648. if err != nil {
  649. t.Fatal(err)
  650. }
  651. resp, err := c.CreateServiceAccountKey(context.Background(), request)
  652. if st, ok := gstatus.FromError(err); !ok {
  653. t.Errorf("got error %v, expected grpc error", err)
  654. } else if c := st.Code(); c != errCode {
  655. t.Errorf("got error code %q, want %q", c, errCode)
  656. }
  657. _ = resp
  658. }
  659. func TestIamDeleteServiceAccountKey(t *testing.T) {
  660. var expectedResponse *emptypb.Empty = &emptypb.Empty{}
  661. mockIam.err = nil
  662. mockIam.reqs = nil
  663. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  664. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s/keys/%s", "[PROJECT]", "[SERVICE_ACCOUNT]", "[KEY]")
  665. var request = &adminpb.DeleteServiceAccountKeyRequest{
  666. Name: formattedName,
  667. }
  668. c, err := NewIamClient(context.Background(), clientOpt)
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. err = c.DeleteServiceAccountKey(context.Background(), request)
  673. if err != nil {
  674. t.Fatal(err)
  675. }
  676. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  677. t.Errorf("wrong request %q, want %q", got, want)
  678. }
  679. }
  680. func TestIamDeleteServiceAccountKeyError(t *testing.T) {
  681. errCode := codes.PermissionDenied
  682. mockIam.err = gstatus.Error(errCode, "test error")
  683. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s/keys/%s", "[PROJECT]", "[SERVICE_ACCOUNT]", "[KEY]")
  684. var request = &adminpb.DeleteServiceAccountKeyRequest{
  685. Name: formattedName,
  686. }
  687. c, err := NewIamClient(context.Background(), clientOpt)
  688. if err != nil {
  689. t.Fatal(err)
  690. }
  691. err = c.DeleteServiceAccountKey(context.Background(), request)
  692. if st, ok := gstatus.FromError(err); !ok {
  693. t.Errorf("got error %v, expected grpc error", err)
  694. } else if c := st.Code(); c != errCode {
  695. t.Errorf("got error code %q, want %q", c, errCode)
  696. }
  697. }
  698. func TestIamSignBlob(t *testing.T) {
  699. var keyId string = "keyId-1134673157"
  700. var signature []byte = []byte("-72")
  701. var expectedResponse = &adminpb.SignBlobResponse{
  702. KeyId: keyId,
  703. Signature: signature,
  704. }
  705. mockIam.err = nil
  706. mockIam.reqs = nil
  707. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  708. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  709. var bytesToSign []byte = []byte("45")
  710. var request = &adminpb.SignBlobRequest{
  711. Name: formattedName,
  712. BytesToSign: bytesToSign,
  713. }
  714. c, err := NewIamClient(context.Background(), clientOpt)
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. resp, err := c.SignBlob(context.Background(), request)
  719. if err != nil {
  720. t.Fatal(err)
  721. }
  722. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  723. t.Errorf("wrong request %q, want %q", got, want)
  724. }
  725. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  726. t.Errorf("wrong response %q, want %q)", got, want)
  727. }
  728. }
  729. func TestIamSignBlobError(t *testing.T) {
  730. errCode := codes.PermissionDenied
  731. mockIam.err = gstatus.Error(errCode, "test error")
  732. var formattedName string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  733. var bytesToSign []byte = []byte("45")
  734. var request = &adminpb.SignBlobRequest{
  735. Name: formattedName,
  736. BytesToSign: bytesToSign,
  737. }
  738. c, err := NewIamClient(context.Background(), clientOpt)
  739. if err != nil {
  740. t.Fatal(err)
  741. }
  742. resp, err := c.SignBlob(context.Background(), request)
  743. if st, ok := gstatus.FromError(err); !ok {
  744. t.Errorf("got error %v, expected grpc error", err)
  745. } else if c := st.Code(); c != errCode {
  746. t.Errorf("got error code %q, want %q", c, errCode)
  747. }
  748. _ = resp
  749. }
  750. func TestIamGetIamPolicy(t *testing.T) {
  751. var version int32 = 351608024
  752. var etag []byte = []byte("21")
  753. var expectedResponse = &iampb.Policy{
  754. Version: version,
  755. Etag: etag,
  756. }
  757. mockIam.err = nil
  758. mockIam.reqs = nil
  759. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  760. var formattedResource string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  761. var request = &iampb.GetIamPolicyRequest{
  762. Resource: formattedResource,
  763. }
  764. c, err := NewIamClient(context.Background(), clientOpt)
  765. if err != nil {
  766. t.Fatal(err)
  767. }
  768. resp, err := c.getIamPolicy(context.Background(), request)
  769. if err != nil {
  770. t.Fatal(err)
  771. }
  772. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  773. t.Errorf("wrong request %q, want %q", got, want)
  774. }
  775. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  776. t.Errorf("wrong response %q, want %q)", got, want)
  777. }
  778. }
  779. func TestIamGetIamPolicyError(t *testing.T) {
  780. errCode := codes.PermissionDenied
  781. mockIam.err = gstatus.Error(errCode, "test error")
  782. var formattedResource string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  783. var request = &iampb.GetIamPolicyRequest{
  784. Resource: formattedResource,
  785. }
  786. c, err := NewIamClient(context.Background(), clientOpt)
  787. if err != nil {
  788. t.Fatal(err)
  789. }
  790. resp, err := c.getIamPolicy(context.Background(), request)
  791. if st, ok := gstatus.FromError(err); !ok {
  792. t.Errorf("got error %v, expected grpc error", err)
  793. } else if c := st.Code(); c != errCode {
  794. t.Errorf("got error code %q, want %q", c, errCode)
  795. }
  796. _ = resp
  797. }
  798. func TestIamSetIamPolicy(t *testing.T) {
  799. var version int32 = 351608024
  800. var etag []byte = []byte("21")
  801. var expectedResponse = &iampb.Policy{
  802. Version: version,
  803. Etag: etag,
  804. }
  805. mockIam.err = nil
  806. mockIam.reqs = nil
  807. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  808. var formattedResource string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  809. var policy *iampb.Policy = &iampb.Policy{}
  810. var request = &iampb.SetIamPolicyRequest{
  811. Resource: formattedResource,
  812. Policy: policy,
  813. }
  814. c, err := NewIamClient(context.Background(), clientOpt)
  815. if err != nil {
  816. t.Fatal(err)
  817. }
  818. resp, err := c.setIamPolicy(context.Background(), request)
  819. if err != nil {
  820. t.Fatal(err)
  821. }
  822. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  823. t.Errorf("wrong request %q, want %q", got, want)
  824. }
  825. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  826. t.Errorf("wrong response %q, want %q)", got, want)
  827. }
  828. }
  829. func TestIamSetIamPolicyError(t *testing.T) {
  830. errCode := codes.PermissionDenied
  831. mockIam.err = gstatus.Error(errCode, "test error")
  832. var formattedResource string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  833. var policy *iampb.Policy = &iampb.Policy{}
  834. var request = &iampb.SetIamPolicyRequest{
  835. Resource: formattedResource,
  836. Policy: policy,
  837. }
  838. c, err := NewIamClient(context.Background(), clientOpt)
  839. if err != nil {
  840. t.Fatal(err)
  841. }
  842. resp, err := c.setIamPolicy(context.Background(), request)
  843. if st, ok := gstatus.FromError(err); !ok {
  844. t.Errorf("got error %v, expected grpc error", err)
  845. } else if c := st.Code(); c != errCode {
  846. t.Errorf("got error code %q, want %q", c, errCode)
  847. }
  848. _ = resp
  849. }
  850. func TestIamTestIamPermissions(t *testing.T) {
  851. var expectedResponse *iampb.TestIamPermissionsResponse = &iampb.TestIamPermissionsResponse{}
  852. mockIam.err = nil
  853. mockIam.reqs = nil
  854. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  855. var formattedResource string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  856. var permissions []string = nil
  857. var request = &iampb.TestIamPermissionsRequest{
  858. Resource: formattedResource,
  859. Permissions: permissions,
  860. }
  861. c, err := NewIamClient(context.Background(), clientOpt)
  862. if err != nil {
  863. t.Fatal(err)
  864. }
  865. resp, err := c.TestIamPermissions(context.Background(), request)
  866. if err != nil {
  867. t.Fatal(err)
  868. }
  869. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  870. t.Errorf("wrong request %q, want %q", got, want)
  871. }
  872. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  873. t.Errorf("wrong response %q, want %q)", got, want)
  874. }
  875. }
  876. func TestIamTestIamPermissionsError(t *testing.T) {
  877. errCode := codes.PermissionDenied
  878. mockIam.err = gstatus.Error(errCode, "test error")
  879. var formattedResource string = fmt.Sprintf("projects/%s/serviceAccounts/%s", "[PROJECT]", "[SERVICE_ACCOUNT]")
  880. var permissions []string = nil
  881. var request = &iampb.TestIamPermissionsRequest{
  882. Resource: formattedResource,
  883. Permissions: permissions,
  884. }
  885. c, err := NewIamClient(context.Background(), clientOpt)
  886. if err != nil {
  887. t.Fatal(err)
  888. }
  889. resp, err := c.TestIamPermissions(context.Background(), request)
  890. if st, ok := gstatus.FromError(err); !ok {
  891. t.Errorf("got error %v, expected grpc error", err)
  892. } else if c := st.Code(); c != errCode {
  893. t.Errorf("got error code %q, want %q", c, errCode)
  894. }
  895. _ = resp
  896. }
  897. func TestIamQueryGrantableRoles(t *testing.T) {
  898. var nextPageToken string = "nextPageToken-1530815211"
  899. var expectedResponse = &adminpb.QueryGrantableRolesResponse{
  900. NextPageToken: nextPageToken,
  901. }
  902. mockIam.err = nil
  903. mockIam.reqs = nil
  904. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  905. var fullResourceName string = "fullResourceName1300993644"
  906. var request = &adminpb.QueryGrantableRolesRequest{
  907. FullResourceName: fullResourceName,
  908. }
  909. c, err := NewIamClient(context.Background(), clientOpt)
  910. if err != nil {
  911. t.Fatal(err)
  912. }
  913. resp, err := c.QueryGrantableRoles(context.Background(), request)
  914. if err != nil {
  915. t.Fatal(err)
  916. }
  917. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  918. t.Errorf("wrong request %q, want %q", got, want)
  919. }
  920. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  921. t.Errorf("wrong response %q, want %q)", got, want)
  922. }
  923. }
  924. func TestIamQueryGrantableRolesError(t *testing.T) {
  925. errCode := codes.PermissionDenied
  926. mockIam.err = gstatus.Error(errCode, "test error")
  927. var fullResourceName string = "fullResourceName1300993644"
  928. var request = &adminpb.QueryGrantableRolesRequest{
  929. FullResourceName: fullResourceName,
  930. }
  931. c, err := NewIamClient(context.Background(), clientOpt)
  932. if err != nil {
  933. t.Fatal(err)
  934. }
  935. resp, err := c.QueryGrantableRoles(context.Background(), request)
  936. if st, ok := gstatus.FromError(err); !ok {
  937. t.Errorf("got error %v, expected grpc error", err)
  938. } else if c := st.Code(); c != errCode {
  939. t.Errorf("got error code %q, want %q", c, errCode)
  940. }
  941. _ = resp
  942. }
  943. func TestIamSignJwt(t *testing.T) {
  944. var keyId string = "keyId-1134673157"
  945. var signedJwt string = "signedJwt-979546844"
  946. var expectedResponse = &adminpb.SignJwtResponse{
  947. KeyId: keyId,
  948. SignedJwt: signedJwt,
  949. }
  950. mockIam.err = nil
  951. mockIam.reqs = nil
  952. mockIam.resps = append(mockIam.resps[:0], expectedResponse)
  953. var name string = "name3373707"
  954. var payload string = "payload-786701938"
  955. var request = &adminpb.SignJwtRequest{
  956. Name: name,
  957. Payload: payload,
  958. }
  959. c, err := NewIamClient(context.Background(), clientOpt)
  960. if err != nil {
  961. t.Fatal(err)
  962. }
  963. resp, err := c.SignJwt(context.Background(), request)
  964. if err != nil {
  965. t.Fatal(err)
  966. }
  967. if want, got := request, mockIam.reqs[0]; !proto.Equal(want, got) {
  968. t.Errorf("wrong request %q, want %q", got, want)
  969. }
  970. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  971. t.Errorf("wrong response %q, want %q)", got, want)
  972. }
  973. }
  974. func TestIamSignJwtError(t *testing.T) {
  975. errCode := codes.PermissionDenied
  976. mockIam.err = gstatus.Error(errCode, "test error")
  977. var name string = "name3373707"
  978. var payload string = "payload-786701938"
  979. var request = &adminpb.SignJwtRequest{
  980. Name: name,
  981. Payload: payload,
  982. }
  983. c, err := NewIamClient(context.Background(), clientOpt)
  984. if err != nil {
  985. t.Fatal(err)
  986. }
  987. resp, err := c.SignJwt(context.Background(), request)
  988. if st, ok := gstatus.FromError(err); !ok {
  989. t.Errorf("got error %v, expected grpc error", err)
  990. } else if c := st.Code(); c != errCode {
  991. t.Errorf("got error code %q, want %q", c, errCode)
  992. }
  993. _ = resp
  994. }