-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): adminDeleteUserAttributes full support
- Loading branch information
Showing
6 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
integration-tests/aws-sdk/adminDeleteUserAttributes.test.ts
This file contains 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,56 @@ | ||
import { UUID } from "../../src/__tests__/patterns"; | ||
import { withCognitoSdk } from "./setup"; | ||
|
||
describe( | ||
"CognitoIdentityServiceProvider.adminDeleteUserAttributes", | ||
withCognitoSdk((Cognito) => { | ||
it("updates a user's attributes", async () => { | ||
const client = Cognito(); | ||
|
||
await client | ||
.adminCreateUser({ | ||
UserAttributes: [ | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "custom:example", Value: "1" }, | ||
], | ||
Username: "abc", | ||
UserPoolId: "test", | ||
DesiredDeliveryMediums: ["EMAIL"], | ||
}) | ||
.promise(); | ||
|
||
let user = await client | ||
.adminGetUser({ | ||
UserPoolId: "test", | ||
Username: "abc", | ||
}) | ||
.promise(); | ||
|
||
expect(user.UserAttributes).toEqual([ | ||
{ Name: "sub", Value: expect.stringMatching(UUID) }, | ||
{ Name: "email", Value: "[email protected]" }, | ||
{ Name: "custom:example", Value: "1" }, | ||
]); | ||
|
||
await client | ||
.adminDeleteUserAttributes({ | ||
UserPoolId: "test", | ||
Username: "abc", | ||
UserAttributeNames: ["custom:example"], | ||
}) | ||
.promise(); | ||
|
||
user = await client | ||
.adminGetUser({ | ||
UserPoolId: "test", | ||
Username: "abc", | ||
}) | ||
.promise(); | ||
|
||
expect(user.UserAttributes).toEqual([ | ||
{ Name: "sub", Value: expect.stringMatching(UUID) }, | ||
{ Name: "email", Value: "[email protected]" }, | ||
]); | ||
}); | ||
}) | ||
); |
This file contains 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
This file contains 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,60 @@ | ||
import { ClockFake } from "../__tests__/clockFake"; | ||
import { newMockCognitoService } from "../__tests__/mockCognitoService"; | ||
import { newMockUserPoolService } from "../__tests__/mockUserPoolService"; | ||
import { TestContext } from "../__tests__/testContext"; | ||
import { NotAuthorizedError } from "../errors"; | ||
import { UserPoolService } from "../services"; | ||
import { attribute } from "../services/userPoolService"; | ||
import { | ||
AdminDeleteUserAttributes, | ||
AdminDeleteUserAttributesTarget, | ||
} from "./adminDeleteUserAttributes"; | ||
import * as TDB from "../__tests__/testDataBuilder"; | ||
|
||
describe("AdminDeleteUserAttributes target", () => { | ||
let adminDeleteUserAttributes: AdminDeleteUserAttributesTarget; | ||
let mockUserPoolService: jest.Mocked<UserPoolService>; | ||
let clock: ClockFake; | ||
|
||
beforeEach(() => { | ||
mockUserPoolService = newMockUserPoolService(); | ||
clock = new ClockFake(new Date()); | ||
adminDeleteUserAttributes = AdminDeleteUserAttributes({ | ||
clock, | ||
cognito: newMockCognitoService(mockUserPoolService), | ||
}); | ||
}); | ||
|
||
it("throws if the user doesn't exist", async () => { | ||
await expect( | ||
adminDeleteUserAttributes(TestContext, { | ||
UserPoolId: "test", | ||
Username: "abc", | ||
UserAttributeNames: ["custom:example"], | ||
}) | ||
).rejects.toEqual(new NotAuthorizedError()); | ||
}); | ||
|
||
it("saves the updated attributes on the user", async () => { | ||
const user = TDB.user({ | ||
Attributes: [ | ||
attribute("email", "[email protected]"), | ||
attribute("custom:example", "1"), | ||
], | ||
}); | ||
|
||
mockUserPoolService.getUserByUsername.mockResolvedValue(user); | ||
|
||
await adminDeleteUserAttributes(TestContext, { | ||
UserPoolId: "test", | ||
Username: "abc", | ||
UserAttributeNames: ["custom:example"], | ||
}); | ||
|
||
expect(mockUserPoolService.saveUser).toHaveBeenCalledWith(TestContext, { | ||
...user, | ||
Attributes: [attribute("email", "[email protected]")], | ||
UserLastModifiedDate: clock.get(), | ||
}); | ||
}); | ||
}); |
This file contains 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,38 @@ | ||
import { | ||
AdminDeleteUserAttributesRequest, | ||
AdminDeleteUserAttributesResponse, | ||
} from "aws-sdk/clients/cognitoidentityserviceprovider"; | ||
import { NotAuthorizedError } from "../errors"; | ||
import { Services } from "../services"; | ||
import { attributesRemove } from "../services/userPoolService"; | ||
import { Target } from "./router"; | ||
|
||
export type AdminDeleteUserAttributesTarget = Target< | ||
AdminDeleteUserAttributesRequest, | ||
AdminDeleteUserAttributesResponse | ||
>; | ||
|
||
type AdminDeleteUserAttributesServices = Pick<Services, "clock" | "cognito">; | ||
|
||
export const AdminDeleteUserAttributes = | ||
({ | ||
clock, | ||
cognito, | ||
}: AdminDeleteUserAttributesServices): AdminDeleteUserAttributesTarget => | ||
async (ctx, req) => { | ||
const userPool = await cognito.getUserPool(ctx, req.UserPoolId); | ||
const user = await userPool.getUserByUsername(ctx, req.Username); | ||
if (!user) { | ||
throw new NotAuthorizedError(); | ||
} | ||
|
||
const updatedUser = { | ||
...user, | ||
Attributes: attributesRemove(user.Attributes, ...req.UserAttributeNames), | ||
UserLastModifiedDate: clock.get(), | ||
}; | ||
|
||
await userPool.saveUser(ctx, updatedUser); | ||
|
||
return {}; | ||
}; |
This file contains 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