2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-18 16:43:52 +00:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import { env } from 'consul-ui/env';
|
|
|
|
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class EnvService extends Service {
|
2020-04-08 17:03:18 +00:00
|
|
|
// deprecated
|
|
|
|
// TODO: Remove this elsewhere in the app and use var instead
|
2020-11-09 09:25:35 +00:00
|
|
|
env(key) {
|
2021-05-26 16:43:46 +00:00
|
|
|
return this.var(key);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var(key) {
|
2020-04-08 17:03:18 +00:00
|
|
|
return env(key);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-18 14:07:12 +00:00
|
|
|
/**
|
|
|
|
* Stub class that can be used in testing when we want to test
|
|
|
|
* interactions with the EnvService. We can use `EnvStub.stubEnv` to setup
|
|
|
|
* an Env-Service that returns certain values we need to execute our tests.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* // some-test.js
|
|
|
|
* test('testing interaction with Env-service', async function(assert) {
|
|
|
|
* this.owner.register('service:env', class Stub extends EnvStub {
|
|
|
|
* . stubEnv = {
|
|
|
|
* CONSUL_ACLS_ENABLED: true
|
|
|
|
* }
|
|
|
|
* })
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class EnvStub extends EnvService {
|
|
|
|
var(key) {
|
|
|
|
const { stubEnv } = this;
|
|
|
|
|
|
|
|
const stubbed = stubEnv[key];
|
|
|
|
|
|
|
|
if (stubbed) {
|
|
|
|
return stubbed;
|
|
|
|
} else {
|
|
|
|
return super.var(...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to allow stubbing out data that is accessed by the application
|
|
|
|
* based on the Env-service. You will need to call this before the env-service gets
|
|
|
|
* initialized because it overrides the env-service injection on the owner.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* test('test something env related', async function(assert) {
|
|
|
|
* setupTestEnv(this.owner, {
|
|
|
|
* CONSUL_ACLS_ENABLED: true
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // ...
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {*} owner - the owner of the test instance (usually `this.owner`)
|
|
|
|
* @param {*} stubEnv - an object that holds the stubbed env-data
|
|
|
|
*/
|
|
|
|
export function setupTestEnv(owner, stubEnv) {
|
|
|
|
owner.register(
|
|
|
|
'service:env',
|
|
|
|
class Stub extends EnvStub {
|
|
|
|
stubEnv = stubEnv;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|