2023-03-01 09:39:41 -06:00
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-08-24 18:03:45 -04:00
import { module , test } from 'qunit' ;
import { setupTest } from 'ember-qunit' ;
2022-12-20 12:07:52 -05:00
import { TYPE_TARGET_TCP , TYPE_TARGET_SSH } from 'api/models/target' ;
2020-08-24 18:03:45 -04:00
2020-09-16 14:29:02 -04:00
module ( 'Unit | Serializer | target' , function ( hooks ) {
2020-08-24 18:03:45 -04:00
setupTest ( hooks );
2021-07-21 15:45:45 -04:00
test ( 'it serializes targets normally, without host sets or credential libraries' , function ( assert ) {
2020-08-24 18:03:45 -04:00
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
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
],
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [{ value : '1' }, { value : '2' }],
2022-09-01 10:07:36 -07:00
injected_application_credential_source_ids : [
{ value : '4' },
{ value : '5' },
],
2020-09-03 14:48:59 -04:00
scope : {
scope_id : 'org_1' ,
2020-09-16 14:29:02 -04:00
type : 'org' ,
2020-09-03 14:48:59 -04:00
},
2022-01-18 13:44:27 -05:00
default_port : 1234 ,
2020-09-10 16:49:00 -04:00
version : 1 ,
2022-12-20 12:07:52 -05:00
type : TYPE_TARGET_TCP ,
2023-01-09 12:35:42 -05:00
address : '0.0.0.0' ,
2020-08-24 18:03:45 -04:00
});
const snapshot = record . _createSnapshot ();
snapshot . adapterOptions = {};
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
2022-12-20 12:07:52 -05:00
type : TYPE_TARGET_TCP ,
2020-09-15 16:36:40 -04:00
scope_id : 'org_1' ,
2020-10-02 14:50:35 -04:00
session_max_seconds : 28800 ,
2022-07-12 08:36:35 -07:00
session_connection_limit : null ,
2022-04-12 16:16:51 -05:00
worker_filter : null ,
2023-01-10 15:28:20 -06:00
egress_worker_filter : null ,
ingress_worker_filter : null ,
2023-03-22 10:52:33 -04:00
storage_bucket_id : null ,
2020-09-15 16:36:40 -04:00
attributes : {
2022-01-18 13:44:27 -05:00
default_port : 1234 ,
2020-09-16 14:29:02 -04:00
},
2023-01-09 12:35:42 -05:00
address : '0.0.0.0' ,
2020-08-24 18:03:45 -04:00
});
});
2021-09-22 19:15:09 -04:00
test ( 'it serializes only host sources and version when an `adapterOptions.hostSetIDs` array is passed' , function ( assert ) {
2020-08-24 18:03:45 -04:00
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
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
],
2020-09-16 14:29:02 -04:00
version : 1 ,
2020-08-24 18:03:45 -04:00
});
const snapshot = record . _createSnapshot ();
snapshot . adapterOptions = {
2020-09-16 14:29:02 -04:00
hostSetIDs : [ '4' , '5' ],
2020-08-24 18:03:45 -04:00
};
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
2021-09-22 19:15:09 -04:00
host_source_ids : [ '4' , '5' ],
2020-09-16 14:29:02 -04:00
version : 1 ,
2020-08-24 18:03:45 -04:00
});
});
2022-09-01 10:07:36 -07:00
test ( 'it serializes only credential sources and version when an `adapterOptions.brokeredCredentialSourceIDs` array is passed' , function ( assert ) {
2021-07-21 15:45:45 -04:00
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
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 ,
});
const snapshot = record . _createSnapshot ();
snapshot . adapterOptions = {
2022-09-01 10:07:36 -07:00
brokeredCredentialSourceIDs : [ '4' , '5' ],
2021-07-21 15:45:45 -04:00
};
const serializedRecord = serializer . serialize ( snapshot );
2022-09-01 10:07:36 -07:00
2021-07-21 15:45:45 -04:00
assert . deepEqual ( serializedRecord , {
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [ '4' , '5' ],
2021-07-21 15:45:45 -04:00
version : 1 ,
});
});
2022-09-01 10:07:36 -07:00
test ( 'it serializes only credential sources and version when an `adapterOptions.injectedApplicationCredentialSourceIDs` array is passed' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
description : 'Description' ,
injected_application_credential_source_ids : [
{ value : '1' },
{ value : '2' },
],
version : 1 ,
});
const snapshot = record . _createSnapshot ();
snapshot . adapterOptions = {
injectedApplicationCredentialSourceIDs : [ '4' , '5' ],
};
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
injected_application_credential_source_ids : [ '4' , '5' ],
version : 1 ,
});
});
2023-01-10 15:28:20 -06:00
2023-03-22 10:52:33 -04:00
test ( 'it serializes only storage_bucket_id and version when an `adapterOptions.method` is passed for set storage bucket' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
description : 'Description' ,
storage_bucket_id : 'bucketID' ,
version : 1 ,
});
const snapshot = record . _createSnapshot ();
snapshot . adapterOptions = {
method : 'set-storage-bucket' ,
};
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
storage_bucket_id : 'bucketID' ,
version : 1 ,
});
});
test ( 'it serializes only version when an `adapterOptions.method` is passed for remove storage bucket' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
description : 'Description' ,
storage_bucket_id : 'bucketID' ,
version : 1 ,
});
const snapshot = record . _createSnapshot ();
snapshot . adapterOptions = {
method : 'remove-storage-bucket' ,
};
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
version : 1 ,
});
});
2022-04-12 16:16:51 -05:00
test ( 'it serializes the worker_filter attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
2022-12-20 12:07:52 -05:00
type : TYPE_TARGET_SSH ,
2022-04-12 16:16:51 -05:00
worker_filter : 'worker' ,
default_port : 1234 ,
scope : {
scope_id : 'org_1' ,
},
});
const snapshot = record . _createSnapshot ();
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
2022-12-20 12:07:52 -05:00
type : TYPE_TARGET_SSH ,
2022-04-12 16:16:51 -05:00
scope_id : 'org_1' ,
session_max_seconds : 28800 ,
2022-07-12 08:36:35 -07:00
session_connection_limit : null ,
2022-04-12 16:16:51 -05:00
worker_filter : 'worker' ,
2023-01-10 15:28:20 -06:00
egress_worker_filter : null ,
ingress_worker_filter : null ,
2023-03-22 10:52:33 -04:00
storage_bucket_id : null ,
2023-01-10 15:28:20 -06:00
attributes : {
default_port : 1234 ,
},
address : null ,
});
});
test ( 'it serializes the egress_worker_filter attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
type : TYPE_TARGET_SSH ,
egress_worker_filter : 'egress worker' ,
default_port : 1234 ,
scope : {
scope_id : 'org_1' ,
},
});
const snapshot = record . _createSnapshot ();
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
type : TYPE_TARGET_SSH ,
scope_id : 'org_1' ,
session_max_seconds : 28800 ,
session_connection_limit : null ,
worker_filter : null ,
egress_worker_filter : 'egress worker' ,
ingress_worker_filter : null ,
2023-03-22 10:52:33 -04:00
storage_bucket_id : null ,
2023-01-10 15:28:20 -06:00
attributes : {
default_port : 1234 ,
},
address : null ,
});
});
test ( 'it serializes the ingress_worker_filter attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const record = store . createRecord ( 'target' , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
type : TYPE_TARGET_SSH ,
ingress_worker_filter : 'ingress worker' ,
default_port : 1234 ,
scope : {
scope_id : 'org_1' ,
},
});
const snapshot = record . _createSnapshot ();
const serializedRecord = serializer . serialize ( snapshot );
assert . deepEqual ( serializedRecord , {
name : 'User' ,
description : 'Description' ,
version : 1 ,
type : TYPE_TARGET_SSH ,
scope_id : 'org_1' ,
session_max_seconds : 28800 ,
session_connection_limit : null ,
worker_filter : null ,
egress_worker_filter : null ,
ingress_worker_filter : 'ingress worker' ,
2023-03-22 10:52:33 -04:00
storage_bucket_id : null ,
2022-04-12 16:16:51 -05:00
attributes : {
default_port : 1234 ,
},
2023-01-09 12:35:42 -05:00
address : null ,
2022-04-12 16:16:51 -05:00
});
});
2020-08-24 18:03:45 -04:00
test ( 'it normalizes records with array fields' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const targetModelClass = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
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-09-16 14:29:02 -04:00
],
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [ '1' ],
2022-09-01 10:07:36 -07:00
injected_application_credential_source_ids : [ '3' ],
2020-08-24 18:03:45 -04:00
};
const normalized = serializer . normalizeSingleResponse (
store ,
targetModelClass ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
2021-08-04 17:58:08 -04:00
authorized_actions : [],
2020-08-24 18:03:45 -04:00
name : 'Target 1' ,
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-09-16 14:29:02 -04:00
],
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [{ value : '1' }],
2022-09-01 10:07:36 -07:00
injected_application_credential_source_ids : [{ value : '3' }],
2020-08-24 18:03:45 -04:00
},
relationships : {},
},
});
});
2022-01-18 13:44:27 -05:00
test ( 'it normalizes records with nested attributes' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const targetModelClass = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
attributes : {
default_port : 1234 ,
},
};
const normalized = serializer . normalizeSingleResponse (
store ,
targetModelClass ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [],
2022-09-01 10:07:36 -07:00
injected_application_credential_source_ids : [],
2022-01-18 13:44:27 -05:00
authorized_actions : [],
host_sources : [],
name : 'Target 1' ,
default_port : 1234 ,
},
relationships : {},
},
});
});
2022-08-15 09:48:12 -07:00
test ( 'it normalizes missing host_sources and brokered_credential_source_ids to empty array' , function ( assert ) {
2020-09-12 11:40:21 -04:00
assert . expect ( 1 );
2020-09-12 10:42:23 -04:00
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const target = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
2020-09-16 14:29:02 -04:00
scope : { id : 'o_123' },
2020-09-12 10:42:23 -04:00
};
const normalized = serializer . normalizeSingleResponse (
store ,
target ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
2021-08-04 17:58:08 -04:00
authorized_actions : [],
2020-09-12 10:42:23 -04:00
name : 'Target 1' ,
2022-07-18 11:30:53 -07:00
scope : { id : 'o_123' , scope_id : 'o_123' },
2021-09-22 19:15:09 -04:00
host_sources : [],
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [],
2022-09-01 10:07:36 -07:00
injected_application_credential_source_ids : [],
2020-09-12 10:42:23 -04:00
},
relationships : {},
},
});
});
2022-04-12 16:16:51 -05:00
test ( 'it normalizes the worker_filter attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const target = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
worker_filter : 'worker' ,
};
const normalized = serializer . normalizeSingleResponse (
store ,
target ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
authorized_actions : [],
name : 'Target 1' ,
host_sources : [],
2022-08-15 09:48:12 -07:00
brokered_credential_source_ids : [],
2022-09-01 10:07:36 -07:00
injected_application_credential_source_ids : [],
2022-04-12 16:16:51 -05:00
worker_filter : 'worker' ,
},
relationships : {},
},
});
});
2023-01-10 15:28:20 -06:00
test ( 'it normalizes the egress_worker_filter attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const target = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
egress_worker_filter : 'egress worker' ,
};
const normalized = serializer . normalizeSingleResponse (
store ,
target ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
authorized_actions : [],
name : 'Target 1' ,
host_sources : [],
brokered_credential_source_ids : [],
injected_application_credential_source_ids : [],
egress_worker_filter : 'egress worker' ,
},
relationships : {},
},
});
});
test ( 'it normalizes the ingress_worker_filter attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const target = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
ingress_worker_filter : 'ingress worker' ,
};
const normalized = serializer . normalizeSingleResponse (
store ,
target ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
authorized_actions : [],
name : 'Target 1' ,
host_sources : [],
brokered_credential_source_ids : [],
injected_application_credential_source_ids : [],
ingress_worker_filter : 'ingress worker' ,
},
relationships : {},
},
});
});
2023-03-22 10:52:33 -04:00
test ( 'it normalizes the storage_bucket_id attribute if present' , function ( assert ) {
assert . expect ( 1 );
const store = this . owner . lookup ( 'service:store' );
const serializer = store . serializerFor ( 'target' );
const target = store . createRecord ( 'target' ). constructor ;
const payload = {
id : '1' ,
name : 'Target 1' ,
storage_bucket_id : 'bucketID' ,
};
const normalized = serializer . normalizeSingleResponse (
store ,
target ,
payload
);
assert . deepEqual ( normalized , {
included : [],
data : {
id : '1' ,
type : 'target' ,
attributes : {
authorized_actions : [],
name : 'Target 1' ,
host_sources : [],
brokered_credential_source_ids : [],
injected_application_credential_source_ids : [],
storage_bucket_id : 'bucketID' ,
},
relationships : {},
},
});
});
2020-08-24 18:03:45 -04:00
});