• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / boxcli / 4025218550

pending completion
4025218550

Pull #456

github

GitHub
Merge 5e271050e into 34aff89ac
Pull Request #456: fix: Fix unit tests

1081 of 1391 branches covered (77.71%)

Branch coverage included in aggregate %.

3965 of 4452 relevant lines covered (89.06%)

483.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.41
/src/commands/configure/environments/add.js
1
'use strict';
2

3
const BoxCommand = require('../../../box-command');
12✔
4
const { flags } = require('@oclif/command');
12✔
5
const fs = require('fs');
12✔
6
const BoxCLIError = require('../../../cli-error');
12✔
7
const chalk = require('chalk');
12✔
8
const utils = require('../../../util');
12✔
9

10
class EnvironmentsAddCommand extends BoxCommand {
11
        async run() {
12
                const { flags, args } = this.parse(EnvironmentsAddCommand);
×
13
                let environmentsObj = await this.getEnvironments();
×
14
                let environmentName = flags.name;
×
15
                let configFilePath = utils.parsePath(args.path);
×
16
                let configObj;
17
                try {
×
18
                        /* eslint-disable no-sync */
19
                        configObj = JSON.parse(fs.readFileSync(configFilePath));
×
20
                        /* eslint-enable no-sync */
21
                } catch (ex) {
22
                        throw new BoxCLIError(
×
23
                                `Could not read environment config file ${args.path}`,
24
                                ex
25
                        );
26
                }
27

28
                const isCCG = flags['ccg-auth'];
×
29

30
                utils.validateConfigObject(configObj, isCCG);
×
31

32
                let newEnvironment = {
×
33
                        clientId: configObj.boxAppSettings.clientID,
34
                        enterpriseId: configObj.enterpriseID,
35
                        boxConfigFilePath: configFilePath,
36
                        hasInLinePrivateKey: true,
37
                        privateKeyPath: null,
38
                        name: environmentName,
39
                        defaultAsUserId: null,
40
                        useDefaultAsUser: false,
41
                        // @NOTE: These are legacy properties no longer in use
42
                        // adminAsUserId: null,
43
                        // tempAsUserId: null,
44
                        // useTempAsUser: false,
45
                        // userSessionExpiration: null,
46
                        // userSessionEnabled: false,
47
                        cacheTokens: true,
48
                };
49

50
                if (environmentsObj.environments.hasOwnProperty(environmentName)) {
×
51
                        throw new BoxCLIError('There already is an environment with this name');
×
52
                }
53
                if (!configObj.boxAppSettings.clientID) {
×
54
                        throw new BoxCLIError('Your configuration file is missing the client ID');
×
55
                }
56
                if (!configObj.boxAppSettings.clientSecret) {
×
57
                        throw new BoxCLIError(
×
58
                                'Your configuration file is missing the client secret'
59
                        );
60
                }
61
                if (!isCCG) {
×
62
                        if (!configObj.boxAppSettings.appAuth.publicKeyID) {
×
63
                                throw new BoxCLIError(
×
64
                                        'Your configuration file is missing the public key ID'
65
                                );
66
                        }
67
                        if (
×
68
                                !configObj.boxAppSettings.appAuth.privateKey &&
×
69
                                !flags['private-key-path']
70
                        ) {
71
                                throw new BoxCLIError('Your environment does not have a private key');
×
72
                        }
73
                        if (!configObj.boxAppSettings.appAuth.passphrase) {
×
74
                                throw new BoxCLIError('Your environment does not have a passphrase');
×
75
                        }
76
                }
77
                if (!configObj.enterpriseID) {
×
78
                        throw new BoxCLIError('Your environment does not have an enterprise ID');
×
79
                }
80

81
                if (flags['private-key-path']) {
×
82
                        /* eslint-disable no-sync */
83
                        if (
×
84
                                !fs.existsSync(flags['private-key-path']) ||
×
85
                                fs.statSync(flags['private-key-path']).isDirectory()
86
                        ) {
87
                                throw new BoxCLIError(
×
88
                                        `The private key path ${flags['private-key-path']} does not point to a file`
89
                                );
90
                        }
91
                        /* eslint-enable no-sync */
92
                        newEnvironment.privateKeyPath = flags['private-key-path'];
×
93
                        newEnvironment.hasInLinePrivateKey = false;
×
94
                }
95
                if (flags['set-as-current']) {
×
96
                        configObj.default = args.name;
×
97
                }
98
                // If no default environment is defined, this newly added environment will be set as the default
99
                if (!environmentsObj.default) {
×
100
                        environmentsObj.default = environmentName;
×
101
                }
102

103
                if (isCCG) {
×
104
                        newEnvironment.clientSecret = configObj.boxAppSettings.clientSecret;
×
105
                        newEnvironment.ccgUser = flags['ccg-user'];
×
106
                        newEnvironment.authMethod = 'ccg';
×
107
                }
108

109
                environmentsObj.environments[environmentName] = newEnvironment;
×
110
                await this.updateEnvironments(environmentsObj);
×
111
                this.info(
×
112
                        chalk`{green Successfully added CLI environment "${flags.name}"}`
113
                );
114
        }
115
}
116

117
// @NOTE: This command MUST skip client setup, since it is used to add the first environment
118
EnvironmentsAddCommand.noClient = true;
12✔
119

120
EnvironmentsAddCommand.description = 'Add a new Box environment';
12✔
121

122
EnvironmentsAddCommand.flags = {
12✔
123
        ...BoxCommand.minFlags,
124
        'private-key-path': flags.string({
125
                description: 'Provide a path to application private key',
126
                parse: utils.parsePath,
127
        }),
128
        'set-as-current': flags.boolean({
129
                description: 'Set this new environment as your current environment',
130
        }),
131
        name: flags.string({
132
                char: 'n',
133
                description: 'Set a name for the environment',
134
                default: 'default',
135
        }),
136
        'ccg-auth': flags.boolean({
137
                description: 'Add a CCG environment that will use service account. You will have to provide enterprise ID with client id and secret.',
138
        }),
139
        'ccg-user': flags.string({
140
                description: 'Provide an ID for a user for CCG. Use it to obtain user token. ' +
141
                        'In order to enable generating user token you have to go to your application ' +
142
                        'configuration that can be found here https://app.box.com/developers/console.\n' +
143
                        'In`Configuration` tab, in section `Advanced Features` select `Generate user access tokens`. \n' +
144
                        'Do not forget to re-authorize application if it was already authorized.',
145
                dependsOn: ['ccg-auth'],
146
        }),
147
};
148

149
EnvironmentsAddCommand.args = [
12✔
150
        {
151
                name: 'path',
152
                required: true,
153
                hidden: false,
154
                description: 'Provide a file path to configuration file',
155
        },
156
];
157

158
module.exports = EnvironmentsAddCommand;
12✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc