|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | require('should'); |
| 4 | +const fs = require('fs'); |
| 5 | +const os = require('os'); |
| 6 | +const path = require('path'); |
4 | 7 |
|
5 | 8 | describe('env', function () { |
| 9 | + function writeTempFile(fileName, data) { |
| 10 | + const fullPath = path.join(os.tmpdir(), fileName); |
| 11 | + fs.writeFileSync(fullPath, data); |
| 12 | + return fullPath; |
| 13 | + } |
| 14 | + |
| 15 | + it('should not set the API key without API_SECRET or API_SECRET_FILE', function () { |
| 16 | + delete process.env.API_SECRET; |
| 17 | + var env = require( '../lib/server/env' )(); |
| 18 | + env.enclave.isApiKeySet().should.equal(false); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should read the API key from API_SECRET_FILE if it is valid', function () { |
| 22 | + const apiSecretFile = 'this is another pass phrase'; |
| 23 | + const hashFile = 'c79c6db1070da3537d0162e60647b0a588769f8d'; |
| 24 | + process.env.API_SECRET_FILE = writeTempFile('api_secret_file', apiSecretFile); |
| 25 | + |
| 26 | + var env = require( '../lib/server/env' )(); |
| 27 | + env.enclave.isApiKeySet().should.equal(true); |
| 28 | + env.enclave.isApiKey(hashFile).should.equal(true); |
| 29 | + |
| 30 | + fs.rmSync(process.env.API_SECRET_FILE); |
| 31 | + delete process.env.API_SECRET_FILE; |
| 32 | + }); |
| 33 | + |
| 34 | + it('should raise an error when API_SECRET_FILE does not exist', function () { |
| 35 | + const nonexistentPath = path.join(os.tmpdir(), 'api_secret_file'); |
| 36 | + process.env.API_SECRET_FILE = nonexistentPath; |
| 37 | + |
| 38 | + var env = require( '../lib/server/env' )(); |
| 39 | + env.enclave.isApiKeySet().should.equal(false); |
| 40 | + env.err.length.should.equal(1); |
| 41 | + |
| 42 | + const error = env.err.pop(); |
| 43 | + error.should.have.property('desc'); |
| 44 | + error.desc.should.match(/API_SECRET_FILE/); |
| 45 | + error.desc.should.match(/no such file or directory/); |
| 46 | + |
| 47 | + delete process.env.API_SECRET_FILE; |
| 48 | + }); |
| 49 | + |
| 50 | + it('should use API_SECRET when API_SECRET_FILE is also specified', function () { |
| 51 | + const apiSecretEnv = 'this is my long pass phrase'; |
| 52 | + const hashEnv = 'b723e97aa97846eb92d5264f084b2823f57c4aa1'; |
| 53 | + process.env.API_SECRET = apiSecretEnv; |
| 54 | + |
| 55 | + const apiSecretFile = 'this is another pass phrase'; |
| 56 | + const hashFile = 'c79c6db1070da3537d0162e60647b0a588769f8d'; |
| 57 | + process.env.API_SECRET_FILE = writeTempFile('api_secret_file', apiSecretFile); |
| 58 | + |
| 59 | + var env = require( '../lib/server/env' )(); |
| 60 | + env.enclave.isApiKeySet().should.equal(true); |
| 61 | + env.enclave.isApiKey(hashEnv).should.equal(true); |
| 62 | + env.enclave.isApiKey(hashFile).should.equal(false); |
| 63 | + |
| 64 | + fs.rmSync(process.env.API_SECRET_FILE); |
| 65 | + delete process.env.API_SECRET_FILE; |
| 66 | + delete process.env.API_SECRET; |
| 67 | + }); |
| 68 | + |
6 | 69 | it( 'show the right plugins', function () { |
7 | 70 | process.env.SHOW_PLUGINS = 'iob'; |
8 | 71 | process.env.ENABLE = 'iob cob'; |
|
0 commit comments