Skip to content
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

fix(core): Don't abort import if user already owns credential #14027

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/cli/src/commands/import/credentials.ts
Original file line number Diff line number Diff line change
@@ -154,6 +154,11 @@ export class ImportCredentialsCommand extends BaseCommand {
continue;
}

// check if credential is already owned by the provided userId
if (userId && user?.id === userId) {
continue;
}

if (ownerProject.id !== projectId) {
const currentOwner =
ownerProject.type === 'personal'
67 changes: 67 additions & 0 deletions packages/cli/test/integration/commands/credentials.cmd.test.ts
Original file line number Diff line number Diff line change
@@ -296,6 +296,73 @@ test('`import:credential --projectId ...` should fail if the credential already
});
});

test('`import:credential --userId ...` should succeed if the credential already exists and is already owned by the user', async () => {
//
// ARRANGE
//
const owner = await createOwner();
const ownerProject = await getPersonalProject(owner);
const member = await createMember();
const memberProject = await getPersonalProject(member);

// import credential the first time, assigning it to the owner
await command.run([
'--input=./test/integration/commands/import-credentials/credentials.json',
`--userId=${owner.id}`,
]);

// making sure the import worked
const before = {
credentials: await getAllCredentials(),
sharings: await getAllSharedCredentials(),
};
expect(before).toMatchObject({
credentials: [expect.objectContaining({ id: '123', name: 'cred-aws-test' })],
sharings: [
expect.objectContaining({
credentialsId: '123',
projectId: ownerProject.id,
role: 'credential:owner',
}),
],
});

//
// ACT
//

// Import again changing nothing but the name and passing `--userId`
await command.run([
'--input=./test/integration/commands/import-credentials/credentials-updated.json',
`--userId=${owner.id}`,
]);

//
// ASSERT
//
const after = {
credentials: await getAllCredentials(),
sharings: await getAllSharedCredentials(),
};

expect(after).toMatchObject({
credentials: [
expect.objectContaining({
id: '123',
// only the name was updated
name: 'cred-aws-prod',
}),
],
sharings: [
expect.objectContaining({
credentialsId: '123',
projectId: ownerProject.id,
role: 'credential:owner',
}),
],
});
});

test('`import:credential --projectId ... --userId ...` fails explaining that only one of the options can be used at a time', async () => {
await expect(
command.run([