|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { expect: jestExpect } = require('@jest/globals'); |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const os = require('os'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +const ScalewayProvider = require('../../provider/scalewayProvider'); |
| 9 | +const { createTmpDir } = require('../utils/fs'); |
| 10 | + |
| 11 | +class MockServerless { |
| 12 | + constructor() { |
| 13 | + this.service = {}; |
| 14 | + this.service.provider = {}; |
| 15 | + |
| 16 | + this.cli = {}; |
| 17 | + this.cli.log = (logMsg) => { |
| 18 | + console.log(logMsg); |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + setProvider(provName, prov) { |
| 23 | + this.service.provider = prov; |
| 24 | + }; |
| 25 | +}; |
| 26 | + |
| 27 | +describe('Scaleway credentials test', () => { |
| 28 | + this.expectedToken = null; |
| 29 | + this.expectedProject = null; |
| 30 | + |
| 31 | + this.serverless = new MockServerless(); |
| 32 | + this.prov = new ScalewayProvider(this.serverless); |
| 33 | + |
| 34 | + beforeAll(() => { |
| 35 | + // Override scw config file location |
| 36 | + this.dummyScwConfigDir = createTmpDir(); |
| 37 | + this.dummyScwConfigPath = path.join(this.dummyScwConfigDir, 'config.yml'); |
| 38 | + |
| 39 | + ScalewayProvider.scwConfigFile = this.dummyScwConfigPath; |
| 40 | + }); |
| 41 | + |
| 42 | + afterAll(() => { |
| 43 | + // Delete the dummy config file and directory |
| 44 | + if(fs.existsSync(this.dummyScwConfigPath)) { |
| 45 | + fs.unlinkSync(this.dummyScwConfigPath); |
| 46 | + } |
| 47 | + |
| 48 | + if(fs.existsSync(this.dummyScwConfigDir)) { |
| 49 | + fs.rmdirSync(this.dummyScwConfigDir); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + this.checkCreds = (options) => { |
| 54 | + // Set the credentials |
| 55 | + this.prov.setCredentials(options); |
| 56 | + |
| 57 | + // Check they're as expected |
| 58 | + expect(this.prov.scwToken).to.equal(this.expectedToken); |
| 59 | + expect(this.prov.scwProject).to.equal(this.expectedProject); |
| 60 | + }; |
| 61 | + |
| 62 | + // ------------------------------------- |
| 63 | + // These tests must be written in order of increasing precedence, each one getting superceded by the next. |
| 64 | + // ------------------------------------- |
| 65 | + |
| 66 | + it('should return nothing when no credentials found', () => { |
| 67 | + this.expectedToken = ''; |
| 68 | + this.expectedProject = ''; |
| 69 | + |
| 70 | + this.checkCreds({}); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should read from scw config file if present', () => { |
| 74 | + // Write the dummy file |
| 75 | + const dummyScwConfigContents = 'secret_key: scw-key\ndefault_project_id: scw-proj\n'; |
| 76 | + fs.writeFileSync(this.dummyScwConfigPath, dummyScwConfigContents); |
| 77 | + |
| 78 | + this.expectedToken = 'scw-key'; |
| 79 | + this.expectedProject = 'scw-proj'; |
| 80 | + |
| 81 | + this.checkCreds({}); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should take values from serverless.yml if present', () => { |
| 85 | + this.expectedToken = 'conf-token'; |
| 86 | + this.expectedProject = 'conf-proj'; |
| 87 | + |
| 88 | + this.serverless.service.provider.scwToken = this.expectedToken; |
| 89 | + this.serverless.service.provider.scwProject = this.expectedProject; |
| 90 | + |
| 91 | + this.checkCreds({}); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should read from legacy environment variables if present', () => { |
| 95 | + let originalToken = process.env.SCW_TOKEN; |
| 96 | + let originalProject = process.env.SCW_PROJECT; |
| 97 | + |
| 98 | + this.expectedToken = 'legacy-token'; |
| 99 | + this.expectedProject = 'legacy-proj'; |
| 100 | + |
| 101 | + process.env.SCW_TOKEN = this.expectedToken; |
| 102 | + process.env.SCW_PROJECT = this.expectedProject; |
| 103 | + |
| 104 | + this.checkCreds({}); |
| 105 | + |
| 106 | + process.env.SCW_TOKEN = originalToken; |
| 107 | + process.env.SCW_PROJECT = originalProject; |
| 108 | + }); |
| 109 | + |
| 110 | + it('should read from environment variables if present', () => { |
| 111 | + let originalToken = process.env.SCW_SECRET_KEY; |
| 112 | + let originalProject = process.env.SCW_DEFAULT_PROJECT_ID; |
| 113 | + |
| 114 | + this.expectedToken = 'env-token'; |
| 115 | + this.expectedProject = 'env-proj'; |
| 116 | + |
| 117 | + process.env.SCW_SECRET_KEY = this.expectedToken; |
| 118 | + process.env.SCW_DEFAULT_PROJECT_ID = this.expectedProject; |
| 119 | + |
| 120 | + this.checkCreds({}); |
| 121 | + |
| 122 | + process.env.SCW_SECRET_KEY = originalToken; |
| 123 | + process.env.SCW_DEFAULT_PROJECT_ID = originalProject; |
| 124 | + }); |
| 125 | + |
| 126 | + it('should read credentials from options if present', () => { |
| 127 | + let options = {}; |
| 128 | + options['scw-token'] = 'opt-token'; |
| 129 | + options['scw-project'] = 'opt-proj'; |
| 130 | + |
| 131 | + this.expectedToken = 'opt-token'; |
| 132 | + this.expectedProject = 'opt-proj'; |
| 133 | + |
| 134 | + this.checkCreds(options); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
0 commit comments