Skip to content

Commit

Permalink
fix: Respect using environment defaultAsUserId for CCG Auth and OAu…
Browse files Browse the repository at this point in the history
…th (#554)
  • Loading branch information
lukaszsocha2 authored Dec 6, 2024
1 parent 6329355 commit b3a691e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/box-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,13 +799,6 @@ class BoxCommand extends Command {
);
DEBUG.init('Initialized client from environment config');

if (environment.useDefaultAsUser) {
client.asUser(environment.defaultAsUserId);
DEBUG.init(
'Impersonating default user ID %s',
environment.defaultAsUserId
);
}
} else {
// No environments set up yet!
throw new BoxCLIError(
Expand All @@ -815,11 +808,18 @@ class BoxCommand extends Command {
Or, supply a token with your command with --token.`.replace(/^\s+/gmu, '')
);
}

// Using the as-user flag should have precedence over the environment setting
if (this.flags['as-user']) {
client.asUser(this.flags['as-user']);
DEBUG.init('Impersonating user ID %s', this.flags['as-user']);
DEBUG.init('Impersonating user ID %s using the ID provided via the --as-user flag', this.flags['as-user']);
} else if (!this.flags.token && environment.useDefaultAsUser) { // We don't want to use any environment settings if a token is passed in the command
client.asUser(environment.defaultAsUserId);
DEBUG.init(
'Impersonating default user ID %s using environment configuration',
environment.defaultAsUserId
);
}

return client;
}

Expand Down
18 changes: 18 additions & 0 deletions test/box-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ describe('BoxCommand', () => {
const stderr = isWin() ? ctx.stderr.replace(':', '') : ctx.stderr;
assert.notInclude(stderr, ':');
});

describe('As-User', () => {
test
.nock(TEST_API_ROOT, api => api
.get('/2.0/users/me')
.matchHeader('As-user', '12345')
.reply(200, {})
)
.stdout()
.stderr()
.command([
'users:get',
'me',
'--token=test',
'--as-user=12345'
])
.it('should send as-user header when --as-user flag is passed');
});
});

describe('normalizeDateString()', () => {
Expand Down

0 comments on commit b3a691e

Please sign in to comment.