Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
MonPote committed Apr 29, 2024
1 parent e9699ba commit df49f34
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
19 changes: 12 additions & 7 deletions src/react/ui-elements/SelectAccountIAMRole.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ const AssumeDefaultIAMRole = ({
(acc) => acc.name === defaultValue?.accountName,
);

/**
* This set state will trigger a warning because it's not in a useEffect.
* This is fine because the set state is under an if and it should not be called too many times.
* The only time it could break is if for some reason the user use an account that is named like
* INTERNAL_DEFAULT_ACCOUNT_NAME_FOR_INITIALIZATION and use the component with a defaultValue.
*/
setAssumeRole({
roleArn: acc?.preferredAssumableRoleArn ?? '',
});
Expand Down Expand Up @@ -163,18 +169,17 @@ const SelectAccountIAMRoleWithAccount = (
const rolesQuery = getListRolesQuery(accountName, IAMClient);
const queryClient = useQueryClient();

const id1 = regexArn.exec(assumedRole?.AssumedRoleUser?.Arn)?.groups?.[
'account_id'
];
const id2 = regexArn.exec(account?.preferredAssumableRoleArn)?.groups?.[
'account_id'
];
const assumedRoleAccountId = regexArn.exec(assumedRole?.AssumedRoleUser?.Arn)
?.groups?.['account_id'];
const selectedAccountId = regexArn.exec(account?.preferredAssumableRoleArn)
?.groups?.['account_id'];

/**
* When we change account, it will take some time to assume the role for the new account.
* We need this check to make sure we don't show the roles for the old account.
*/
const assumedRoleAccountMatchSelectedAccount = id1 === id2;
const assumedRoleAccountMatchSelectedAccount =
assumedRoleAccountId === selectedAccountId;

const listRolesQuery = {
...rolesQuery,
Expand Down
52 changes: 26 additions & 26 deletions src/react/ui-elements/__tests__/SelectAccountIAMRole.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ const genFn = (getPayloadFn: jest.Mock) => {
return rest.post(`${TEST_API_BASE_URL}/`, (req, res, ctx) => {
//@ts-ignore
const params = new URLSearchParams(req.body);
console.log('reqbody', req.body);
getPayloadFn(params);
// Assume Role

if (params.get('Action') === 'AssumeRoleWithWebIdentity') {
console.log('return assume role');
return res(
ctx.status(200),
ctx.xml(`
Expand Down Expand Up @@ -217,7 +215,7 @@ describe('SelectAccountIAMRole', () => {
screen.getByRole('option', {
name: new RegExp(name, 'i'),
}),
loading: () => screen.getByText(/Loading.../i),
loadingAccount: () => screen.getByText(/Loading.../i),
};
beforeAll(() => {
server.listen({ onUnhandledRequest: 'error' });
Expand Down Expand Up @@ -248,7 +246,7 @@ describe('SelectAccountIAMRole', () => {

await userEvent.click(seletors.selectOption(/no\-bucket/i));

await waitForElementToBeRemoved(() => seletors.loading());
await waitForElementToBeRemoved(() => seletors.loadingAccount());

await waitFor(() => {
expect(seletors.roleSelect()).toBeInTheDocument();
Expand Down Expand Up @@ -313,6 +311,29 @@ describe('SelectAccountIAMRole', () => {
debug();
});

it('renders with wrong default value', async () => {
const getPayloadFn = jest.fn();
server.use(genFn(getPayloadFn));
const onChange = jest.fn();
render(
<LocalWrapper>
<SelectAccountIAMRole
onChange={onChange}
defaultValue={{
accountName: 'william1',
roleName: 'backbeat-gc-1',
}}
/>
</LocalWrapper>,
);

await waitFor(() => {
expect(seletors.accountSelect()).toBeInTheDocument();
});

expect(seletors.accountSelect()).toBeInTheDocument();
});

it('renders with hidden account roles', async () => {
const getPayloadFn = jest.fn();
server.use(genFn(getPayloadFn));
Expand Down Expand Up @@ -348,25 +369,4 @@ describe('SelectAccountIAMRole', () => {

debug();
});

it('renders with wrong default value', async () => {
const getPayloadFn = jest.fn();
server.use(genFn(getPayloadFn));
const onChange = jest.fn();
render(
<LocalWrapper>
<SelectAccountIAMRole
onChange={onChange}
defaultValue={{
accountName: 'william1',
roleName: 'backbeat-gc-1',
}}
/>
</LocalWrapper>,
);

await waitFor(() => {
expect(seletors.accountSelect()).toBeInTheDocument();
});
});
});

0 comments on commit df49f34

Please sign in to comment.