-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test: improve test coverage for resourceManager #10344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+224
−0
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fa9e53
test: improve test coverage for resourceManager
joehan e9b9da2
fix: resolve ESLint errors in tests
joehan bb71141
Merge branch 'main' into test/resourceManager
joehan 72715b3
fix: remove type escape hatches in nock configurations in resourceMan…
joehan de06991
fix: use arrow functions for nock body matching in resourceManager tests
joehan 019768e
format
joehan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| import { expect } from "chai"; | ||
| import * as nock from "nock"; | ||
| import { addServiceAccountToRoles, serviceAccountHasRoles } from "./resourceManager"; | ||
| import { Policy } from "./iam"; | ||
|
|
||
| const PROJECT_ID = "test-project"; | ||
| const SERVICE_ACCOUNT_NAME = "test-sa"; | ||
| const FULL_SA_NAME = `projects/${PROJECT_ID}/serviceAccounts/${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`; | ||
| const MEMBER_NAME = `serviceAccount:${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`; | ||
|
|
||
| describe("resourceManager", () => { | ||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| describe("addServiceAccountToRoles", () => { | ||
| it("should add roles when skipAccountLookup is true", async () => { | ||
| const initialPolicy: Policy = { | ||
| bindings: [], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| const expectedPolicy: Policy = { | ||
| bindings: [ | ||
| { | ||
| role: "roles/viewer", | ||
| members: [MEMBER_NAME], | ||
| }, | ||
| ], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:getIamPolicy`) | ||
| .reply(200, initialPolicy); | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:setIamPolicy`, { | ||
|
Check failure on line 40 in src/gcp/resourceManager.spec.ts
|
||
| policy: expectedPolicy, | ||
| updateMask: "bindings", | ||
| } as unknown as Record<string, unknown>) | ||
| .reply(200, expectedPolicy); | ||
|
|
||
| const result = await addServiceAccountToRoles( | ||
| PROJECT_ID, | ||
| `${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`, | ||
| ["roles/viewer"], | ||
| true, | ||
| ); | ||
|
|
||
| expect(result).to.deep.equal(expectedPolicy); | ||
| }); | ||
|
|
||
| it("should add roles when skipAccountLookup is false", async () => { | ||
| const initialPolicy: Policy = { | ||
| bindings: [], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| const expectedPolicy: Policy = { | ||
| bindings: [ | ||
| { | ||
| role: "roles/viewer", | ||
| members: [MEMBER_NAME], | ||
| }, | ||
| ], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| nock("https://iam.googleapis.com") | ||
| .get( | ||
| `/v1/projects/${PROJECT_ID}/serviceAccounts/${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`, | ||
| ) | ||
| .reply(200, { name: FULL_SA_NAME }); | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:getIamPolicy`) | ||
| .reply(200, initialPolicy); | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:setIamPolicy`, { | ||
|
Check failure on line 85 in src/gcp/resourceManager.spec.ts
|
||
| policy: expectedPolicy, | ||
| updateMask: "bindings", | ||
| } as unknown as Record<string, unknown>) | ||
|
joehan marked this conversation as resolved.
Outdated
|
||
| .reply(200, expectedPolicy); | ||
|
|
||
| const result = await addServiceAccountToRoles( | ||
| PROJECT_ID, | ||
| SERVICE_ACCOUNT_NAME, | ||
| ["roles/viewer"], | ||
| false, | ||
| ); | ||
|
|
||
| expect(result).to.deep.equal(expectedPolicy); | ||
| }); | ||
|
|
||
| it("should not duplicate roles if already present", async () => { | ||
| const initialPolicy: Policy = { | ||
| bindings: [ | ||
| { | ||
| role: "roles/viewer", | ||
| members: [MEMBER_NAME], | ||
| }, | ||
| ], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:getIamPolicy`) | ||
| .reply(200, initialPolicy); | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:setIamPolicy`, { | ||
|
Check failure on line 118 in src/gcp/resourceManager.spec.ts
|
||
| policy: initialPolicy, | ||
| updateMask: "bindings", | ||
| } as unknown as Record<string, unknown>) | ||
|
joehan marked this conversation as resolved.
Outdated
|
||
| .reply(200, initialPolicy); | ||
|
|
||
| const result = await addServiceAccountToRoles( | ||
| PROJECT_ID, | ||
| `${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`, | ||
| ["roles/viewer"], | ||
| true, | ||
| ); | ||
|
|
||
| expect(result).to.deep.equal(initialPolicy); | ||
| }); | ||
| }); | ||
|
|
||
| describe("serviceAccountHasRoles", () => { | ||
| it("should return true if account has all roles", async () => { | ||
| const policy: Policy = { | ||
| bindings: [ | ||
| { | ||
| role: "roles/viewer", | ||
| members: [MEMBER_NAME], | ||
| }, | ||
| { | ||
| role: "roles/editor", | ||
| members: [MEMBER_NAME], | ||
| }, | ||
| ], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:getIamPolicy`) | ||
| .reply(200, policy); | ||
|
|
||
| const result = await serviceAccountHasRoles( | ||
| PROJECT_ID, | ||
| `${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`, | ||
| ["roles/viewer", "roles/editor"], | ||
| true, | ||
| ); | ||
|
|
||
| expect(result).to.be.true; | ||
| }); | ||
|
|
||
| it("should return false if account is missing a role", async () => { | ||
| const policy: Policy = { | ||
| bindings: [ | ||
| { | ||
| role: "roles/viewer", | ||
| members: [MEMBER_NAME], | ||
| }, | ||
| ], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:getIamPolicy`) | ||
| .reply(200, policy); | ||
|
|
||
| const result = await serviceAccountHasRoles( | ||
| PROJECT_ID, | ||
| `${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`, | ||
| ["roles/viewer", "roles/editor"], | ||
| true, | ||
| ); | ||
|
|
||
| expect(result).to.be.false; | ||
| }); | ||
|
|
||
| it("should return false if role exists but member is missing", async () => { | ||
| const policy: Policy = { | ||
| bindings: [ | ||
| { | ||
| role: "roles/viewer", | ||
| members: ["serviceAccount:other@example.com"], | ||
| }, | ||
| ], | ||
| etag: "etag", | ||
| version: 1, | ||
| }; | ||
|
|
||
| nock("https://cloudresourcemanager.googleapis.com") | ||
| .post(`/v1/projects/${PROJECT_ID}:getIamPolicy`) | ||
| .reply(200, policy); | ||
|
|
||
| const result = await serviceAccountHasRoles( | ||
| PROJECT_ID, | ||
| `${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com`, | ||
| ["roles/viewer"], | ||
| true, | ||
| ); | ||
|
|
||
| expect(result).to.be.false; | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.