refactor assigning required env variables
This commit is contained in:
parent
98b1950e30
commit
79c82b9e0b
|
@ -41,6 +41,23 @@ function ensureValidJSON(object) {
|
||||||
return JSON.parse(stringified);
|
return JSON.parse(stringified);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get variable value from environment (process.env)
|
||||||
|
* Exit with code 1 if variable is not set or empty
|
||||||
|
* @param {string} name variable name
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function getRequiredEnvironmentVariable(name) {
|
||||||
|
const value = process.env[name];
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
console.log(`${name} cannot be empty`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Authenticate with given credentials and return auth cookie
|
* Authenticate with given credentials and return auth cookie
|
||||||
* Creates new account if username does not exist
|
* Creates new account if username does not exist
|
||||||
|
@ -57,23 +74,15 @@ function getAuthCookie(forceAuth = false) {
|
||||||
// do not authenticate if it is not required by portal limit access rule
|
// do not authenticate if it is not required by portal limit access rule
|
||||||
if (!forceAuth && !["authenticated", "subscription"].includes(process.env.ACCOUNTS_LIMIT_ACCESS)) return "";
|
if (!forceAuth && !["authenticated", "subscription"].includes(process.env.ACCOUNTS_LIMIT_ACCESS)) return "";
|
||||||
|
|
||||||
const portalDomain = process.env.PORTAL_DOMAIN;
|
// assign all required environment variables
|
||||||
const email = process.env.ACCOUNTS_TEST_USER_EMAIL;
|
const portalDomain = getRequiredEnvironmentVariable("PORTAL_DOMAIN");
|
||||||
const password = process.env.ACCOUNTS_TEST_USER_PASSWORD;
|
const email = getRequiredEnvironmentVariable("ACCOUNTS_TEST_USER_EMAIL");
|
||||||
|
const password = getRequiredEnvironmentVariable("ACCOUNTS_TEST_USER_PASSWORD");
|
||||||
try {
|
|
||||||
if (!portalDomain) throw new Error("PORTAL_DOMAIN cannot be empty");
|
|
||||||
if (!email) throw new Error("ACCOUNTS_TEST_USER_EMAIL cannot be empty");
|
|
||||||
if (!password) throw new Error("ACCOUNTS_TEST_USER_PASSWORD cannot be empty");
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error.message);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function authenticate() {
|
async function authenticate() {
|
||||||
try {
|
try {
|
||||||
// authenticate with given test user credentials
|
// authenticate with given test user credentials
|
||||||
const response = await got.post(`https://account.${process.env.PORTAL_DOMAIN}/api/login`, {
|
const response = await got.post(`https://account.${portalDomain}/api/login`, {
|
||||||
json: { email, password },
|
json: { email, password },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -95,7 +104,7 @@ function getAuthCookie(forceAuth = false) {
|
||||||
// 401 means that service worked but user could not have been authenticated
|
// 401 means that service worked but user could not have been authenticated
|
||||||
if (error.response && error.response.statusCode === 401) {
|
if (error.response && error.response.statusCode === 401) {
|
||||||
// sign up with the given credentials
|
// sign up with the given credentials
|
||||||
await got.post(`https://account.${process.env.PORTAL_DOMAIN}/api/user`, {
|
await got.post(`https://account.${portalDomain}/api/user`, {
|
||||||
json: { email, password },
|
json: { email, password },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Reference in New Issue