Skip to content

Commit

Permalink
[#175461739] Enable correct response by UpdateUser (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleDore authored Oct 27, 2020
1 parent 7703f15 commit a3ef2a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
21 changes: 12 additions & 9 deletions UpdateUser/__tests__/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { GraphRbacManagementClient } from "@azure/graph";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { NonEmptyString } from "italia-ts-commons/lib/strings";
import { EmailAddress } from "../../generated/definitions/EmailAddress";
import { UserCreated } from "../../generated/definitions/UserCreated";
import { UserStateEnum } from "../../generated/definitions/UserState";
import { UserUpdated } from "../../generated/definitions/UserUpdated";
import { UserUpdatePayload } from "../../generated/definitions/UserUpdatePayload";
import { IServicePrincipalCreds } from "../../utils/apim";
import { UpdateUserHandler } from "../handler";
Expand Down Expand Up @@ -46,16 +46,19 @@ mockGetToken.mockImplementation(() => {
return Promise.resolve(undefined);
});
const mockUsersUpdate = jest.fn();
const mockListUsers = jest.fn();
mockListUsers.mockImplementation(() =>
Promise.resolve([
{
email: aUserEmail,
objectId: fakeObjectId
}
])
);

mockGraphRbacManagementClient.mockImplementation(() => ({
users: {
list: jest.fn(() =>
Promise.resolve([
{
email: "user@example.com"
}
])
),
list: mockListUsers,
update: mockUsersUpdate
}
}));
Expand Down Expand Up @@ -126,7 +129,7 @@ describe("UpdateUser", () => {
state: UserStateEnum.active,
type: "Microsoft.ApiManagement/service/users"
};
const expectedUpdatedUser: UserCreated = {
const expectedUpdatedUser: UserUpdated = {
email: fakeApimUser.email,
first_name: fakeApimUser.firstName,
id: fakeApimUser.name,
Expand Down
43 changes: 26 additions & 17 deletions UpdateUser/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ const updateUser = (
})
),
toError
).chain(updateUserResponse =>
fromEither(
UserUpdated.decode({
email,
first_name: userPayload.first_name,
id: updateUserResponse.objectId,
last_name: userPayload.last_name,
token_name: userPayload.token_name
}).mapLeft(
errs =>
new Error(`Error decoding UserUpdated ERROR=${readableReport(errs)}`)
).chain(() =>
getUserFromList(client, email).chain(userResponse =>
fromEither(
UserUpdated.decode({
email,
first_name: userPayload.first_name,
id: userResponse.objectId,
last_name: userPayload.last_name,
token_name: userPayload.token_name
}).mapLeft(
errs =>
new Error(
`Error decoding UserUpdated ERROR=${readableReport(errs)}`
)
)
)
)
);
Expand All @@ -103,19 +107,24 @@ export function UpdateUserHandler(
)
.chain(graphRbacManagementClient =>
getUserFromList(graphRbacManagementClient, email)
.mapLeft(userFromListError =>
internalErrorHandler(
"Could not retrieve user from list on the ADB2C",
userFromListError
)
)
.chain(user =>
updateUser(
graphRbacManagementClient,
email,
user,
adb2cTokenAttributeName,
userPayload
)
)
.mapLeft(error =>
internalErrorHandler(
"Could not update the user on the ADB2C",
new Error(JSON.stringify(error))
).mapLeft(updateUserError =>
internalErrorHandler(
"Could not update the user on the ADB2C",
new Error(JSON.stringify(updateUserError))
)
)
)
)
Expand Down

0 comments on commit a3ef2a9

Please sign in to comment.