test: 💍 Add delete tests for JSON Credentials (#1526)

* test: 💍 Add delete tests for JSON Credentials

✅ Closes: ICU-6806

* refactor: 💡 update JSON variable names
This commit is contained in:
Cameron Perera authored and GitHub committed 2022-12-14 13:36:30 -06:00
1 parent f6cad6a9ee
commit c933aa8840
1 file changed
+94
@@ -13,6 +13,7 @@ module(
let getUsernamePasswordCredentialCount;
let getUsernameKeyPairCredentialCount;
let getJSONCredentialCount;
const instances = {
scopes: {
@@ -28,6 +29,7 @@ module(
credentials: null,
usernamePasswordCredential: null,
usernameKeyPairCredential: null,
jsonCredential: null,
};
hooks.beforeEach(function () {
@@ -54,6 +56,11 @@ module(
credentialStore: instances.staticCredentialStore,
type: 'ssh_private_key',
});
instances.jsonCredential = this.server.create('credential', {
scope: instances.scopes.project,
credentialStore: instances.staticCredentialStore,
type: 'json',
});
// Generate route URLs for resources
urls.projectScope = `/scopes/${instances.scopes.project.id}`;
urls.credentialStores = `${urls.projectScope}/credential-stores`;
@@ -61,6 +68,7 @@ module(
urls.credentials = `${urls.staticCredentialStore}/credentials`;
urls.usernamePasswordCredential = `${urls.credentials}/${instances.usernamePasswordCredential.id}`;
urls.usernameKeyPairCredential = `${urls.credentials}/${instances.usernameKeyPairCredential.id}`;
urls.jsonCredential = `${urls.credentials}/${instances.jsonCredential.id}`;
// Generate resource counter
getUsernamePasswordCredentialCount = () => {
return this.server.schema.credentials.where({
@@ -71,6 +79,10 @@ module(
return this.server.schema.credentials.where({ type: 'ssh_private_key' })
.length;
};
getJSONCredentialCount = () => {
return this.server.schema.credentials.where({ type: 'json' })
.length;
};
authenticateSession({});
});
@@ -100,6 +112,19 @@ module(
);
});
test('can delete JSON credential', async function (assert) {
assert.expect(2);
const jsonCredentialCount =
getJSONCredentialCount();
await visit(urls.jsonCredential);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(
getJSONCredentialCount(),
jsonCredentialCount - 1
);
});
test('cannot delete a username & password credential without proper authorization', async function (assert) {
assert.expect(3);
const usernamePasswordCredentialCount =
@@ -138,6 +163,25 @@ module(
);
});
test('cannot delete a JSON credential without proper authorization', async function (assert) {
assert.expect(3);
const jsonCredentialCount =
getJSONCredentialCount();
instances.jsonCredential.authorized_actions =
instances.jsonCredential.authorized_actions.filter(
(item) => item !== 'delete'
);
await visit(urls.jsonCredential);
assert.strictEqual(currentURL(), urls.jsonCredential);
assert
.dom('.rose-layout-page-actions .rose-dropdown-button-danger')
.doesNotExist();
assert.strictEqual(
getJSONCredentialCount(),
jsonCredentialCount
);
});
test('can accept delete username & password credential via dialog', async function (assert) {
assert.expect(2);
const confirmService = this.owner.lookup('service:confirm');
@@ -170,6 +214,22 @@ module(
);
});
test('can accept delete JSON credential via dialog', async function (assert) {
assert.expect(2);
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const jsonCredentialCount =
getJSONCredentialCount();
await visit(urls.jsonCredential);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
await click('.rose-dialog footer .rose-button-primary');
assert.strictEqual(currentURL(), urls.credentials);
assert.strictEqual(
getJSONCredentialCount(),
jsonCredentialCount - 1
);
});
test('can cancel delete username & password credential via dialog', async function (assert) {
assert.expect(2);
const confirmService = this.owner.lookup('service:confirm');
@@ -202,6 +262,22 @@ module(
);
});
test('can cancel delete JSON credential via dialog', async function (assert) {
assert.expect(2);
const confirmService = this.owner.lookup('service:confirm');
confirmService.enabled = true;
const jsonCredentialCount =
getJSONCredentialCount();
await visit(urls.jsonCredential);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
await click('.rose-dialog footer .rose-button-secondary');
assert.strictEqual(currentURL(), urls.jsonCredential);
assert.strictEqual(
getJSONCredentialCount(),
jsonCredentialCount
);
});
test('deleting a username & password credential which errors displays error message', async function (assert) {
assert.expect(1);
this.server.del('/credentials/:id', () => {
@@ -237,5 +313,23 @@ module(
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.ok(find('[role="alert"]').textContent.trim(), 'Oops.');
});
test('deleting a JSON credential which errors displays error message', async function (assert) {
assert.expect(1);
this.server.del('/credentials/:id', () => {
return new Response(
490,
{},
{
status: 490,
code: 'error',
message: 'Oops.',
}
);
});
await visit(urls.jsonCredential);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.ok(find('[role="alert"]').textContent.trim(), 'Oops.');
});
}
);