-
Notifications
You must be signed in to change notification settings - Fork 4
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
[#175227073] Allow updating ADB2C User and introduce Token Name management on Create/Update operations #92
Merged
AleDore
merged 9 commits into
master
from
175227073_update_b2c_user_and_token_name_mgmt
Oct 15, 2020
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a82f342
[#175227073] Add UpdateUser &refactoring on Create
AleDore f5dc43a
[#175227073] remove config
AleDore fcaa6c4
[#175227073] Update User Refactoring
AleDore 924224c
[#175227073] Refactor over review
AleDore c56eee6
[#175227073] Minor refactor due to review
AleDore a174b87
[#175227073] insert UserUpdated definition
AleDore 1ec5272
[#175227073] fix operation return type
AleDore 8407bcb
[#175227073] Fix Non grouped import
AleDore 2948303
[#175227073] Refactor on test conditions
AleDore 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 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
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,147 @@ | ||||||||||||||||
// tslint:disable:no-any | ||||||||||||||||
|
||||||||||||||||
import { GraphRbacManagementClient } from "@azure/graph"; | ||||||||||||||||
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth"; | ||||||||||||||||
import { NonEmptyString } from "italia-ts-commons/lib/strings"; | ||||||||||||||||
import { UserCreated } from "../../generated/definitions/UserCreated"; | ||||||||||||||||
import { UserPayload } from "../../generated/definitions/UserPayload"; | ||||||||||||||||
import { UserStateEnum } from "../../generated/definitions/UserState"; | ||||||||||||||||
import { IServicePrincipalCreds } from "../../utils/apim"; | ||||||||||||||||
import { UpdateUserHandler } from "../handler"; | ||||||||||||||||
|
||||||||||||||||
const aTokenName = "ATokenName" as NonEmptyString; | ||||||||||||||||
const fakeServicePrincipalCredentials: IServicePrincipalCreds = { | ||||||||||||||||
clientId: "client-id", | ||||||||||||||||
secret: "secret", | ||||||||||||||||
tenantId: "tenant-id" | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
const fakeRequestPayload = { | ||||||||||||||||
email: "user@example.com", | ||||||||||||||||
first_name: "first-name", | ||||||||||||||||
last_name: "family-name", | ||||||||||||||||
token_name: aTokenName | ||||||||||||||||
} as UserPayload; | ||||||||||||||||
|
||||||||||||||||
const fakeObjectId = "ADB2C-user"; | ||||||||||||||||
|
||||||||||||||||
const mockLoginWithServicePrincipalSecret = jest.spyOn( | ||||||||||||||||
msRestNodeAuth, | ||||||||||||||||
"loginWithServicePrincipalSecret" | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
jest.mock("@azure/graph"); | ||||||||||||||||
jest.mock("@azure/arm-apimanagement"); | ||||||||||||||||
const mockGraphRbacManagementClient = GraphRbacManagementClient as jest.Mock; | ||||||||||||||||
const mockLog = jest.fn(); | ||||||||||||||||
const mockGetToken = jest.fn(); | ||||||||||||||||
|
||||||||||||||||
mockLoginWithServicePrincipalSecret.mockImplementation(() => { | ||||||||||||||||
return Promise.resolve({ getToken: mockGetToken }); | ||||||||||||||||
}); | ||||||||||||||||
mockGetToken.mockImplementation(() => { | ||||||||||||||||
return Promise.resolve(undefined); | ||||||||||||||||
}); | ||||||||||||||||
const mockUsersCreate = jest.fn(); | ||||||||||||||||
|
||||||||||||||||
mockGraphRbacManagementClient.mockImplementation(() => ({ | ||||||||||||||||
users: { | ||||||||||||||||
list: jest.fn(() => | ||||||||||||||||
Promise.resolve([ | ||||||||||||||||
{ | ||||||||||||||||
email: "user@example.com" | ||||||||||||||||
} | ||||||||||||||||
]) | ||||||||||||||||
), | ||||||||||||||||
update: mockUsersCreate | ||||||||||||||||
} | ||||||||||||||||
})); | ||||||||||||||||
|
||||||||||||||||
const fakeAdb2cExtensionAppClientId = "extension-client-id" as NonEmptyString; | ||||||||||||||||
|
||||||||||||||||
const mockedContext = { log: { error: mockLog } }; | ||||||||||||||||
|
||||||||||||||||
describe("UpdateUser", () => { | ||||||||||||||||
it("should return an internal error response if the ADB2C client can not be got", async () => { | ||||||||||||||||
mockLoginWithServicePrincipalSecret.mockImplementationOnce(() => | ||||||||||||||||
Promise.reject("Error from ApiManagementClient constructor") | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
const updateUserHandler = UpdateUserHandler( | ||||||||||||||||
fakeServicePrincipalCredentials, | ||||||||||||||||
fakeAdb2cExtensionAppClientId | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
const response = await updateUserHandler( | ||||||||||||||||
mockedContext as any, | ||||||||||||||||
undefined as any, | ||||||||||||||||
undefined as any | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
expect(response.kind).toEqual("IResponseErrorInternal"); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
it("should return an internal error response if the ADB2C client can not update the user", async () => { | ||||||||||||||||
mockUsersCreate.mockImplementationOnce(() => | ||||||||||||||||
Promise.reject("Users update error") | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
const updateUserHandler = UpdateUserHandler( | ||||||||||||||||
fakeServicePrincipalCredentials, | ||||||||||||||||
fakeAdb2cExtensionAppClientId | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
const response = await updateUserHandler( | ||||||||||||||||
mockedContext as any, | ||||||||||||||||
undefined as any, | ||||||||||||||||
fakeRequestPayload | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
expect(response.kind).toEqual("IResponseErrorInternal"); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we relate the behaviour of this test with the test conditions?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2948303 |
||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
it("should return the user updated", async () => { | ||||||||||||||||
const fakeApimUser = { | ||||||||||||||||
email: fakeRequestPayload.email, | ||||||||||||||||
firstName: fakeRequestPayload.first_name, | ||||||||||||||||
id: "user-id", | ||||||||||||||||
identities: [ | ||||||||||||||||
{ | ||||||||||||||||
id: fakeObjectId, | ||||||||||||||||
provider: "AadB2C" | ||||||||||||||||
} | ||||||||||||||||
], | ||||||||||||||||
lastName: fakeRequestPayload.last_name, | ||||||||||||||||
name: fakeObjectId, | ||||||||||||||||
registrationDate: new Date(), | ||||||||||||||||
state: UserStateEnum.active, | ||||||||||||||||
type: "Microsoft.ApiManagement/service/users" | ||||||||||||||||
}; | ||||||||||||||||
const expectedUpdatedUser: UserCreated = { | ||||||||||||||||
email: fakeApimUser.email, | ||||||||||||||||
first_name: fakeApimUser.firstName, | ||||||||||||||||
id: fakeApimUser.name, | ||||||||||||||||
last_name: fakeApimUser.lastName, | ||||||||||||||||
token_name: aTokenName | ||||||||||||||||
}; | ||||||||||||||||
mockUsersCreate.mockImplementationOnce(() => | ||||||||||||||||
Promise.resolve({ objectId: fakeObjectId }) | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
const updateUserHandler = UpdateUserHandler( | ||||||||||||||||
fakeServicePrincipalCredentials, | ||||||||||||||||
fakeAdb2cExtensionAppClientId | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
const response = await updateUserHandler( | ||||||||||||||||
mockedContext as any, | ||||||||||||||||
undefined as any, | ||||||||||||||||
fakeRequestPayload | ||||||||||||||||
); | ||||||||||||||||
expect(response).toEqual({ | ||||||||||||||||
apply: expect.any(Function), | ||||||||||||||||
kind: "IResponseSuccessJson", | ||||||||||||||||
value: expectedUpdatedUser | ||||||||||||||||
}); | ||||||||||||||||
}); | ||||||||||||||||
}); |
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,20 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "function", | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req", | ||
"route": "adm/users", | ||
"methods": [ | ||
"put" | ||
] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/UpdateUser/index.js" | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be this
mockUsersUpdate
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2948303