2020-08-21 11:48:24 -04:00
import { module , test } from 'qunit' ;
import { setupTest } from 'ember-qunit' ;
2020-08-24 18:03:45 -04:00
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage' ;
2020-08-21 11:48:24 -04:00
2020-08-24 18:03:45 -04:00
module ( 'Unit | Model | target' , function ( hooks ) {
2020-08-21 11:48:24 -04:00
setupTest ( hooks );
2020-08-24 18:03:45 -04:00
setupMirage ( hooks );
2020-08-21 11:48:24 -04:00
2020-09-16 14:29:02 -04:00
test ( 'it has a `hostSets` array of resolved model instances (if those instances are already in the store)' , function ( assert ) {
2020-08-25 10:39:48 -04:00
assert . expect ( 6 );
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
2021-09-22 19:15:09 -04:00
host_sources : [
2021-09-21 09:37:47 -04:00
{ host_source_id : '1' , host_catalog_id : '2' },
{ host_source_id : '3' , host_catalog_id : '2' },
2020-09-16 14:29:02 -04:00
],
},
},
2020-08-25 10:39:48 -04:00
});
const target = store . peekRecord ( 'target' , '123abc' );
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2021-09-22 19:15:09 -04:00
target . host_sources . length ,
2020-09-16 14:29:02 -04:00
2 ,
2021-09-22 19:15:09 -04:00
'Target has two entires in host_sources'
2020-09-16 14:29:02 -04:00
);
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2020-09-16 14:29:02 -04:00
target . hostSets . length ,
0 ,
'Target has no resolved hostSets because they are not loaded yet'
);
2020-08-25 10:39:48 -04:00
store . push ({
data : {
id : '1' ,
type : 'host-set' ,
2020-09-16 14:29:02 -04:00
attributes : {},
},
2020-08-25 10:39:48 -04:00
});
store . push ({
data : {
id : '3' ,
type : 'host-set' ,
2020-09-16 14:29:02 -04:00
attributes : {},
},
2020-08-25 10:39:48 -04:00
});
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2021-09-22 19:15:09 -04:00
target . host_sources . length ,
2020-09-16 14:29:02 -04:00
2 ,
2021-09-22 19:15:09 -04:00
'Target has two entires in host_sources'
2020-09-16 14:29:02 -04:00
);
2022-07-15 13:24:04 -07:00
assert . strictEqual (
target . hostSets . length ,
2 ,
'Target has two resolved hostSets'
);
2020-09-16 14:29:02 -04:00
assert . notOk (
target . hostSets [ 0 ]. hostCatalog ,
'Host catalog was not resolved because it is not loaded yet'
);
2020-08-25 10:39:48 -04:00
store . push ({
data : {
id : '2' ,
type : 'host-catalog' ,
2020-09-16 14:29:02 -04:00
attributes : {},
},
2020-08-25 10:39:48 -04:00
});
// eslint-disable-next-line no-self-assign
2021-09-22 19:15:09 -04:00
target . host_sources = target . host_sources ;
2020-08-25 10:39:48 -04:00
assert . ok ( target . hostSets [ 0 ]. hostCatalog , 'Host catalog is resolved' );
2020-08-21 11:48:24 -04:00
});
2020-08-24 18:03:45 -04:00
2021-09-21 14:53:56 -04:00
test ( 'it has an `addHostSources` method that targets a specific POST API endpoint and serialization' , async function ( assert ) {
2020-08-24 18:03:45 -04:00
assert . expect ( 1 );
2021-09-22 18:52:38 -04:00
this . server . post (
'/v1/targets/123abc:add-host-sources' ,
( schema , request ) => {
const body = JSON . parse ( request . requestBody );
assert . deepEqual ( body , {
2021-09-22 19:15:09 -04:00
host_source_ids : [ '123_abc' , 'foobar' ],
2021-09-22 18:52:38 -04:00
version : 1 ,
});
return { id : '123abc' };
}
);
2020-08-26 13:52:54 -04:00
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
name : 'Target' ,
description : 'Description' ,
2021-09-22 19:15:09 -04:00
host_sources : [
2021-09-21 09:37:47 -04:00
{ host_source_id : '1' , host_catalog_id : '2' },
{ host_source_id : '3' , host_catalog_id : '4' },
2020-08-26 13:52:54 -04:00
],
version : 1 ,
scope : {
scope_id : 'o_1' ,
2020-09-16 14:29:02 -04:00
type : 'scope' ,
},
},
},
2020-08-26 13:52:54 -04:00
});
const model = store . peekRecord ( 'target' , '123abc' );
2021-09-21 14:53:56 -04:00
await model . addHostSources ([ '123_abc' , 'foobar' ]);
2020-08-26 13:52:54 -04:00
});
2021-09-21 14:53:56 -04:00
test ( 'it has a `removeHostSources` method that targets a specific POST API endpoint and serialization' , async function ( assert ) {
2020-08-26 13:52:54 -04:00
assert . expect ( 1 );
2020-09-16 14:29:02 -04:00
this . server . post (
2021-09-22 18:52:38 -04:00
'/v1/targets/123abc:remove-host-sources' ,
2020-09-16 14:29:02 -04:00
( schema , request ) => {
const body = JSON . parse ( request . requestBody );
assert . deepEqual ( body , {
2021-09-22 19:15:09 -04:00
host_source_ids : [ '1' , '3' ],
2020-09-16 14:29:02 -04:00
version : 1 ,
});
2020-10-06 11:53:03 -04:00
return { id : '123abc' };
2020-09-16 14:29:02 -04:00
}
);
2020-08-24 18:03:45 -04:00
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
name : 'Target' ,
description : 'Description' ,
2021-09-22 19:15:09 -04:00
host_sources : [
2021-09-21 09:37:47 -04:00
{ host_source_id : '1' , host_catalog_id : '2' },
{ host_source_id : '3' , host_catalog_id : '4' },
2020-08-24 18:03:45 -04:00
],
version : 1 ,
scope : {
scope_id : 'o_1' ,
2020-09-16 14:29:02 -04:00
type : 'scope' ,
},
},
},
2020-08-24 18:03:45 -04:00
});
const model = store . peekRecord ( 'target' , '123abc' );
2021-09-21 14:53:56 -04:00
await model . removeHostSources ([ '1' , '3' ]);
2020-08-24 18:03:45 -04:00
});
2020-08-27 12:59:26 -04:00
2021-09-21 14:53:56 -04:00
test ( 'it has a `removeHostSource` method that deletes a single host set using `removeHostSources` method' , async function ( assert ) {
2020-08-27 12:59:26 -04:00
assert . expect ( 1 );
2020-09-16 14:29:02 -04:00
this . server . post (
2021-09-22 18:52:38 -04:00
'/v1/targets/123abc:remove-host-sources' ,
2020-09-16 14:29:02 -04:00
( schema , request ) => {
const body = JSON . parse ( request . requestBody );
assert . deepEqual ( body , {
2021-09-22 19:15:09 -04:00
host_source_ids : [ '3' ],
2020-09-16 14:29:02 -04:00
version : 1 ,
});
2020-10-06 11:53:03 -04:00
return { id : '123abc' };
2020-09-16 14:29:02 -04:00
}
);
2020-08-27 12:59:26 -04:00
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
name : 'Target' ,
description : 'Description' ,
2021-09-22 19:15:09 -04:00
host_sources : [
2021-09-21 09:37:47 -04:00
{ host_source_id : '1' , host_catalog_id : '2' },
{ host_source_id : '3' , host_catalog_id : '4' },
2020-08-27 12:59:26 -04:00
],
version : 1 ,
scope : {
scope_id : 'o_1' ,
2020-09-16 14:29:02 -04:00
type : 'scope' ,
},
},
},
2020-08-27 12:59:26 -04:00
});
const model = store . peekRecord ( 'target' , '123abc' );
2021-09-21 14:53:56 -04:00
await model . removeHostSource ( '3' );
2020-08-27 12:59:26 -04:00
});
2021-02-05 16:57:06 -05:00
test ( 'it has a `sessions` array of session instances associated with the target (if those instances are already in the store)' , function ( assert ) {
assert . expect ( 3 );
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
2021-03-04 15:47:38 -05:00
type : 'target' ,
},
2021-02-05 16:57:06 -05:00
});
const target = store . peekRecord ( 'target' , '123abc' );
2022-07-07 11:18:51 -07:00
assert . strictEqual ( target . sessions . length , 0 );
2021-02-05 16:57:06 -05:00
store . push ({
data : {
id : '1' ,
type : 'session' ,
2021-03-04 15:47:38 -05:00
attributes : { target_id : '123abc' },
},
2021-02-05 16:57:06 -05:00
});
store . push ({
data : {
id : '2' ,
type : 'session' ,
2021-03-04 15:47:38 -05:00
attributes : { target_id : '123abc' },
},
2021-02-05 16:57:06 -05:00
});
store . push ({
data : {
id : '3' ,
type : 'session' ,
2021-03-04 15:47:38 -05:00
attributes : { target_id : '456xyz' },
},
2021-02-05 16:57:06 -05:00
});
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2021-03-04 15:47:38 -05:00
store . peekAll ( 'session' ). length ,
3 ,
'There are 3 sessions loaded in the store'
);
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2021-02-05 16:57:06 -05:00
target . sessions . length ,
2 ,
'Target has two associated sessions loaded in the store'
);
});
2021-07-21 15:45:45 -04:00
2022-08-18 09:11:38 -07:00
test ( 'it has a `brokeredCredentialSources` array of resolved model instances (if those instances are already in the store)' , function ( assert ) {
2021-07-21 15:45:45 -04:00
assert . expect ( 4 );
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [{ value : '1' }, { value : '2' }],
2021-07-21 15:45:45 -04:00
},
},
});
const target = store . peekRecord ( 'target' , '123abc' );
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2022-08-15 09:48:12 -07:00
target . brokered_credential_source_ids . length ,
2021-07-21 15:45:45 -04:00
2 ,
2022-08-15 09:48:12 -07:00
'Target has two entires in brokered_credential_source_ids'
2021-07-21 15:45:45 -04:00
);
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2022-08-18 09:11:38 -07:00
target . brokeredCredentialSources . length ,
2021-07-21 15:45:45 -04:00
0 ,
2022-08-31 09:55:59 -07:00
'Target has no resolved credentialSources, because they are not yet loaded'
2021-07-21 15:45:45 -04:00
);
store . push ({
data : {
id : '1' ,
type : 'credential-library' ,
attributes : {},
},
});
store . push ({
data : {
id : '2' ,
type : 'credential-library' ,
attributes : {},
},
});
/* eslint-enable no-self-assign */
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2022-08-15 09:48:12 -07:00
target . brokered_credential_source_ids . length ,
2021-07-21 15:45:45 -04:00
2 ,
2022-08-15 09:48:12 -07:00
'Target has two entires in brokered_credential_source_ids'
2021-07-21 15:45:45 -04:00
);
2022-07-07 11:18:51 -07:00
assert . strictEqual (
2022-08-18 09:11:38 -07:00
target . brokeredCredentialSources . length ,
2021-07-21 15:45:45 -04:00
2 ,
2022-08-08 09:21:07 -07:00
'Target has two resolved credentialSources'
2021-07-21 15:45:45 -04:00
);
});
2022-08-18 09:11:38 -07:00
test ( 'it has an `addBrokeredCredentialSources` method that targets a specific POST API endpoint and serialization' , async function ( assert ) {
2021-07-21 15:45:45 -04:00
assert . expect ( 1 );
this . server . post (
2021-09-22 18:34:35 -04:00
'/v1/targets/123abc:add-credential-sources' ,
2021-07-21 15:45:45 -04:00
( schema , request ) => {
const body = JSON . parse ( request . requestBody );
assert . deepEqual ( body , {
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [ '123_abc' , 'foobar' ],
2021-07-21 15:45:45 -04:00
version : 1 ,
});
return { id : '123abc' };
}
);
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
name : 'Target' ,
description : 'Description' ,
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [{ value : '1' }, { value : '2' }],
2021-07-21 15:45:45 -04:00
version : 1 ,
scope : {
scope_id : 'o_1' ,
type : 'scope' ,
},
},
},
});
const model = store . peekRecord ( 'target' , '123abc' );
2022-08-18 09:11:38 -07:00
await model . addBrokeredCredentialSources ([ '123_abc' , 'foobar' ]);
2021-07-21 15:45:45 -04:00
});
2022-08-18 09:11:38 -07:00
test ( 'it has a `removeBrokeredCredentialSources` method that targets a specific POST API endpoint and serialization' , async function ( assert ) {
2021-07-21 15:45:45 -04:00
assert . expect ( 1 );
this . server . post (
2021-09-22 18:34:35 -04:00
'/v1/targets/123abc:remove-credential-sources' ,
2021-07-21 15:45:45 -04:00
( schema , request ) => {
const body = JSON . parse ( request . requestBody );
assert . deepEqual ( body , {
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [ '1' , '2' ],
2021-07-21 15:45:45 -04:00
version : 1 ,
});
return { id : '123abc' };
}
);
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
name : 'Target' ,
description : 'Description' ,
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [{ value : '1' }, { value : '2' }],
2021-07-21 15:45:45 -04:00
version : 1 ,
scope : {
scope_id : 'o_1' ,
type : 'scope' ,
},
},
},
});
const model = store . peekRecord ( 'target' , '123abc' );
2022-08-18 09:11:38 -07:00
await model . removeBrokeredCredentialSources ([ '1' , '2' ]);
2021-07-21 15:45:45 -04:00
});
2022-08-18 09:11:38 -07:00
test ( 'it has a `removeBrokeredCredentialSource` method that deletes a single credential library using `removeBrokeredCredentialSources` method' , async function ( assert ) {
2021-07-21 15:45:45 -04:00
assert . expect ( 1 );
this . server . post (
2021-09-22 18:34:35 -04:00
'/v1/targets/123abc:remove-credential-sources' ,
2021-07-21 15:45:45 -04:00
( schema , request ) => {
const body = JSON . parse ( request . requestBody );
assert . deepEqual ( body , {
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [ '2' ],
2021-07-21 15:45:45 -04:00
version : 1 ,
});
return { id : '123abc' };
}
);
const store = this . owner . lookup ( 'service:store' );
store . push ({
data : {
id : '123abc' ,
type : 'target' ,
attributes : {
name : 'Target' ,
description : 'Description' ,
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [{ value : '1' }, { value : '2' }],
2021-07-21 15:45:45 -04:00
version : 1 ,
scope : {
scope_id : 'o_1' ,
type : 'scope' ,
},
},
},
});
const model = store . peekRecord ( 'target' , '123abc' );
2022-08-18 09:11:38 -07:00
await model . removeBrokeredCredentialSource ( '2' );
2021-07-21 15:45:45 -04:00
});
2022-03-14 09:53:57 -07:00
test ( 'it has isSSH property and returns the expected values' , function ( assert ) {
assert . expect ( 2 );
const store = this . owner . lookup ( 'service:store' );
const modelSSH = store . createRecord ( 'target' , {
type : 'ssh' ,
});
assert . true ( modelSSH . isSSH );
assert . false ( modelSSH . isTCP );
});
test ( 'it has isTCP property and returns the expected values' , function ( assert ) {
assert . expect ( 2 );
const store = this . owner . lookup ( 'service:store' );
const modelTCP = store . createRecord ( 'target' , {
type : 'tcp' ,
});
assert . true ( modelTCP . isTCP );
assert . false ( modelTCP . isSSH );
});
2020-08-21 11:48:24 -04:00
});