How to use readEnv method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

env.js

Source: env.js Github

copy
1'use strict';
2
3var _each = require('lodash/each');
4var _trim = require('lodash/trim');
5var _forIn = require('lodash/forIn');
6var _startsWith = require('lodash/startsWith');
7var _camelCase = require('lodash/camelCase');
8
9var fs = require('fs');
10var crypto = require('crypto');
11var consts = require('./lib/constants');
12
13var env = {
14  settings: require('./lib/settings')()
15};
16
17// Module to constrain all config and environment parsing to one spot.
18// See the
19function config ( ) {
20  /*
21   * See README.md for info about all the supported ENV VARs
22   */
23  env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
24  env.PORT = readENV('PORT', 1337);
25  env.HOSTNAME = readENV('HOSTNAME', null);
26  env.IMPORT_CONFIG = readENV('IMPORT_CONFIG', null);
27  env.static_files = readENV('NIGHTSCOUT_STATIC_FILES', __dirname + '/static/');
28  env.debug = {
29    minify: readENVTruthy('DEBUG_MINIFY', true)
30  };
31
32  if (env.err) {
33    delete env.err;
34  }
35
36  setSSL();
37  setAPISecret();
38  setVersion();
39  setStorage();
40  updateSettings();
41
42  return env;
43}
44
45function setSSL() {
46  env.SSL_KEY = readENV('SSL_KEY');
47  env.SSL_CERT = readENV('SSL_CERT');
48  env.SSL_CA = readENV('SSL_CA');
49  env.ssl = false;
50  if (env.SSL_KEY && env.SSL_CERT) {
51    env.ssl = {
52      key: fs.readFileSync(env.SSL_KEY), cert: fs.readFileSync(env.SSL_CERT)
53    };
54    if (env.SSL_CA) {
55      env.ca = fs.readFileSync(env.SSL_CA);
56    }
57  }
58
59  env.insecureUseHttp = readENVTruthy("INSECURE_USE_HTTP", false);
60  env.secureHstsHeader = readENVTruthy("SECURE_HSTS_HEADER", true);
61  env.secureHstsHeaderIncludeSubdomains = readENVTruthy("SECURE_HSTS_HEADER_INCLUDESUBDOMAINS", false);
62  env.secureHstsHeaderPreload= readENVTruthy("SECURE_HSTS_HEADER_PRELOAD", false);
63  env.secureCsp = readENVTruthy("SECURE_CSP", false);
64
65}
66
67// A little ugly, but we don't want to read the secret into a var
68function setAPISecret() {
69  var useSecret = (readENV('API_SECRET') && readENV('API_SECRET').length > 0);
70  //TODO: should we clear API_SECRET from process env?
71  env.api_secret = null;
72  // if a passphrase was provided, get the hex digest to mint a single token
73  if (useSecret) {
74    if (readENV('API_SECRET').length < consts.MIN_PASSPHRASE_LENGTH) {
75      var msg = ['API_SECRET should be at least', consts.MIN_PASSPHRASE_LENGTH, 'characters'].join(' ');
76      console.error(msg);
77      env.err = {desc: msg};
78    } else {
79      var shasum = crypto.createHash('sha1');
80      shasum.update(readENV('API_SECRET'));
81      env.api_secret = shasum.digest('hex');
82
83      if (!readENV('TREATMENTS_AUTH', true)) {
84
85      }
86
87
88    }
89  }
90}
91
92function setVersion() {
93  var software = require('./package.json');
94  env.version = software.version;
95  env.name = software.name;
96}
97
98function setStorage() {
99  env.storageURI = readENV('STORAGE_URI') || readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI');
100  env.entries_collection = readENV('ENTRIES_COLLECTION') || readENV('MONGO_COLLECTION', 'entries');
101  env.authentication_collections_prefix = readENV('MONGO_AUTHENTICATION_COLLECTIONS_PREFIX', 'auth_');
102  env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
103  env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
104  env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');
105  env.food_collection = readENV('MONGO_FOOD_COLLECTION', 'food');
106  env.activity_collection = readENV('MONGO_ACTIVITY_COLLECTION', 'activity');
107
108  // TODO: clean up a bit
109  // Some people prefer to use a json configuration file instead.
110  // This allows a provided json config to override environment variables
111  var DB = require('./database_configuration.json'),
112    DB_URL = DB.url ? DB.url : env.storageURI,
113    DB_COLLECTION = DB.collection ? DB.collection : env.entries_collection;
114  env.storageURI = DB_URL;
115  env.entries_collection = DB_COLLECTION;
116}
117
118function updateSettings() {
119
120  var envNameOverrides = {
121    UNITS: 'DISPLAY_UNITS'
122  };
123
124  env.settings.eachSettingAsEnv(function settingFromEnv (name) {
125    var envName = envNameOverrides[name] || name;
126    return readENV(envName);
127  });
128
129  //should always find extended settings last
130  env.extendedSettings = findExtendedSettings(process.env);
131
132  if (!readENVTruthy('TREATMENTS_AUTH', true)) {
133    env.settings.authDefaultRoles = env.settings.authDefaultRoles || "";
134    env.settings.authDefaultRoles += ' careportal';
135  }
136
137
138}
139
140function readENV(varName, defaultValue) {
141  //for some reason Azure uses this prefix, maybe there is a good reason
142  var value = process.env['CUSTOMCONNSTR_' + varName]
143    || process.env['CUSTOMCONNSTR_' + varName.toLowerCase()]
144    || process.env[varName]
145    || process.env[varName.toLowerCase()];
146
147
148  return value != null ? value : defaultValue;
149}
150
151function readENVTruthy(varName, defaultValue) {
152  var value = readENV(varName, defaultValue);
153  if (typeof value === 'string' && (value.toLowerCase() === 'on' || value.toLowerCase() === 'true')) { value = true; }
154  else if (typeof value === 'string' && (value.toLowerCase() === 'off' || value.toLowerCase() === 'false')) { value = false; }
155  else { value=defaultValue }
156  return value;
157}
158
159function findExtendedSettings (envs) {
160  var extended = {};
161
162  extended.devicestatus = {};
163  extended.devicestatus.advanced = true;
164
165  function normalizeEnv (key) {
166    return key.toUpperCase().replace('CUSTOMCONNSTR_', '');
167  }
168
169  _each(env.settings.enable, function eachEnable(enable) {
170    if (_trim(enable)) {
171      _forIn(envs, function eachEnvPair (value, key) {
172        var env = normalizeEnv(key);
173        if (_startsWith(env, enable.toUpperCase() + '_')) {
174          var split = env.indexOf('_');
175          if (split > -1 && split <= env.length) {
176            var exts = extended[enable] || {};
177            extended[enable] = exts;
178            var ext = _camelCase(env.substring(split + 1).toLowerCase());
179            if (!isNaN(value)) { value = Number(value); }
180            if (typeof value === 'string' && (value.toLowerCase() === 'on' || value.toLowerCase() === 'true')) { value = true; }
181            if (typeof value === 'string' && (value.toLowerCase() === 'off' || value.toLowerCase() === 'false')) { value = false; }
182            exts[ext] = value;
183          }
184        }
185      });
186    }
187  });
188  return extended;
189  }
190
191module.exports = config;
192
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)